MDL-14967 Bug in DB upgrade for get_coursemodule_from_instance: named params were...
[moodle.git] / grade / report / grader / lib.php
CommitLineData
4ba9941c 1<?php // $Id$
8ad36f4c 2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
10// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
4ba9941c 25/**
26 * File in which the grader_report class is defined.
27 * @package gradebook
28 */
29
eea6690a 30require_once($CFG->dirroot . '/grade/report/lib.php');
4ba9941c 31require_once($CFG->libdir.'/tablelib.php');
4ba9941c 32
33/**
34 * Class providing an API for the grader report building and displaying.
eea6690a 35 * @uses grade_report
4ba9941c 36 * @package gradebook
37 */
eea6690a 38class grade_report_grader extends grade_report {
4ba9941c 39 /**
40 * The final grades.
b89a70ce 41 * @var array $grades
4ba9941c 42 */
b89a70ce 43 var $grades;
4ba9941c 44
45 /**
46 * Array of errors for bulk grades updating.
47 * @var array $gradeserror
48 */
49 var $gradeserror = array();
50
4ba9941c 51//// SQL-RELATED
52
4ba9941c 53 /**
54 * The id of the grade_item by which this report will be sorted.
55 * @var int $sortitemid
56 */
57 var $sortitemid;
58
59 /**
60 * Sortorder used in the SQL selections.
61 * @var int $sortorder
62 */
63 var $sortorder;
64
65 /**
66 * An SQL fragment affecting the search for users.
67 * @var string $userselect
68 */
69 var $userselect;
70
4faf5f99 71 /**
384960dd 72 * List of collapsed categories from user preference
4faf5f99 73 * @var array $collapsed
74 */
75 var $collapsed;
76
66ef0471 77 /**
78 * A count of the rows, used for css classes.
79 * @var int $rowcount
80 */
81 var $rowcount = 0;
82
6cc3e350 83 /**
57068674 84 * Capability check caching
85 * */
86 var $canviewhidden;
87
4ba9941c 88 /**
89 * Constructor. Sets local copies of user preferences and initialises grade_tree.
90 * @param int $courseid
d30c4481 91 * @param object $gpr grade plugin return tracking object
eea6690a 92 * @param string $context
93 * @param int $page The current page being viewed (when report is paged)
94 * @param int $sortitemid The id of the grade_item by which to sort the table
4ba9941c 95 */
d30c4481 96 function grade_report_grader($courseid, $gpr, $context, $page=null, $sortitemid=null) {
4ba9941c 97 global $CFG;
d30c4481 98 parent::grade_report($courseid, $gpr, $context, $page);
4ba9941c 99
57068674 100 $this->canviewhidden = has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $this->course->id));
101
4faf5f99 102 // load collapsed settings for this report
103 if ($collapsed = get_user_preferences('grade_report_grader_collapsed_categories')) {
104 $this->collapsed = unserialize($collapsed);
4faf5f99 105 } else {
384960dd 106 $this->collapsed = array('aggregatesonly' => array(), 'gradesonly' => array());
4faf5f99 107 }
384960dd 108
aea4df41 109 if (empty($CFG->enableoutcomes)) {
110 $nooutcomes = false;
111 } else {
112 $nooutcomes = get_user_preferences('grade_report_shownooutcomes');
113 }
114
e0724506 115 // if user report preference set or site report setting set use it, otherwise use course or site setting
116 $switch = $this->get_pref('aggregationposition');
05766b50 117 if ($switch == '') {
e0724506 118 $switch = grade_get_setting($this->courseid, 'aggregationposition', $CFG->grade_aggregationposition);
119 }
120
4faf5f99 121 // Grab the grade_tree for this course
e0724506 122 $this->gtree = new grade_tree($this->courseid, true, $switch, $this->collapsed, $nooutcomes);
4faf5f99 123
4ba9941c 124 $this->sortitemid = $sortitemid;
125
4ba9941c 126 // base url for sorting by first/last name
09cef06a 127 $studentsperpage = $this->get_pref('studentsperpage');
128 $perpage = '';
129 $curpage = '';
130
131 if (!empty($studentsperpage)) {
132 $perpage = '&amp;perpage='.$studentsperpage;
133 $curpage = '&amp;page='.$this->page;
134 }
135 $this->baseurl = 'index.php?id='.$this->courseid. $perpage.$curpage.'&amp;';
136
137 $this->pbarurl = 'index.php?id='.$this->courseid.$perpage.'&amp;';
4ba9941c 138
90d3960c 139 // Setup groups if requested
936f1350 140 if ($this->get_pref('showgroups')) {
4ba9941c 141 $this->setup_groups();
142 }
143
144 $this->setup_sortitemid();
145 }
146
4ba9941c 147 /**
148 * Processes the data sent by the form (grades and feedbacks).
75674470 149 * Caller is reposible for all access control checks
150 * @param array $data form submission (with magic quotes)
151 * @return array empty array if success, array of warnings if something fails.
4ba9941c 152 */
153 function process_data($data) {
75674470 154 $warnings = array();
2cc773f5 155
4ba9941c 156 // always initialize all arrays
157 $queue = array();
4ba9941c 158 foreach ($data as $varname => $postedvalue) {
4ba9941c 159
160 $needsupdate = false;
4ba9941c 161
162 // skip, not a grade nor feedback
79eabc2a 163 if (strpos($varname, 'grade') === 0) {
4ba9941c 164 $data_type = 'grade';
79eabc2a 165 } else if (strpos($varname, 'feedback') === 0) {
4ba9941c 166 $data_type = 'feedback';
167 } else {
168 continue;
169 }
170
171 $gradeinfo = explode("_", $varname);
4ba9941c 172 $userid = clean_param($gradeinfo[1], PARAM_INT);
173 $itemid = clean_param($gradeinfo[2], PARAM_INT);
174
29a5680e 175 $oldvalue = $data->{'old'.$varname};
176
177 // was change requested?
99ccfda8 178 if ($oldvalue == $postedvalue) { // string comparison
29a5680e 179 continue;
180 }
181
4ba9941c 182 if (!$grade_item = grade_item::fetch(array('id'=>$itemid, 'courseid'=>$this->courseid))) { // we must verify course id here!
14398fd6 183 print_error('invalidgradeitmeid');
4ba9941c 184 }
185
186 // Pre-process grade
187 if ($data_type == 'grade') {
4256a134 188 $feedback = false;
189 $feedbackformat = false;
4ba9941c 190 if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
191 if ($postedvalue == -1) { // -1 means no grade
192 $finalgrade = null;
193 } else {
29a5680e 194 $finalgrade = $postedvalue;
4ba9941c 195 }
196 } else {
76317c73 197 $finalgrade = unformat_float($postedvalue);
4ba9941c 198 }
79eabc2a 199
0a2c8485 200 $errorstr = '';
379ea949 201 // Warn if the grade is out of bounds.
202 if (is_null($finalgrade)) {
203 // ok
204 } else if ($finalgrade < $grade_item->grademin) {
0a2c8485 205 $errorstr = 'lessthanmin';
206 } else if ($finalgrade > $grade_item->grademax) {
207 $errorstr = 'morethanmax';
208 }
209 if ($errorstr) {
75674470 210 $user = get_record('user', 'id', $userid, '', '', '', '', 'id, firstname, lastname');
379ea949 211 $gradestr = new object();
0a2c8485 212 $gradestr->username = fullname($user);
213 $gradestr->itemname = $grade_item->get_name();
9d35e66e 214 $warnings[] = get_string($errorstr, 'grades', $gradestr);
0a2c8485 215 }
216
79eabc2a 217 } else if ($data_type == 'feedback') {
4256a134 218 $finalgrade = false;
79eabc2a 219 $trimmed = trim($postedvalue);
220 if (empty($trimmed)) {
29a5680e 221 $feedback = NULL;
e1d2692a 222 } else {
29a5680e 223 $feedback = stripslashes($postedvalue);
e1d2692a 224 }
4ba9941c 225 }
4ba9941c 226
0f392ff4 227 $grade_item->update_final_grade($userid, $finalgrade, 'gradebook', $feedback, FORMAT_MOODLE);
4ba9941c 228 }
229
75674470 230 return $warnings;
4ba9941c 231 }
232
4ba9941c 233
234 /**
235 * Setting the sort order, this depends on last state
236 * all this should be in the new table class that we might need to use
237 * for displaying grades.
238 */
239 function setup_sortitemid() {
63d6efa2 240
241 global $SESSION;
242
4ba9941c 243 if ($this->sortitemid) {
244 if (!isset($SESSION->gradeuserreport->sort)) {
a80112f0 245 if ($this->sortitemid == 'firstname' || $this->sortitemid == 'lastname') {
246 $this->sortorder = $SESSION->gradeuserreport->sort = 'ASC';
247 } else {
248 $this->sortorder = $SESSION->gradeuserreport->sort = 'DESC';
249 }
4ba9941c 250 } else {
251 // this is the first sort, i.e. by last name
252 if (!isset($SESSION->gradeuserreport->sortitemid)) {
a80112f0 253 if ($this->sortitemid == 'firstname' || $this->sortitemid == 'lastname') {
254 $this->sortorder = $SESSION->gradeuserreport->sort = 'ASC';
255 } else {
256 $this->sortorder = $SESSION->gradeuserreport->sort = 'DESC';
257 }
4ba9941c 258 } else if ($SESSION->gradeuserreport->sortitemid == $this->sortitemid) {
259 // same as last sort
260 if ($SESSION->gradeuserreport->sort == 'ASC') {
261 $this->sortorder = $SESSION->gradeuserreport->sort = 'DESC';
262 } else {
263 $this->sortorder = $SESSION->gradeuserreport->sort = 'ASC';
264 }
265 } else {
a80112f0 266 if ($this->sortitemid == 'firstname' || $this->sortitemid == 'lastname') {
267 $this->sortorder = $SESSION->gradeuserreport->sort = 'ASC';
268 } else {
269 $this->sortorder = $SESSION->gradeuserreport->sort = 'DESC';
270 }
4ba9941c 271 }
272 }
273 $SESSION->gradeuserreport->sortitemid = $this->sortitemid;
274 } else {
275 // not requesting sort, use last setting (for paging)
276
277 if (isset($SESSION->gradeuserreport->sortitemid)) {
278 $this->sortitemid = $SESSION->gradeuserreport->sortitemid;
d20737e8 279 }else{
280 $this->sortitemid = 'lastname';
4ba9941c 281 }
d20737e8 282
4ba9941c 283 if (isset($SESSION->gradeuserreport->sort)) {
284 $this->sortorder = $SESSION->gradeuserreport->sort;
285 } else {
286 $this->sortorder = 'ASC';
287 }
288 }
289 }
290
4ba9941c 291 /**
292 * pulls out the userids of the users to be display, and sort them
293 * the right outer join is needed because potentially, it is possible not
294 * to have the corresponding entry in grade_grades table for some users
295 * this is check for user roles because there could be some users with grades
296 * but not supposed to be displayed
297 */
298 function load_users() {
299 global $CFG;
300
301 if (is_numeric($this->sortitemid)) {
09d0ef21 302 $sort = "g.finalgrade $this->sortorder";
303
9d35e66e 304 $sql = "SELECT u.id, u.firstname, u.lastname, u.imagealt, u.picture, u.idnumber
09d0ef21 305 FROM {$CFG->prefix}grade_grades g RIGHT OUTER JOIN
306 {$CFG->prefix}user u ON (u.id = g.userid AND g.itemid = $this->sortitemid)
307 LEFT JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
308 $this->groupsql
309 WHERE ra.roleid in ($this->gradebookroles)
310 $this->groupwheresql
311 AND ra.contextid ".get_related_contexts_string($this->context)."
312 ORDER BY $sort";
313
4ba9941c 314 } else {
09d0ef21 315 switch($this->sortitemid) {
316 case 'lastname':
317 $sort = "u.lastname $this->sortorder, u.firstname $this->sortorder"; break;
318 case 'firstname':
319 $sort = "u.firstname $this->sortorder, u.lastname $this->sortorder"; break;
320 case 'idnumber':
321 default:
322 $sort = "u.idnumber $this->sortorder"; break;
c421ad4b 323 }
4ba9941c 324
09d0ef21 325 $sql = "SELECT u.id, u.firstname, u.lastname, u.imagealt, u.picture, u.idnumber
326 FROM {$CFG->prefix}user u
05d820ea 327 JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
09d0ef21 328 $this->groupsql
329 WHERE ra.roleid in ($this->gradebookroles)
330 $this->groupwheresql
331 AND ra.contextid ".get_related_contexts_string($this->context)."
332 ORDER BY $sort";
4ba9941c 333 }
334
09d0ef21 335
336 $this->users = get_records_sql($sql, $this->get_pref('studentsperpage') * $this->page,
337 $this->get_pref('studentsperpage'));
338
4ba9941c 339 if (empty($this->users)) {
340 $this->userselect = '';
341 $this->users = array();
342 } else {
343 $this->userselect = 'AND g.userid in ('.implode(',', array_keys($this->users)).')';
344 }
345
346 return $this->users;
347 }
348
4ba9941c 349 /**
350 * we supply the userids in this query, and get all the grades
351 * pulls out all the grades, this does not need to worry about paging
352 */
353 function load_final_grades() {
354 global $CFG;
355
23207a1a 356 // please note that we must fetch all grade_grades fields if we want to contruct grade_grade object from it!
b89a70ce 357 $sql = "SELECT g.*
23207a1a 358 FROM {$CFG->prefix}grade_items gi,
359 {$CFG->prefix}grade_grades g
b89a70ce 360 WHERE g.itemid = gi.id AND gi.courseid = {$this->courseid} {$this->userselect}";
361
362 $userids = array_keys($this->users);
4ba9941c 363
d297269d 364
4ba9941c 365 if ($grades = get_records_sql($sql)) {
b89a70ce 366 foreach ($grades as $graderec) {
d297269d 367 if (in_array($graderec->userid, $userids) and array_key_exists($graderec->itemid, $this->gtree->items)) { // some items may not be present!!
b89a70ce 368 $this->grades[$graderec->userid][$graderec->itemid] = new grade_grade($graderec, false);
369 $this->grades[$graderec->userid][$graderec->itemid]->grade_item =& $this->gtree->items[$graderec->itemid]; // db caching
370 }
371 }
372 }
373
374 // prefil grades that do not exist yet
375 foreach ($userids as $userid) {
376 foreach ($this->gtree->items as $itemid=>$unused) {
377 if (!isset($this->grades[$userid][$itemid])) {
378 $this->grades[$userid][$itemid] = new grade_grade();
478f4322 379 $this->grades[$userid][$itemid]->itemid = $itemid;
3b34f698 380 $this->grades[$userid][$itemid]->userid = $userid;
b89a70ce 381 $this->grades[$userid][$itemid]->grade_item =& $this->gtree->items[$itemid]; // db caching
382 }
4ba9941c 383 }
384 }
385 }
386
387 /**
388 * Builds and returns a div with on/off toggles.
389 * @return string HTML code
390 */
391 function get_toggles_html() {
aea4df41 392 global $CFG, $USER;
393
4ba9941c 394 $html = '<div id="grade-report-toggles">';
2cc773f5 395 if ($USER->gradeediting[$this->courseid]) {
396 if (has_capability('moodle/grade:manage', $this->context) or has_capability('moodle/grade:hide', $this->context)) {
397 $html .= $this->print_toggle('eyecons', true);
398 }
399 if (has_capability('moodle/grade:manage', $this->context)
400 or has_capability('moodle/grade:lock', $this->context)
401 or has_capability('moodle/grade:unlock', $this->context)) {
402 $html .= $this->print_toggle('locks', true);
403 }
2ca093fa 404 if (has_capability('moodle/grade:manage', $this->context)) {
405 $html .= $this->print_toggle('quickfeedback', true);
406 }
407
2cc773f5 408 if (has_capability('moodle/grade:manage', $this->context)) {
409 $html .= $this->print_toggle('calculations', true);
410 }
4ba9941c 411 }
412
57068674 413 if ($this->canviewhidden) {
414 $html .= $this->print_toggle('averages', true);
415 }
aae94377 416
417 if (has_capability('moodle/grade:viewall', $this->context)
418 and has_capability('moodle/site:accessallgroups', $this->context)
419 and $course_has_groups = true) { // TODO replace that last condition with proper check
420 $html .= $this->print_toggle('groups', true);
421 }
422
61649211 423 $html .= $this->print_toggle('ranges', true);
aea4df41 424 if (!empty($CFG->enableoutcomes)) {
425 $html .= $this->print_toggle('nooutcomes', true);
426 }
4ba9941c 427 $html .= '</div>';
428 return $html;
429 }
430
431 /**
432 * Shortcut function for printing the grader report toggles.
433 * @param string $type The type of toggle
434 * @param bool $return Whether to return the HTML string rather than printing it
435 * @return void
436 */
437 function print_toggle($type, $return=false) {
438 global $CFG;
439
aea4df41 440 $icons = array('eyecons' => 't/hide.gif',
441 'calculations' => 't/calc.gif',
442 'locks' => 't/lock.gif',
ece966f0 443 'averages' => 't/mean.gif',
2ca093fa 444 'quickfeedback' => 't/feedback.gif',
d4795a07 445 'nooutcomes' => 't/outcomes.gif');
4ba9941c 446
447 $pref_name = 'grade_report_show' . $type;
aea4df41 448
449 if (array_key_exists($pref_name, $CFG)) {
450 $show_pref = get_user_preferences($pref_name, $CFG->$pref_name);
451 } else {
452 $show_pref = get_user_preferences($pref_name);
453 }
4ba9941c 454
388234f4 455 $strshow = $this->get_lang_string('show' . $type, 'grades');
456 $strhide = $this->get_lang_string('hide' . $type, 'grades');
4ba9941c 457
458 $show_hide = 'show';
459 $toggle_action = 1;
460
461 if ($show_pref) {
462 $show_hide = 'hide';
463 $toggle_action = 0;
464 }
465
466 if (array_key_exists($type, $icons)) {
467 $image_name = $icons[$type];
468 } else {
aea4df41 469 $image_name = "t/$type.gif";
4ba9941c 470 }
471
472 $string = ${'str' . $show_hide};
473
aea4df41 474 $img = '<img src="'.$CFG->pixpath.'/'.$image_name.'" class="iconsmall" alt="'
4ba9941c 475 .$string.'" title="'.$string.'" />'. "\n";
476
477 $retval = '<div class="gradertoggle">' . $img . '<a href="' . $this->baseurl . "&amp;toggle=$toggle_action&amp;toggle_type=$type\">"
478 . $string . '</a></div>';
479
480 if ($return) {
481 return $retval;
482 } else {
483 echo $retval;
484 }
485 }
486
487 /**
488 * Builds and returns the HTML code for the headers.
489 * @return string $headerhtml
490 */
d30c4481 491 function get_headerhtml() {
4ba9941c 492 global $CFG, $USER;
493
48b5d8f3 494 $strsortasc = $this->get_lang_string('sortasc', 'grades');
495 $strsortdesc = $this->get_lang_string('sortdesc', 'grades');
496 $strfirstname = $this->get_lang_string('firstname');
497 $strlastname = $this->get_lang_string('lastname');
9d35e66e 498 $showuseridnumber = $this->get_pref('showuseridnumber');
48b5d8f3 499
4ba9941c 500 if ($this->sortitemid === 'lastname') {
501 if ($this->sortorder == 'ASC') {
502 $lastarrow = print_arrow('up', $strsortasc, true);
503 } else {
504 $lastarrow = print_arrow('down', $strsortdesc, true);
505 }
506 } else {
507 $lastarrow = '';
508 }
509
510 if ($this->sortitemid === 'firstname') {
511 if ($this->sortorder == 'ASC') {
512 $firstarrow = print_arrow('up', $strsortasc, true);
513 } else {
514 $firstarrow = print_arrow('down', $strsortdesc, true);
515 }
516 } else {
517 $firstarrow = '';
518 }
519 // Prepare Table Headers
520 $headerhtml = '';
521
522 $numrows = count($this->gtree->levels);
523
cb7fe7b4 524 $columns_to_unset = array();
525
66ef0471 526
4ba9941c 527 foreach ($this->gtree->levels as $key=>$row) {
66ef0471 528 $columncount = 0;
4ba9941c 529 if ($key == 0) {
cb7fe7b4 530 // do not display course grade category
4ba9941c 531 // continue;
532 }
533
66ef0471 534 $headerhtml .= '<tr class="heading r'.$this->rowcount++.'">';
4ba9941c 535
536 if ($key == $numrows - 1) {
38c77dc6 537 $headerhtml .= '<th class="header c'.$columncount++.'" scope="col"><a href="'.$this->baseurl.'&amp;sortitemid=firstname">'
80fb1cf9 538 . $strfirstname . '</a> '
48b5d8f3 539 . $firstarrow. '/ <a href="'.$this->baseurl.'&amp;sortitemid=lastname">' . $strlastname . '</a>'. $lastarrow .'</th>';
9d35e66e 540 if ($showuseridnumber) {
541 if ('idnumber' == $this->sortitemid) {
542 if ($this->sortorder == 'ASC') {
543 $idnumberarrow = print_arrow('up', $strsortasc, true);
544 } else {
545 $idnumberarrow = print_arrow('down', $strsortdesc, true);
546 }
547 } else {
548 $idnumberarrow = '';
549 }
550 $headerhtml .= '<th class="header c'.$columncount++.' useridnumber" scope="col"><a href="'.$this->baseurl.'&amp;sortitemid=idnumber">'
551 . get_string('idnumber') . '</a> ' . $idnumberarrow . '</th>';
552 }
553 } else {
554 $colspan='';
555 if ($showuseridnumber) {
556 $colspan = 'colspan="2" ';
557 }
558
559 $headerhtml .= '<td '.$colspan.'class="cell c'.$columncount++.' topleft">&nbsp;</td>';
560
561 if ($showuseridnumber) {
562 $columncount++;
563 }
4ba9941c 564 }
565
cb7fe7b4 566 foreach ($row as $columnkey => $element) {
2e3987a9 567 $sort_link = '';
568 if (isset($element['object']->id)) {
569 $sort_link = $this->baseurl.'&amp;sortitemid=' . $element['object']->id;
570 }
571
cb7fe7b4 572 $eid = $element['eid'];
573 $object = $element['object'];
574 $type = $element['type'];
438a5aa9 575 $categorystate = @$element['categorystate'];
2e3987a9 576 $itemmodule = null;
577 $iteminstance = null;
8c5a416e 578
66ef0471 579 $columnclass = 'c' . $columncount++;
4ba9941c 580 if (!empty($element['colspan'])) {
581 $colspan = 'colspan="'.$element['colspan'].'"';
66ef0471 582 $columnclass = '';
4ba9941c 583 } else {
584 $colspan = '';
585 }
586
587 if (!empty($element['depth'])) {
588 $catlevel = ' catlevel'.$element['depth'];
589 } else {
590 $catlevel = '';
591 }
592
cb7fe7b4 593// Element is a filler
4ba9941c 594 if ($type == 'filler' or $type == 'fillerfirst' or $type == 'fillerlast') {
66ef0471 595 $headerhtml .= '<th class="'.$columnclass.' '.$type.$catlevel.'" '.$colspan.' scope="col">&nbsp;</th>';
cb7fe7b4 596 }
597// Element is a category
4faf5f99 598 else if ($type == 'category') {
66ef0471 599 $headerhtml .= '<th class="header '. $columnclass.' category'.$catlevel.'" '.$colspan.' scope="col">'
9ecd4386 600 . shorten_text($element['object']->get_name());
4faf5f99 601 $headerhtml .= $this->get_collapsing_icon($element);
4ba9941c 602
603 // Print icons
2cc773f5 604 if ($USER->gradeediting[$this->courseid]) {
d30c4481 605 $headerhtml .= $this->get_icons($element);
4ba9941c 606 }
607
63d6efa2 608 $headerhtml .= '</th>';
cb7fe7b4 609 }
610// Element is a grade_item
4faf5f99 611 else {
2e3987a9 612 $itemmodule = $element['object']->itemmodule;
613 $iteminstance = $element['object']->iteminstance;
614
4ba9941c 615 if ($element['object']->id == $this->sortitemid) {
616 if ($this->sortorder == 'ASC') {
2e3987a9 617 $arrow = $this->get_sort_arrow('up', $sort_link);
4ba9941c 618 } else {
2e3987a9 619 $arrow = $this->get_sort_arrow('down', $sort_link);
4ba9941c 620 }
621 } else {
2e3987a9 622 $arrow = $this->get_sort_arrow('move', $sort_link);
4ba9941c 623 }
624
80fb1cf9 625 $hidden = '';
4ba9941c 626 if ($element['object']->is_hidden()) {
80fb1cf9 627 $hidden = ' hidden ';
4ba9941c 628 }
629
6cc3e350 630 $headerlink = $this->gtree->get_element_header($element, true, $this->get_pref('showactivityicons'), false);
9ecd4386 631 $headerhtml .= '<th class="header '.$columnclass.' '.$type.$catlevel.$hidden.'" scope="col" onclick="set_col(this.cellIndex)">'
632 . shorten_text($headerlink) . $arrow;
633 $headerhtml .= '</th>';
4ba9941c 634 }
635
636 }
637
638 $headerhtml .= '</tr>';
639 }
640 return $headerhtml;
641 }
642
643 /**
644 * Builds and return the HTML rows of the table (grades headed by student).
645 * @return string HTML
646 */
d30c4481 647 function get_studentshtml() {
44e1b7d7 648 global $CFG, $USER, $DB;
d297269d 649
4ba9941c 650 $studentshtml = '';
d297269d 651 $strfeedback = $this->get_lang_string("feedback");
652 $strgrade = $this->get_lang_string('grade');
18effef4 653 $gradetabindex = 1;
e7536c92 654 $showuserimage = $this->get_pref('showuserimage');
9d35e66e 655 $showuseridnumber = $this->get_pref('showuseridnumber');
d297269d 656 $numusers = count($this->users);
4ba9941c 657
388234f4 658 // Preload scale objects for items with a scaleid
44e1b7d7 659 $scales_list = array();
c0c1e7c2 660 $tabindices = array();
d297269d 661
b89a70ce 662 foreach ($this->gtree->items as $item) {
388234f4 663 if (!empty($item->scaleid)) {
44e1b7d7 664 $scales_list[] = $item->scaleid;
388234f4 665 }
d297269d 666
c0c1e7c2 667 $tabindices[$item->id]['grade'] = $gradetabindex;
668 $tabindices[$item->id]['feedback'] = $gradetabindex + $numusers;
669 $gradetabindex += $numusers * 2;
388234f4 670 }
671 $scales_array = array();
672
673 if (!empty($scales_list)) {
44e1b7d7 674 $scales_array = $DB->get_records_list('scale', 'id', $scales_list);
388234f4 675 }
292e3e12 676
677 $row_classes = array(' even ', ' odd ');
a5b8be62 678
a47d38f2 679 $row_classes = array(' even ', ' odd ');
680
4ba9941c 681 foreach ($this->users as $userid => $user) {
b89a70ce 682
57068674 683 if ($this->canviewhidden) {
d297269d 684 $altered = array();
685 $unknown = array();
b89a70ce 686 } else {
687 $hiding_affected = grade_grade::get_hiding_affected($this->grades[$userid], $this->gtree->items);
d297269d 688 $altered = $hiding_affected['altered'];
689 $unknown = $hiding_affected['unknown'];
690 unset($hiding_affected);
b89a70ce 691 }
692
66ef0471 693 $columncount = 0;
4ba9941c 694 // Student name and link
e7536c92 695 $user_pic = null;
696 if ($showuserimage) {
d48ebf97 697 $user_pic = '<div class="userpic">' . print_user_picture($user, $this->courseid, true, 0, true) . '</div>';
e7536c92 698 }
699
292e3e12 700 $studentshtml .= '<tr class="r'.$this->rowcount++ . $row_classes[$this->rowcount % 2] . '">'
701 .'<th class="header c'.$columncount++.' user" scope="row" onclick="set_row(this.parentNode.rowIndex);">'.$user_pic
8f2ddada 702 .'<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$this->course->id.'">'
703 .fullname($user).'</a></th>';
e7536c92 704
9d35e66e 705 if ($showuseridnumber) {
706 $studentshtml .= '<th class="header c'.$columncount++.' useridnumber" onclick="set_row(this.parentNode.rowIndex);">'.
707 $user->idnumber.'</a></th>';
708 }
709
b89a70ce 710 foreach ($this->gtree->items as $itemid=>$unused) {
711 $item =& $this->gtree->items[$itemid];
d297269d 712 $grade = $this->grades[$userid][$item->id];
b89a70ce 713
e50ce569 714 // Get the decimal points preference for this item
31a6c06c 715 $decimalpoints = $item->get_decimals();
4ba9941c 716
d297269d 717 if (in_array($itemid, $unknown)) {
718 $gradeval = null;
719 } else if (array_key_exists($itemid, $altered)) {
720 $gradeval = $altered[$itemid];
721 } else {
722 $gradeval = $grade->finalgrade;
723 }
4ba9941c 724
00374cc5 725 // MDL-11274
726 // Hide grades in the grader report if the current grader doesn't have 'moodle/grade:viewhidden'
57068674 727 if (!$this->canviewhidden and $grade->is_hidden()) {
6cc3e350 728 if (!empty($CFG->grade_hiddenasdate) and $grade->get_datesubmitted() and !$item->is_category_item() and !$item->is_course_item()) {
d297269d 729 // 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
6cc3e350 730 $studentshtml .= '<td class="cell c'.$columncount++.'"><span class="datesubmitted">'.userdate($grade->get_datesubmitted(),get_string('strftimedatetimeshort')).'</span></td>';
bb49f77b 731 } else {
00374cc5 732 $studentshtml .= '<td class="cell c'.$columncount++.'">-</td>';
733 }
a5b8be62 734 continue;
00374cc5 735 }
736
2cc773f5 737 // emulate grade element
d3c3da1b 738 $eid = $this->gtree->get_grade_eid($grade);
739 $element = array('eid'=>$eid, 'object'=>$grade, 'type'=>'grade');
2cc773f5 740
dff9d94d 741 $cellclasses = 'cell c'.$columncount++;
742 if ($item->is_category_item()) {
743 $cellclasses .= ' cat';
b89a70ce 744 }
dff9d94d 745 if ($item->is_course_item()) {
746 $cellclasses .= ' course';
b89a70ce 747 }
4ba9941c 748 if ($grade->is_overridden()) {
dff9d94d 749 $cellclasses .= ' overridden';
4ba9941c 750 }
85db09fb 751
5ebce7bb 752 if ($grade->is_excluded()) {
753 $cellclasses .= ' excluded';
754 }
4ba9941c 755
dff9d94d 756 $studentshtml .= '<td class="'.$cellclasses.'">';
757
23207a1a 758 if ($grade->is_excluded()) {
5ebce7bb 759 $studentshtml .= get_string('excluded', 'grades') . ' ';
23207a1a 760 }
761
4ba9941c 762 // Do not show any icons if no grade (no record in DB to match)
d3c3da1b 763 if (!$item->needsupdate and $USER->gradeediting[$this->courseid]) {
2cc773f5 764 $studentshtml .= $this->get_icons($element);
4ba9941c 765 }
766
80fb1cf9 767 $hidden = '';
768 if ($grade->is_hidden()) {
769 $hidden = ' hidden ';
770 }
6cc3e350 771
46e6df89 772 $gradepass = ' gradefail ';
773 if ($grade->is_passed($item)) {
774 $gradepass = ' gradepass ';
e6477988 775 } elseif (is_null($grade->is_passed($item))) {
776 $gradepass = '';
46e6df89 777 }
778
4ba9941c 779 // if in editting mode, we need to print either a text box
780 // or a drop down (for scales)
4ba9941c 781 // grades in item of type grade category or course are not directly editable
d14ae855 782 if ($item->needsupdate) {
80fb1cf9 783 $studentshtml .= '<span class="gradingerror'.$hidden.'">'.get_string('error').'</span>';
d14ae855 784
2cc773f5 785 } else if ($USER->gradeediting[$this->courseid]) {
4ba9941c 786
388234f4 787 if ($item->scaleid && !empty($scales_array[$item->scaleid])) {
788 $scale = $scales_array[$item->scaleid];
99ccfda8 789 $gradeval = (int)$gradeval; // scales use only integers
388234f4 790 $scales = explode(",", $scale->scale);
791 // reindex because scale is off 1
9d35e66e 792
914ea002 793 // MDL-12104 some previous scales might have taken up part of the array
794 // so this needs to be reset
d48ebf97 795 $scaleopt = array();
388234f4 796 $i = 0;
797 foreach ($scales as $scaleoption) {
798 $i++;
799 $scaleopt[$i] = $scaleoption;
800 }
801
802 if ($this->get_pref('quickgrading') and $grade->is_editable()) {
0658afc9 803 $oldval = empty($gradeval) ? -1 : $gradeval;
804 if (empty($item->outcomeid)) {
d4795a07 805 $nogradestr = $this->get_lang_string('nograde');
0658afc9 806 } else {
d4795a07 807 $nogradestr = $this->get_lang_string('nooutcome', 'grades');
0658afc9 808 }
29a5680e 809 $studentshtml .= '<input type="hidden" name="oldgrade_'.$userid.'_'
0658afc9 810 .$item->id.'" value="'.$oldval.'"/>';
388234f4 811 $studentshtml .= choose_from_menu($scaleopt, 'grade_'.$userid.'_'.$item->id,
0658afc9 812 $gradeval, $nogradestr, '', '-1',
c0c1e7c2 813 true, false, $tabindices[$item->id]['grade']);
388234f4 814 } elseif(!empty($scale)) {
4ba9941c 815 $scales = explode(",", $scale->scale);
4ba9941c 816
388234f4 817 // invalid grade if gradeval < 1
99ccfda8 818 if ($gradeval < 1) {
46e6df89 819 $studentshtml .= '<span class="gradevalue'.$hidden.$gradepass.'">-</span>';
4ba9941c 820 } else {
465b310d 821 $gradeval = (int)bounded_number($grade->grade_item->grademin, $gradeval, $grade->grade_item->grademax); //just in case somebody changes scale
46e6df89 822 $studentshtml .= '<span class="gradevalue'.$hidden.$gradepass.'">'.$scales[$gradeval-1].'</span>';
4ba9941c 823 }
388234f4 824 } else {
825 // no such scale, throw error?
4ba9941c 826 }
79eabc2a 827
bb384a8e 828 } else if ($item->gradetype != GRADE_TYPE_TEXT) { // Value type
936f1350 829 if ($this->get_pref('quickgrading') and $grade->is_editable()) {
76317c73 830 $value = format_float($gradeval, $decimalpoints);
29a5680e 831 $studentshtml .= '<input type="hidden" name="oldgrade_'.$userid.'_'.$item->id.'" value="'.$value.'" />';
be55a047 832 $studentshtml .= '<input size="6" tabindex="' . $tabindices[$item->id]['grade']
833 . '" type="text" title="'. $strgrade .'" name="grade_'
c0c1e7c2 834 .$userid.'_' .$item->id.'" value="'.$value.'" />';
4ba9941c 835 } else {
46e6df89 836 $studentshtml .= '<span class="gradevalue'.$hidden.$gradepass.'">'.format_float($gradeval, $decimalpoints).'</span>';
4ba9941c 837 }
838 }
839
840
841 // If quickfeedback is on, print an input element
2ca093fa 842 if ($this->get_pref('showquickfeedback') and $grade->is_editable()) {
936f1350 843 if ($this->get_pref('quickgrading')) {
4ba9941c 844 $studentshtml .= '<br />';
845 }
29a5680e 846 $studentshtml .= '<input type="hidden" name="oldfeedback_'
847 .$userid.'_'.$item->id.'" value="' . s($grade->feedback) . '" />';
be55a047 848 $studentshtml .= '<input class="quickfeedback" tabindex="' . $tabindices[$item->id]['feedback']
849 . '" size="6" title="' . $strfeedback . '" type="text" name="feedback_'
29a5680e 850 .$userid.'_'.$item->id.'" value="' . s($grade->feedback) . '" />';
4ba9941c 851 }
852
78a2d9f0 853 } else { // Not editing
41f22daa 854 $gradedisplaytype = $item->get_displaytype();
e50ce569 855
78a2d9f0 856 // If feedback present, surround grade with feedback tooltip: Open span here
4ba9941c 857 if (!empty($grade->feedback)) {
9fa46707 858 $overlib = '';
b89a70ce 859 $feedback = addslashes_js(trim(format_string($grade->feedback, $grade->feedbackformat)));
860 $overlib = "return overlib('$feedback', BORDER, 0, FGCLASS, 'feedback', "
861 ."CAPTIONFONTCLASS, 'caption', CAPTION, '$strfeedback');";
862 $studentshtml .= '<span onmouseover="'.s($overlib).'" onmouseout="return nd();">';
4ba9941c 863 }
864
d14ae855 865 if ($item->needsupdate) {
46e6df89 866 $studentshtml .= '<span class="gradingerror'.$hidden.$gradepass.'">'.get_string('error').'</span>';
4ba9941c 867
4ba9941c 868 } else {
46e6df89 869 $studentshtml .= '<span class="gradevalue'.$hidden.$gradepass.'">'.grade_format_gradevalue($gradeval, $item, true, $gradedisplaytype, null).'</span>';
4ba9941c 870 }
78a2d9f0 871
872 // Close feedback span
4ba9941c 873 if (!empty($grade->feedback)) {
874 $studentshtml .= '</span>';
875 }
876 }
877
878 if (!empty($this->gradeserror[$item->id][$userid])) {
879 $studentshtml .= $this->gradeserror[$item->id][$userid];
880 }
881
882 $studentshtml .= '</td>' . "\n";
883 }
884 $studentshtml .= '</tr>';
885 }
886 return $studentshtml;
887 }
888
889 /**
5b508a08 890 * Builds and return the HTML row of column totals.
891 * @param bool $grouponly Whether to return only group averages or all averages.
4ba9941c 892 * @return string HTML
893 */
5b508a08 894 function get_avghtml($grouponly=false) {
2f61fc0e 895 global $CFG, $USER;
4ba9941c 896
57068674 897 if (!$this->canviewhidden) {
898 // totals might be affected by hiding, if user can not see hidden grades the aggregations might be altered
899 // better not show them at all if user can not see all hideen grades
900 return;
901 }
902
5b508a08 903 $averagesdisplaytype = $this->get_pref('averagesdisplaytype');
904 $averagesdecimalpoints = $this->get_pref('averagesdecimalpoints');
098042ba 905 $meanselection = $this->get_pref('meanselection');
906 $shownumberofgrades = $this->get_pref('shownumberofgrades');
9d35e66e 907 $showuseridnumber = $this->get_pref('showuseridnumber');
098042ba 908
5b508a08 909 $avghtml = '';
aae94377 910 $avgcssclass = 'avg';
3446013d 911
5b508a08 912 if ($grouponly) {
913 $straverage = get_string('groupavg', 'grades');
914 $showaverages = $this->currentgroup && $this->get_pref('showgroups');
915 $groupsql = $this->groupsql;
916 $groupwheresql = $this->groupwheresql;
aae94377 917 $avgcssclass = 'groupavg';
3446013d 918 } else {
6308b91c 919 $straverage = get_string('overallaverage', 'grades');
5b508a08 920 $showaverages = $this->get_pref('showaverages');
0dba6cb2 921 $groupsql = "";
922 $groupwheresql = "";
3446013d 923 }
924
a5b8be62 925 if ($shownumberofgrades) {
926 $straverage .= ' (' . get_string('submissions', 'grades') . ') ';
927 }
928
f8ae1f86 929 $totalcount = $this->get_numusers($grouponly);
04259694 930
5b508a08 931 if ($showaverages) {
04259694 932
828af11c 933 // find sums of all grade items in course
934 $SQL = "SELECT g.itemid, SUM(g.finalgrade) AS sum
883187d0 935 FROM {$CFG->prefix}grade_items gi
828af11c 936 JOIN {$CFG->prefix}grade_grades g ON g.itemid = gi.id
937 JOIN {$CFG->prefix}user u ON u.id = g.userid
938 JOIN {$CFG->prefix}role_assignments ra ON ra.userid = u.id
939 $groupsql
883187d0 940 WHERE gi.courseid = $this->courseid
828af11c 941 AND ra.roleid in ($this->gradebookroles)
942 AND ra.contextid ".get_related_contexts_string($this->context)."
943 AND g.finalgrade IS NOT NULL
883187d0 944 $groupwheresql
883187d0 945 GROUP BY g.itemid";
5b508a08 946 $sum_array = array();
ab3444d7 947 if ($sums = get_records_sql($SQL)) {
948 foreach ($sums as $itemid => $csum) {
949 $sum_array[$itemid] = $csum->sum;
7b61efbe 950 }
4ba9941c 951 }
66ef0471 952
9d35e66e 953 $columncount=0;
954 $colspan='';
955 if ($showuseridnumber) {
956 $colspan = 'colspan="2" ';
957 }
958
959 $avghtml = '<tr class="' . $avgcssclass . ' r'.$this->rowcount++.'"><th class="header c0" '.$colspan.'scope="row">'.$straverage.'</th>';
66ef0471 960
9d35e66e 961 if ($showuseridnumber) {
962 $columncount++;
963 }
6df5b04e 964
883187d0 965 // MDL-10875 Empty grades must be evaluated as grademin, NOT always 0
966 // This query returns a count of ungraded grades (NULL finalgrade OR no matching record in grade_grades table)
967 $SQL = "SELECT gi.id, COUNT(u.id) AS count
828af11c 968 FROM {$CFG->prefix}grade_items gi
85db09fb 969 CROSS JOIN {$CFG->prefix}user u
828af11c 970 JOIN {$CFG->prefix}role_assignments ra ON ra.userid = u.id
971 LEFT OUTER JOIN {$CFG->prefix}grade_grades g ON (g.itemid = gi.id AND g.userid = u.id AND g.finalgrade IS NOT NULL)
972 $groupsql
883187d0 973 WHERE gi.courseid = $this->courseid
828af11c 974 AND ra.roleid in ($this->gradebookroles)
975 AND ra.contextid ".get_related_contexts_string($this->context)."
976 AND g.id IS NULL
977 $groupwheresql
883187d0 978 GROUP BY gi.id";
828af11c 979
883187d0 980 $ungraded_counts = get_records_sql($SQL);
981
b89a70ce 982 foreach ($this->gtree->items as $itemid=>$unused) {
983 $item =& $this->gtree->items[$itemid];
984
67881aa8 985 if ($item->needsupdate) {
986 $avghtml .= '<td class="cell c' . $columncount++.'"><span class="gradingerror">'.get_string('error').'</span></td>';
987 continue;
988 }
989
828af11c 990 if (!isset($sum_array[$item->id])) {
33a34cd4 991 $sum_array[$item->id] = 0;
992 }
883187d0 993
828af11c 994 if (empty($ungraded_counts[$itemid])) {
d1556c09 995 $ungraded_count = 0;
828af11c 996 } else {
997 $ungraded_count = $ungraded_counts[$itemid]->count;
d1556c09 998 }
33a34cd4 999
c2efb501 1000 if ($meanselection == GRADE_REPORT_MEAN_GRADED) {
33a34cd4 1001 $mean_count = $totalcount - $ungraded_count;
1002 } else { // Bump up the sum by the number of ungraded items * grademin
828af11c 1003 $sum_array[$item->id] += $ungraded_count * $item->grademin;
33a34cd4 1004 $mean_count = $totalcount;
1005 }
1006
31a6c06c 1007 $decimalpoints = $item->get_decimals();
41f22daa 1008
bb384a8e 1009 // Determine which display type to use for this average
2cc773f5 1010 if ($USER->gradeediting[$this->courseid]) {
1796708d 1011 $displaytype = GRADE_DISPLAY_TYPE_REAL;
d622930b 1012
e0724506 1013 } else if ($averagesdisplaytype == GRADE_REPORT_PREFERENCE_INHERIT) { // no ==0 here, please resave the report and user preferences
d622930b 1014 $displaytype = $item->get_displaytype();
1015
1016 } else {
bb384a8e 1017 $displaytype = $averagesdisplaytype;
1018 }
1019
31a6c06c 1020 // Override grade_item setting if a display preference (not inherit) was set for the averages
e0724506 1021 if ($averagesdecimalpoints == GRADE_REPORT_PREFERENCE_INHERIT) {
d622930b 1022 $decimalpoints = $item->get_decimals();
1023
1024 } else {
5b508a08 1025 $decimalpoints = $averagesdecimalpoints;
1026 }
1027
33a34cd4 1028 if (!isset($sum_array[$item->id]) || $mean_count == 0) {
66ef0471 1029 $avghtml .= '<td class="cell c' . $columncount++.'">-</td>';
4ba9941c 1030 } else {
5b508a08 1031 $sum = $sum_array[$item->id];
d622930b 1032 $avgradeval = $sum/$mean_count;
1033 $gradehtml = grade_format_gradevalue($avgradeval, $item, true, $displaytype, $decimalpoints);
bb384a8e 1034
098042ba 1035 $numberofgrades = '';
098042ba 1036 if ($shownumberofgrades) {
1037 $numberofgrades = " ($mean_count)";
1038 }
1039
1040 $avghtml .= '<td class="cell c' . $columncount++.'">'.$gradehtml.$numberofgrades.'</td>';
4ba9941c 1041 }
1042 }
5b508a08 1043 $avghtml .= '</tr>';
4ba9941c 1044 }
5b508a08 1045 return $avghtml;
4ba9941c 1046 }
1047
e5161d0c 1048 /**
2f61fc0e 1049 * Builds and return the HTML row of ranges for each column (i.e. range).
e5161d0c 1050 * @return string HTML
1051 */
2f61fc0e 1052 function get_rangehtml() {
1053 global $USER;
9d35e66e 1054 $showuseridnumber = $this->get_pref('showuseridnumber');
2f61fc0e 1055
4ba9941c 1056 $scalehtml = '';
61649211 1057 if ($this->get_pref('showranges')) {
d622930b 1058 $rangesdisplaytype = $this->get_pref('rangesdisplaytype');
5b508a08 1059 $rangesdecimalpoints = $this->get_pref('rangesdecimalpoints');
d622930b 1060
9d35e66e 1061 $columncount=0;
1062 $colspan='';
1063 if ($showuseridnumber) {
1064 $colspan = 'colspan="2" ';
1065 }
1066
66ef0471 1067 $scalehtml = '<tr class="r'.$this->rowcount++.'">'
9d35e66e 1068 . '<th class="header c0 range" '.$colspan.'scope="row">'.$this->get_lang_string('range','grades').'</th>';
1069
1070 if ($showuseridnumber) {
1071 $columncount++;
1072 }
5b508a08 1073
b89a70ce 1074 foreach ($this->gtree->items as $itemid=>$unused) {
1075 $item =& $this->gtree->items[$itemid];
66ef0471 1076
d622930b 1077 // Determine which display type to use for this average
2cc773f5 1078 if ($USER->gradeediting[$this->courseid]) {
1796708d 1079 $displaytype = GRADE_DISPLAY_TYPE_REAL;
d622930b 1080
e0724506 1081 } else if ($rangesdisplaytype == GRADE_REPORT_PREFERENCE_INHERIT) { // no ==0 here, please resave report and user prefs
d622930b 1082 $displaytype = $item->get_displaytype();
1083
1084 } else {
2f61fc0e 1085 $displaytype = $rangesdisplaytype;
1086 }
1087
d622930b 1088 // Override grade_item setting if a display preference (not default) was set for the averages
e0724506 1089 if ($rangesdecimalpoints == GRADE_REPORT_PREFERENCE_INHERIT) {
d622930b 1090 $decimalpoints = $item->get_decimals();
1091
1092 } else {
5b508a08 1093 $decimalpoints = $rangesdecimalpoints;
1094 }
1095
d622930b 1096 if ($displaytype == GRADE_DISPLAY_TYPE_PERCENTAGE) {
1097 $grademin = "0 %";
1098 $grademax = "100 %";
1099
1100 } else {
1101 $grademin = grade_format_gradevalue($item->grademin, $item, true, $displaytype, $decimalpoints);
1102 $grademax = grade_format_gradevalue($item->grademax, $item, true, $displaytype, $decimalpoints);
2f61fc0e 1103 }
1104
80fb1cf9 1105 $hidden = '';
1106 if ($item->is_hidden()) {
1107 $hidden = ' hidden ';
1108 }
1109
1110 $scalehtml .= '<th class="header c'.$columncount++.' range"><span class="rangevalues'.$hidden.'">'. $grademin.'&ndash;'. $grademax.'</span></th>';
4ba9941c 1111 }
1112 $scalehtml .= '</tr>';
1113 }
1114 return $scalehtml;
1115 }
9ecd4386 1116
1117 /**
1118 * Builds and return the HTML row of ranges for each column (i.e. range).
1119 * @return string HTML
1120 */
1121 function get_iconshtml() {
1122 global $USER;
1123
1124 $iconshtml = '';
322a5f63 1125 if ($USER->gradeediting[$this->courseid]) {
1126
1127 $colspan='';
1128 if ($this->get_pref('showuseridnumber')) {
1129 $colspan = 'colspan="2" ';
1130 }
9ecd4386 1131
1132 $iconshtml = '<tr class="r'.$this->rowcount++.'">'
322a5f63 1133 . '<th class="header c0 range" scope="row" '.$colspan.'>'.$this->get_lang_string('controls','grades').'</th>';
9ecd4386 1134
1135 $columncount = 1;
1136 foreach ($this->gtree->items as $itemid=>$unused) {
1137 // emulate grade element
1138 $item =& $this->gtree->items[$itemid];
1139
1140 $eid = $this->gtree->get_item_eid($item);
1141 $element = $this->gtree->locate_element($eid);
1142
1143 $iconshtml .= '<td class="cell c'.$columncount++.' icons">' . $this->get_icons($element) . '</td>';
1144 }
1145 $iconshtml .= '</tr>';
1146 }
1147 return $iconshtml;
1148 }
4ba9941c 1149
1150 /**
1151 * Given a grade_category, grade_item or grade_grade, this function
1152 * figures out the state of the object and builds then returns a div
1153 * with the icons needed for the grader report.
1154 *
1155 * @param object $object
4ba9941c 1156 * @return string HTML
1157 */
2cc773f5 1158 function get_icons($element) {
1159 global $CFG, $USER;
4ba9941c 1160
2cc773f5 1161 if (!$USER->gradeediting[$this->courseid]) {
1162 return '<div class="grade_icons" />';
79eabc2a 1163 }
4ba9941c 1164
2cc773f5 1165 // Init all icons
1166 $edit_icon = $this->gtree->get_edit_icon($element, $this->gpr);
95d6df77 1167 $edit_calculation_icon = '';
2cc773f5 1168 $show_hide_icon = '';
2cc773f5 1169 $lock_unlock_icon = '';
4ba9941c 1170
a5b8be62 1171 if (has_capability('moodle/grade:manage', $this->context)) {
4ba9941c 1172
a5b8be62 1173 if ($this->get_pref('showcalculations')) {
1174 $edit_calculation_icon = $this->gtree->get_calculation_icon($element, $this->gpr);
1175 }
1176
1177 if ($this->get_pref('showeyecons')) {
1178 $show_hide_icon = $this->gtree->get_hiding_icon($element, $this->gpr);
1179 }
4ba9941c 1180
a5b8be62 1181 if ($this->get_pref('showlocks')) {
1182 $lock_unlock_icon = $this->gtree->get_locking_icon($element, $this->gpr);
1183 }
4ba9941c 1184 }
1185
4faf5f99 1186 return '<div class="grade_icons">'.$edit_icon.$edit_calculation_icon.$show_hide_icon.$lock_unlock_icon.'</div>';
1187 }
1188
1189 /**
1190 * Given a category element returns collapsing +/- icon if available
1191 * @param object $object
1192 * @return string HTML
1193 */
1194 function get_collapsing_icon($element) {
1195 global $CFG;
1196
1197 $contract_expand_icon = '';
2cc773f5 1198 // If object is a category, display expand/contract icon
384960dd 1199 if ($element['type'] == 'category') {
2cc773f5 1200 // Load language strings
384960dd 1201 $strswitch_minus = $this->get_lang_string('aggregatesonly', 'grades');
1202 $strswitch_plus = $this->get_lang_string('gradesonly', 'grades');
1203 $strswitch_whole = $this->get_lang_string('fullmode', 'grades');
1204
2cc773f5 1205 $expand_contract = 'switch_minus'; // Default: expanded
384960dd 1206 // $this->get_pref('aggregationview', $element['object']->id) == GRADE_REPORT_AGGREGATION_VIEW_COMPACT
48b5d8f3 1207
384960dd 1208 if (in_array($element['object']->id, $this->collapsed['aggregatesonly'])) {
2cc773f5 1209 $expand_contract = 'switch_plus';
384960dd 1210 } elseif (in_array($element['object']->id, $this->collapsed['gradesonly'])) {
1211 $expand_contract = 'switch_whole';
2cc773f5 1212 }
4faf5f99 1213 $url = $this->gpr->get_return_url(null, array('target'=>$element['eid'], 'action'=>$expand_contract, 'sesskey'=>sesskey()));
1214 $contract_expand_icon = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/'.$expand_contract.'.gif" class="iconsmall" alt="'
1215 .${'str'.$expand_contract}.'" title="'.${'str'.$expand_contract}.'" /></a>';
4ba9941c 1216 }
4faf5f99 1217 return $contract_expand_icon;
4ba9941c 1218 }
48b5d8f3 1219
1220 /**
1221 * Processes a single action against a category, grade_item or grade.
1222 * @param string $target eid ({type}{id}, e.g. c4 for category4)
1223 * @param string $action Which action to take (edit, delete etc...)
1224 * @return
1225 */
1226 function process_action($target, $action) {
4faf5f99 1227 // TODO: this code should be in some grade_tree static method
48b5d8f3 1228 $targettype = substr($target, 0, 1);
1229 $targetid = substr($target, 1);
4faf5f99 1230 // TODO: end
1231
1232 if ($collapsed = get_user_preferences('grade_report_grader_collapsed_categories')) {
1233 $collapsed = unserialize($collapsed);
1234 } else {
384960dd 1235 $collapsed = array('aggregatesonly' => array(), 'gradesonly' => array());
4faf5f99 1236 }
1237
48b5d8f3 1238 switch ($action) {
384960dd 1239 case 'switch_minus': // Add category to array of aggregatesonly
1240 if (!in_array($targetid, $collapsed['aggregatesonly'])) {
1241 $collapsed['aggregatesonly'][] = $targetid;
4faf5f99 1242 set_user_preference('grade_report_grader_collapsed_categories', serialize($collapsed));
1243 }
48b5d8f3 1244 break;
4faf5f99 1245
384960dd 1246 case 'switch_plus': // Remove category from array of aggregatesonly, and add it to array of gradesonly
1247 $key = array_search($targetid, $collapsed['aggregatesonly']);
4faf5f99 1248 if ($key !== false) {
384960dd 1249 unset($collapsed['aggregatesonly'][$key]);
4faf5f99 1250 }
384960dd 1251 if (!in_array($targetid, $collapsed['gradesonly'])) {
1252 $collapsed['gradesonly'][] = $targetid;
1253 }
1254 set_user_preference('grade_report_grader_collapsed_categories', serialize($collapsed));
48b5d8f3 1255 break;
384960dd 1256 case 'switch_whole': // Remove the category from the array of collapsed cats
1257 $key = array_search($targetid, $collapsed['gradesonly']);
1258 if ($key !== false) {
1259 unset($collapsed['gradesonly'][$key]);
1260 set_user_preference('grade_report_grader_collapsed_categories', serialize($collapsed));
1261 }
4faf5f99 1262
384960dd 1263 break;
48b5d8f3 1264 default:
1265 break;
1266 }
1267
1268 return true;
1269 }
4ba9941c 1270}
1271?>