Merge branch 'MDL-35762_fix' of git://github.com/andyjdavis/moodle
[moodle.git] / grade / report / user / lib.php
CommitLineData
e060e33d 1<?php
e060e33d 2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
8ad36f4c 16
f7998fbc 17/**
a153c9f2
AD
18 * Definition of the grade_user_report class is defined
19 *
20 * @package gradereport_user
21 * @copyright 2007 Nicolas Connault
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
f7998fbc 23 */
24
25require_once($CFG->dirroot . '/grade/report/lib.php');
26require_once($CFG->libdir.'/tablelib.php');
27
a83507f2
AD
28//showhiddenitems values
29define("GRADE_REPORT_USER_HIDE_HIDDEN", 0);
30define("GRADE_REPORT_USER_HIDE_UNTIL", 1);
31define("GRADE_REPORT_USER_SHOW_HIDDEN", 2);
32
f7998fbc 33/**
34 * Class providing an API for the user report building and displaying.
35 * @uses grade_report
a153c9f2 36 * @package gradereport_user
f7998fbc 37 */
38class grade_report_user extends grade_report {
39
40 /**
41 * The user.
42 * @var object $user
43 */
5c75a0a3 44 public $user;
f7998fbc 45
46 /**
47 * A flexitable to hold the data.
48 * @var object $table
49 */
5c75a0a3 50 public $table;
f7998fbc 51
a071dbb6 52 /**
53 * An array of table headers
54 * @var array
55 */
56 public $tableheaders = array();
57
58 /**
59 * An array of table columns
60 * @var array
61 */
62 public $tablecolumns = array();
63
64 /**
65 * An array containing rows of data for the table.
66 * @var type
67 */
68 public $tabledata = array();
69
70 /**
71 * The grade tree structure
72 * @var grade_tree
73 */
74 public $gtree;
653a8648 75
e0724506 76 /**
77 * Flat structure similar to grade tree
78 */
5c75a0a3 79 public $gseq;
e0724506 80
26ed0305 81 /**
82 * show student ranks
83 */
5c75a0a3 84 public $showrank;
26ed0305 85
699da6b5 86 /**
87 * show grade percentages
88 */
89 public $showpercentage;
90
5528217e 91 /**
92 * Show range
93 */
a071dbb6 94 public $showrange = true;
5528217e 95
a071dbb6 96 /**
97 * Show grades in the report, default true
98 * @var bool
99 */
100 public $showgrade = true;
5528217e 101
a071dbb6 102 /**
103 * Decimal points to use for values in the report, default 2
104 * @var int
105 */
106 public $decimals = 2;
5528217e 107
a071dbb6 108 /**
109 * The number of decimal places to round range to, default 0
110 * @var int
111 */
112 public $rangedecimals = 0;
5528217e 113
a071dbb6 114 /**
115 * Show grade feedback in the report, default true
116 * @var bool
117 */
118 public $showfeedback = true;
119
120 /**
121 * Show grade weighting in the report, default false
122 * @var bool
123 */
124 public $showweight = false;
125
126 /**
127 * Show letter grades in the report, default false
128 * @var bool
129 */
130 public $showlettergrade = false;
131
132 /**
133 * Show average grades in the report, default false.
134 * @var false
135 */
136 public $showaverage = false;
137
138 public $maxdepth;
139 public $evenodd;
140
141 public $canviewhidden;
142
143 public $switch;
5528217e 144
26ed0305 145 /**
146 * Show hidden items even when user does not have required cap
147 */
5c75a0a3 148 public $showhiddenitems;
a071dbb6 149 public $showtotalsifcontainhidden;
150
151 public $baseurl;
152 public $pbarurl;
26ed0305 153
f7998fbc 154 /**
155 * Constructor. Sets local copies of user preferences and initialises grade_tree.
156 * @param int $courseid
d30c4481 157 * @param object $gpr grade plugin return tracking object
f7998fbc 158 * @param string $context
159 * @param int $userid The id of the user
160 */
5c75a0a3 161 public function __construct($courseid, $gpr, $context, $userid) {
a071dbb6 162 global $DB, $CFG;
5c75a0a3 163 parent::__construct($courseid, $gpr, $context);
f7998fbc 164
597f50e6 165 $this->showrank = grade_get_setting($this->courseid, 'report_user_showrank', $CFG->grade_report_user_showrank);
699da6b5 166 $this->showpercentage = grade_get_setting($this->courseid, 'report_user_showpercentage', $CFG->grade_report_user_showpercentage);
597f50e6 167 $this->showhiddenitems = grade_get_setting($this->courseid, 'report_user_showhiddenitems', $CFG->grade_report_user_showhiddenitems);
61541a5a 168 $this->showtotalsifcontainhidden = grade_get_setting($this->courseid, 'report_user_showtotalsifcontainhidden', $CFG->grade_report_user_showtotalsifcontainhidden);
653a8648 169
a071dbb6 170 $this->showgrade = grade_get_setting($this->courseid, 'report_user_showgrade', !empty($CFG->grade_report_user_showgrade));
171 $this->showrange = grade_get_setting($this->courseid, 'report_user_showrange', !empty($CFG->grade_report_user_showrange));
172 $this->showfeedback = grade_get_setting($this->courseid, 'report_user_showfeedback', !empty($CFG->grade_report_user_showfeedback));
173 $this->showweight = grade_get_setting($this->courseid, 'report_user_showweight', !empty($CFG->grade_report_user_showweight));
174 $this->showlettergrade = grade_get_setting($this->courseid, 'report_user_showlettergrade', !empty($CFG->grade_report_user_showlettergrade));
175 $this->showaverage = grade_get_setting($this->courseid, 'report_user_showaverage', !empty($CFG->grade_report_user_showaverage));
176
177 // The default grade decimals is 2
178 $defaultdecimals = 2;
179 if (property_exists($CFG, 'grade_decimalpoints')) {
180 $defaultdecimals = $CFG->grade_decimalpoints;
181 }
182 $this->decimals = grade_get_setting($this->courseid, 'decimalpoints', $defaultdecimals);
183
184 // The default range decimals is 0
185 $defaultrangedecimals = 0;
186 if (property_exists($CFG, 'grade_report_user_rangedecimals')) {
187 $defaultrangedecimals = $CFG->grade_report_user_rangedecimals;
188 }
189 $this->rangedecimals = grade_get_setting($this->courseid, 'report_user_rangedecimals', $defaultrangedecimals);
190
5528217e 191 $this->switch = grade_get_setting($this->courseid, 'aggregationposition', $CFG->grade_aggregationposition);
192
193 // Grab the grade_tree for this course
4d27bc79 194 $this->gtree = new grade_tree($this->courseid, false, $this->switch, null, !$CFG->enableoutcomes);
26ed0305 195
5528217e 196 // Determine the number of rows and indentation
197 $this->maxdepth = 1;
198 $this->inject_rowspans($this->gtree->top_element);
199 $this->maxdepth++; // Need to account for the lead column that spans all children
200 for ($i = 1; $i <= $this->maxdepth; $i++) {
201 $this->evenodd[$i] = 0;
202 }
203
204 $this->tabledata = array();
e0724506 205
d4060472 206 $this->canviewhidden = has_capability('moodle/grade:viewhidden', context_course::instance($this->courseid));
4faf5f99 207
f7998fbc 208 // get the user (for full name)
5c75a0a3 209 $this->user = $DB->get_record('user', array('id' => $userid));
f7998fbc 210
211 // base url for sorting by first/last name
212 $this->baseurl = $CFG->wwwroot.'/grade/report?id='.$courseid.'&amp;userid='.$userid;
90d3960c 213 $this->pbarurl = $this->baseurl;
f7998fbc 214
d1214909 215 // no groups on this report - rank is from all course users
90d3960c 216 $this->setup_table();
a071dbb6 217
218 //optionally calculate grade item averages
219 $this->calculate_averages();
f7998fbc 220 }
221
5528217e 222 function inject_rowspans(&$element) {
223 if ($element['depth'] > $this->maxdepth) {
224 $this->maxdepth = $element['depth'];
225 }
226 if (empty($element['children'])) {
227 return 1;
228 }
229 $count = 1;
230 foreach ($element['children'] as $key=>$child) {
231 $count += $this->inject_rowspans($element['children'][$key]);
232 }
233 $element['rowspan'] = $count;
234 return $count;
235 }
236
237
f7998fbc 238 /**
239 * Prepares the headers and attributes of the flexitable.
240 */
5c75a0a3 241 public function setup_table() {
f7998fbc 242 /*
a071dbb6 243 * Table has 1-8 columns
244 *| All columns except for itemname/description are optional
d1214909 245 */
f7998fbc 246
247 // setting up table headers
a071dbb6 248
249 $this->tablecolumns = array('itemname');
250 $this->tableheaders = array($this->get_lang_string('gradeitem', 'grades'));
251
252 if ($this->showweight) {
253 $this->tablecolumns[] = 'weight';
254 $this->tableheaders[] = $this->get_lang_string('weightuc', 'grades');
255 }
256
257 if ($this->showgrade) {
258 $this->tablecolumns[] = 'grade';
259 $this->tableheaders[] = $this->get_lang_string('grade', 'grades');
260 }
699da6b5 261
5528217e 262 if ($this->showrange) {
263 $this->tablecolumns[] = 'range';
264 $this->tableheaders[] = $this->get_lang_string('range', 'grades');
265 }
266
699da6b5 267 if ($this->showpercentage) {
5528217e 268 $this->tablecolumns[] = 'percentage';
269 $this->tableheaders[] = $this->get_lang_string('percentage', 'grades');
699da6b5 270 }
271
a071dbb6 272 if ($this->showlettergrade) {
273 $this->tablecolumns[] = 'lettergrade';
274 $this->tableheaders[] = $this->get_lang_string('lettergrade', 'grades');
275 }
276
26ed0305 277 if ($this->showrank) {
5528217e 278 $this->tablecolumns[] = 'rank';
279 $this->tableheaders[] = $this->get_lang_string('rank', 'grades');
02973505 280 }
f7998fbc 281
a071dbb6 282 if ($this->showaverage) {
283 $this->tablecolumns[] = 'average';
284 $this->tableheaders[] = $this->get_lang_string('average', 'grades');
285 }
f7998fbc 286
a071dbb6 287 if ($this->showfeedback) {
288 $this->tablecolumns[] = 'feedback';
289 $this->tableheaders[] = $this->get_lang_string('feedback', 'grades');
290 }
5528217e 291 }
f7998fbc 292
5528217e 293 function fill_table() {
294 //print "<pre>";
295 //print_r($this->gtree->top_element);
296 $this->fill_table_recursive($this->gtree->top_element);
297 //print_r($this->tabledata);
298 //print "</pre>";
299 return true;
f7998fbc 300 }
301
5528217e 302 private function fill_table_recursive(&$element) {
a071dbb6 303 global $DB, $CFG;
f7998fbc 304
5528217e 305 $type = $element['type'];
306 $depth = $element['depth'];
307 $grade_object = $element['object'];
308 $eid = $grade_object->id;
c97933ff 309 $element['userid'] = $this->user->id;
5528217e 310 $fullname = $this->gtree->get_element_header($element, true, true, true);
311 $data = array();
312 $hidden = '';
313 $excluded = '';
314 $class = '';
62a966ad 315 $classfeedback = '';
f7998fbc 316
a83507f2 317 // If this is a hidden grade category, hide it completely from the user
9e322c8c 318 if ($type == 'category' && $grade_object->is_hidden() && !$this->canviewhidden && (
a83507f2
AD
319 $this->showhiddenitems == GRADE_REPORT_USER_HIDE_HIDDEN ||
320 ($this->showhiddenitems == GRADE_REPORT_USER_HIDE_UNTIL && !$grade_object->is_hiddenuntil()))) {
653a8648 321 return false;
322 }
6391ebe7 323
5528217e 324 if ($type == 'category') {
325 $this->evenodd[$depth] = (($this->evenodd[$depth] + 1) % 2);
b89a70ce 326 }
5528217e 327 $alter = ($this->evenodd[$depth] == 0) ? 'even' : 'odd';
6391ebe7 328
5528217e 329 /// Process those items that have scores associated
330 if ($type == 'item' or $type == 'categoryitem' or $type == 'courseitem') {
331 if (! $grade_grade = grade_grade::fetch(array('itemid'=>$grade_object->id,'userid'=>$this->user->id))) {
332 $grade_grade = new grade_grade();
333 $grade_grade->userid = $this->user->id;
334 $grade_grade->itemid = $grade_object->id;
d297269d 335 }
6391ebe7 336
5528217e 337 $grade_grade->load_grade_item();
f7998fbc 338
5528217e 339 /// Hidden Items
340 if ($grade_grade->grade_item->is_hidden()) {
341 $hidden = ' hidden';
ced5ee59 342 }
343
5fee56d5 344 $hide = false;
a83507f2 345 // If this is a hidden grade item, hide it completely from the user.
653a8648 346 if ($grade_grade->is_hidden() && !$this->canviewhidden && (
a83507f2
AD
347 $this->showhiddenitems == GRADE_REPORT_USER_HIDE_HIDDEN ||
348 ($this->showhiddenitems == GRADE_REPORT_USER_HIDE_UNTIL && !$grade_grade->is_hiddenuntil()))) {
5fee56d5
FM
349 $hide = true;
350 } else if (!empty($grade_object->itemmodule) && !empty($grade_object->iteminstance)) {
5e762271
AD
351 // The grade object can be marked visible but still be hidden if...
352 // 1) "enablegroupmembersonly" is on and the activity is assigned to a grouping the user is not in.
353 // 2) the student cannot see the activity due to conditional access and its set to be hidden entirely.
5fee56d5
FM
354 $instances = $this->gtree->modinfo->get_instances_of($grade_object->itemmodule);
355 if (!empty($instances[$grade_object->iteminstance])) {
356 $cm = $instances[$grade_object->iteminstance];
5e762271
AD
357 if (!$cm->uservisible) {
358 // Further checks are required to determine whether the activity is entirely hidden or just greyed out.
359 if ($cm->is_user_access_restricted_by_group() || $cm->is_user_access_restricted_by_conditional_access()) {
360 $hide = true;
361 }
aee748d3
AD
362 }
363 }
5fee56d5
FM
364 }
365
366 if (!$hide) {
1762a264
AD
367 /// Excluded Item
368 if ($grade_grade->is_excluded()) {
369 $fullname .= ' ['.get_string('excluded', 'grades').']';
370 $excluded = ' excluded';
371 }
653a8648 372
1762a264
AD
373 /// Other class information
374 $class = "$hidden $excluded";
375 if ($this->switch) { // alter style based on whether aggregation is first or last
376 $class .= ($type == 'categoryitem' or $type == 'courseitem') ? " ".$alter."d$depth baggt b2b" : " item b1b";
377 } else {
378 $class .= ($type == 'categoryitem' or $type == 'courseitem') ? " ".$alter."d$depth baggb" : " item b1b";
379 }
f7998fbc 380
1762a264
AD
381 /// Name
382 $data['itemname']['content'] = $fullname;
383 $data['itemname']['class'] = $class;
384 $data['itemname']['colspan'] = ($this->maxdepth - $depth);
e0724506 385
1762a264
AD
386 /// Actual Grade
387 $gradeval = $grade_grade->finalgrade;
a071dbb6 388
62a966ad
AD
389 if ($this->showfeedback) {
390 // Copy $class before appending itemcenter as feedback should not be centered
391 $classfeedback = $class;
392 }
a071dbb6 393 $class .= " itemcenter ";
394 if ($this->showweight) {
395 $data['weight']['class'] = $class;
396 $data['weight']['content'] = '-';
397 // has a weight assigned, might be extra credit
398 if ($grade_object->aggregationcoef > 0 && $type <> 'courseitem') {
399 $data['weight']['content'] = number_format($grade_object->aggregationcoef,2).'%';
400 }
699da6b5 401 }
6391ebe7 402
a071dbb6 403 if ($this->showgrade) {
404 if ($grade_grade->grade_item->needsupdate) {
405 $data['grade']['class'] = $class.' gradingerror';
406 $data['grade']['content'] = get_string('error');
407 } else if (!empty($CFG->grade_hiddenasdate) and $grade_grade->get_datesubmitted() and !$this->canviewhidden and $grade_grade->is_hidden()
408 and !$grade_grade->grade_item->is_category_item() and !$grade_grade->grade_item->is_course_item()) {
409 // 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
410 $class .= ' datesubmitted';
411 $data['grade']['class'] = $class;
412 $data['grade']['content'] = get_string('submittedon', 'grades', userdate($grade_grade->get_datesubmitted(), get_string('strftimedatetimeshort')));
413
414 } elseif ($grade_grade->is_hidden()) {
415 $data['grade']['class'] = $class.' hidden';
416 $data['grade']['content'] = '-';
417 } else {
418 $data['grade']['class'] = $class;
419 $gradeval = $this->blank_hidden_total($this->courseid, $grade_grade->grade_item, $gradeval);
420 $data['grade']['content'] = grade_format_gradevalue($gradeval, $grade_grade->grade_item, true);
421 }
422 }
423
424 // Range
425 if ($this->showrange) {
426 $data['range']['class'] = $class;
427 $data['range']['content'] = $grade_grade->grade_item->get_formatted_range(GRADE_DISPLAY_TYPE_REAL, $this->rangedecimals);
428 }
429
430 // Percentage
1762a264
AD
431 if ($this->showpercentage) {
432 if ($grade_grade->grade_item->needsupdate) {
433 $data['percentage']['class'] = $class.' gradingerror';
434 $data['percentage']['content'] = get_string('error');
a071dbb6 435 } else if ($grade_grade->is_hidden()) {
436 $data['percentage']['class'] = $class.' hidden';
437 $data['percentage']['content'] = '-';
1762a264
AD
438 } else {
439 $data['percentage']['class'] = $class;
440 $data['percentage']['content'] = grade_format_gradevalue($gradeval, $grade_grade->grade_item, true, GRADE_DISPLAY_TYPE_PERCENTAGE);
441 }
442 }
443
a071dbb6 444 // Lettergrade
445 if ($this->showlettergrade) {
446 if ($grade_grade->grade_item->needsupdate) {
447 $data['lettergrade']['class'] = $class.' gradingerror';
448 $data['lettergrade']['content'] = get_string('error');
449 } else if ($grade_grade->is_hidden()) {
450 $data['lettergrade']['class'] = $class.' hidden';
451 if (!$this->canviewhidden) {
452 $data['lettergrade']['content'] = '-';
453 } else {
454 $data['lettergrade']['content'] = grade_format_gradevalue($gradeval, $grade_grade->grade_item, true, GRADE_DISPLAY_TYPE_LETTER);
455 }
456 } else {
457 $data['lettergrade']['class'] = $class;
458 $data['lettergrade']['content'] = grade_format_gradevalue($gradeval, $grade_grade->grade_item, true, GRADE_DISPLAY_TYPE_LETTER);
459 }
460 }
461
462 // Rank
1762a264 463 if ($this->showrank) {
1762a264
AD
464 if ($grade_grade->grade_item->needsupdate) {
465 $data['rank']['class'] = $class.' gradingerror';
466 $data['rank']['content'] = get_string('error');
467 } elseif ($grade_grade->is_hidden()) {
468 $data['rank']['class'] = $class.' hidden';
469 $data['rank']['content'] = '-';
470 } else if (is_null($gradeval)) {
471 // no grade, no rank
472 $data['rank']['class'] = $class;
653a8648 473 $data['rank']['content'] = '-';
26ed0305 474
1762a264
AD
475 } else {
476 /// find the number of users with a higher grade
477 $sql = "SELECT COUNT(DISTINCT(userid))
478 FROM {grade_grades}
479 WHERE finalgrade > ?
a071dbb6 480 AND itemid = ?
481 AND hidden = 0";
1762a264
AD
482 $rank = $DB->count_records_sql($sql, array($grade_grade->finalgrade, $grade_grade->grade_item->id)) + 1;
483
484 $data['rank']['class'] = $class;
485 $data['rank']['content'] = "$rank/".$this->get_numusers(false); // total course users
486 }
02973505 487 }
f7998fbc 488
a071dbb6 489 // Average
490 if ($this->showaverage) {
491 $data['average']['class'] = $class;
492 if (!empty($this->gtree->items[$eid]->avg)) {
493 $data['average']['content'] = $this->gtree->items[$eid]->avg;
494 } else {
495 $data['average']['content'] = '-';
496 }
1762a264 497 }
6391ebe7 498
a071dbb6 499 // Feedback
500 if ($this->showfeedback) {
501 if ($grade_grade->overridden > 0 AND ($type == 'categoryitem' OR $type == 'courseitem')) {
62a966ad 502 $data['feedback']['class'] = $classfeedback.' feedbacktext';
a071dbb6 503 $data['feedback']['content'] = get_string('overridden', 'grades').': ' . format_text($grade_grade->feedback, $grade_grade->feedbackformat);
504 } else if (empty($grade_grade->feedback) or (!$this->canviewhidden and $grade_grade->is_hidden())) {
62a966ad 505 $data['feedback']['class'] = $classfeedback.' feedbacktext';
a071dbb6 506 $data['feedback']['content'] = '&nbsp;';
507 } else {
62a966ad 508 $data['feedback']['class'] = $classfeedback.' feedbacktext';
a071dbb6 509 $data['feedback']['content'] = format_text($grade_grade->feedback, $grade_grade->feedbackformat);
510 }
1762a264 511 }
5528217e 512 }
f7998fbc 513 }
e0724506 514
5528217e 515 /// Category
516 if ($type == 'category') {
517 $data['leader']['class'] = $class.' '.$alter."d$depth b1t b2b b1l";
518 $data['leader']['rowspan'] = $element['rowspan'];
519
520 if ($this->switch) { // alter style based on whether aggregation is first or last
521 $data['itemname']['class'] = $class.' '.$alter."d$depth b1b b1t";
522 } else {
523 $data['itemname']['class'] = $class.' '.$alter."d$depth b2t";
524 }
525 $data['itemname']['colspan'] = ($this->maxdepth - $depth + count($this->tablecolumns) - 1);
526 $data['itemname']['content'] = $fullname;
527 }
528
529 /// Add this row to the overall system
530 $this->tabledata[] = $data;
531
532 /// Recursively iterate through all child elements
533 if (isset($element['children'])) {
534 foreach ($element['children'] as $key=>$child) {
535 $this->fill_table_recursive($element['children'][$key]);
536 }
537 }
f7998fbc 538 }
539
540 /**
541 * Prints or returns the HTML from the flexitable.
542 * @param bool $return Whether or not to return the data instead of printing it directly.
543 * @return string
544 */
5c75a0a3 545 public function print_table($return=false) {
5528217e 546 $maxspan = $this->maxdepth;
547
548 /// Build table structure
549 $html = "
81f4918e 550 <table cellspacing='0' cellpadding='0' class='boxaligncenter generaltable user-grade'>
5528217e 551 <thead>
552 <tr>
553 <th class=\"header\" colspan='$maxspan'>".$this->tableheaders[0]."</th>\n";
554
555 for ($i = 1; $i < count($this->tableheaders); $i++) {
556 $html .= "<th class=\"header\">".$this->tableheaders[$i]."</th>\n";
557 }
558
559 $html .= "
560 </tr>
561 </thead>
562 <tbody>\n";
563
564 /// Print out the table data
565 for ($i = 0; $i < count($this->tabledata); $i++) {
566 $html .= "<tr>\n";
567 if (isset($this->tabledata[$i]['leader'])) {
568 $rowspan = $this->tabledata[$i]['leader']['rowspan'];
569 $class = $this->tabledata[$i]['leader']['class'];
570 $html .= "<td class='$class' rowspan='$rowspan'></td>\n";
571 }
572 for ($j = 0; $j < count($this->tablecolumns); $j++) {
573 $name = $this->tablecolumns[$j];
574 $class = (isset($this->tabledata[$i][$name]['class'])) ? $this->tabledata[$i][$name]['class'] : '';
575 $colspan = (isset($this->tabledata[$i][$name]['colspan'])) ? "colspan='".$this->tabledata[$i][$name]['colspan']."'" : '';
576 $content = (isset($this->tabledata[$i][$name]['content'])) ? $this->tabledata[$i][$name]['content'] : null;
577 if (isset($content)) {
578 $html .= "<td class='$class' $colspan>$content</td>\n";
579 }
580 }
581 $html .= "</tr>\n";
582 }
583
584 $html .= "</tbody></table>";
585
f7998fbc 586 if ($return) {
587 return $html;
588 } else {
589 echo $html;
590 }
591 }
592
593 /**
594 * Processes the data sent by the form (grades and feedbacks).
595 * @var array $data
596 * @return bool Success or Failure (array of errors).
597 */
653a8648 598 function process_data($data) {
5c75a0a3 599 }
653a8648 600 function process_action($target, $action) {
f7998fbc 601 }
a071dbb6 602
603 /**
604 * Builds the grade item averages.
605 *
606 */
607 function calculate_averages() {
608 global $USER, $DB;
609
610 if ($this->showaverage) {
611 // this settings are actually grader report settings (not user report)
612 // however we're using them as having two separate but identical settings the
613 // user would have to keep in sync would be annoying
614 $averagesdisplaytype = $this->get_pref('averagesdisplaytype');
615 $averagesdecimalpoints = $this->get_pref('averagesdecimalpoints');
616 $meanselection = $this->get_pref('meanselection');
617 $shownumberofgrades = $this->get_pref('shownumberofgrades');
618
619 $avghtml = '';
620 $avgcssclass = 'avg';
621
622 $straverage = get_string('overallaverage', 'grades');
623
624 $groupsql = $this->groupsql;
625 $groupwheresql = $this->groupwheresql;
626 //$groupwheresqlparams = ;
627
628 if ($shownumberofgrades) {
629 $straverage .= ' (' . get_string('submissions', 'grades') . ') ';
630 }
631
632 $totalcount = $this->get_numusers(false);
633
634 //limit to users with a gradeable role ie students
635 list($gradebookrolessql, $gradebookrolesparams) = $DB->get_in_or_equal(explode(',', $this->gradebookroles), SQL_PARAMS_NAMED, 'grbr0');
636
637 //limit to users with an active enrolment
638 list($enrolledsql, $enrolledparams) = get_enrolled_sql($this->context);
639
640 $params = array_merge($this->groupwheresql_params, $gradebookrolesparams, $enrolledparams);
641 $params['courseid'] = $this->courseid;
642
643 // find sums of all grade items in course
644 $sql = "SELECT gg.itemid, SUM(gg.finalgrade) AS sum
645 FROM {grade_items} gi
656d17c2
AD
646 JOIN {grade_grades} gg ON gg.itemid = gi.id
647 JOIN {user} u ON u.id = gg.userid
648 JOIN ($enrolledsql) je ON je.id = gg.userid
649 JOIN (
650 SELECT DISTINCT ra.userid
651 FROM {role_assignments} ra
652 WHERE ra.roleid $gradebookrolessql
653 AND ra.contextid " . get_related_contexts_string($this->context) . "
654 ) rainner ON rainner.userid = u.id
a071dbb6 655 $groupsql
656 WHERE gi.courseid = :courseid
656d17c2
AD
657 AND u.deleted = 0
658 AND gg.finalgrade IS NOT NULL
659 AND gg.hidden = 0
660 $groupwheresql
a071dbb6 661 GROUP BY gg.itemid";
662
663 $sum_array = array();
cc65dc36
EL
664 $sums = $DB->get_recordset_sql($sql, $params);
665 foreach ($sums as $itemid => $csum) {
666 $sum_array[$itemid] = $csum->sum;
a071dbb6 667 }
cc65dc36 668 $sums->close();
a071dbb6 669
670 $columncount=0;
671
672 // Empty grades must be evaluated as grademin, NOT always 0
673 // This query returns a count of ungraded grades (NULL finalgrade OR no matching record in grade_grades table)
674 // No join condition when joining grade_items and user to get a grade item row for every user
675 // Then left join with grade_grades and look for rows with null final grade (which includes grade items with no grade_grade)
676 $sql = "SELECT gi.id, COUNT(u.id) AS count
677 FROM {grade_items} gi
d3e30347 678 JOIN {user} u ON u.deleted = 0
656d17c2
AD
679 JOIN ($enrolledsql) je ON je.id = u.id
680 JOIN (
681 SELECT DISTINCT ra.userid
682 FROM {role_assignments} ra
683 WHERE ra.roleid $gradebookrolessql
684 AND ra.contextid " . get_related_contexts_string($this->context) . "
685 ) rainner ON rainner.userid = u.id
a1e26b5e 686 LEFT JOIN {grade_grades} gg
d3e30347 687 ON (gg.itemid = gi.id AND gg.userid = u.id AND gg.finalgrade IS NOT NULL AND gg.hidden = 0)
a071dbb6 688 $groupsql
656d17c2 689 WHERE gi.courseid = :courseid
656d17c2
AD
690 AND gg.finalgrade IS NULL
691 $groupwheresql
692 GROUP BY gi.id";
a071dbb6 693
694 $ungraded_counts = $DB->get_records_sql($sql, $params);
695
696 foreach ($this->gtree->items as $itemid=>$unused) {
697 if (!empty($this->gtree->items[$itemid]->avg)) {
698 continue;
699 }
700 $item = $this->gtree->items[$itemid];
701
702 if ($item->needsupdate) {
703 $avghtml .= '<td class="cell c' . $columncount++.'"><span class="gradingerror">'.get_string('error').'</span></td>';
704 continue;
705 }
706
707 if (empty($sum_array[$item->id])) {
708 $sum_array[$item->id] = 0;
709 }
710
711 if (empty($ungraded_counts[$itemid])) {
712 $ungraded_count = 0;
713 } else {
714 $ungraded_count = $ungraded_counts[$itemid]->count;
715 }
716
717 //do they want the averages to include all grade items
718 if ($meanselection == GRADE_REPORT_MEAN_GRADED) {
719 $mean_count = $totalcount - $ungraded_count;
720 } else { // Bump up the sum by the number of ungraded items * grademin
721 $sum_array[$item->id] += ($ungraded_count * $item->grademin);
722 $mean_count = $totalcount;
723 }
724
725 $decimalpoints = $item->get_decimals();
726
727 // Determine which display type to use for this average
728 if (!empty($USER->gradeediting) && $USER->gradeediting[$this->courseid]) {
729 $displaytype = GRADE_DISPLAY_TYPE_REAL;
730
731 } else if ($averagesdisplaytype == GRADE_REPORT_PREFERENCE_INHERIT) { // no ==0 here, please resave the report and user preferences
732 $displaytype = $item->get_displaytype();
733
734 } else {
735 $displaytype = $averagesdisplaytype;
736 }
737
738 // Override grade_item setting if a display preference (not inherit) was set for the averages
739 if ($averagesdecimalpoints == GRADE_REPORT_PREFERENCE_INHERIT) {
740 $decimalpoints = $item->get_decimals();
741
742 } else {
743 $decimalpoints = $averagesdecimalpoints;
744 }
745
746 if (empty($sum_array[$item->id]) || $mean_count == 0) {
747 $this->gtree->items[$itemid]->avg = '-';
748 } else {
749 $sum = $sum_array[$item->id];
750 $avgradeval = $sum/$mean_count;
751 $gradehtml = grade_format_gradevalue($avgradeval, $item, true, $displaytype, $decimalpoints);
752
753 $numberofgrades = '';
754 if ($shownumberofgrades) {
755 $numberofgrades = " ($mean_count)";
756 }
757
758 $this->gtree->items[$itemid]->avg = $gradehtml.$numberofgrades;
759 }
760 }
761 }
762 }
26ed0305 763}
764
765function grade_report_user_settings_definition(&$mform) {
766 global $CFG;
767
768 $options = array(-1 => get_string('default', 'grades'),
769 0 => get_string('hide'),
770 1 => get_string('show'));
771
cd52d909 772 if (empty($CFG->grade_report_user_showrank)) {
26ed0305 773 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
774 } else {
775 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
776 }
777
778 $mform->addElement('select', 'report_user_showrank', get_string('showrank', 'grades'), $options);
1cc3165e 779 $mform->addHelpButton('report_user_showrank', 'showrank', 'grades');
26ed0305 780
699da6b5 781 if (empty($CFG->grade_report_user_showpercentage)) {
aea45e7e 782 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
5528217e 783 } else {
784 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
699da6b5 785 }
786
787 $mform->addElement('select', 'report_user_showpercentage', get_string('showpercentage', 'grades'), $options);
ff268dcb 788 $mform->addHelpButton('report_user_showpercentage', 'showpercentage', 'grades');
699da6b5 789
a071dbb6 790 if (empty($CFG->grade_report_user_showgrade)) {
791 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
792 } else {
793 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
794 }
795
796 $mform->addElement('select', 'report_user_showgrade', get_string('showgrade', 'grades'), $options);
797
798 if (empty($CFG->grade_report_user_showfeedback)) {
799 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
800 } else {
801 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
802 }
803
804 $mform->addElement('select', 'report_user_showfeedback', get_string('showfeedback', 'grades'), $options);
805
806 if (empty($CFG->grade_report_user_showweight)) {
807 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
808 } else {
809 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
810 }
811
812 $mform->addElement('select', 'report_user_showweight', get_string('showweight', 'grades'), $options);
813
814 if (empty($CFG->grade_report_user_showaverage)) {
815 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
816 } else {
817 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
818 }
819
820 $mform->addElement('select', 'report_user_showaverage', get_string('showaverage', 'grades'), $options);
821 $mform->addHelpButton('report_user_showaverage', 'showaverage', 'grades');
822
823 if (empty($CFG->grade_report_user_showlettergrade)) {
824 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
825 } else {
826 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
827 }
828
829 $mform->addElement('select', 'report_user_showlettergrade', get_string('showlettergrade', 'grades'), $options);
830
831 if (empty($CFG->grade_report_user_showrange)) {
832 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
833 } else {
834 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
835 }
836
837 $mform->addElement('select', 'report_user_showrange', get_string('showrange', 'grades'), $options);
838
839 $options = array(0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5);
840 if (! empty($CFG->grade_report_user_rangedecimals)) {
841 $options[-1] = $options[$CFG->grade_report_user_rangedecimals];
842 }
843 $mform->addElement('select', 'report_user_rangedecimals', get_string('rangedecimals', 'grades'), $options);
844
26ed0305 845 $options = array(-1 => get_string('default', 'grades'),
d69ef28b 846 0 => get_string('shownohidden', 'grades'),
597f50e6 847 1 => get_string('showhiddenuntilonly', 'grades'),
d69ef28b 848 2 => get_string('showallhidden', 'grades'));
26ed0305 849
cd52d909 850 if (empty($CFG->grade_report_user_showhiddenitems)) {
26ed0305 851 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
852 } else {
d69ef28b 853 $options[-1] = get_string('defaultprev', 'grades', $options[$CFG->grade_report_user_showhiddenitems]);
26ed0305 854 }
855
856 $mform->addElement('select', 'report_user_showhiddenitems', get_string('showhiddenitems', 'grades'), $options);
69e3e962 857 $mform->addHelpButton('report_user_showhiddenitems', 'showhiddenitems', 'grades');
61541a5a
AD
858
859 //showtotalsifcontainhidden
860 $options = array(-1 => get_string('default', 'grades'),
861 GRADE_REPORT_HIDE_TOTAL_IF_CONTAINS_HIDDEN => get_string('hide'),
862 GRADE_REPORT_SHOW_TOTAL_IF_CONTAINS_HIDDEN => get_string('hidetotalshowexhiddenitems', 'grades'),
863 GRADE_REPORT_SHOW_REAL_TOTAL_IF_CONTAINS_HIDDEN => get_string('hidetotalshowinchiddenitems', 'grades') );
864
865 if (empty($CFG->grade_report_user_showtotalsifcontainhidden)) {
866 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
867 } else {
868 $options[-1] = get_string('defaultprev', 'grades', $options[$CFG->grade_report_user_showtotalsifcontainhidden]);
869 }
870
871 $mform->addElement('select', 'report_user_showtotalsifcontainhidden', get_string('hidetotalifhiddenitems', 'grades'), $options);
0459cc4d 872 $mform->addHelpButton('report_user_showtotalsifcontainhidden', 'hidetotalifhiddenitems', 'grades');
0a784551 873}
874
875function grade_report_user_profilereport($course, $user) {
91152a35 876 global $OUTPUT;
0a784551 877 if (!empty($course->showgrades)) {
878
d4060472 879 $context = context_course::instance($course->id);
0a784551 880
881 //first make sure we have proper final grades - this must be done before constructing of the grade tree
882 grade_regrade_final_grades($course->id);
883
884 /// return tracking object
885 $gpr = new grade_plugin_return(array('type'=>'report', 'plugin'=>'user', 'courseid'=>$course->id, 'userid'=>$user->id));
886 // Create a report instance
887 $report = new grade_report_user($course->id, $gpr, $context, $user->id);
f7998fbc 888
0a784551 889 // print the page
890 echo '<div class="grade-report-user">'; // css fix to share styles with real report page
b5e7b2bf 891 echo $OUTPUT->heading(get_string('pluginname', 'gradereport_user'). ' - '.fullname($report->user));
0a784551 892
893 if ($report->fill_table()) {
894 echo $report->print_table(true);
895 }
896 echo '</div>';
897 }
f7998fbc 898}
0a784551 899
6c3ef410 900