MDL-31353 grade: added a missing YUI requires
[moodle.git] / grade / report / grader / lib.php
CommitLineData
e060e33d 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
8ad36f4c 17
4ba9941c 18/**
19 * File in which the grader_report class is defined.
20 * @package gradebook
21 */
22
eea6690a 23require_once($CFG->dirroot . '/grade/report/lib.php');
4ba9941c 24require_once($CFG->libdir.'/tablelib.php');
4ba9941c 25
26/**
27 * Class providing an API for the grader report building and displaying.
eea6690a 28 * @uses grade_report
4ba9941c 29 * @package gradebook
30 */
eea6690a 31class grade_report_grader extends grade_report {
4ba9941c 32 /**
33 * The final grades.
b89a70ce 34 * @var array $grades
4ba9941c 35 */
d24832f9 36 public $grades;
4ba9941c 37
38 /**
39 * Array of errors for bulk grades updating.
40 * @var array $gradeserror
41 */
d24832f9 42 public $gradeserror = array();
4ba9941c 43
4ba9941c 44//// SQL-RELATED
45
4ba9941c 46 /**
47 * The id of the grade_item by which this report will be sorted.
48 * @var int $sortitemid
49 */
d24832f9 50 public $sortitemid;
4ba9941c 51
52 /**
53 * Sortorder used in the SQL selections.
54 * @var int $sortorder
55 */
d24832f9 56 public $sortorder;
4ba9941c 57
58 /**
59 * An SQL fragment affecting the search for users.
60 * @var string $userselect
61 */
d24832f9 62 public $userselect;
63
64 /**
65 * The bound params for $userselect
319770d7 66 * @var array $userselectparams
d24832f9 67 */
319770d7 68 public $userselectparams = array();
4ba9941c 69
4faf5f99 70 /**
384960dd 71 * List of collapsed categories from user preference
4faf5f99 72 * @var array $collapsed
73 */
d24832f9 74 public $collapsed;
4faf5f99 75
66ef0471 76 /**
77 * A count of the rows, used for css classes.
78 * @var int $rowcount
79 */
d24832f9 80 public $rowcount = 0;
66ef0471 81
6cc3e350 82 /**
57068674 83 * Capability check caching
84 * */
d24832f9 85 public $canviewhidden;
57068674 86
319770d7 87 var $preferencespage=false;
653a8648 88
92ff6236
AD
89 /**
90 * Length at which feedback will be truncated (to the nearest word) and an ellipsis be added.
91 * TODO replace this by a report preference
92 * @var int $feedback_trunc_length
93 */
94 protected $feedback_trunc_length = 50;
95
4ba9941c 96 /**
97 * Constructor. Sets local copies of user preferences and initialises grade_tree.
98 * @param int $courseid
d30c4481 99 * @param object $gpr grade plugin return tracking object
eea6690a 100 * @param string $context
101 * @param int $page The current page being viewed (when report is paged)
102 * @param int $sortitemid The id of the grade_item by which to sort the table
4ba9941c 103 */
d24832f9 104 public function __construct($courseid, $gpr, $context, $page=null, $sortitemid=null) {
4ba9941c 105 global $CFG;
d24832f9 106 parent::__construct($courseid, $gpr, $context, $page);
4ba9941c 107
57068674 108 $this->canviewhidden = has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $this->course->id));
109
4faf5f99 110 // load collapsed settings for this report
111 if ($collapsed = get_user_preferences('grade_report_grader_collapsed_categories')) {
112 $this->collapsed = unserialize($collapsed);
4faf5f99 113 } else {
384960dd 114 $this->collapsed = array('aggregatesonly' => array(), 'gradesonly' => array());
4faf5f99 115 }
384960dd 116
aea4df41 117 if (empty($CFG->enableoutcomes)) {
118 $nooutcomes = false;
119 } else {
120 $nooutcomes = get_user_preferences('grade_report_shownooutcomes');
121 }
122
e0724506 123 // if user report preference set or site report setting set use it, otherwise use course or site setting
124 $switch = $this->get_pref('aggregationposition');
05766b50 125 if ($switch == '') {
e0724506 126 $switch = grade_get_setting($this->courseid, 'aggregationposition', $CFG->grade_aggregationposition);
127 }
128
4faf5f99 129 // Grab the grade_tree for this course
e0724506 130 $this->gtree = new grade_tree($this->courseid, true, $switch, $this->collapsed, $nooutcomes);
4faf5f99 131
4ba9941c 132 $this->sortitemid = $sortitemid;
133
4ba9941c 134 // base url for sorting by first/last name
09cef06a 135
319770d7 136 $this->baseurl = new moodle_url('index.php', array('id' => $this->courseid));
137
138 $studentsperpage = $this->get_pref('studentsperpage');
09cef06a 139 if (!empty($studentsperpage)) {
319770d7 140 $this->baseurl->params(array('perpage' => $studentsperpage, 'page' => $this->page));
09cef06a 141 }
09cef06a 142
a6855934 143 $this->pbarurl = new moodle_url('/grade/report/grader/index.php', array('id' => $this->courseid, 'perpage' => $studentsperpage));
4ba9941c 144
35079f53 145 $this->setup_groups();
4ba9941c 146
147 $this->setup_sortitemid();
148 }
149
4ba9941c 150 /**
151 * Processes the data sent by the form (grades and feedbacks).
6ef4878b 152 * Caller is responsible for all access control checks
75674470 153 * @param array $data form submission (with magic quotes)
154 * @return array empty array if success, array of warnings if something fails.
4ba9941c 155 */
d24832f9 156 public function process_data($data) {
5c75a0a3 157 global $DB;
75674470 158 $warnings = array();
2cc773f5 159
30ebb74f 160 $separategroups = false;
161 $mygroups = array();
162 if ($this->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $this->context)) {
163 $separategroups = true;
164 $mygroups = groups_get_user_groups($this->course->id);
165 $mygroups = $mygroups[0]; // ignore groupings
ce8df92d 166 // reorder the groups fro better perf below
30ebb74f 167 $current = array_search($this->currentgroup, $mygroups);
168 if ($current !== false) {
169 unset($mygroups[$current]);
170 array_unshift($mygroups, $this->currentgroup);
171 }
172 }
173
4ba9941c 174 // always initialize all arrays
175 $queue = array();
4ba9941c 176 foreach ($data as $varname => $postedvalue) {
4ba9941c 177
178 $needsupdate = false;
4ba9941c 179
180 // skip, not a grade nor feedback
79eabc2a 181 if (strpos($varname, 'grade') === 0) {
319770d7 182 $datatype = 'grade';
79eabc2a 183 } else if (strpos($varname, 'feedback') === 0) {
319770d7 184 $datatype = 'feedback';
4ba9941c 185 } else {
186 continue;
187 }
188
189 $gradeinfo = explode("_", $varname);
4ba9941c 190 $userid = clean_param($gradeinfo[1], PARAM_INT);
191 $itemid = clean_param($gradeinfo[2], PARAM_INT);
192
29a5680e 193 $oldvalue = $data->{'old'.$varname};
194
195 // was change requested?
99ccfda8 196 if ($oldvalue == $postedvalue) { // string comparison
29a5680e 197 continue;
198 }
199
319770d7 200 if (!$gradeitem = grade_item::fetch(array('id'=>$itemid, 'courseid'=>$this->courseid))) { // we must verify course id here!
14398fd6 201 print_error('invalidgradeitmeid');
4ba9941c 202 }
203
204 // Pre-process grade
319770d7 205 if ($datatype == 'grade') {
4256a134 206 $feedback = false;
207 $feedbackformat = false;
319770d7 208 if ($gradeitem->gradetype == GRADE_TYPE_SCALE) {
4ba9941c 209 if ($postedvalue == -1) { // -1 means no grade
210 $finalgrade = null;
211 } else {
29a5680e 212 $finalgrade = $postedvalue;
4ba9941c 213 }
214 } else {
76317c73 215 $finalgrade = unformat_float($postedvalue);
4ba9941c 216 }
79eabc2a 217
0a2c8485 218 $errorstr = '';
379ea949 219 // Warn if the grade is out of bounds.
220 if (is_null($finalgrade)) {
221 // ok
653a8648 222 } else {
319770d7 223 $bounded = $gradeitem->bounded_grade($finalgrade);
653a8648 224 if ($bounded > $finalgrade) {
0a2c8485 225 $errorstr = 'lessthanmin';
653a8648 226 } else if ($bounded < $finalgrade) {
78946b9b
PS
227 $errorstr = 'morethanmax';
228 }
0a2c8485 229 }
230 if ($errorstr) {
5c75a0a3 231 $user = $DB->get_record('user', array('id' => $userid), 'id, firstname, lastname');
ace9051c 232 $gradestr = new stdClass();
0a2c8485 233 $gradestr->username = fullname($user);
319770d7 234 $gradestr->itemname = $gradeitem->get_name();
9d35e66e 235 $warnings[] = get_string($errorstr, 'grades', $gradestr);
0a2c8485 236 }
237
319770d7 238 } else if ($datatype == 'feedback') {
4256a134 239 $finalgrade = false;
79eabc2a 240 $trimmed = trim($postedvalue);
241 if (empty($trimmed)) {
29a5680e 242 $feedback = NULL;
e1d2692a 243 } else {
e6fec85d 244 $feedback = $postedvalue;
e1d2692a 245 }
4ba9941c 246 }
4ba9941c 247
30ebb74f 248 // group access control
249 if ($separategroups) {
250 // note: we can not use $this->currentgroup because it would fail badly
251 // when having two browser windows each with different group
252 $sharinggroup = false;
253 foreach($mygroups as $groupid) {
254 if (groups_is_member($groupid, $userid)) {
255 $sharinggroup = true;
256 break;
257 }
258 }
259 if (!$sharinggroup) {
6ef4878b 260 // either group membership changed or somebody is hacking grades of other group
30ebb74f 261 $warnings[] = get_string('errorsavegrade', 'grades');
262 continue;
263 }
264 }
265
319770d7 266 $gradeitem->update_final_grade($userid, $finalgrade, 'gradebook', $feedback, FORMAT_MOODLE);
4ba9941c 267 }
268
75674470 269 return $warnings;
4ba9941c 270 }
271
4ba9941c 272
273 /**
274 * Setting the sort order, this depends on last state
275 * all this should be in the new table class that we might need to use
276 * for displaying grades.
277 */
d24832f9 278 private function setup_sortitemid() {
63d6efa2 279
280 global $SESSION;
281
4ba9941c 282 if ($this->sortitemid) {
283 if (!isset($SESSION->gradeuserreport->sort)) {
a80112f0 284 if ($this->sortitemid == 'firstname' || $this->sortitemid == 'lastname') {
285 $this->sortorder = $SESSION->gradeuserreport->sort = 'ASC';
286 } else {
287 $this->sortorder = $SESSION->gradeuserreport->sort = 'DESC';
288 }
4ba9941c 289 } else {
290 // this is the first sort, i.e. by last name
291 if (!isset($SESSION->gradeuserreport->sortitemid)) {
a80112f0 292 if ($this->sortitemid == 'firstname' || $this->sortitemid == 'lastname') {
d24832f9 293 $this->sortorder = $SESSION->gradeuserreport->sort = 'ASC';
a80112f0 294 } else {
295 $this->sortorder = $SESSION->gradeuserreport->sort = 'DESC';
296 }
4ba9941c 297 } else if ($SESSION->gradeuserreport->sortitemid == $this->sortitemid) {
298 // same as last sort
299 if ($SESSION->gradeuserreport->sort == 'ASC') {
300 $this->sortorder = $SESSION->gradeuserreport->sort = 'DESC';
301 } else {
302 $this->sortorder = $SESSION->gradeuserreport->sort = 'ASC';
303 }
304 } else {
a80112f0 305 if ($this->sortitemid == 'firstname' || $this->sortitemid == 'lastname') {
306 $this->sortorder = $SESSION->gradeuserreport->sort = 'ASC';
307 } else {
308 $this->sortorder = $SESSION->gradeuserreport->sort = 'DESC';
309 }
4ba9941c 310 }
311 }
312 $SESSION->gradeuserreport->sortitemid = $this->sortitemid;
313 } else {
314 // not requesting sort, use last setting (for paging)
315
316 if (isset($SESSION->gradeuserreport->sortitemid)) {
317 $this->sortitemid = $SESSION->gradeuserreport->sortitemid;
d20737e8 318 }else{
319 $this->sortitemid = 'lastname';
4ba9941c 320 }
d20737e8 321
4ba9941c 322 if (isset($SESSION->gradeuserreport->sort)) {
323 $this->sortorder = $SESSION->gradeuserreport->sort;
324 } else {
325 $this->sortorder = 'ASC';
326 }
327 }
328 }
329
4ba9941c 330 /**
b50371da 331 * pulls out the userids of the users to be display, and sorts them
4ba9941c 332 */
d24832f9 333 public function load_users() {
334 global $CFG, $DB;
b50371da 335
ef8f084c 336 //limit to users with a gradeable role
337 list($gradebookrolessql, $gradebookrolesparams) = $DB->get_in_or_equal(explode(',', $this->gradebookroles), SQL_PARAMS_NAMED, 'grbr0');
4ba9941c 338
ef8f084c 339 //limit to users with an active enrollment
340 list($enrolledsql, $enrolledparams) = get_enrolled_sql($this->context);
341
342 //fields we need from the user table
e4aec5be 343 $userfields = user_picture::fields('u');
344 $userfields .= get_extra_user_fields_sql($this->context);
ef8f084c 345
656d17c2
AD
346 $sortjoin = $sort = $params = null;
347
ef8f084c 348 //if the user has clicked one of the sort asc/desc arrows
4ba9941c 349 if (is_numeric($this->sortitemid)) {
ef8f084c 350 $params = array_merge(array('gitemid'=>$this->sortitemid), $gradebookrolesparams, $this->groupwheresql_params, $enrolledparams);
09d0ef21 351
656d17c2
AD
352 $sortjoin = "LEFT JOIN {grade_grades} g ON g.userid = u.id AND g.itemid = $this->sortitemid";
353 $sort = "g.finalgrade $this->sortorder";
09d0ef21 354
4ba9941c 355 } else {
656d17c2 356 $sortjoin = '';
09d0ef21 357 switch($this->sortitemid) {
358 case 'lastname':
656d17c2
AD
359 $sort = "u.lastname $this->sortorder, u.firstname $this->sortorder";
360 break;
09d0ef21 361 case 'firstname':
656d17c2
AD
362 $sort = "u.firstname $this->sortorder, u.lastname $this->sortorder";
363 break;
09d0ef21 364 case 'idnumber':
365 default:
656d17c2
AD
366 $sort = "u.idnumber $this->sortorder";
367 break;
c421ad4b 368 }
4ba9941c 369
ef8f084c 370 $params = array_merge($gradebookrolesparams, $this->groupwheresql_params, $enrolledparams);
4ba9941c 371 }
372
656d17c2
AD
373 $sql = "SELECT $userfields
374 FROM {user} u
375 JOIN ($enrolledsql) je ON je.id = u.id
376 $this->groupsql
377 $sortjoin
378 JOIN (
379 SELECT DISTINCT ra.userid
380 FROM {role_assignments} ra
381 WHERE ra.roleid IN ($this->gradebookroles)
382 AND ra.contextid " . get_related_contexts_string($this->context) . "
383 ) rainner ON rainner.userid = u.id
384 AND u.deleted = 0
385 $this->groupwheresql
386 ORDER BY $sort";
09d0ef21 387
d24832f9 388 $this->users = $DB->get_records_sql($sql, $params, $this->get_pref('studentsperpage') * $this->page, $this->get_pref('studentsperpage'));
09d0ef21 389
4ba9941c 390 if (empty($this->users)) {
391 $this->userselect = '';
392 $this->users = array();
0a695c36 393 $this->userselect_params = array();
4ba9941c 394 } else {
3aafffc0 395 list($usql, $uparams) = $DB->get_in_or_equal(array_keys($this->users), SQL_PARAMS_NAMED, 'usid0');
d24832f9 396 $this->userselect = "AND g.userid $usql";
3aafffc0
AD
397 $this->userselect_params = $uparams;
398
399 //add a flag to each user indicating whether their enrolment is active
400 $sql = "SELECT ue.userid
401 FROM {user_enrolments} ue
402 JOIN {enrol} e ON e.id = ue.enrolid
403 WHERE ue.userid $usql
404 AND ue.status = :uestatus
405 AND e.status = :estatus
406 AND e.courseid = :courseid
407 GROUP BY ue.userid";
408 $coursecontext = get_course_context($this->context);
409 $params = array_merge($uparams, array('estatus'=>ENROL_INSTANCE_ENABLED, 'uestatus'=>ENROL_USER_ACTIVE, 'courseid'=>$coursecontext->instanceid));
410 $useractiveenrolments = $DB->get_records_sql($sql, $params);
411
412 foreach ($this->users as $user) {
413 $this->users[$user->id]->suspendedenrolment = !array_key_exists($user->id, $useractiveenrolments);
414 }
4ba9941c 415 }
416
417 return $this->users;
418 }
419
4ba9941c 420 /**
421 * we supply the userids in this query, and get all the grades
422 * pulls out all the grades, this does not need to worry about paging
423 */
d24832f9 424 public function load_final_grades() {
425 global $CFG, $DB;
4ba9941c 426
6ef4878b 427 // please note that we must fetch all grade_grades fields if we want to construct grade_grade object from it!
b50371da 428 $params = array_merge(array('courseid'=>$this->courseid), $this->userselect_params);
b89a70ce 429 $sql = "SELECT g.*
d24832f9 430 FROM {grade_items} gi,
431 {grade_grades} g
b50371da 432 WHERE g.itemid = gi.id AND gi.courseid = :courseid {$this->userselect}";
b89a70ce 433
434 $userids = array_keys($this->users);
4ba9941c 435
d297269d 436
d24832f9 437 if ($grades = $DB->get_records_sql($sql, $params)) {
b89a70ce 438 foreach ($grades as $graderec) {
d24832f9 439 if (in_array($graderec->userid, $userids) and array_key_exists($graderec->itemid, $this->gtree->get_items())) { // some items may not be present!!
b89a70ce 440 $this->grades[$graderec->userid][$graderec->itemid] = new grade_grade($graderec, false);
d24832f9 441 $this->grades[$graderec->userid][$graderec->itemid]->grade_item =& $this->gtree->get_item($graderec->itemid); // db caching
b89a70ce 442 }
443 }
444 }
445
446 // prefil grades that do not exist yet
447 foreach ($userids as $userid) {
d24832f9 448 foreach ($this->gtree->get_items() as $itemid=>$unused) {
b89a70ce 449 if (!isset($this->grades[$userid][$itemid])) {
450 $this->grades[$userid][$itemid] = new grade_grade();
478f4322 451 $this->grades[$userid][$itemid]->itemid = $itemid;
3b34f698 452 $this->grades[$userid][$itemid]->userid = $userid;
d24832f9 453 $this->grades[$userid][$itemid]->grade_item =& $this->gtree->get_item($itemid); // db caching
b89a70ce 454 }
4ba9941c 455 }
456 }
457 }
458
459 /**
460 * Builds and returns a div with on/off toggles.
461 * @return string HTML code
462 */
d24832f9 463 public function get_toggles_html() {
319770d7 464 global $CFG, $USER, $COURSE, $OUTPUT;
aea4df41 465
319770d7 466 $html = '';
2cc773f5 467 if ($USER->gradeediting[$this->courseid]) {
468 if (has_capability('moodle/grade:manage', $this->context) or has_capability('moodle/grade:hide', $this->context)) {
319770d7 469 $html .= $this->print_toggle('eyecons');
2cc773f5 470 }
471 if (has_capability('moodle/grade:manage', $this->context)
472 or has_capability('moodle/grade:lock', $this->context)
473 or has_capability('moodle/grade:unlock', $this->context)) {
319770d7 474 $html .= $this->print_toggle('locks');
2cc773f5 475 }
2ca093fa 476 if (has_capability('moodle/grade:manage', $this->context)) {
319770d7 477 $html .= $this->print_toggle('quickfeedback');
2ca093fa 478 }
479
2cc773f5 480 if (has_capability('moodle/grade:manage', $this->context)) {
319770d7 481 $html .= $this->print_toggle('calculations');
2cc773f5 482 }
4ba9941c 483 }
484
57068674 485 if ($this->canviewhidden) {
319770d7 486 $html .= $this->print_toggle('averages');
57068674 487 }
aae94377 488
319770d7 489 $html .= $this->print_toggle('ranges');
aea4df41 490 if (!empty($CFG->enableoutcomes)) {
319770d7 491 $html .= $this->print_toggle('nooutcomes');
aea4df41 492 }
319770d7 493
494 return $OUTPUT->container($html, 'grade-report-toggles');
4ba9941c 495 }
496
497 /**
498 * Shortcut function for printing the grader report toggles.
499 * @param string $type The type of toggle
500 * @param bool $return Whether to return the HTML string rather than printing it
501 * @return void
502 */
319770d7 503 public function print_toggle($type) {
e63f88c9 504 global $CFG, $OUTPUT;
4ba9941c 505
319770d7 506 $icons = array('eyecons' => 't/hide',
507 'calculations' => 't/calc',
508 'locks' => 't/lock',
509 'averages' => 't/mean',
510 'quickfeedback' => 't/feedback',
511 'nooutcomes' => 't/outcomes');
4ba9941c 512
319770d7 513 $prefname = 'grade_report_show' . $type;
aea4df41 514
319770d7 515 if (array_key_exists($prefname, $CFG)) {
516 $showpref = get_user_preferences($prefname, $CFG->$prefname);
aea4df41 517 } else {
319770d7 518 $showpref = get_user_preferences($prefname);
aea4df41 519 }
4ba9941c 520
388234f4 521 $strshow = $this->get_lang_string('show' . $type, 'grades');
522 $strhide = $this->get_lang_string('hide' . $type, 'grades');
4ba9941c 523
319770d7 524 $showhide = 'show';
525 $toggleaction = 1;
4ba9941c 526
319770d7 527 if ($showpref) {
528 $showhide = 'hide';
529 $toggleaction = 0;
4ba9941c 530 }
531
532 if (array_key_exists($type, $icons)) {
319770d7 533 $imagename = $icons[$type];
4ba9941c 534 } else {
b11681e0 535 $imagename = "t/$type";
4ba9941c 536 }
537
319770d7 538 $string = ${'str' . $showhide};
4ba9941c 539
e1513ca9 540 $url = new moodle_url($this->baseurl, array('toggle' => $toggleaction, 'toggle_type' => $type));
4ba9941c 541
c63923bd 542 $retval = $OUTPUT->container($OUTPUT->action_icon($url, new pix_icon($imagename, $string))); // TODO: this container looks wrong here
4ba9941c 543
319770d7 544 return $retval;
4ba9941c 545 }
546
547 /**
6c096a49
NC
548 * Builds and returns the rows that will make up the left part of the grader report
549 * This consists of student names and icons, links to user reports and id numbers, as well
550 * as header cells for these columns. It also includes the fillers required for the
551 * categories displayed on the right side of the report.
552 * @return array Array of html_table_row objects
4ba9941c 553 */
6c096a49 554 public function get_left_rows() {
319770d7 555 global $CFG, $USER, $OUTPUT;
4ba9941c 556
6c096a49
NC
557 $rows = array();
558
559 $showuserimage = $this->get_pref('showuserimage');
795b745a 560 $fixedstudents = $this->is_fixed_students();
203b7e2e 561
6c096a49
NC
562 $strfeedback = $this->get_lang_string("feedback");
563 $strgrade = $this->get_lang_string('grade');
203b7e2e 564
e4aec5be 565 $extrafields = get_extra_user_fields($this->context);
6c096a49 566
e4aec5be 567 $arrows = $this->get_sort_arrows($extrafields);
6c096a49 568
e4aec5be 569 $colspan = 1;
3e7ca6b3 570 if (has_capability('gradereport/'.$CFG->grade_profilereport.':view', $this->context)) {
6c096a49
NC
571 $colspan++;
572 }
e4aec5be 573 $colspan += count($extrafields);
6c096a49
NC
574
575 $levels = count($this->gtree->levels) - 1;
576
577 for ($i = 0; $i < $levels; $i++) {
578 $fillercell = new html_table_cell();
16be8974 579 $fillercell->attributes['class'] = 'fixedcolumn cell topleft';
6c096a49
NC
580 $fillercell->text = ' ';
581 $fillercell->colspan = $colspan;
8cea545e 582 $row = new html_table_row(array($fillercell));
6c096a49
NC
583 $rows[] = $row;
584 }
585
586 $headerrow = new html_table_row();
16be8974 587 $headerrow->attributes['class'] = 'heading';
6c096a49
NC
588
589 $studentheader = new html_table_cell();
16be8974 590 $studentheader->attributes['class'] = 'header';
6c096a49
NC
591 $studentheader->scope = 'col';
592 $studentheader->header = true;
593 $studentheader->id = 'studentheader';
3e7ca6b3 594 if (has_capability('gradereport/'.$CFG->grade_profilereport.':view', $this->context)) {
6c096a49
NC
595 $studentheader->colspan = 2;
596 }
597 $studentheader->text = $arrows['studentname'];
598
599 $headerrow->cells[] = $studentheader;
600
e4aec5be 601 foreach ($extrafields as $field) {
602 $fieldheader = new html_table_cell();
603 $fieldheader->attributes['class'] = 'header userfield user' . $field;
604 $fieldheader->scope = 'col';
605 $fieldheader->header = true;
606 $fieldheader->text = $arrows[$field];
6c096a49 607
e4aec5be 608 $headerrow->cells[] = $fieldheader;
6c096a49
NC
609 }
610
611 $rows[] = $headerrow;
612
613 $rows = $this->get_left_icons_row($rows, $colspan);
614
615 $rowclasses = array('even', 'odd');
616
3aafffc0 617 $suspendedstring = null;
6c096a49
NC
618 foreach ($this->users as $userid => $user) {
619 $userrow = new html_table_row();
fe213365 620 $userrow->id = 'fixed_user_'.$userid;
abfe77ea 621 $userrow->attributes['class'] = 'r'.$this->rowcount++.' '.$rowclasses[$this->rowcount % 2];
6c096a49
NC
622
623 $usercell = new html_table_cell();
16be8974 624 $usercell->attributes['class'] = 'user';
3aafffc0 625
6c096a49
NC
626 $usercell->header = true;
627 $usercell->scope = 'row';
6c096a49
NC
628
629 if ($showuserimage) {
bea2086c 630 $usercell->text = $OUTPUT->user_picture($user);
203b7e2e 631 }
632
a6855934 633 $usercell->text .= html_writer::link(new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $this->course->id)), fullname($user));
6c096a49 634
3aafffc0
AD
635 if (!empty($user->suspendedenrolment)) {
636 $usercell->attributes['class'] .= ' usersuspended';
637
638 //may be lots of suspended users so only get the string once
639 if (empty($suspendedstring)) {
640 $suspendedstring = get_string('userenrolmentsuspended', 'grades');
641 }
642 $usercell->text .= html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('i/enrolmentsuspended'), 'title'=>$suspendedstring, 'alt'=>$suspendedstring, 'class'=>'usersuspendedicon'));
643 }
644
6c096a49
NC
645 $userrow->cells[] = $usercell;
646
3e7ca6b3 647 if (has_capability('gradereport/'.$CFG->grade_profilereport.':view', $this->context)) {
6c096a49 648 $userreportcell = new html_table_cell();
16be8974 649 $userreportcell->attributes['class'] = 'userreport';
6c096a49 650 $userreportcell->header = true;
ace9051c 651 $a = new stdClass();
6c096a49
NC
652 $a->user = fullname($user);
653 $strgradesforuser = get_string('gradesforuser', 'grades', $a);
a6855934 654 $url = new moodle_url('/grade/report/'.$CFG->grade_profilereport.'/index.php', array('userid' => $user->id, 'id' => $this->course->id));
c63923bd 655 $userreportcell->text = $OUTPUT->action_icon($url, new pix_icon('t/grades', $strgradesforuser));
6c096a49 656 $userrow->cells[] = $userreportcell;
203b7e2e 657 }
658
e4aec5be 659 foreach ($extrafields as $field) {
660 $fieldcell = new html_table_cell();
661 $fieldcell->attributes['class'] = 'header userfield user' . $field;
662 $fieldcell->header = true;
663 $fieldcell->scope = 'row';
664 $fieldcell->text = $user->{$field};
665 $userrow->cells[] = $fieldcell;
6c096a49
NC
666 }
667
668 $rows[] = $userrow;
203b7e2e 669 }
4ba9941c 670
6c096a49
NC
671 $rows = $this->get_left_range_row($rows, $colspan);
672 $rows = $this->get_left_avg_row($rows, $colspan, true);
673 $rows = $this->get_left_avg_row($rows, $colspan);
4ba9941c 674
6c096a49
NC
675 return $rows;
676 }
677
678 /**
679 * Builds and returns the rows that will make up the right part of the grader report
680 * @return array Array of html_table_row objects
681 */
682 public function get_right_rows() {
fe213365 683 global $CFG, $USER, $OUTPUT, $DB, $PAGE;
4ba9941c 684
6c096a49
NC
685 $rows = array();
686 $this->rowcount = 0;
687 $numrows = count($this->gtree->get_levels());
688 $numusers = count($this->users);
689 $gradetabindex = 1;
319770d7 690 $columnstounset = array();
6c096a49 691 $strgrade = $this->get_lang_string('grade');
fe213365 692 $strfeedback = $this->get_lang_string("feedback");
6c096a49 693 $arrows = $this->get_sort_arrows();
cb7fe7b4 694
fe213365
SH
695 $jsarguments = array(
696 'id' => '#fixed_column',
697 'cfg' => array('ajaxenabled'=>false),
698 'items' => array(),
699 'users' => array(),
700 'feedback' => array()
701 );
702 $jsscales = array();
703
d24832f9 704 foreach ($this->gtree->get_levels() as $key=>$row) {
4ba9941c 705 if ($key == 0) {
cb7fe7b4 706 // do not display course grade category
4ba9941c 707 // continue;
708 }
709
6c096a49 710 $headingrow = new html_table_row();
16be8974 711 $headingrow->attributes['class'] = 'heading_name_row';
4ba9941c 712
cb7fe7b4 713 foreach ($row as $columnkey => $element) {
319770d7 714 $sortlink = clone($this->baseurl);
2e3987a9 715 if (isset($element['object']->id)) {
319770d7 716 $sortlink->param('sortitemid', $element['object']->id);
2e3987a9 717 }
718
cb7fe7b4 719 $eid = $element['eid'];
720 $object = $element['object'];
721 $type = $element['type'];
438a5aa9 722 $categorystate = @$element['categorystate'];
8c5a416e 723
4ba9941c 724 if (!empty($element['colspan'])) {
6c096a49 725 $colspan = $element['colspan'];
4ba9941c 726 } else {
6c096a49 727 $colspan = 1;
4ba9941c 728 }
729
730 if (!empty($element['depth'])) {
6c096a49 731 $catlevel = 'catlevel'.$element['depth'];
4ba9941c 732 } else {
733 $catlevel = '';
734 }
735
cb7fe7b4 736// Element is a filler
4ba9941c 737 if ($type == 'filler' or $type == 'fillerfirst' or $type == 'fillerlast') {
6c096a49 738 $fillercell = new html_table_cell();
16be8974 739 $fillercell->attributes['class'] = $type . ' ' . $catlevel;
6c096a49
NC
740 $fillercell->colspan = $colspan;
741 $fillercell->text = '&nbsp;';
742 $fillercell->header = true;
743 $fillercell->scope = 'col';
744 $headingrow->cells[] = $fillercell;
cb7fe7b4 745 }
746// Element is a category
4faf5f99 747 else if ($type == 'category') {
6c096a49 748 $categorycell = new html_table_cell();
16be8974 749 $categorycell->attributes['class'] = 'category ' . $catlevel;
6c096a49
NC
750 $categorycell->colspan = $colspan;
751 $categorycell->text = shorten_text($element['object']->get_name());
752 $categorycell->text .= $this->get_collapsing_icon($element);
753 $categorycell->header = true;
754 $categorycell->scope = 'col';
4ba9941c 755
756 // Print icons
2cc773f5 757 if ($USER->gradeediting[$this->courseid]) {
6c096a49 758 $categorycell->text .= $this->get_icons($element);
4ba9941c 759 }
760
6c096a49 761 $headingrow->cells[] = $categorycell;
cb7fe7b4 762 }
763// Element is a grade_item
4faf5f99 764 else {
7bac3777
AD
765 //$itemmodule = $element['object']->itemmodule;
766 //$iteminstance = $element['object']->iteminstance;
2e3987a9 767
4ba9941c 768 if ($element['object']->id == $this->sortitemid) {
769 if ($this->sortorder == 'ASC') {
319770d7 770 $arrow = $this->get_sort_arrow('up', $sortlink);
4ba9941c 771 } else {
319770d7 772 $arrow = $this->get_sort_arrow('down', $sortlink);
4ba9941c 773 }
774 } else {
319770d7 775 $arrow = $this->get_sort_arrow('move', $sortlink);
4ba9941c 776 }
777
6c096a49
NC
778 $headerlink = $this->gtree->get_element_header($element, true, $this->get_pref('showactivityicons'), false);
779
780 $itemcell = new html_table_cell();
16be8974 781 $itemcell->attributes['class'] = $type . ' ' . $catlevel . 'highlightable';
6c096a49 782
4ba9941c 783 if ($element['object']->is_hidden()) {
16be8974 784 $itemcell->attributes['class'] .= ' hidden';
4ba9941c 785 }
786
6c096a49
NC
787 $itemcell->colspan = $colspan;
788 $itemcell->text = shorten_text($headerlink) . $arrow;
789 $itemcell->header = true;
790 $itemcell->scope = 'col';
6c096a49
NC
791
792 $headingrow->cells[] = $itemcell;
4ba9941c 793 }
4ba9941c 794 }
6c096a49 795 $rows[] = $headingrow;
4ba9941c 796 }
4ba9941c 797
6c096a49 798 $rows = $this->get_right_icons_row($rows);
4ba9941c 799
f17bb885 800 // Preload scale objects for items with a scaleid and initialize tab indices
319770d7 801 $scaleslist = array();
c0c1e7c2 802 $tabindices = array();
d297269d 803
fe213365
SH
804 foreach ($this->gtree->get_items() as $itemid=>$item) {
805 $scale = null;
388234f4 806 if (!empty($item->scaleid)) {
319770d7 807 $scaleslist[] = $item->scaleid;
fe213365
SH
808 $jsarguments['items'][$itemid] = array('id'=>$itemid, 'name'=>$item->get_name(true), 'type'=>'scale', 'scale'=>$item->scaleid, 'decimals'=>$item->get_decimals());
809 } else {
810 $jsarguments['items'][$itemid] = array('id'=>$itemid, 'name'=>$item->get_name(true), 'type'=>'value', 'scale'=>false, 'decimals'=>$item->get_decimals());
388234f4 811 }
c0c1e7c2 812 $tabindices[$item->id]['grade'] = $gradetabindex;
813 $tabindices[$item->id]['feedback'] = $gradetabindex + $numusers;
814 $gradetabindex += $numusers * 2;
388234f4 815 }
319770d7 816 $scalesarray = array();
388234f4 817
319770d7 818 if (!empty($scaleslist)) {
819 $scalesarray = $DB->get_records_list('scale', 'id', $scaleslist);
388234f4 820 }
fe213365 821 $jsscales = $scalesarray;
d24832f9 822
6c096a49 823 $rowclasses = array('even', 'odd');
a47d38f2 824
4ba9941c 825 foreach ($this->users as $userid => $user) {
b89a70ce 826
57068674 827 if ($this->canviewhidden) {
d297269d 828 $altered = array();
829 $unknown = array();
b89a70ce 830 } else {
319770d7 831 $hidingaffected = grade_grade::get_hiding_affected($this->grades[$userid], $this->gtree->get_items());
832 $altered = $hidingaffected['altered'];
833 $unknown = $hidingaffected['unknown'];
834 unset($hidingaffected);
b89a70ce 835 }
836
bb9b58a6 837
6c096a49 838 $itemrow = new html_table_row();
fe213365 839 $itemrow->id = 'user_'.$userid;
16be8974 840 $itemrow->attributes['class'] = $rowclasses[$this->rowcount % 2];
9d35e66e 841
fe213365
SH
842 $jsarguments['users'][$userid] = fullname($user);
843
dc482cfa 844 foreach ($this->gtree->items as $itemid=>$unused) {
845 $item =& $this->gtree->items[$itemid];
d297269d 846 $grade = $this->grades[$userid][$item->id];
b89a70ce 847
6c096a49
NC
848 $itemcell = new html_table_cell();
849
fe213365
SH
850 $itemcell->id = 'u'.$userid.'i'.$itemid;
851
e50ce569 852 // Get the decimal points preference for this item
31a6c06c 853 $decimalpoints = $item->get_decimals();
4ba9941c 854
d297269d 855 if (in_array($itemid, $unknown)) {
856 $gradeval = null;
857 } else if (array_key_exists($itemid, $altered)) {
858 $gradeval = $altered[$itemid];
859 } else {
860 $gradeval = $grade->finalgrade;
861 }
4ba9941c 862
00374cc5 863 // MDL-11274
864 // Hide grades in the grader report if the current grader doesn't have 'moodle/grade:viewhidden'
57068674 865 if (!$this->canviewhidden and $grade->is_hidden()) {
6cc3e350 866 if (!empty($CFG->grade_hiddenasdate) and $grade->get_datesubmitted() and !$item->is_category_item() and !$item->is_course_item()) {
d297269d 867 // the problem here is that we do not have the time when grade value was modified, 'timemodified' is general modification date for grade_grades records
26acc814 868 $itemcell->text = html_writer::tag('span', userdate($grade->get_datesubmitted(),get_string('strftimedatetimeshort')), array('class'=>'datesubmitted'));
bb49f77b 869 } else {
6c096a49 870 $itemcell->text = '-';
00374cc5 871 }
6c096a49 872 $itemrow->cells[] = $itemcell;
a5b8be62 873 continue;
00374cc5 874 }
875
2cc773f5 876 // emulate grade element
d3c3da1b 877 $eid = $this->gtree->get_grade_eid($grade);
878 $element = array('eid'=>$eid, 'object'=>$grade, 'type'=>'grade');
2cc773f5 879
16be8974 880 $itemcell->attributes['class'] .= ' grade';
dff9d94d 881 if ($item->is_category_item()) {
16be8974 882 $itemcell->attributes['class'] .= ' cat';
b89a70ce 883 }
dff9d94d 884 if ($item->is_course_item()) {
16be8974 885 $itemcell->attributes['class'] .= ' course';
b89a70ce 886 }
4ba9941c 887 if ($grade->is_overridden()) {
16be8974 888 $itemcell->attributes['class'] .= ' overridden';
4ba9941c 889 }
85db09fb 890
5ebce7bb 891 if ($grade->is_excluded()) {
16be8974 892 // $itemcell->attributes['class'] .= ' excluded';
5ebce7bb 893 }
4ba9941c 894
fe213365 895 if (!empty($grade->feedback)) {
92ff6236 896 //should we be truncating feedback? ie $short_feedback = shorten_text($feedback, $this->feedback_trunc_length);
fe213365 897 $jsarguments['feedback'][] = array('user'=>$userid, 'item'=>$itemid, 'content'=>wordwrap(trim(format_string($grade->feedback, $grade->feedbackformat)), 34, '<br/ >'));
653a8648 898 }
899
23207a1a 900 if ($grade->is_excluded()) {
26acc814 901 $itemcell->text .= html_writer::tag('span', get_string('excluded', 'grades'), array('class'=>'excludedfloater'));
23207a1a 902 }
903
4ba9941c 904 // Do not show any icons if no grade (no record in DB to match)
d3c3da1b 905 if (!$item->needsupdate and $USER->gradeediting[$this->courseid]) {
6c096a49 906 $itemcell->text .= $this->get_icons($element);
4ba9941c 907 }
908
80fb1cf9 909 $hidden = '';
910 if ($grade->is_hidden()) {
911 $hidden = ' hidden ';
912 }
6cc3e350 913
d24832f9 914 $gradepass = ' gradefail ';
46e6df89 915 if ($grade->is_passed($item)) {
916 $gradepass = ' gradepass ';
e6477988 917 } elseif (is_null($grade->is_passed($item))) {
918 $gradepass = '';
46e6df89 919 }
920
6ef4878b 921 // if in editing mode, we need to print either a text box
4ba9941c 922 // or a drop down (for scales)
4ba9941c 923 // grades in item of type grade category or course are not directly editable
d14ae855 924 if ($item->needsupdate) {
26acc814 925 $itemcell->text .= html_writer::tag('span', get_string('error'), array('class'=>"gradingerror$hidden"));
d14ae855 926
2cc773f5 927 } else if ($USER->gradeediting[$this->courseid]) {
4ba9941c 928
319770d7 929 if ($item->scaleid && !empty($scalesarray[$item->scaleid])) {
930 $scale = $scalesarray[$item->scaleid];
99ccfda8 931 $gradeval = (int)$gradeval; // scales use only integers
388234f4 932 $scales = explode(",", $scale->scale);
933 // reindex because scale is off 1
9d35e66e 934
914ea002 935 // MDL-12104 some previous scales might have taken up part of the array
936 // so this needs to be reset
d48ebf97 937 $scaleopt = array();
388234f4 938 $i = 0;
939 foreach ($scales as $scaleoption) {
940 $i++;
941 $scaleopt[$i] = $scaleoption;
942 }
943
944 if ($this->get_pref('quickgrading') and $grade->is_editable()) {
0658afc9 945 $oldval = empty($gradeval) ? -1 : $gradeval;
946 if (empty($item->outcomeid)) {
d4795a07 947 $nogradestr = $this->get_lang_string('nograde');
0658afc9 948 } else {
d4795a07 949 $nogradestr = $this->get_lang_string('nooutcome', 'grades');
0658afc9 950 }
fe213365
SH
951 $itemcell->text .= '<input type="hidden" id="oldgrade_'.$userid.'_'.$item->id.'" name="oldgrade_'.$userid.'_'.$item->id.'" value="'.$oldval.'"/>';
952 $attributes = array('tabindex' => $tabindices[$item->id]['grade'], 'id'=>'grade_'.$userid.'_'.$item->id);
064527d6 953 $itemcell->text .= html_writer::select($scaleopt, 'grade_'.$userid.'_'.$item->id, $gradeval, array(-1=>$nogradestr), $attributes);;
388234f4 954 } elseif(!empty($scale)) {
4ba9941c 955 $scales = explode(",", $scale->scale);
4ba9941c 956
388234f4 957 // invalid grade if gradeval < 1
99ccfda8 958 if ($gradeval < 1) {
26acc814 959 $itemcell->text .= html_writer::tag('span', '-', array('class'=>"gradevalue$hidden$gradepass"));
4ba9941c 960 } else {
653a8648 961 $gradeval = $grade->grade_item->bounded_grade($gradeval); //just in case somebody changes scale
26acc814 962 $itemcell->text .= html_writer::tag('span', $scales[$gradeval-1], array('class'=>"gradevalue$hidden$gradepass"));
4ba9941c 963 }
388234f4 964 } else {
965 // no such scale, throw error?
4ba9941c 966 }
79eabc2a 967
bb384a8e 968 } else if ($item->gradetype != GRADE_TYPE_TEXT) { // Value type
936f1350 969 if ($this->get_pref('quickgrading') and $grade->is_editable()) {
76317c73 970 $value = format_float($gradeval, $decimalpoints);
fe213365 971 $itemcell->text .= '<input type="hidden" id="oldgrade_'.$userid.'_'.$item->id.'" name="oldgrade_'.$userid.'_'.$item->id.'" value="'.$value.'" />';
6c096a49 972 $itemcell->text .= '<input size="6" tabindex="' . $tabindices[$item->id]['grade']
319770d7 973 . '" type="text" class="text" title="'. $strgrade .'" name="grade_'
fe213365 974 .$userid.'_' .$item->id.'" id="grade_'.$userid.'_'.$item->id.'" value="'.$value.'" />';
4ba9941c 975 } else {
26acc814 976 $itemcell->text .= html_writer::tag('span', format_float($gradeval, $decimalpoints), array('class'=>"gradevalue$hidden$gradepass"));
4ba9941c 977 }
978 }
979
980
981 // If quickfeedback is on, print an input element
2ca093fa 982 if ($this->get_pref('showquickfeedback') and $grade->is_editable()) {
dc482cfa 983
fe213365
SH
984 $itemcell->text .= '<input type="hidden" id="oldfeedback_'.$userid.'_'.$item->id.'" name="oldfeedback_'.$userid.'_'.$item->id.'" value="' . s($grade->feedback) . '" />';
985 $itemcell->text .= '<input class="quickfeedback" tabindex="' . $tabindices[$item->id]['feedback'].'" id="feedback_'.$userid.'_'.$item->id
986 . '" size="6" title="' . $strfeedback . '" type="text" name="feedback_'.$userid.'_'.$item->id.'" value="' . s($grade->feedback) . '" />';
4ba9941c 987 }
988
78a2d9f0 989 } else { // Not editing
41f22daa 990 $gradedisplaytype = $item->get_displaytype();
e50ce569 991
fe213365 992 if ($item->scaleid && !empty($scalesarray[$item->scaleid])) {
16be8974 993 $itemcell->attributes['class'] .= ' grade_type_scale';
fe213365 994 } else if ($item->gradetype != GRADE_TYPE_TEXT) {
16be8974 995 $itemcell->attributes['class'] .= ' grade_type_text';
fe213365 996 }
4ba9941c 997
6712973f
AD
998 if ($this->get_pref('enableajax')) {
999 $itemcell->attributes['class'] .= ' clickable';
1000 }
1001
d14ae855 1002 if ($item->needsupdate) {
26acc814 1003 $itemcell->text .= html_writer::tag('span', get_string('error'), array('class'=>"gradingerror$hidden$gradepass"));
4ba9941c 1004 } else {
26acc814 1005 $itemcell->text .= html_writer::tag('span', grade_format_gradevalue($gradeval, $item, true, $gradedisplaytype, null), array('class'=>"gradevalue$hidden$gradepass"));
c97933ff 1006 if ($this->get_pref('showanalysisicon')) {
f17bb885 1007 $itemcell->text .= $this->gtree->get_grade_analysis_icon($grade);
bb17ac1e 1008 }
4ba9941c 1009 }
4ba9941c 1010 }
1011
1012 if (!empty($this->gradeserror[$item->id][$userid])) {
6c096a49 1013 $itemcell->text .= $this->gradeserror[$item->id][$userid];
4ba9941c 1014 }
1015
6c096a49 1016 $itemrow->cells[] = $itemcell;
4ba9941c 1017 }
6c096a49 1018 $rows[] = $itemrow;
4ba9941c 1019 }
4ba9941c 1020
fe213365
SH
1021 if ($this->get_pref('enableajax')) {
1022 $jsarguments['cfg']['ajaxenabled'] = true;
1023 $jsarguments['cfg']['scales'] = array();
1024 foreach ($jsscales as $scale) {
1025 $jsarguments['cfg']['scales'][$scale->id] = explode(',',$scale->scale);
1026 }
1027 $jsarguments['cfg']['feedbacktrunclength'] = $this->feedback_trunc_length;
92ff6236
AD
1028
1029 //feedbacks are now being stored in $jsarguments['feedback'] in get_right_rows()
1030 //$jsarguments['cfg']['feedback'] = $this->feedbacks;
fe213365
SH
1031 }
1032 $jsarguments['cfg']['isediting'] = (bool)$USER->gradeediting[$this->courseid];
1033 $jsarguments['cfg']['courseid'] = $this->courseid;
1034 $jsarguments['cfg']['studentsperpage'] = $this->get_pref('studentsperpage');
1035 $jsarguments['cfg']['showquickfeedback'] = (bool)$this->get_pref('showquickfeedback');
1036
1037 $module = array(
1038 'name' => 'gradereport_grader',
1039 'fullpath' => '/grade/report/grader/module.js',
227e9023 1040 'requires' => array('base', 'dom', 'event', 'event-mouseenter', 'event-key', 'io-queue', 'json-parse', 'overlay')
fe213365
SH
1041 );
1042 $PAGE->requires->js_init_call('M.gradereport_grader.init_report', $jsarguments, false, $module);
1043 $PAGE->requires->strings_for_js(array('addfeedback','feedback', 'grade'), 'grades');
1044 $PAGE->requires->strings_for_js(array('ajaxchoosescale','ajaxclicktoclose','ajaxerror','ajaxfailedupdate', 'ajaxfieldchanged'), 'gradereport_grader');
1045
6c096a49
NC
1046 $rows = $this->get_right_range_row($rows);
1047 $rows = $this->get_right_avg_row($rows, true);
1048 $rows = $this->get_right_avg_row($rows);
dc482cfa 1049
6c096a49
NC
1050 return $rows;
1051 }
dc482cfa 1052
6c096a49
NC
1053 /**
1054 * Depending on the style of report (fixedstudents vs traditional one-table),
1055 * arranges the rows of data in one or two tables, and returns the output of
1056 * these tables in HTML
1057 * @return string HTML
1058 */
1059 public function get_grade_table() {
1060 global $OUTPUT;
1061 $fixedstudents = $this->is_fixed_students();
dc482cfa 1062
6c096a49
NC
1063 $leftrows = $this->get_left_rows();
1064 $rightrows = $this->get_right_rows();
dc482cfa 1065
6c096a49 1066 $html = '';
dc482cfa 1067
fe213365 1068
203b7e2e 1069 if ($fixedstudents) {
319770d7 1070 $fixedcolumntable = new html_table();
1071 $fixedcolumntable->id = 'fixed_column';
6c096a49 1072 $fixedcolumntable->data = $leftrows;
16be8974 1073 $html .= $OUTPUT->container(html_writer::table($fixedcolumntable), 'left_scroller');
dc482cfa 1074
6c096a49
NC
1075 $righttable = new html_table();
1076 $righttable->id = 'user-grades';
6c096a49 1077 $righttable->data = $rightrows;
319770d7 1078
16be8974 1079 $html .= $OUTPUT->container(html_writer::table($righttable), 'right_scroller');
6c096a49
NC
1080 } else {
1081 $fulltable = new html_table();
16be8974 1082 $fulltable->attributes['class'] = 'gradestable flexible boxaligncenter generaltable';
6c096a49
NC
1083 $fulltable->id = 'user-grades';
1084
1085 // Extract rows from each side (left and right) and collate them into one row each
1086 foreach ($leftrows as $key => $row) {
1087 $row->cells = array_merge($row->cells, $rightrows[$key]->cells);
1088 $fulltable->data[] = $row;
203b7e2e 1089 }
16be8974 1090 $html .= html_writer::table($fulltable);
6c096a49
NC
1091 }
1092 return $OUTPUT->container($html, 'gradeparent');
1093 }
dc482cfa 1094
6c096a49
NC
1095 /**
1096 * Builds and return the row of icons for the left side of the report.
1097 * It only has one cell that says "Controls"
1098 * @param array $rows The Array of rows for the left part of the report
1099 * @param int $colspan The number of columns this cell has to span
1100 * @return array Array of rows for the left part of the report
1101 */
1102 public function get_left_icons_row($rows=array(), $colspan=1) {
1103 global $USER;
dc482cfa 1104
6c096a49
NC
1105 if ($USER->gradeediting[$this->courseid]) {
1106 $controlsrow = new html_table_row();
16be8974 1107 $controlsrow->attributes['class'] = 'controls';
6c096a49 1108 $controlscell = new html_table_cell();
16be8974 1109 $controlscell->attributes['class'] = 'header controls';
6c096a49
NC
1110 $controlscell->colspan = $colspan;
1111 $controlscell->text = $this->get_lang_string('controls','grades');
1112
1113 $controlsrow->cells[] = $controlscell;
1114 $rows[] = $controlsrow;
1115 }
1116 return $rows;
1117 }
dc482cfa 1118
6c096a49
NC
1119 /**
1120 * Builds and return the header for the row of ranges, for the left part of the grader report.
1121 * @param array $rows The Array of rows for the left part of the report
1122 * @param int $colspan The number of columns this cell has to span
1123 * @return array Array of rows for the left part of the report
1124 */
1125 public function get_left_range_row($rows=array(), $colspan=1) {
1126 global $CFG, $USER;
bb9b58a6 1127
6c096a49
NC
1128 if ($this->get_pref('showranges')) {
1129 $rangerow = new html_table_row();
16be8974 1130 $rangerow->attributes['class'] = 'range r'.$this->rowcount++;
6c096a49 1131 $rangecell = new html_table_cell();
16be8974 1132 $rangecell->attributes['class'] = 'header range';
6c096a49
NC
1133 $rangecell->colspan = $colspan;
1134 $rangecell->header = true;
1135 $rangecell->scope = 'row';
1136 $rangecell->text = $this->get_lang_string('range','grades');
1137 $rangerow->cells[] = $rangecell;
1138 $rows[] = $rangerow;
1139 }
dc482cfa 1140
6c096a49
NC
1141 return $rows;
1142 }
dc482cfa 1143
6c096a49
NC
1144 /**
1145 * Builds and return the headers for the rows of averages, for the left part of the grader report.
1146 * @param array $rows The Array of rows for the left part of the report
1147 * @param int $colspan The number of columns this cell has to span
1148 * @param bool $groupavg If true, returns the row for group averages, otherwise for overall averages
1149 * @return array Array of rows for the left part of the report
1150 */
1151 public function get_left_avg_row($rows=array(), $colspan=1, $groupavg=false) {
1152 if (!$this->canviewhidden) {
1153 // totals might be affected by hiding, if user can not see hidden grades the aggregations might be altered
1154 // better not show them at all if user can not see all hideen grades
1155 return $rows;
1156 }
dc482cfa 1157
6c096a49
NC
1158 $showaverages = $this->get_pref('showaverages');
1159 $showaveragesgroup = $this->currentgroup && $showaverages;
1160 $straveragegroup = get_string('groupavg', 'grades');
319770d7 1161
6c096a49 1162 if ($groupavg) {
319770d7 1163 if ($showaveragesgroup) {
1164 $groupavgrow = new html_table_row();
16be8974 1165 $groupavgrow->attributes['class'] = 'groupavg r'.$this->rowcount++;
319770d7 1166 $groupavgcell = new html_table_cell();
16be8974 1167 $groupavgcell->attributes['class'] = 'header range';
319770d7 1168 $groupavgcell->colspan = $colspan;
1169 $groupavgcell->header = true;
1170 $groupavgcell->scope = 'row';
1171 $groupavgcell->text = $straveragegroup;
1172 $groupavgrow->cells[] = $groupavgcell;
6c096a49 1173 $rows[] = $groupavgrow;
203b7e2e 1174 }
6c096a49
NC
1175 } else {
1176 $straverage = get_string('overallaverage', 'grades');
dc482cfa 1177
203b7e2e 1178 if ($showaverages) {
319770d7 1179 $avgrow = new html_table_row();
16be8974 1180 $avgrow->attributes['class'] = 'avg r'.$this->rowcount++;
319770d7 1181 $avgcell = new html_table_cell();
16be8974 1182 $avgcell->attributes['class'] = 'header range';
319770d7 1183 $avgcell->colspan = $colspan;
1184 $avgcell->header = true;
1185 $avgcell->scope = 'row';
1186 $avgcell->text = $straverage;
1187 $avgrow->cells[] = $avgcell;
6c096a49 1188 $rows[] = $avgrow;
203b7e2e 1189 }
6c096a49 1190 }
203b7e2e 1191
6c096a49
NC
1192 return $rows;
1193 }
dc482cfa 1194
6c096a49
NC
1195 /**
1196 * Builds and return the row of icons when editing is on, for the right part of the grader report.
1197 * @param array $rows The Array of rows for the right part of the report
1198 * @return array Array of rows for the right part of the report
1199 */
1200 public function get_right_icons_row($rows=array()) {
1201 global $USER;
1202 if ($USER->gradeediting[$this->courseid]) {
1203 $iconsrow = new html_table_row();
16be8974 1204 $iconsrow->attributes['class'] = 'controls';
6c096a49 1205
6c096a49
NC
1206 foreach ($this->gtree->items as $itemid=>$unused) {
1207 // emulate grade element
1208 $item =& $this->gtree->get_item($itemid);
1209
1210 $eid = $this->gtree->get_item_eid($item);
1211 $element = $this->gtree->locate_element($eid);
1212 $itemcell = new html_table_cell();
16be8974 1213 $itemcell->attributes['class'] = 'controls icons';
6c096a49
NC
1214 $itemcell->text = $this->get_icons($element);
1215 $iconsrow->cells[] = $itemcell;
1216 }
1217 $rows[] = $iconsrow;
1218 }
1219 return $rows;
dc482cfa 1220 }
1221
18a00359 1222 /**
6c096a49
NC
1223 * Builds and return the row of ranges for the right part of the grader report.
1224 * @param array $rows The Array of rows for the right part of the report
1225 * @return array Array of rows for the right part of the report
18a00359 1226 */
6c096a49
NC
1227 public function get_right_range_row($rows=array()) {
1228 global $OUTPUT;
18a00359 1229
6c096a49
NC
1230 if ($this->get_pref('showranges')) {
1231 $rangesdisplaytype = $this->get_pref('rangesdisplaytype');
1232 $rangesdecimalpoints = $this->get_pref('rangesdecimalpoints');
1233 $rangerow = new html_table_row();
16be8974 1234 $rangerow->attributes['class'] = 'heading range';
18a00359 1235
6c096a49
NC
1236 foreach ($this->gtree->items as $itemid=>$unused) {
1237 $item =& $this->gtree->items[$itemid];
1238 $itemcell = new html_table_cell();
1239 $itemcell->header = true;
16be8974 1240 $itemcell->attributes['class'] .= ' header range';
6c096a49
NC
1241
1242 $hidden = '';
1243 if ($item->is_hidden()) {
1244 $hidden = ' hidden ';
1245 }
1246
1247 $formattedrange = $item->get_formatted_range($rangesdisplaytype, $rangesdecimalpoints);
1248
1249 $itemcell->text = $OUTPUT->container($formattedrange, 'rangevalues'.$hidden);
1250 $rangerow->cells[] = $itemcell;
1251 }
1252 $rows[] = $rangerow;
18a00359 1253 }
6c096a49 1254 return $rows;
18a00359 1255 }
1256
4ba9941c 1257 /**
6c096a49
NC
1258 * Builds and return the row of averages for the right part of the grader report.
1259 * @param array $rows Whether to return only group averages or all averages.
1260 * @param bool $grouponly Whether to return only group averages or all averages.
1261 * @return array Array of rows for the right part of the report
4ba9941c 1262 */
6c096a49
NC
1263 public function get_right_avg_row($rows=array(), $grouponly=false) {
1264 global $CFG, $USER, $DB, $OUTPUT;
4ba9941c 1265
57068674 1266 if (!$this->canviewhidden) {
1267 // totals might be affected by hiding, if user can not see hidden grades the aggregations might be altered
6ef4878b 1268 // better not show them at all if user can not see all hidden grades
6c096a49 1269 return $rows;
57068674 1270 }
1271
6c096a49
NC
1272 $showaverages = $this->get_pref('showaverages');
1273 $showaveragesgroup = $this->currentgroup && $showaverages;
1274
5b508a08 1275 $averagesdisplaytype = $this->get_pref('averagesdisplaytype');
1276 $averagesdecimalpoints = $this->get_pref('averagesdecimalpoints');
098042ba 1277 $meanselection = $this->get_pref('meanselection');
1278 $shownumberofgrades = $this->get_pref('shownumberofgrades');
1279
5b508a08 1280 $avghtml = '';
aae94377 1281 $avgcssclass = 'avg';
3446013d 1282
5b508a08 1283 if ($grouponly) {
1284 $straverage = get_string('groupavg', 'grades');
35079f53 1285 $showaverages = $this->currentgroup && $this->get_pref('showaverages');
5b508a08 1286 $groupsql = $this->groupsql;
1287 $groupwheresql = $this->groupwheresql;
319770d7 1288 $groupwheresqlparams = $this->groupwheresql_params;
aae94377 1289 $avgcssclass = 'groupavg';
3446013d 1290 } else {
6308b91c 1291 $straverage = get_string('overallaverage', 'grades');
5b508a08 1292 $showaverages = $this->get_pref('showaverages');
0dba6cb2 1293 $groupsql = "";
1294 $groupwheresql = "";
319770d7 1295 $groupwheresqlparams = array();
3446013d 1296 }
1297
a5b8be62 1298 if ($shownumberofgrades) {
1299 $straverage .= ' (' . get_string('submissions', 'grades') . ') ';
1300 }
1301
f8ae1f86 1302 $totalcount = $this->get_numusers($grouponly);
04259694 1303
387815db 1304 //limit to users with a gradeable role
1305 list($gradebookrolessql, $gradebookrolesparams) = $DB->get_in_or_equal(explode(',', $this->gradebookroles), SQL_PARAMS_NAMED, 'grbr0');
1306
1307 //limit to users with an active enrollment
1308 list($enrolledsql, $enrolledparams) = get_enrolled_sql($this->context);
d24832f9 1309
5b508a08 1310 if ($showaverages) {
387815db 1311 $params = array_merge(array('courseid'=>$this->courseid), $gradebookrolesparams, $enrolledparams, $groupwheresqlparams);
04259694 1312
828af11c 1313 // find sums of all grade items in course
656d17c2 1314 $sql = "SELECT g.itemid, SUM(g.finalgrade) AS sum
d24832f9 1315 FROM {grade_items} gi
656d17c2
AD
1316 JOIN {grade_grades} g ON g.itemid = gi.id
1317 JOIN {user} u ON u.id = g.userid
1318 JOIN ($enrolledsql) je ON je.id = u.id
1319 JOIN (
1320 SELECT DISTINCT ra.userid
1321 FROM {role_assignments} ra
1322 WHERE ra.roleid $gradebookrolessql
1323 AND ra.contextid " . get_related_contexts_string($this->context) . "
1324 ) rainner ON rainner.userid = u.id
25081b95 1325 $groupsql
b50371da 1326 WHERE gi.courseid = :courseid
656d17c2
AD
1327 AND u.deleted = 0
1328 AND g.finalgrade IS NOT NULL
1329 $groupwheresql
1330 GROUP BY g.itemid";
319770d7 1331 $sumarray = array();
656d17c2 1332 if ($sums = $DB->get_records_sql($sql, $params)) {
ab3444d7 1333 foreach ($sums as $itemid => $csum) {
319770d7 1334 $sumarray[$itemid] = $csum->sum;
7b61efbe 1335 }
4ba9941c 1336 }
66ef0471 1337
883187d0 1338 // MDL-10875 Empty grades must be evaluated as grademin, NOT always 0
1339 // This query returns a count of ungraded grades (NULL finalgrade OR no matching record in grade_grades table)
656d17c2 1340 $sql = "SELECT gi.id, COUNT(DISTINCT u.id) AS count
d24832f9 1341 FROM {grade_items} gi
25081b95
SH
1342 CROSS JOIN {user} u
1343 JOIN ($enrolledsql) je
1344 ON je.id = u.id
1345 JOIN {role_assignments} ra
1346 ON ra.userid = u.id
1347 LEFT OUTER JOIN {grade_grades} g
1348 ON (g.itemid = gi.id AND g.userid = u.id AND g.finalgrade IS NOT NULL)
1349 $groupsql
b50371da 1350 WHERE gi.courseid = :courseid
387815db 1351 AND ra.roleid $gradebookrolessql
828af11c 1352 AND ra.contextid ".get_related_contexts_string($this->context)."
656d17c2 1353 AND u.deleted = 0
828af11c 1354 AND g.id IS NULL
1355 $groupwheresql
883187d0 1356 GROUP BY gi.id";
828af11c 1357
656d17c2 1358 $ungradedcounts = $DB->get_records_sql($sql, $params);
883187d0 1359
6c096a49 1360 $avgrow = new html_table_row();
16be8974 1361 $avgrow->attributes['class'] = 'avg';
653a8648 1362
b89a70ce 1363 foreach ($this->gtree->items as $itemid=>$unused) {
1364 $item =& $this->gtree->items[$itemid];
1365
67881aa8 1366 if ($item->needsupdate) {
6c096a49
NC
1367 $avgcell = new html_table_cell();
1368 $avgcell->text = $OUTPUT->container(get_string('error'), 'gradingerror');
1369 $avgrow->cells[] = $avgcell;
67881aa8 1370 continue;
1371 }
1372
319770d7 1373 if (!isset($sumarray[$item->id])) {
1374 $sumarray[$item->id] = 0;
33a34cd4 1375 }
883187d0 1376
319770d7 1377 if (empty($ungradedcounts[$itemid])) {
1378 $ungradedcount = 0;
828af11c 1379 } else {
319770d7 1380 $ungradedcount = $ungradedcounts[$itemid]->count;
d1556c09 1381 }
33a34cd4 1382
c2efb501 1383 if ($meanselection == GRADE_REPORT_MEAN_GRADED) {
319770d7 1384 $meancount = $totalcount - $ungradedcount;
33a34cd4 1385 } else { // Bump up the sum by the number of ungraded items * grademin
319770d7 1386 $sumarray[$item->id] += $ungradedcount * $item->grademin;
1387 $meancount = $totalcount;
33a34cd4 1388 }
1389
31a6c06c 1390 $decimalpoints = $item->get_decimals();
41f22daa 1391
bb384a8e 1392 // Determine which display type to use for this average
2cc773f5 1393 if ($USER->gradeediting[$this->courseid]) {
1796708d 1394 $displaytype = GRADE_DISPLAY_TYPE_REAL;
d622930b 1395
e0724506 1396 } else if ($averagesdisplaytype == GRADE_REPORT_PREFERENCE_INHERIT) { // no ==0 here, please resave the report and user preferences
d622930b 1397 $displaytype = $item->get_displaytype();
1398
1399 } else {
bb384a8e 1400 $displaytype = $averagesdisplaytype;
1401 }
1402
31a6c06c 1403 // Override grade_item setting if a display preference (not inherit) was set for the averages
e0724506 1404 if ($averagesdecimalpoints == GRADE_REPORT_PREFERENCE_INHERIT) {
d622930b 1405 $decimalpoints = $item->get_decimals();
1406
1407 } else {
5b508a08 1408 $decimalpoints = $averagesdecimalpoints;
1409 }
1410
319770d7 1411 if (!isset($sumarray[$item->id]) || $meancount == 0) {
6c096a49
NC
1412 $avgcell = new html_table_cell();
1413 $avgcell->text = '-';
1414 $avgrow->cells[] = $avgcell;
1415
4ba9941c 1416 } else {
319770d7 1417 $sum = $sumarray[$item->id];
1418 $avgradeval = $sum/$meancount;
d622930b 1419 $gradehtml = grade_format_gradevalue($avgradeval, $item, true, $displaytype, $decimalpoints);
bb384a8e 1420
098042ba 1421 $numberofgrades = '';
098042ba 1422 if ($shownumberofgrades) {
319770d7 1423 $numberofgrades = " ($meancount)";
098042ba 1424 }
1425
6c096a49
NC
1426 $avgcell = new html_table_cell();
1427 $avgcell->text = $gradehtml.$numberofgrades;
1428 $avgrow->cells[] = $avgcell;
80fb1cf9 1429 }
9ecd4386 1430 }
6c096a49 1431 $rows[] = $avgrow;
9ecd4386 1432 }
6c096a49 1433 return $rows;
9ecd4386 1434 }
4ba9941c 1435
1436 /**
1437 * Given a grade_category, grade_item or grade_grade, this function
1438 * figures out the state of the object and builds then returns a div
1439 * with the icons needed for the grader report.
1440 *
c97933ff 1441 * @param array $object
4ba9941c 1442 * @return string HTML
1443 */
d24832f9 1444 protected function get_icons($element) {
319770d7 1445 global $CFG, $USER, $OUTPUT;
4ba9941c 1446
2cc773f5 1447 if (!$USER->gradeediting[$this->courseid]) {
1448 return '<div class="grade_icons" />';
79eabc2a 1449 }
4ba9941c 1450
2cc773f5 1451 // Init all icons
319770d7 1452 $editicon = '';
dc482cfa 1453
1454 if ($element['type'] != 'categoryitem' && $element['type'] != 'courseitem') {
319770d7 1455 $editicon = $this->gtree->get_edit_icon($element, $this->gpr);
dc482cfa 1456 }
1457
319770d7 1458 $editcalculationicon = '';
1459 $showhideicon = '';
1460 $lockunlockicon = '';
4ba9941c 1461
a5b8be62 1462 if (has_capability('moodle/grade:manage', $this->context)) {
a5b8be62 1463 if ($this->get_pref('showcalculations')) {
319770d7 1464 $editcalculationicon = $this->gtree->get_calculation_icon($element, $this->gpr);
a5b8be62 1465 }
1466
1467 if ($this->get_pref('showeyecons')) {
319770d7 1468 $showhideicon = $this->gtree->get_hiding_icon($element, $this->gpr);
a5b8be62 1469 }
4ba9941c 1470
a5b8be62 1471 if ($this->get_pref('showlocks')) {
319770d7 1472 $lockunlockicon = $this->gtree->get_locking_icon($element, $this->gpr);
a5b8be62 1473 }
c97933ff
MG
1474
1475 }
1476
1477 $gradeanalysisicon = '';
1478 if ($this->get_pref('showanalysisicon') && $element['type'] == 'grade') {
1479 $gradeanalysisicon .= $this->gtree->get_grade_analysis_icon($element['object']);
4ba9941c 1480 }
1481
c97933ff 1482 return $OUTPUT->container($editicon.$editcalculationicon.$showhideicon.$lockunlockicon.$gradeanalysisicon, 'grade_icons');
4faf5f99 1483 }
1484
1485 /**
1486 * Given a category element returns collapsing +/- icon if available
1487 * @param object $object
1488 * @return string HTML
1489 */
d24832f9 1490 protected function get_collapsing_icon($element) {
5d3b9994 1491 global $OUTPUT;
4faf5f99 1492
319770d7 1493 $icon = '';
2cc773f5 1494 // If object is a category, display expand/contract icon
384960dd 1495 if ($element['type'] == 'category') {
2cc773f5 1496 // Load language strings
319770d7 1497 $strswitchminus = $this->get_lang_string('aggregatesonly', 'grades');
1498 $strswitchplus = $this->get_lang_string('gradesonly', 'grades');
1499 $strswitchwhole = $this->get_lang_string('fullmode', 'grades');
384960dd 1500
8ae8bf8a 1501 $url = new moodle_url($this->gpr->get_return_url(null, array('target'=>$element['eid'], 'sesskey'=>sesskey())));
48b5d8f3 1502
384960dd 1503 if (in_array($element['object']->id, $this->collapsed['aggregatesonly'])) {
8ae8bf8a 1504 $url->param('action', 'switch_plus');
c63923bd 1505 $icon = $OUTPUT->action_icon($url, new pix_icon('t/switch_plus', $strswitchplus));
8ae8bf8a
PS
1506
1507 } else if (in_array($element['object']->id, $this->collapsed['gradesonly'])) {
1508 $url->param('action', 'switch_whole');
c63923bd 1509 $icon = $OUTPUT->action_icon($url, new pix_icon('t/switch_whole', $strswitchwhole));
8ae8bf8a
PS
1510
1511 } else {
1512 $url->param('action', 'switch_minus');
c63923bd 1513 $icon = $OUTPUT->action_icon($url, new pix_icon('t/switch_minus', $strswitchminus));
2cc773f5 1514 }
4ba9941c 1515 }
319770d7 1516 return $icon;
4ba9941c 1517 }
48b5d8f3 1518
1519 /**
1520 * Processes a single action against a category, grade_item or grade.
1521 * @param string $target eid ({type}{id}, e.g. c4 for category4)
1522 * @param string $action Which action to take (edit, delete etc...)
1523 * @return
1524 */
d24832f9 1525 public function process_action($target, $action) {
4faf5f99 1526 // TODO: this code should be in some grade_tree static method
48b5d8f3 1527 $targettype = substr($target, 0, 1);
1528 $targetid = substr($target, 1);
4faf5f99 1529 // TODO: end
1530
1531 if ($collapsed = get_user_preferences('grade_report_grader_collapsed_categories')) {
1532 $collapsed = unserialize($collapsed);
1533 } else {
384960dd 1534 $collapsed = array('aggregatesonly' => array(), 'gradesonly' => array());
4faf5f99 1535 }
1536
48b5d8f3 1537 switch ($action) {
384960dd 1538 case 'switch_minus': // Add category to array of aggregatesonly
1539 if (!in_array($targetid, $collapsed['aggregatesonly'])) {
1540 $collapsed['aggregatesonly'][] = $targetid;
4faf5f99 1541 set_user_preference('grade_report_grader_collapsed_categories', serialize($collapsed));
1542 }
48b5d8f3 1543 break;
4faf5f99 1544
384960dd 1545 case 'switch_plus': // Remove category from array of aggregatesonly, and add it to array of gradesonly
1546 $key = array_search($targetid, $collapsed['aggregatesonly']);
4faf5f99 1547 if ($key !== false) {
384960dd 1548 unset($collapsed['aggregatesonly'][$key]);
4faf5f99 1549 }
384960dd 1550 if (!in_array($targetid, $collapsed['gradesonly'])) {
1551 $collapsed['gradesonly'][] = $targetid;
1552 }
1553 set_user_preference('grade_report_grader_collapsed_categories', serialize($collapsed));
48b5d8f3 1554 break;
384960dd 1555 case 'switch_whole': // Remove the category from the array of collapsed cats
1556 $key = array_search($targetid, $collapsed['gradesonly']);
1557 if ($key !== false) {
1558 unset($collapsed['gradesonly'][$key]);
1559 set_user_preference('grade_report_grader_collapsed_categories', serialize($collapsed));
1560 }
4faf5f99 1561
384960dd 1562 break;
48b5d8f3 1563 default:
1564 break;
1565 }
1566
1567 return true;
1568 }
03fcc729 1569
795b745a 1570 /**
1571 * Returns whether or not to display fixed students column.
1572 * Includes a browser check, because IE6 doesn't support the scrollbar.
1573 *
1574 * @return bool
1575 */
1576 public function is_fixed_students() {
1577 global $USER, $CFG;
03fcc729 1578 return empty($USER->screenreader) && $CFG->grade_report_fixedstudents &&
1579 (check_browser_version('MSIE', '7.0') ||
795b745a 1580 check_browser_version('Firefox', '2.0') ||
1581 check_browser_version('Gecko', '2006010100') ||
1582 check_browser_version('Camino', '1.0') ||
1583 check_browser_version('Opera', '6.0') ||
06671035
PS
1584 check_browser_version('Chrome', '6') ||
1585 check_browser_version('Safari', '300'));
795b745a 1586 }
6c096a49
NC
1587
1588 /**
1589 * Refactored function for generating HTML of sorting links with matching arrows.
1590 * Returns an array with 'studentname' and 'idnumber' as keys, with HTML ready
1591 * to inject into a table header cell.
e4aec5be 1592 * @param array $extrafields Array of extra fields being displayed, such as
1593 * user idnumber
6c096a49
NC
1594 * @return array An associative array of HTML sorting links+arrows
1595 */
e4aec5be 1596 public function get_sort_arrows(array $extrafields = array()) {
6c096a49
NC
1597 global $OUTPUT;
1598 $arrows = array();
1599
1600 $strsortasc = $this->get_lang_string('sortasc', 'grades');
1601 $strsortdesc = $this->get_lang_string('sortdesc', 'grades');
1602 $strfirstname = $this->get_lang_string('firstname');
1603 $strlastname = $this->get_lang_string('lastname');
1604
0f4c64b7
PS
1605 $firstlink = html_writer::link(new moodle_url($this->baseurl, array('sortitemid'=>'firstname')), $strfirstname);
1606 $lastlink = html_writer::link(new moodle_url($this->baseurl, array('sortitemid'=>'lastname')), $strlastname);
6c096a49 1607
0f4c64b7 1608 $arrows['studentname'] = $lastlink;
6c096a49
NC
1609
1610 if ($this->sortitemid === 'lastname') {
1611 if ($this->sortorder == 'ASC') {
1612 $arrows['studentname'] .= print_arrow('up', $strsortasc, true);
1613 } else {
1614 $arrows['studentname'] .= print_arrow('down', $strsortdesc, true);
1615 }
1616 }
1617
0f4c64b7 1618 $arrows['studentname'] .= ' ' . $firstlink;
6c096a49
NC
1619
1620 if ($this->sortitemid === 'firstname') {
1621 if ($this->sortorder == 'ASC') {
1622 $arrows['studentname'] .= print_arrow('up', $strsortasc, true);
1623 } else {
1624 $arrows['studentname'] .= print_arrow('down', $strsortdesc, true);
1625 }
1626 }
1627
e4aec5be 1628 foreach ($extrafields as $field) {
1629 $fieldlink = html_writer::link(new moodle_url($this->baseurl,
1630 array('sortitemid'=>$field)), get_user_field_name($field));
1631 $arrows[$field] = $fieldlink;
6c096a49 1632
e4aec5be 1633 if ($field == $this->sortitemid) {
1634 if ($this->sortorder == 'ASC') {
1635 $arrows[$field] .= print_arrow('up', $strsortasc, true);
1636 } else {
1637 $arrows[$field] .= print_arrow('down', $strsortdesc, true);
1638 }
6c096a49
NC
1639 }
1640 }
1641
1642 return $arrows;
1643 }
4ba9941c 1644}
6c3ef410 1645