Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / grade / report / outcomes / index.php
blobaa7c2e4a55c2617001d3898170ad743779917886
1 <?php //$Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
9 // //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
11 // //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation; either version 2 of the License, or //
15 // (at your option) any later version. //
16 // //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details: //
21 // //
22 // http://www.gnu.org/copyleft/gpl.html //
23 // //
24 ///////////////////////////////////////////////////////////////////////////
26 include_once('../../../config.php');
27 require_once($CFG->libdir . '/gradelib.php');
28 require_once $CFG->dirroot.'/grade/lib.php';
30 $courseid = required_param('id', PARAM_INT); // course id
32 if (!$course = get_record('course', 'id', $courseid)) {
33 print_error('nocourseid');
36 require_login($course->id);
37 $context = get_context_instance(CONTEXT_COURSE, $course->id);
39 require_capability('gradereport/outcomes:view', $context);
41 // Build navigation
42 $strgrades = get_string('grades');
43 $stroutcomes = get_string('outcomes', 'grades');
45 $navigation = grade_build_nav(__FILE__, $stroutcomes, $course->id);
47 /// Print header
48 print_header_simple($strgrades.':'.$stroutcomes, ':'.$strgrades, $navigation, '', '', true);
49 print_grade_plugin_selector($courseid, 'report', 'outcomes');
51 //first make sure we have proper final grades
52 grade_regrade_final_grades($courseid);
54 // Grab all outcomes used in course
55 $report_info = array();
56 $outcomes = grade_outcome::fetch_all_available($courseid);
58 // Get grade_items that use each outcome
59 foreach ($outcomes as $outcomeid => $outcome) {
60 $report_info[$outcomeid]['items'] = get_records_select('grade_items', "outcomeid = $outcomeid AND courseid = $courseid");
61 $report_info[$outcomeid]['outcome'] = $outcome;
63 // Get average grades for each item
64 if (is_array($report_info[$outcomeid]['items'])) {
65 foreach ($report_info[$outcomeid]['items'] as $itemid => $item) {
66 $sql = "SELECT itemid, AVG(finalgrade) AS avg, COUNT(finalgrade) AS count
67 FROM {$CFG->prefix}grade_grades
68 WHERE itemid = $itemid
69 GROUP BY itemid";
70 $info = get_records_sql($sql);
72 if (!$info) {
73 unset($report_info[$outcomeid]['items'][$itemid]);
74 continue;
75 } else {
76 $info = reset($info);
77 $avg = round($info->avg, 2);
78 $count = $info->count;
81 $report_info[$outcomeid]['items'][$itemid]->avg = $avg;
82 $report_info[$outcomeid]['items'][$itemid]->count = $count;
87 $html = '<table class="generaltable boxaligncenter" width="90%" cellspacing="1" cellpadding="5" summary="Outcomes Report">' . "\n";
88 $html .= '<tr><th class="header c0" scope="col">' . get_string('outcomename', 'grades') . '</th>';
89 $html .= '<th class="header c1" scope="col">' . get_string('courseavg', 'grades') . '</th>';
90 $html .= '<th class="header c2" scope="col">' . get_string('sitewide', 'grades') . '</th>';
91 $html .= '<th class="header c3" scope="col">' . get_string('activities', 'grades') . '</th>';
92 $html .= '<th class="header c4" scope="col">' . get_string('average', 'grades') . '</th>';
93 $html .= '<th class="header c5" scope="col">' . get_string('numberofgrades', 'grades') . '</th></tr>' . "\n";
95 $row = 0;
96 foreach ($report_info as $outcomeid => $outcomedata) {
97 $rowspan = count($outcomedata['items']);
98 // If there are no items for this outcome, rowspan will equal 0, which is not good
99 if ($rowspan == 0) {
100 $rowspan = 1;
103 $shortname_html = '<tr class="r' . $row . '"><td class="cell c0" rowspan="' . $rowspan . '">' . $outcomedata['outcome']->shortname . "</td>\n";
105 $sitewide = get_string('no');
106 if (empty($outcomedata['outcome']->courseid)) {
107 $sitewide = get_string('yes');
110 $sitewide_html = '<td class="cell c2" rowspan="' . $rowspan . '">' . $sitewide . "</td>\n";
112 $outcomedata['outcome']->sum = 0;
113 $scale = new grade_scale(array('id' => $outcomedata['outcome']->scaleid), false);
115 $print_tr = false;
116 $items_html = '';
118 if (!empty($outcomedata['items'])) {
119 foreach ($outcomedata['items'] as $itemid => $item) {
120 if ($print_tr) {
121 $row++;
122 $items_html .= "<tr class=\"r$row\">\n";
125 $grade_item = new grade_item($item, false);
127 if ($item->itemtype == 'mod') {
128 $cm = get_coursemodule_from_instance($item->itemmodule, $item->iteminstance, $item->courseid);
129 $itemname = '<a href="'.$CFG->wwwroot.'/mod/'.$item->itemmodule.'/view.php?id='.$cm->id.'">'.$grade_item->get_name().'</a>';
130 } else {
131 $itemname = $grade_item->get_name();
134 $outcomedata['outcome']->sum += $item->avg;
135 $gradehtml = $scale->get_nearest_item($item->avg);
137 $items_html .= "<td class=\"cell c3\">$itemname</td>"
138 . "<td class=\"cell c4\">$gradehtml ($item->avg)</td>"
139 . "<td class=\"cell c5\">$item->count</td></tr>\n";
140 $print_tr = true;
142 } else {
143 $items_html .= "<td class=\"cell c3\"> - </td><td class=\"cell c4\"> - </td><td class=\"cell c5\"> 0 </td></tr>\n";
146 // Calculate outcome average
147 if (is_array($outcomedata['items'])) {
148 $count = count($outcomedata['items']);
149 if ($count > 0) {
150 $avg = $outcomedata['outcome']->sum / $count;
151 } else {
152 $avg = $outcomedata['outcome']->sum;
154 $avg_html = $scale->get_nearest_item($avg) . " (" . round($avg, 2) . ")\n";
155 } else {
156 $avg_html = ' - ';
159 $outcomeavg_html = '<td class="cell c1" rowspan="' . $rowspan . '">' . $avg_html . "</td>\n";
161 $html .= $shortname_html . $outcomeavg_html . $sitewide_html . $items_html;
162 $row++;
167 $html .= '</table>';
168 print_heading($stroutcomes);
170 echo $html;
171 print_footer($course);