MDL-45894 navigation: My grades update to overview report.
[moodle.git] / grade / report / overview / lib.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
aa330ebb 17/**
a153c9f2
AD
18 * Definition of the grade_overview_report class
19 *
20 * @package gradereport_overview
21 * @copyright 2007 Nicolas Connault
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
aa330ebb 23 */
24
25require_once($CFG->dirroot . '/grade/report/lib.php');
26require_once($CFG->libdir.'/tablelib.php');
27
28/**
29 * Class providing an API for the overview report building and displaying.
30 * @uses grade_report
a153c9f2 31 * @package gradereport_overview
aa330ebb 32 */
33class grade_report_overview extends grade_report {
34
35 /**
36 * The user.
37 * @var object $user
38 */
5c75a0a3 39 public $user;
aa330ebb 40
9db8d030
AD
41 /**
42 * The user's courses
43 * @var array $courses
44 */
45 public $courses;
46
aa330ebb 47 /**
48 * A flexitable to hold the data.
49 * @var object $table
50 */
5c75a0a3 51 public $table;
aa330ebb 52
60574063 53 /**
9db8d030
AD
54 * Show student ranks within each course.
55 * @var array $showrank
60574063 56 */
5c75a0a3 57 public $showrank;
60574063 58
61541a5a
AD
59 /**
60 * show course/category totals if they contain hidden items
61 */
62 var $showtotalsifcontainhidden;
63
aa330ebb 64 /**
65 * Constructor. Sets local copies of user preferences and initialises grade_tree.
66 * @param int $userid
67 * @param object $gpr grade plugin return tracking object
68 * @param string $context
69 */
5c75a0a3 70 public function __construct($userid, $gpr, $context) {
71 global $CFG, $COURSE, $DB;
72 parent::__construct($COURSE->id, $gpr, $context);
f7e21436 73
9db8d030 74 // Get the user (for full name).
5c75a0a3 75 $this->user = $DB->get_record('user', array('id' => $userid));
aa330ebb 76
9db8d030
AD
77 // Load the user's courses.
78 $this->courses = enrol_get_users_courses($this->user->id, false, 'id, shortname, showgrades');
79
80 $this->showrank = array();
81 $this->showrank['any'] = false;
5df9bc39
AD
82
83 $this->showtotalsifcontainhidden = array();
84
9db8d030
AD
85 if ($this->courses) {
86 foreach ($this->courses as $course) {
87 $this->showrank[$course->id] = grade_get_setting($course->id, 'report_overview_showrank', !empty($CFG->grade_report_overview_showrank));
88 if ($this->showrank[$course->id]) {
89 $this->showrank['any'] = true;
90 }
5df9bc39
AD
91
92 $this->showtotalsifcontainhidden[$course->id] = grade_get_setting($course->id, 'report_overview_showtotalsifcontainhidden', $CFG->grade_report_overview_showtotalsifcontainhidden);
9db8d030
AD
93 }
94 }
95
96
aa330ebb 97 // base url for sorting by first/last name
98 $this->baseurl = $CFG->wwwroot.'/grade/overview/index.php?id='.$userid;
99 $this->pbarurl = $this->baseurl;
100
101 $this->setup_table();
102 }
103
104 /**
105 * Prepares the headers and attributes of the flexitable.
106 */
5c75a0a3 107 public function setup_table() {
aa330ebb 108 /*
60574063 109 * Table has 3 columns
110 *| course | final grade | rank (optional) |
111 */
aa330ebb 112
113 // setting up table headers
9db8d030 114 if ($this->showrank['any']) {
60574063 115 $tablecolumns = array('coursename', 'grade', 'rank');
116 $tableheaders = array($this->get_lang_string('coursename', 'grades'),
117 $this->get_lang_string('grade'),
118 $this->get_lang_string('rank', 'grades'));
119 } else {
120 $tablecolumns = array('coursename', 'grade');
121 $tableheaders = array($this->get_lang_string('coursename', 'grades'),
122 $this->get_lang_string('grade'));
123 }
aa330ebb 124 $this->table = new flexible_table('grade-report-overview-'.$this->user->id);
125
126 $this->table->define_columns($tablecolumns);
127 $this->table->define_headers($tableheaders);
128 $this->table->define_baseurl($this->baseurl);
129
130 $this->table->set_attribute('cellspacing', '0');
131 $this->table->set_attribute('id', 'overview-grade');
132 $this->table->set_attribute('class', 'boxaligncenter generaltable');
133
134 $this->table->setup();
135 }
136
c9451960
AG
137 /**
138 * Fill the table for displaying.
139 *
140 * @param bool $activitylink If this report link to the activity report or the user report.
141 */
142 public function fill_table($activitylink = false) {
78527462 143 global $CFG, $DB, $OUTPUT, $USER;
aa330ebb 144
9db8d030
AD
145 // Only show user's courses instead of all courses.
146 if ($this->courses) {
38cb3391 147 $numusers = $this->get_numusers(false);
60574063 148
9db8d030 149 foreach ($this->courses as $course) {
252d14db 150 if (!$course->showgrades) {
151 continue;
152 }
31eae0eb 153
d4060472 154 $coursecontext = context_course::instance($course->id);
31eae0eb
AD
155
156 if (!$course->visible && !has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
157 // The course is hidden and the user isn't allowed to see it
158 continue;
159 }
160
c9451960
AG
161 if (!has_capability('moodle/user:viewuseractivitiesreport', context_user::instance($this->user->id)) &&
162 ((!has_capability('moodle/grade:view', $coursecontext) || $this->user->id != $USER->id) &&
163 !has_capability('moodle/grade:viewall', $coursecontext))) {
78527462
PAM
164 continue;
165 }
166
c9451960
AG
167 $coursename = format_string(get_course_display_name_for_list($course), true, array('context' => $coursecontext));
168 // Link to the activity report version of the user grade report.
169 if ($activitylink) {
170 $courselink = html_writer::link(new moodle_url('/course/user.php', array('mode' => 'grade', 'id' => $course->id,
171 'user' => $this->user->id)), $coursename);
172 } else {
173 $courselink = html_writer::link(new moodle_url('/grade/report/user/index.php', array('id' => $course->id,
174 'userid' => $this->user->id)), $coursename);
175 }
8ebbb06a 176 $canviewhidden = has_capability('moodle/grade:viewhidden', $coursecontext);
60574063 177
aa330ebb 178 // Get course grade_item
00006755 179 $course_item = grade_item::fetch_course_item($course->id);
180
181 // Get the stored grade
182 $course_grade = new grade_grade(array('itemid'=>$course_item->id, 'userid'=>$this->user->id));
183 $course_grade->grade_item =& $course_item;
184 $finalgrade = $course_grade->finalgrade;
185
186 if (!$canviewhidden and !is_null($finalgrade)) {
187 if ($course_grade->is_hidden()) {
188 $finalgrade = null;
00006755 189 } else {
5232d3f2
DW
190 $adjustedgrade = $this->blank_hidden_total_and_adjust_bounds($course->id,
191 $course_item,
192 $finalgrade);
193
194 // We temporarily adjust the view of this grade item - because the min and
195 // max are affected by the hidden values in the aggregation.
196 $finalgrade = $adjustedgrade['grade'];
197 $course_item->grademax = $adjustedgrade['grademax'];
198 $course_item->grademin = $adjustedgrade['grademin'];
00006755 199 }
5a59aeb1
DW
200 } else {
201 // We must use the rawgrademin / rawgrademax because it can be different for
202 // each grade_grade when items are excluded from sum of grades.
203 if (!is_null($finalgrade)) {
204 $course_item->grademin = $course_grade->rawgrademin;
205 $course_item->grademax = $course_grade->rawgrademax;
206 }
60574063 207 }
208
00006755 209 $data = array($courselink, grade_format_gradevalue($finalgrade, $course_item, true));
aa330ebb 210
9db8d030 211 if (!$this->showrank['any']) {
60574063 212 //nothing to do
213
9db8d030 214 } else if ($this->showrank[$course->id] && !is_null($finalgrade)) {
aa330ebb 215 /// find the number of users with a higher grade
00006755 216 /// please note this can not work if hidden grades involved :-( to be fixed in 2.0
217 $params = array($finalgrade, $course_item->id);
aa330ebb 218 $sql = "SELECT COUNT(DISTINCT(userid))
d24832f9 219 FROM {grade_grades}
5c75a0a3 220 WHERE finalgrade IS NOT NULL AND finalgrade > ?
221 AND itemid = ?";
222 $rank = $DB->count_records_sql($sql, $params) + 1;
aa330ebb 223
60574063 224 $data[] = "$rank/$numusers";
225
aa330ebb 226 } else {
9db8d030
AD
227 // No grade, no rank.
228 // Or this course wants rank hidden.
60574063 229 $data[] = '-';
aa330ebb 230 }
231
60574063 232 $this->table->add_data($data);
aa330ebb 233 }
aa330ebb 234 return true;
60574063 235
aa330ebb 236 } else {
c9451960 237 echo $OUTPUT->notification(get_string('notenrolled', 'grades'), 'notifymessage');
aa330ebb 238 return false;
239 }
240 }
241
242 /**
243 * Prints or returns the HTML from the flexitable.
244 * @param bool $return Whether or not to return the data instead of printing it directly.
245 * @return string
246 */
5c75a0a3 247 public function print_table($return=false) {
aa330ebb 248 ob_start();
249 $this->table->print_html();
250 $html = ob_get_clean();
251 if ($return) {
252 return $html;
253 } else {
254 echo $html;
255 }
256 }
257
258 /**
259 * Processes the data sent by the form (grades and feedbacks).
260 * @var array $data
261 * @return bool Success or Failure (array of errors).
262 */
653a8648 263 function process_data($data) {
5c75a0a3 264 }
653a8648 265 function process_action($target, $action) {
aa330ebb 266 }
c9451960
AG
267
268 /**
269 * This report supports being set as the 'my grades' report.
270 */
271 public static function supports_mygrades() {
272 return true;
273 }
60574063 274}
aa330ebb 275
60574063 276function grade_report_overview_settings_definition(&$mform) {
277 global $CFG;
278
61541a5a 279 //show rank
60574063 280 $options = array(-1 => get_string('default', 'grades'),
281 0 => get_string('hide'),
282 1 => get_string('show'));
283
180acd64 284 if (empty($CFG->grade_report_overview_showrank)) {
60574063 285 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
286 } else {
287 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
288 }
289
290 $mform->addElement('select', 'report_overview_showrank', get_string('showrank', 'grades'), $options);
bf03b43f 291 $mform->addHelpButton('report_overview_showrank', 'showrank', 'grades');
61541a5a
AD
292
293 //showtotalsifcontainhidden
294 $options = array(-1 => get_string('default', 'grades'),
295 GRADE_REPORT_HIDE_TOTAL_IF_CONTAINS_HIDDEN => get_string('hide'),
296 GRADE_REPORT_SHOW_TOTAL_IF_CONTAINS_HIDDEN => get_string('hidetotalshowexhiddenitems', 'grades'),
297 GRADE_REPORT_SHOW_REAL_TOTAL_IF_CONTAINS_HIDDEN => get_string('hidetotalshowinchiddenitems', 'grades') );
298
299 if (empty($CFG->grade_report_overview_showtotalsifcontainhidden)) {
300 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
301 } else {
302 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
303 }
304
305 $mform->addElement('select', 'report_overview_showtotalsifcontainhidden', get_string('hidetotalifhiddenitems', 'grades'), $options);
0459cc4d 306 $mform->addHelpButton('report_overview_showtotalsifcontainhidden', 'hidetotalifhiddenitems', 'grades');
aa330ebb 307}
60574063 308
6c3ef410 309