MDL-57296 gradelib: Fix get_hidding_effect for grade_type_none items
[moodle.git] / grade / report / outcomes / index.php
CommitLineData
e060e33d 1<?php
e060e33d 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.
13//
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/>.
8ad36f4c 16
a153c9f2
AD
17/**
18 * The gradebook outcomes report
19 *
20 * @package gradereport_outcomes
21 * @copyright 2007 Nicolas Connault
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
ba92364c 25include_once('../../../config.php');
26require_once($CFG->libdir . '/gradelib.php');
082443a9 27require_once $CFG->dirroot.'/grade/lib.php';
ba92364c 28
173a9d21 29$courseid = required_param('id', PARAM_INT); // course id
ba92364c 30
a6855934 31$PAGE->set_url('/grade/report/outcomes/index.php', array('id'=>$courseid));
beebcf26 32
5c75a0a3 33if (!$course = $DB->get_record('course', array('id' => $courseid))) {
ba92364c 34 print_error('nocourseid');
35}
36
cdbea7ee 37require_login($course);
d4060472 38$context = context_course::instance($course->id);
ba92364c 39
173a9d21 40require_capability('gradereport/outcomes:view', $context);
41
38c1dd19 42// First make sure we have proper final grades.
0a802c9c 43grade_regrade_final_grades_if_required($course);
173a9d21 44
38c1dd19 45// Grab all outcomes used in course.
e2537ca3 46$report_info = array();
bb3f9f08 47$outcomes = grade_outcome::fetch_all_available($courseid);
e2537ca3 48
38c1dd19
RT
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);
53if ($showonlyactiveenrol) {
54 $suspendedusers = get_suspended_userids($context);
55}
56
57// Get grade_items that use each outcome.
e2537ca3 58foreach ($outcomes as $outcomeid => $outcome) {
5c75a0a3 59 $report_info[$outcomeid]['items'] = $DB->get_records_select('grade_items', "outcomeid = ? AND courseid = ?", array($outcomeid, $courseid));
e2537ca3 60 $report_info[$outcomeid]['outcome'] = $outcome;
61
38c1dd19 62 // Get average grades for each item.
e2537ca3 63 if (is_array($report_info[$outcomeid]['items'])) {
64 foreach ($report_info[$outcomeid]['items'] as $itemid => $item) {
38c1dd19
RT
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;
70 }
71 $params = array_merge(array($itemid), $params);
72
ecc11af0 73 $sql = "SELECT itemid, AVG(finalgrade) AS avg, COUNT(finalgrade) AS count
d24832f9 74 FROM {grade_grades}
38c1dd19
RT
75 WHERE itemid = ?".
76 $hidesuspendedsql.
54867385 77 " GROUP BY itemid";
38c1dd19 78 $info = $DB->get_records_sql($sql, $params);
e96cb4fe 79
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;
87 }
88
89 $report_info[$outcomeid]['items'][$itemid]->avg = $avg;
90 $report_info[$outcomeid]['items'][$itemid]->count = $count;
e2537ca3 91 }
92 }
ba92364c 93}
ba92364c 94
23281a6a 95$html = '<table class="generaltable boxaligncenter" width="90%" cellspacing="1" cellpadding="5" summary="Outcomes Report">' . "\n";
29cbe431 96$html .= '<tr><th class="header c0" scope="col">' . get_string('outcomeshortname', 'grades') . '</th>';
6fd0984c 97$html .= '<th class="header c1" scope="col">' . get_string('courseavg', 'grades') . '</th>';
23281a6a 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";
e2537ca3 102
23281a6a 103$row = 0;
e2537ca3 104foreach ($report_info as $outcomeid => $outcomedata) {
105 $rowspan = count($outcomedata['items']);
38c1dd19 106 // If there are no items for this outcome, rowspan will equal 0, which is not good.
a8b5167b 107 if ($rowspan == 0) {
108 $rowspan = 1;
109 }
110
23281a6a 111 $shortname_html = '<tr class="r' . $row . '"><td class="cell c0" rowspan="' . $rowspan . '">' . $outcomedata['outcome']->shortname . "</td>\n";
e2537ca3 112
113 $sitewide = get_string('no');
114 if (empty($outcomedata['outcome']->courseid)) {
115 $sitewide = get_string('yes');
116 }
117
23281a6a 118 $sitewide_html = '<td class="cell c2" rowspan="' . $rowspan . '">' . $sitewide . "</td>\n";
acdc8e8a 119
120 $outcomedata['outcome']->sum = 0;
121 $scale = new grade_scale(array('id' => $outcomedata['outcome']->scaleid), false);
e2537ca3 122
123 $print_tr = false;
acdc8e8a 124 $items_html = '';
125
a8b5167b 126 if (!empty($outcomedata['items'])) {
e2537ca3 127 foreach ($outcomedata['items'] as $itemid => $item) {
128 if ($print_tr) {
23281a6a 129 $row++;
130 $items_html .= "<tr class=\"r$row\">\n";
e2537ca3 131 }
132
173a9d21 133 $grade_item = new grade_item($item, false);
134
135 if ($item->itemtype == 'mod') {
136 $cm = get_coursemodule_from_instance($item->itemmodule, $item->iteminstance, $item->courseid);
70b5b48c 137 $itemname = '<a href="'.$CFG->wwwroot.'/mod/'.$item->itemmodule.'/view.php?id='.$cm->id.'">'.format_string($cm->name, true, $cm->course).'</a>';
173a9d21 138 } else {
139 $itemname = $grade_item->get_name();
140 }
e2537ca3 141
acdc8e8a 142 $outcomedata['outcome']->sum += $item->avg;
143 $gradehtml = $scale->get_nearest_item($item->avg);
e2537ca3 144
23281a6a 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";
e2537ca3 148 $print_tr = true;
149 }
150 } else {
23281a6a 151 $items_html .= "<td class=\"cell c3\"> - </td><td class=\"cell c4\"> - </td><td class=\"cell c5\"> 0 </td></tr>\n";
acdc8e8a 152 }
153
38c1dd19 154 // Calculate outcome average.
acdc8e8a 155 if (is_array($outcomedata['items'])) {
bc430af2 156 $count = count($outcomedata['items']);
157 if ($count > 0) {
158 $avg = $outcomedata['outcome']->sum / $count;
159 } else {
160 $avg = $outcomedata['outcome']->sum;
161 }
acdc8e8a 162 $avg_html = $scale->get_nearest_item($avg) . " (" . round($avg, 2) . ")\n";
163 } else {
164 $avg_html = ' - ';
e2537ca3 165 }
acdc8e8a 166
23281a6a 167 $outcomeavg_html = '<td class="cell c1" rowspan="' . $rowspan . '">' . $avg_html . "</td>\n";
acdc8e8a 168
169 $html .= $shortname_html . $outcomeavg_html . $sitewide_html . $items_html;
23281a6a 170 $row++;
e2537ca3 171}
ba92364c 172
67ab0ff0
MN
173$html .= '</table>';
174
175print_grade_page_head($courseid, 'report', 'outcomes');
176
177echo $html;
178
87f860cd 179$event = \gradereport_outcomes\event\grade_report_viewed::create(
06dc1e7f
AG
180 array(
181 'context' => $context,
182 'courseid' => $courseid,
183 )
184);
185$event->trigger();
acdc8e8a 186
5a931394 187echo $OUTPUT->footer();