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