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