Merge branch 'MDL-44149-27' of git://github.com/damyon/moodle into MOODLE_27_STABLE
[moodle.git] / grade / report / outcomes / index.php
blob73729d8e32eb9a5f33a11ec9fefcd432cdcccd06
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * The gradebook outcomes report
20 * @package gradereport_outcomes
21 * @copyright 2007 Nicolas Connault
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 include_once('../../../config.php');
26 require_once($CFG->libdir . '/gradelib.php');
27 require_once $CFG->dirroot.'/grade/lib.php';
29 $courseid = required_param('id', PARAM_INT); // course id
31 $PAGE->set_url('/grade/report/outcomes/index.php', array('id'=>$courseid));
33 if (!$course = $DB->get_record('course', array('id' => $courseid))) {
34 print_error('nocourseid');
37 require_login($course);
38 $context = context_course::instance($course->id);
40 require_capability('gradereport/outcomes:view', $context);
42 // First make sure we have proper final grades.
43 grade_regrade_final_grades($courseid);
45 // Grab all outcomes used in course.
46 $report_info = array();
47 $outcomes = grade_outcome::fetch_all_available($courseid);
49 // Will exclude grades of suspended users if required.
50 $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
51 $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
52 $showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $context);
53 if ($showonlyactiveenrol) {
54 $suspendedusers = get_suspended_userids($context);
57 // Get grade_items that use each outcome.
58 foreach ($outcomes as $outcomeid => $outcome) {
59 $report_info[$outcomeid]['items'] = $DB->get_records_select('grade_items', "outcomeid = ? AND courseid = ?", array($outcomeid, $courseid));
60 $report_info[$outcomeid]['outcome'] = $outcome;
62 // Get average grades for each item.
63 if (is_array($report_info[$outcomeid]['items'])) {
64 foreach ($report_info[$outcomeid]['items'] as $itemid => $item) {
65 $params = array();
66 $hidesuspendedsql = '';
67 if ($showonlyactiveenrol && !empty($suspendedusers)) {
68 list($notinusers, $params) = $DB->get_in_or_equal($suspendedusers, SQL_PARAMS_QM, null, false);
69 $hidesuspendedsql = ' AND userid ' . $notinusers;
71 $params = array_merge(array($itemid), $params);
73 $sql = "SELECT itemid, AVG(finalgrade) AS avg, COUNT(finalgrade) AS count
74 FROM {grade_grades}
75 WHERE itemid = ?".
76 $hidesuspendedsql.
77 "GROUP BY itemid";
78 $info = $DB->get_records_sql($sql, $params);
80 if (!$info) {
81 unset($report_info[$outcomeid]['items'][$itemid]);
82 continue;
83 } else {
84 $info = reset($info);
85 $avg = round($info->avg, 2);
86 $count = $info->count;
89 $report_info[$outcomeid]['items'][$itemid]->avg = $avg;
90 $report_info[$outcomeid]['items'][$itemid]->count = $count;
95 $html = '<table class="generaltable boxaligncenter" width="90%" cellspacing="1" cellpadding="5" summary="Outcomes Report">' . "\n";
96 $html .= '<tr><th class="header c0" scope="col">' . get_string('outcomeshortname', 'grades') . '</th>';
97 $html .= '<th class="header c1" scope="col">' . get_string('courseavg', 'grades') . '</th>';
98 $html .= '<th class="header c2" scope="col">' . get_string('sitewide', 'grades') . '</th>';
99 $html .= '<th class="header c3" scope="col">' . get_string('activities', 'grades') . '</th>';
100 $html .= '<th class="header c4" scope="col">' . get_string('average', 'grades') . '</th>';
101 $html .= '<th class="header c5" scope="col">' . get_string('numberofgrades', 'grades') . '</th></tr>' . "\n";
103 $row = 0;
104 foreach ($report_info as $outcomeid => $outcomedata) {
105 $rowspan = count($outcomedata['items']);
106 // If there are no items for this outcome, rowspan will equal 0, which is not good.
107 if ($rowspan == 0) {
108 $rowspan = 1;
111 $shortname_html = '<tr class="r' . $row . '"><td class="cell c0" rowspan="' . $rowspan . '">' . $outcomedata['outcome']->shortname . "</td>\n";
113 $sitewide = get_string('no');
114 if (empty($outcomedata['outcome']->courseid)) {
115 $sitewide = get_string('yes');
118 $sitewide_html = '<td class="cell c2" rowspan="' . $rowspan . '">' . $sitewide . "</td>\n";
120 $outcomedata['outcome']->sum = 0;
121 $scale = new grade_scale(array('id' => $outcomedata['outcome']->scaleid), false);
123 $print_tr = false;
124 $items_html = '';
126 if (!empty($outcomedata['items'])) {
127 foreach ($outcomedata['items'] as $itemid => $item) {
128 if ($print_tr) {
129 $row++;
130 $items_html .= "<tr class=\"r$row\">\n";
133 $grade_item = new grade_item($item, false);
135 if ($item->itemtype == 'mod') {
136 $cm = get_coursemodule_from_instance($item->itemmodule, $item->iteminstance, $item->courseid);
137 $itemname = '<a href="'.$CFG->wwwroot.'/mod/'.$item->itemmodule.'/view.php?id='.$cm->id.'">'.format_string($cm->name, true, $cm->course).'</a>';
138 } else {
139 $itemname = $grade_item->get_name();
142 $outcomedata['outcome']->sum += $item->avg;
143 $gradehtml = $scale->get_nearest_item($item->avg);
145 $items_html .= "<td class=\"cell c3\">$itemname</td>"
146 . "<td class=\"cell c4\">$gradehtml ($item->avg)</td>"
147 . "<td class=\"cell c5\">$item->count</td></tr>\n";
148 $print_tr = true;
150 } else {
151 $items_html .= "<td class=\"cell c3\"> - </td><td class=\"cell c4\"> - </td><td class=\"cell c5\"> 0 </td></tr>\n";
154 // Calculate outcome average.
155 if (is_array($outcomedata['items'])) {
156 $count = count($outcomedata['items']);
157 if ($count > 0) {
158 $avg = $outcomedata['outcome']->sum / $count;
159 } else {
160 $avg = $outcomedata['outcome']->sum;
162 $avg_html = $scale->get_nearest_item($avg) . " (" . round($avg, 2) . ")\n";
163 } else {
164 $avg_html = ' - ';
167 $outcomeavg_html = '<td class="cell c1" rowspan="' . $rowspan . '">' . $avg_html . "</td>\n";
169 $html .= $shortname_html . $outcomeavg_html . $sitewide_html . $items_html;
170 $row++;
175 $html .= '</table>';
177 print_grade_page_head($courseid, 'report', 'outcomes');
180 echo $html;
181 echo $OUTPUT->footer();