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