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