Merge branch 'w18_MDL-32676_m23_yuibootstrap' of git://github.com/skodak/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
5528217e 206 $this->canviewhidden = has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $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
a83507f2 344 // If this is a hidden grade item, hide it completely from the user.
653a8648 345 if ($grade_grade->is_hidden() && !$this->canviewhidden && (
a83507f2
AD
346 $this->showhiddenitems == GRADE_REPORT_USER_HIDE_HIDDEN ||
347 ($this->showhiddenitems == GRADE_REPORT_USER_HIDE_UNTIL && !$grade_grade->is_hiddenuntil()))) {
653a8648 348 // return false;
349 } else {
aee748d3
AD
350 // The grade object can be marked visible but still be hidden
351 // if "enablegroupmembersonly" is on and its an activity assigned to a grouping the user is not in
352 if (!empty($grade_object->itemmodule) && !empty($grade_object->iteminstance)) {
aee748d3
AD
353 $instances = $this->gtree->modinfo->get_instances();
354 if (!empty($instances[$grade_object->itemmodule][$grade_object->iteminstance])) {
355 $cm = $instances[$grade_object->itemmodule][$grade_object->iteminstance];
356 if (!$cm->uservisible) {
357 return false;
358 }
359 }
360 }
1762a264
AD
361 /// Excluded Item
362 if ($grade_grade->is_excluded()) {
363 $fullname .= ' ['.get_string('excluded', 'grades').']';
364 $excluded = ' excluded';
365 }
653a8648 366
1762a264
AD
367 /// Other class information
368 $class = "$hidden $excluded";
369 if ($this->switch) { // alter style based on whether aggregation is first or last
370 $class .= ($type == 'categoryitem' or $type == 'courseitem') ? " ".$alter."d$depth baggt b2b" : " item b1b";
371 } else {
372 $class .= ($type == 'categoryitem' or $type == 'courseitem') ? " ".$alter."d$depth baggb" : " item b1b";
373 }
f7998fbc 374
1762a264
AD
375 /// Name
376 $data['itemname']['content'] = $fullname;
377 $data['itemname']['class'] = $class;
378 $data['itemname']['colspan'] = ($this->maxdepth - $depth);
e0724506 379
1762a264
AD
380 /// Actual Grade
381 $gradeval = $grade_grade->finalgrade;
a071dbb6 382
62a966ad
AD
383 if ($this->showfeedback) {
384 // Copy $class before appending itemcenter as feedback should not be centered
385 $classfeedback = $class;
386 }
a071dbb6 387 $class .= " itemcenter ";
388 if ($this->showweight) {
389 $data['weight']['class'] = $class;
390 $data['weight']['content'] = '-';
391 // has a weight assigned, might be extra credit
392 if ($grade_object->aggregationcoef > 0 && $type <> 'courseitem') {
393 $data['weight']['content'] = number_format($grade_object->aggregationcoef,2).'%';
394 }
699da6b5 395 }
6391ebe7 396
a071dbb6 397 if ($this->showgrade) {
398 if ($grade_grade->grade_item->needsupdate) {
399 $data['grade']['class'] = $class.' gradingerror';
400 $data['grade']['content'] = get_string('error');
401 } else if (!empty($CFG->grade_hiddenasdate) and $grade_grade->get_datesubmitted() and !$this->canviewhidden and $grade_grade->is_hidden()
402 and !$grade_grade->grade_item->is_category_item() and !$grade_grade->grade_item->is_course_item()) {
403 // 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
404 $class .= ' datesubmitted';
405 $data['grade']['class'] = $class;
406 $data['grade']['content'] = get_string('submittedon', 'grades', userdate($grade_grade->get_datesubmitted(), get_string('strftimedatetimeshort')));
407
408 } elseif ($grade_grade->is_hidden()) {
409 $data['grade']['class'] = $class.' hidden';
410 $data['grade']['content'] = '-';
411 } else {
412 $data['grade']['class'] = $class;
413 $gradeval = $this->blank_hidden_total($this->courseid, $grade_grade->grade_item, $gradeval);
414 $data['grade']['content'] = grade_format_gradevalue($gradeval, $grade_grade->grade_item, true);
415 }
416 }
417
418 // Range
419 if ($this->showrange) {
420 $data['range']['class'] = $class;
421 $data['range']['content'] = $grade_grade->grade_item->get_formatted_range(GRADE_DISPLAY_TYPE_REAL, $this->rangedecimals);
422 }
423
424 // Percentage
1762a264
AD
425 if ($this->showpercentage) {
426 if ($grade_grade->grade_item->needsupdate) {
427 $data['percentage']['class'] = $class.' gradingerror';
428 $data['percentage']['content'] = get_string('error');
a071dbb6 429 } else if ($grade_grade->is_hidden()) {
430 $data['percentage']['class'] = $class.' hidden';
431 $data['percentage']['content'] = '-';
1762a264
AD
432 } else {
433 $data['percentage']['class'] = $class;
434 $data['percentage']['content'] = grade_format_gradevalue($gradeval, $grade_grade->grade_item, true, GRADE_DISPLAY_TYPE_PERCENTAGE);
435 }
436 }
437
a071dbb6 438 // Lettergrade
439 if ($this->showlettergrade) {
440 if ($grade_grade->grade_item->needsupdate) {
441 $data['lettergrade']['class'] = $class.' gradingerror';
442 $data['lettergrade']['content'] = get_string('error');
443 } else if ($grade_grade->is_hidden()) {
444 $data['lettergrade']['class'] = $class.' hidden';
445 if (!$this->canviewhidden) {
446 $data['lettergrade']['content'] = '-';
447 } else {
448 $data['lettergrade']['content'] = grade_format_gradevalue($gradeval, $grade_grade->grade_item, true, GRADE_DISPLAY_TYPE_LETTER);
449 }
450 } else {
451 $data['lettergrade']['class'] = $class;
452 $data['lettergrade']['content'] = grade_format_gradevalue($gradeval, $grade_grade->grade_item, true, GRADE_DISPLAY_TYPE_LETTER);
453 }
454 }
455
456 // Rank
1762a264 457 if ($this->showrank) {
1762a264
AD
458 if ($grade_grade->grade_item->needsupdate) {
459 $data['rank']['class'] = $class.' gradingerror';
460 $data['rank']['content'] = get_string('error');
461 } elseif ($grade_grade->is_hidden()) {
462 $data['rank']['class'] = $class.' hidden';
463 $data['rank']['content'] = '-';
464 } else if (is_null($gradeval)) {
465 // no grade, no rank
466 $data['rank']['class'] = $class;
653a8648 467 $data['rank']['content'] = '-';
26ed0305 468
1762a264
AD
469 } else {
470 /// find the number of users with a higher grade
471 $sql = "SELECT COUNT(DISTINCT(userid))
472 FROM {grade_grades}
473 WHERE finalgrade > ?
a071dbb6 474 AND itemid = ?
475 AND hidden = 0";
1762a264
AD
476 $rank = $DB->count_records_sql($sql, array($grade_grade->finalgrade, $grade_grade->grade_item->id)) + 1;
477
478 $data['rank']['class'] = $class;
479 $data['rank']['content'] = "$rank/".$this->get_numusers(false); // total course users
480 }
02973505 481 }
f7998fbc 482
a071dbb6 483 // Average
484 if ($this->showaverage) {
485 $data['average']['class'] = $class;
486 if (!empty($this->gtree->items[$eid]->avg)) {
487 $data['average']['content'] = $this->gtree->items[$eid]->avg;
488 } else {
489 $data['average']['content'] = '-';
490 }
1762a264 491 }
6391ebe7 492
a071dbb6 493 // Feedback
494 if ($this->showfeedback) {
495 if ($grade_grade->overridden > 0 AND ($type == 'categoryitem' OR $type == 'courseitem')) {
62a966ad 496 $data['feedback']['class'] = $classfeedback.' feedbacktext';
a071dbb6 497 $data['feedback']['content'] = get_string('overridden', 'grades').': ' . format_text($grade_grade->feedback, $grade_grade->feedbackformat);
498 } else if (empty($grade_grade->feedback) or (!$this->canviewhidden and $grade_grade->is_hidden())) {
62a966ad 499 $data['feedback']['class'] = $classfeedback.' feedbacktext';
a071dbb6 500 $data['feedback']['content'] = '&nbsp;';
501 } else {
62a966ad 502 $data['feedback']['class'] = $classfeedback.' feedbacktext';
a071dbb6 503 $data['feedback']['content'] = format_text($grade_grade->feedback, $grade_grade->feedbackformat);
504 }
1762a264 505 }
5528217e 506 }
f7998fbc 507 }
e0724506 508
5528217e 509 /// Category
510 if ($type == 'category') {
511 $data['leader']['class'] = $class.' '.$alter."d$depth b1t b2b b1l";
512 $data['leader']['rowspan'] = $element['rowspan'];
513
514 if ($this->switch) { // alter style based on whether aggregation is first or last
515 $data['itemname']['class'] = $class.' '.$alter."d$depth b1b b1t";
516 } else {
517 $data['itemname']['class'] = $class.' '.$alter."d$depth b2t";
518 }
519 $data['itemname']['colspan'] = ($this->maxdepth - $depth + count($this->tablecolumns) - 1);
520 $data['itemname']['content'] = $fullname;
521 }
522
523 /// Add this row to the overall system
524 $this->tabledata[] = $data;
525
526 /// Recursively iterate through all child elements
527 if (isset($element['children'])) {
528 foreach ($element['children'] as $key=>$child) {
529 $this->fill_table_recursive($element['children'][$key]);
530 }
531 }
f7998fbc 532 }
533
534 /**
535 * Prints or returns the HTML from the flexitable.
536 * @param bool $return Whether or not to return the data instead of printing it directly.
537 * @return string
538 */
5c75a0a3 539 public function print_table($return=false) {
5528217e 540 $maxspan = $this->maxdepth;
541
542 /// Build table structure
543 $html = "
81f4918e 544 <table cellspacing='0' cellpadding='0' class='boxaligncenter generaltable user-grade'>
5528217e 545 <thead>
546 <tr>
547 <th class=\"header\" colspan='$maxspan'>".$this->tableheaders[0]."</th>\n";
548
549 for ($i = 1; $i < count($this->tableheaders); $i++) {
550 $html .= "<th class=\"header\">".$this->tableheaders[$i]."</th>\n";
551 }
552
553 $html .= "
554 </tr>
555 </thead>
556 <tbody>\n";
557
558 /// Print out the table data
559 for ($i = 0; $i < count($this->tabledata); $i++) {
560 $html .= "<tr>\n";
561 if (isset($this->tabledata[$i]['leader'])) {
562 $rowspan = $this->tabledata[$i]['leader']['rowspan'];
563 $class = $this->tabledata[$i]['leader']['class'];
564 $html .= "<td class='$class' rowspan='$rowspan'></td>\n";
565 }
566 for ($j = 0; $j < count($this->tablecolumns); $j++) {
567 $name = $this->tablecolumns[$j];
568 $class = (isset($this->tabledata[$i][$name]['class'])) ? $this->tabledata[$i][$name]['class'] : '';
569 $colspan = (isset($this->tabledata[$i][$name]['colspan'])) ? "colspan='".$this->tabledata[$i][$name]['colspan']."'" : '';
570 $content = (isset($this->tabledata[$i][$name]['content'])) ? $this->tabledata[$i][$name]['content'] : null;
571 if (isset($content)) {
572 $html .= "<td class='$class' $colspan>$content</td>\n";
573 }
574 }
575 $html .= "</tr>\n";
576 }
577
578 $html .= "</tbody></table>";
579
f7998fbc 580 if ($return) {
581 return $html;
582 } else {
583 echo $html;
584 }
585 }
586
587 /**
588 * Processes the data sent by the form (grades and feedbacks).
589 * @var array $data
590 * @return bool Success or Failure (array of errors).
591 */
653a8648 592 function process_data($data) {
5c75a0a3 593 }
653a8648 594 function process_action($target, $action) {
f7998fbc 595 }
a071dbb6 596
597 /**
598 * Builds the grade item averages.
599 *
600 */
601 function calculate_averages() {
602 global $USER, $DB;
603
604 if ($this->showaverage) {
605 // this settings are actually grader report settings (not user report)
606 // however we're using them as having two separate but identical settings the
607 // user would have to keep in sync would be annoying
608 $averagesdisplaytype = $this->get_pref('averagesdisplaytype');
609 $averagesdecimalpoints = $this->get_pref('averagesdecimalpoints');
610 $meanselection = $this->get_pref('meanselection');
611 $shownumberofgrades = $this->get_pref('shownumberofgrades');
612
613 $avghtml = '';
614 $avgcssclass = 'avg';
615
616 $straverage = get_string('overallaverage', 'grades');
617
618 $groupsql = $this->groupsql;
619 $groupwheresql = $this->groupwheresql;
620 //$groupwheresqlparams = ;
621
622 if ($shownumberofgrades) {
623 $straverage .= ' (' . get_string('submissions', 'grades') . ') ';
624 }
625
626 $totalcount = $this->get_numusers(false);
627
628 //limit to users with a gradeable role ie students
629 list($gradebookrolessql, $gradebookrolesparams) = $DB->get_in_or_equal(explode(',', $this->gradebookroles), SQL_PARAMS_NAMED, 'grbr0');
630
631 //limit to users with an active enrolment
632 list($enrolledsql, $enrolledparams) = get_enrolled_sql($this->context);
633
634 $params = array_merge($this->groupwheresql_params, $gradebookrolesparams, $enrolledparams);
635 $params['courseid'] = $this->courseid;
636
637 // find sums of all grade items in course
638 $sql = "SELECT gg.itemid, SUM(gg.finalgrade) AS sum
639 FROM {grade_items} gi
656d17c2
AD
640 JOIN {grade_grades} gg ON gg.itemid = gi.id
641 JOIN {user} u ON u.id = gg.userid
642 JOIN ($enrolledsql) je ON je.id = gg.userid
643 JOIN (
644 SELECT DISTINCT ra.userid
645 FROM {role_assignments} ra
646 WHERE ra.roleid $gradebookrolessql
647 AND ra.contextid " . get_related_contexts_string($this->context) . "
648 ) rainner ON rainner.userid = u.id
a071dbb6 649 $groupsql
650 WHERE gi.courseid = :courseid
656d17c2
AD
651 AND u.deleted = 0
652 AND gg.finalgrade IS NOT NULL
653 AND gg.hidden = 0
654 $groupwheresql
a071dbb6 655 GROUP BY gg.itemid";
656
657 $sum_array = array();
cc65dc36
EL
658 $sums = $DB->get_recordset_sql($sql, $params);
659 foreach ($sums as $itemid => $csum) {
660 $sum_array[$itemid] = $csum->sum;
a071dbb6 661 }
cc65dc36 662 $sums->close();
a071dbb6 663
664 $columncount=0;
665
666 // Empty grades must be evaluated as grademin, NOT always 0
667 // This query returns a count of ungraded grades (NULL finalgrade OR no matching record in grade_grades table)
668 // No join condition when joining grade_items and user to get a grade item row for every user
669 // Then left join with grade_grades and look for rows with null final grade (which includes grade items with no grade_grade)
670 $sql = "SELECT gi.id, COUNT(u.id) AS count
671 FROM {grade_items} gi
d3e30347 672 JOIN {user} u ON u.deleted = 0
656d17c2
AD
673 JOIN ($enrolledsql) je ON je.id = u.id
674 JOIN (
675 SELECT DISTINCT ra.userid
676 FROM {role_assignments} ra
677 WHERE ra.roleid $gradebookrolessql
678 AND ra.contextid " . get_related_contexts_string($this->context) . "
679 ) rainner ON rainner.userid = u.id
a1e26b5e 680 LEFT JOIN {grade_grades} gg
d3e30347 681 ON (gg.itemid = gi.id AND gg.userid = u.id AND gg.finalgrade IS NOT NULL AND gg.hidden = 0)
a071dbb6 682 $groupsql
656d17c2 683 WHERE gi.courseid = :courseid
656d17c2
AD
684 AND gg.finalgrade IS NULL
685 $groupwheresql
686 GROUP BY gi.id";
a071dbb6 687
688 $ungraded_counts = $DB->get_records_sql($sql, $params);
689
690 foreach ($this->gtree->items as $itemid=>$unused) {
691 if (!empty($this->gtree->items[$itemid]->avg)) {
692 continue;
693 }
694 $item = $this->gtree->items[$itemid];
695
696 if ($item->needsupdate) {
697 $avghtml .= '<td class="cell c' . $columncount++.'"><span class="gradingerror">'.get_string('error').'</span></td>';
698 continue;
699 }
700
701 if (empty($sum_array[$item->id])) {
702 $sum_array[$item->id] = 0;
703 }
704
705 if (empty($ungraded_counts[$itemid])) {
706 $ungraded_count = 0;
707 } else {
708 $ungraded_count = $ungraded_counts[$itemid]->count;
709 }
710
711 //do they want the averages to include all grade items
712 if ($meanselection == GRADE_REPORT_MEAN_GRADED) {
713 $mean_count = $totalcount - $ungraded_count;
714 } else { // Bump up the sum by the number of ungraded items * grademin
715 $sum_array[$item->id] += ($ungraded_count * $item->grademin);
716 $mean_count = $totalcount;
717 }
718
719 $decimalpoints = $item->get_decimals();
720
721 // Determine which display type to use for this average
722 if (!empty($USER->gradeediting) && $USER->gradeediting[$this->courseid]) {
723 $displaytype = GRADE_DISPLAY_TYPE_REAL;
724
725 } else if ($averagesdisplaytype == GRADE_REPORT_PREFERENCE_INHERIT) { // no ==0 here, please resave the report and user preferences
726 $displaytype = $item->get_displaytype();
727
728 } else {
729 $displaytype = $averagesdisplaytype;
730 }
731
732 // Override grade_item setting if a display preference (not inherit) was set for the averages
733 if ($averagesdecimalpoints == GRADE_REPORT_PREFERENCE_INHERIT) {
734 $decimalpoints = $item->get_decimals();
735
736 } else {
737 $decimalpoints = $averagesdecimalpoints;
738 }
739
740 if (empty($sum_array[$item->id]) || $mean_count == 0) {
741 $this->gtree->items[$itemid]->avg = '-';
742 } else {
743 $sum = $sum_array[$item->id];
744 $avgradeval = $sum/$mean_count;
745 $gradehtml = grade_format_gradevalue($avgradeval, $item, true, $displaytype, $decimalpoints);
746
747 $numberofgrades = '';
748 if ($shownumberofgrades) {
749 $numberofgrades = " ($mean_count)";
750 }
751
752 $this->gtree->items[$itemid]->avg = $gradehtml.$numberofgrades;
753 }
754 }
755 }
756 }
26ed0305 757}
758
759function grade_report_user_settings_definition(&$mform) {
760 global $CFG;
761
762 $options = array(-1 => get_string('default', 'grades'),
763 0 => get_string('hide'),
764 1 => get_string('show'));
765
cd52d909 766 if (empty($CFG->grade_report_user_showrank)) {
26ed0305 767 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
768 } else {
769 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
770 }
771
772 $mform->addElement('select', 'report_user_showrank', get_string('showrank', 'grades'), $options);
1cc3165e 773 $mform->addHelpButton('report_user_showrank', 'showrank', 'grades');
26ed0305 774
699da6b5 775 if (empty($CFG->grade_report_user_showpercentage)) {
aea45e7e 776 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
5528217e 777 } else {
778 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
699da6b5 779 }
780
781 $mform->addElement('select', 'report_user_showpercentage', get_string('showpercentage', 'grades'), $options);
ff268dcb 782 $mform->addHelpButton('report_user_showpercentage', 'showpercentage', 'grades');
699da6b5 783
a071dbb6 784 if (empty($CFG->grade_report_user_showgrade)) {
785 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
786 } else {
787 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
788 }
789
790 $mform->addElement('select', 'report_user_showgrade', get_string('showgrade', 'grades'), $options);
791
792 if (empty($CFG->grade_report_user_showfeedback)) {
793 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
794 } else {
795 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
796 }
797
798 $mform->addElement('select', 'report_user_showfeedback', get_string('showfeedback', 'grades'), $options);
799
800 if (empty($CFG->grade_report_user_showweight)) {
801 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
802 } else {
803 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
804 }
805
806 $mform->addElement('select', 'report_user_showweight', get_string('showweight', 'grades'), $options);
807
808 if (empty($CFG->grade_report_user_showaverage)) {
809 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
810 } else {
811 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
812 }
813
814 $mform->addElement('select', 'report_user_showaverage', get_string('showaverage', 'grades'), $options);
815 $mform->addHelpButton('report_user_showaverage', 'showaverage', 'grades');
816
817 if (empty($CFG->grade_report_user_showlettergrade)) {
818 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
819 } else {
820 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
821 }
822
823 $mform->addElement('select', 'report_user_showlettergrade', get_string('showlettergrade', 'grades'), $options);
824
825 if (empty($CFG->grade_report_user_showrange)) {
826 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
827 } else {
828 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
829 }
830
831 $mform->addElement('select', 'report_user_showrange', get_string('showrange', 'grades'), $options);
832
833 $options = array(0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5);
834 if (! empty($CFG->grade_report_user_rangedecimals)) {
835 $options[-1] = $options[$CFG->grade_report_user_rangedecimals];
836 }
837 $mform->addElement('select', 'report_user_rangedecimals', get_string('rangedecimals', 'grades'), $options);
838
26ed0305 839 $options = array(-1 => get_string('default', 'grades'),
d69ef28b 840 0 => get_string('shownohidden', 'grades'),
597f50e6 841 1 => get_string('showhiddenuntilonly', 'grades'),
d69ef28b 842 2 => get_string('showallhidden', 'grades'));
26ed0305 843
cd52d909 844 if (empty($CFG->grade_report_user_showhiddenitems)) {
26ed0305 845 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
846 } else {
d69ef28b 847 $options[-1] = get_string('defaultprev', 'grades', $options[$CFG->grade_report_user_showhiddenitems]);
26ed0305 848 }
849
850 $mform->addElement('select', 'report_user_showhiddenitems', get_string('showhiddenitems', 'grades'), $options);
69e3e962 851 $mform->addHelpButton('report_user_showhiddenitems', 'showhiddenitems', 'grades');
61541a5a
AD
852
853 //showtotalsifcontainhidden
854 $options = array(-1 => get_string('default', 'grades'),
855 GRADE_REPORT_HIDE_TOTAL_IF_CONTAINS_HIDDEN => get_string('hide'),
856 GRADE_REPORT_SHOW_TOTAL_IF_CONTAINS_HIDDEN => get_string('hidetotalshowexhiddenitems', 'grades'),
857 GRADE_REPORT_SHOW_REAL_TOTAL_IF_CONTAINS_HIDDEN => get_string('hidetotalshowinchiddenitems', 'grades') );
858
859 if (empty($CFG->grade_report_user_showtotalsifcontainhidden)) {
860 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
861 } else {
862 $options[-1] = get_string('defaultprev', 'grades', $options[$CFG->grade_report_user_showtotalsifcontainhidden]);
863 }
864
865 $mform->addElement('select', 'report_user_showtotalsifcontainhidden', get_string('hidetotalifhiddenitems', 'grades'), $options);
0459cc4d 866 $mform->addHelpButton('report_user_showtotalsifcontainhidden', 'hidetotalifhiddenitems', 'grades');
0a784551 867}
868
869function grade_report_user_profilereport($course, $user) {
91152a35 870 global $OUTPUT;
0a784551 871 if (!empty($course->showgrades)) {
872
873 $context = get_context_instance(CONTEXT_COURSE, $course->id);
874
875 //first make sure we have proper final grades - this must be done before constructing of the grade tree
876 grade_regrade_final_grades($course->id);
877
878 /// return tracking object
879 $gpr = new grade_plugin_return(array('type'=>'report', 'plugin'=>'user', 'courseid'=>$course->id, 'userid'=>$user->id));
880 // Create a report instance
881 $report = new grade_report_user($course->id, $gpr, $context, $user->id);
f7998fbc 882
0a784551 883 // print the page
884 echo '<div class="grade-report-user">'; // css fix to share styles with real report page
b5e7b2bf 885 echo $OUTPUT->heading(get_string('pluginname', 'gradereport_user'). ' - '.fullname($report->user));
0a784551 886
887 if ($report->fill_table()) {
888 echo $report->print_table(true);
889 }
890 echo '</div>';
891 }
f7998fbc 892}
0a784551 893
6c3ef410 894