[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
1 <?php 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/>. 16 17 /** 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 23 */ 24 25 require_once($CFG->dirroot . '/grade/report/lib.php'); 26 require_once($CFG->libdir.'/tablelib.php'); 27 28 //showhiddenitems values 29 define("GRADE_REPORT_USER_HIDE_HIDDEN", 0); 30 define("GRADE_REPORT_USER_HIDE_UNTIL", 1); 31 define("GRADE_REPORT_USER_SHOW_HIDDEN", 2); 32 33 /** 34 * Class providing an API for the user report building and displaying. 35 * @uses grade_report 36 * @package gradereport_user 37 */ 38 class grade_report_user extends grade_report { 39 40 /** 41 * The user. 42 * @var object $user 43 */ 44 public $user; 45 46 /** 47 * A flexitable to hold the data. 48 * @var object $table 49 */ 50 public $table; 51 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; 75 76 /** 77 * Flat structure similar to grade tree 78 */ 79 public $gseq; 80 81 /** 82 * show student ranks 83 */ 84 public $showrank; 85 86 /** 87 * show grade percentages 88 */ 89 public $showpercentage; 90 91 /** 92 * Show range 93 */ 94 public $showrange = true; 95 96 /** 97 * Show grades in the report, default true 98 * @var bool 99 */ 100 public $showgrade = true; 101 102 /** 103 * Decimal points to use for values in the report, default 2 104 * @var int 105 */ 106 public $decimals = 2; 107 108 /** 109 * The number of decimal places to round range to, default 0 110 * @var int 111 */ 112 public $rangedecimals = 0; 113 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 true. 122 * @var bool 123 */ 124 public $showweight = true; 125 126 /** 127 * Show letter grades in the report, default false 128 * @var bool 129 */ 130 public $showlettergrade = false; 131 132 /** 133 * Show the calculated contribution to the course total column. 134 * @var bool 135 */ 136 public $showcontributiontocoursetotal = true; 137 138 /** 139 * Show average grades in the report, default false. 140 * @var false 141 */ 142 public $showaverage = false; 143 144 public $maxdepth; 145 public $evenodd; 146 147 public $canviewhidden; 148 149 public $switch; 150 151 /** 152 * Show hidden items even when user does not have required cap 153 */ 154 public $showhiddenitems; 155 public $showtotalsifcontainhidden; 156 157 public $baseurl; 158 public $pbarurl; 159 160 /** 161 * The modinfo object to be used. 162 * 163 * @var course_modinfo 164 */ 165 protected $modinfo = null; 166 167 /** 168 * View as user. 169 * 170 * When this is set to true, the visibility checks, and capability checks will be 171 * applied to the user whose grades are being displayed. This is very useful when 172 * a mentor/parent is viewing the report of their mentee because they need to have 173 * access to the same information, but not more, not less. 174 * 175 * @var boolean 176 */ 177 protected $viewasuser = false; 178 179 /** 180 * An array that collects the aggregationhints for every 181 * grade_item. The hints contain grade, grademin, grademax 182 * status, weight and parent. 183 * 184 * @var array 185 */ 186 protected $aggregationhints = array(); 187 188 /** 189 * Constructor. Sets local copies of user preferences and initialises grade_tree. 190 * @param int $courseid 191 * @param object $gpr grade plugin return tracking object 192 * @param string $context 193 * @param int $userid The id of the user 194 * @param bool $viewasuser Set this to true when the current user is a mentor/parent of the targetted user. 195 */ 196 public function __construct($courseid, $gpr, $context, $userid, $viewasuser = null) { 197 global $DB, $CFG; 198 parent::__construct($courseid, $gpr, $context); 199 200 $this->showrank = grade_get_setting($this->courseid, 'report_user_showrank', $CFG->grade_report_user_showrank); 201 $this->showpercentage = grade_get_setting($this->courseid, 'report_user_showpercentage', $CFG->grade_report_user_showpercentage); 202 $this->showhiddenitems = grade_get_setting($this->courseid, 'report_user_showhiddenitems', $CFG->grade_report_user_showhiddenitems); 203 $this->showtotalsifcontainhidden = array($this->courseid => grade_get_setting($this->courseid, 'report_user_showtotalsifcontainhidden', $CFG->grade_report_user_showtotalsifcontainhidden)); 204 205 $this->showgrade = grade_get_setting($this->courseid, 'report_user_showgrade', !empty($CFG->grade_report_user_showgrade)); 206 $this->showrange = grade_get_setting($this->courseid, 'report_user_showrange', !empty($CFG->grade_report_user_showrange)); 207 $this->showfeedback = grade_get_setting($this->courseid, 'report_user_showfeedback', !empty($CFG->grade_report_user_showfeedback)); 208 209 $this->showweight = grade_get_setting($this->courseid, 'report_user_showweight', 210 !empty($CFG->grade_report_user_showweight)); 211 212 $this->showcontributiontocoursetotal = grade_get_setting($this->courseid, 'report_user_showcontributiontocoursetotal', 213 !empty($CFG->grade_report_user_showcontributiontocoursetotal)); 214 215 $this->showlettergrade = grade_get_setting($this->courseid, 'report_user_showlettergrade', !empty($CFG->grade_report_user_showlettergrade)); 216 $this->showaverage = grade_get_setting($this->courseid, 'report_user_showaverage', !empty($CFG->grade_report_user_showaverage)); 217 218 $this->viewasuser = $viewasuser; 219 220 // The default grade decimals is 2 221 $defaultdecimals = 2; 222 if (property_exists($CFG, 'grade_decimalpoints')) { 223 $defaultdecimals = $CFG->grade_decimalpoints; 224 } 225 $this->decimals = grade_get_setting($this->courseid, 'decimalpoints', $defaultdecimals); 226 227 // The default range decimals is 0 228 $defaultrangedecimals = 0; 229 if (property_exists($CFG, 'grade_report_user_rangedecimals')) { 230 $defaultrangedecimals = $CFG->grade_report_user_rangedecimals; 231 } 232 $this->rangedecimals = grade_get_setting($this->courseid, 'report_user_rangedecimals', $defaultrangedecimals); 233 234 $this->switch = grade_get_setting($this->courseid, 'aggregationposition', $CFG->grade_aggregationposition); 235 236 // Grab the grade_tree for this course 237 $this->gtree = new grade_tree($this->courseid, false, $this->switch, null, !$CFG->enableoutcomes); 238 239 // Get the user (for full name). 240 $this->user = $DB->get_record('user', array('id' => $userid)); 241 242 // What user are we viewing this as? 243 $coursecontext = context_course::instance($this->courseid); 244 if ($viewasuser) { 245 $this->modinfo = new course_modinfo($this->course, $this->user->id); 246 $this->canviewhidden = has_capability('moodle/grade:viewhidden', $coursecontext, $this->user->id); 247 } else { 248 $this->modinfo = $this->gtree->modinfo; 249 $this->canviewhidden = has_capability('moodle/grade:viewhidden', $coursecontext); 250 } 251 252 // Determine the number of rows and indentation. 253 $this->maxdepth = 1; 254 $this->inject_rowspans($this->gtree->top_element); 255 $this->maxdepth++; // Need to account for the lead column that spans all children. 256 for ($i = 1; $i <= $this->maxdepth; $i++) { 257 $this->evenodd[$i] = 0; 258 } 259 260 $this->tabledata = array(); 261 262 // base url for sorting by first/last name 263 $this->baseurl = $CFG->wwwroot.'/grade/report?id='.$courseid.'&userid='.$userid; 264 $this->pbarurl = $this->baseurl; 265 266 // no groups on this report - rank is from all course users 267 $this->setup_table(); 268 269 //optionally calculate grade item averages 270 $this->calculate_averages(); 271 } 272 273 /** 274 * Recurses through a tree of elements setting the rowspan property on each element 275 * 276 * @param array $element Either the top element or, during recursion, the current element 277 * @return int The number of elements processed 278 */ 279 function inject_rowspans(&$element) { 280 281 if ($element['depth'] > $this->maxdepth) { 282 $this->maxdepth = $element['depth']; 283 } 284 if (empty($element['children'])) { 285 return 1; 286 } 287 $count = 1; 288 289 foreach ($element['children'] as $key=>$child) { 290 // If category is hidden then do not include it in the rowspan. 291 if ($child['type'] == 'category' && $child['object']->is_hidden() && !$this->canviewhidden 292 && ($this->showhiddenitems == GRADE_REPORT_USER_HIDE_HIDDEN 293 || ($this->showhiddenitems == GRADE_REPORT_USER_HIDE_UNTIL && !$child['object']->is_hiddenuntil()))) { 294 // Just calculate the rowspans for children of this category, don't add them to the count. 295 $this->inject_rowspans($element['children'][$key]); 296 } else { 297 $count += $this->inject_rowspans($element['children'][$key]); 298 } 299 } 300 301 $element['rowspan'] = $count; 302 return $count; 303 } 304 305 306 /** 307 * Prepares the headers and attributes of the flexitable. 308 */ 309 public function setup_table() { 310 /* 311 * Table has 1-8 columns 312 *| All columns except for itemname/description are optional 313 */ 314 315 // setting up table headers 316 317 $this->tablecolumns = array('itemname'); 318 $this->tableheaders = array($this->get_lang_string('gradeitem', 'grades')); 319 320 if ($this->showweight) { 321 $this->tablecolumns[] = 'weight'; 322 $this->tableheaders[] = $this->get_lang_string('weightuc', 'grades'); 323 } 324 325 if ($this->showgrade) { 326 $this->tablecolumns[] = 'grade'; 327 $this->tableheaders[] = $this->get_lang_string('grade', 'grades'); 328 } 329 330 if ($this->showrange) { 331 $this->tablecolumns[] = 'range'; 332 $this->tableheaders[] = $this->get_lang_string('range', 'grades'); 333 } 334 335 if ($this->showpercentage) { 336 $this->tablecolumns[] = 'percentage'; 337 $this->tableheaders[] = $this->get_lang_string('percentage', 'grades'); 338 } 339 340 if ($this->showlettergrade) { 341 $this->tablecolumns[] = 'lettergrade'; 342 $this->tableheaders[] = $this->get_lang_string('lettergrade', 'grades'); 343 } 344 345 if ($this->showrank) { 346 $this->tablecolumns[] = 'rank'; 347 $this->tableheaders[] = $this->get_lang_string('rank', 'grades'); 348 } 349 350 if ($this->showaverage) { 351 $this->tablecolumns[] = 'average'; 352 $this->tableheaders[] = $this->get_lang_string('average', 'grades'); 353 } 354 355 if ($this->showfeedback) { 356 $this->tablecolumns[] = 'feedback'; 357 $this->tableheaders[] = $this->get_lang_string('feedback', 'grades'); 358 } 359 360 if ($this->showcontributiontocoursetotal) { 361 $this->tablecolumns[] = 'contributiontocoursetotal'; 362 $this->tableheaders[] = $this->get_lang_string('contributiontocoursetotal', 'grades'); 363 } 364 } 365 366 function fill_table() { 367 //print "<pre>"; 368 //print_r($this->gtree->top_element); 369 $this->fill_table_recursive($this->gtree->top_element); 370 //print_r($this->tabledata); 371 //print "</pre>"; 372 return true; 373 } 374 375 /** 376 * Fill the table with data. 377 * 378 * @param $element - An array containing the table data for the current row. 379 */ 380 private function fill_table_recursive(&$element) { 381 global $DB, $CFG; 382 383 $type = $element['type']; 384 $depth = $element['depth']; 385 $grade_object = $element['object']; 386 $eid = $grade_object->id; 387 $element['userid'] = $this->user->id; 388 $fullname = $this->gtree->get_element_header($element, true, true, true, true, true); 389 $data = array(); 390 $hidden = ''; 391 $excluded = ''; 392 $itemlevel = ($type == 'categoryitem' || $type == 'category' || $type == 'courseitem') ? $depth : ($depth + 1); 393 $class = 'level' . $itemlevel . ' level' . ($itemlevel % 2 ? 'odd' : 'even'); 394 $classfeedback = ''; 395 396 // If this is a hidden grade category, hide it completely from the user 397 if ($type == 'category' && $grade_object->is_hidden() && !$this->canviewhidden && ( 398 $this->showhiddenitems == GRADE_REPORT_USER_HIDE_HIDDEN || 399 ($this->showhiddenitems == GRADE_REPORT_USER_HIDE_UNTIL && !$grade_object->is_hiddenuntil()))) { 400 return false; 401 } 402 403 if ($type == 'category') { 404 $this->evenodd[$depth] = (($this->evenodd[$depth] + 1) % 2); 405 } 406 $alter = ($this->evenodd[$depth] == 0) ? 'even' : 'odd'; 407 408 /// Process those items that have scores associated 409 if ($type == 'item' or $type == 'categoryitem' or $type == 'courseitem') { 410 $header_row = "row_{$eid}_{$this->user->id}"; 411 $header_cat = "cat_{$grade_object->categoryid}_{$this->user->id}"; 412 413 if (! $grade_grade = grade_grade::fetch(array('itemid'=>$grade_object->id,'userid'=>$this->user->id))) { 414 $grade_grade = new grade_grade(); 415 $grade_grade->userid = $this->user->id; 416 $grade_grade->itemid = $grade_object->id; 417 } 418 419 $grade_grade->load_grade_item(); 420 421 /// Hidden Items 422 if ($grade_grade->grade_item->is_hidden()) { 423 $hidden = ' dimmed_text'; 424 } 425 426 $hide = false; 427 // If this is a hidden grade item, hide it completely from the user. 428 if ($grade_grade->is_hidden() && !$this->canviewhidden && ( 429 $this->showhiddenitems == GRADE_REPORT_USER_HIDE_HIDDEN || 430 ($this->showhiddenitems == GRADE_REPORT_USER_HIDE_UNTIL && !$grade_grade->is_hiddenuntil()))) { 431 $hide = true; 432 } else if (!empty($grade_object->itemmodule) && !empty($grade_object->iteminstance)) { 433 // The grade object can be marked visible but still be hidden if 434 // the student cannot see the activity due to conditional access 435 // and it's set to be hidden entirely. 436 $instances = $this->modinfo->get_instances_of($grade_object->itemmodule); 437 if (!empty($instances[$grade_object->iteminstance])) { 438 $cm = $instances[$grade_object->iteminstance]; 439 if (!$cm->uservisible) { 440 // If there is 'availableinfo' text then it is only greyed 441 // out and not entirely hidden. 442 if (!$cm->availableinfo) { 443 $hide = true; 444 } 445 } 446 } 447 } 448 449 // Actual Grade - We need to calculate this whether the row is hidden or not. 450 $gradeval = $grade_grade->finalgrade; 451 $hint = $grade_grade->get_aggregation_hint(); 452 if (!$this->canviewhidden) { 453 /// Virtual Grade (may be calculated excluding hidden items etc). 454 $adjustedgrade = $this->blank_hidden_total_and_adjust_bounds($this->courseid, 455 $grade_grade->grade_item, 456 $gradeval); 457 458 $gradeval = $adjustedgrade['grade']; 459 460 // We temporarily adjust the view of this grade item - because the min and 461 // max are affected by the hidden values in the aggregation. 462 $grade_grade->grade_item->grademax = $adjustedgrade['grademax']; 463 $grade_grade->grade_item->grademin = $adjustedgrade['grademin']; 464 $hint['status'] = $adjustedgrade['aggregationstatus']; 465 $hint['weight'] = $adjustedgrade['aggregationweight']; 466 } else { 467 // The max and min for an aggregation may be different to the grade_item. 468 if (!is_null($gradeval)) { 469 $grade_grade->grade_item->grademax = $grade_grade->rawgrademax; 470 $grade_grade->grade_item->grademin = $grade_grade->rawgrademin; 471 } 472 } 473 474 475 if (!$hide) { 476 /// Excluded Item 477 /** 478 if ($grade_grade->is_excluded()) { 479 $fullname .= ' ['.get_string('excluded', 'grades').']'; 480 $excluded = ' excluded'; 481 } 482 **/ 483 484 /// Other class information 485 $class .= $hidden . $excluded; 486 if ($this->switch) { // alter style based on whether aggregation is first or last 487 $class .= ($type == 'categoryitem' or $type == 'courseitem') ? " ".$alter."d$depth baggt b2b" : " item b1b"; 488 } else { 489 $class .= ($type == 'categoryitem' or $type == 'courseitem') ? " ".$alter."d$depth baggb" : " item b1b"; 490 } 491 if ($type == 'categoryitem' or $type == 'courseitem') { 492 $header_cat = "cat_{$grade_object->iteminstance}_{$this->user->id}"; 493 } 494 495 /// Name 496 $data['itemname']['content'] = $fullname; 497 $data['itemname']['class'] = $class; 498 $data['itemname']['colspan'] = ($this->maxdepth - $depth); 499 $data['itemname']['celltype'] = 'th'; 500 $data['itemname']['id'] = $header_row; 501 502 if ($this->showfeedback) { 503 // Copy $class before appending itemcenter as feedback should not be centered 504 $classfeedback = $class; 505 } 506 $class .= " itemcenter "; 507 if ($this->showweight) { 508 $data['weight']['class'] = $class; 509 $data['weight']['content'] = '-'; 510 $data['weight']['headers'] = "$header_cat $header_row weight"; 511 // has a weight assigned, might be extra credit 512 513 // This obliterates the weight because it provides a more informative description. 514 if (is_numeric($hint['weight'])) { 515 $data['weight']['content'] = format_float($hint['weight'] * 100.0, 2) . ' %'; 516 } 517 if ($hint['status'] != 'used' && $hint['status'] != 'unknown') { 518 $data['weight']['content'] .= '<br>' . get_string('aggregationhint' . $hint['status'], 'grades'); 519 } 520 } 521 522 if ($this->showgrade) { 523 if ($grade_grade->grade_item->needsupdate) { 524 $data['grade']['class'] = $class.' gradingerror'; 525 $data['grade']['content'] = get_string('error'); 526 } else if (!empty($CFG->grade_hiddenasdate) and $grade_grade->get_datesubmitted() and !$this->canviewhidden and $grade_grade->is_hidden() 527 and !$grade_grade->grade_item->is_category_item() and !$grade_grade->grade_item->is_course_item()) { 528 // 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 529 $class .= ' datesubmitted'; 530 $data['grade']['class'] = $class; 531 $data['grade']['content'] = get_string('submittedon', 'grades', userdate($grade_grade->get_datesubmitted(), get_string('strftimedatetimeshort'))); 532 533 } else if ($grade_grade->is_hidden()) { 534 $data['grade']['class'] = $class.' dimmed_text'; 535 $data['grade']['content'] = '-'; 536 if ($this->canviewhidden) { 537 $data['grade']['content'] = grade_format_gradevalue($gradeval, 538 $grade_grade->grade_item, 539 true); 540 } 541 } else { 542 $data['grade']['class'] = $class; 543 $data['grade']['content'] = grade_format_gradevalue($gradeval, 544 $grade_grade->grade_item, 545 true); 546 } 547 $data['grade']['headers'] = "$header_cat $header_row grade"; 548 } 549 550 // Range 551 if ($this->showrange) { 552 $data['range']['class'] = $class; 553 $data['range']['content'] = $grade_grade->grade_item->get_formatted_range(GRADE_DISPLAY_TYPE_REAL, $this->rangedecimals); 554 $data['range']['headers'] = "$header_cat $header_row range"; 555 } 556 557 // Percentage 558 if ($this->showpercentage) { 559 if ($grade_grade->grade_item->needsupdate) { 560 $data['percentage']['class'] = $class.' gradingerror'; 561 $data['percentage']['content'] = get_string('error'); 562 } else if ($grade_grade->is_hidden()) { 563 $data['percentage']['class'] = $class.' dimmed_text'; 564 $data['percentage']['content'] = '-'; 565 if ($this->canviewhidden) { 566 $data['percentage']['content'] = grade_format_gradevalue($gradeval, $grade_grade->grade_item, true, GRADE_DISPLAY_TYPE_PERCENTAGE); 567 } 568 } else { 569 $data['percentage']['class'] = $class; 570 $data['percentage']['content'] = grade_format_gradevalue($gradeval, $grade_grade->grade_item, true, GRADE_DISPLAY_TYPE_PERCENTAGE); 571 } 572 $data['percentage']['headers'] = "$header_cat $header_row percentage"; 573 } 574 575 // Lettergrade 576 if ($this->showlettergrade) { 577 if ($grade_grade->grade_item->needsupdate) { 578 $data['lettergrade']['class'] = $class.' gradingerror'; 579 $data['lettergrade']['content'] = get_string('error'); 580 } else if ($grade_grade->is_hidden()) { 581 $data['lettergrade']['class'] = $class.' dimmed_text'; 582 if (!$this->canviewhidden) { 583 $data['lettergrade']['content'] = '-'; 584 } else { 585 $data['lettergrade']['content'] = grade_format_gradevalue($gradeval, $grade_grade->grade_item, true, GRADE_DISPLAY_TYPE_LETTER); 586 } 587 } else { 588 $data['lettergrade']['class'] = $class; 589 $data['lettergrade']['content'] = grade_format_gradevalue($gradeval, $grade_grade->grade_item, true, GRADE_DISPLAY_TYPE_LETTER); 590 } 591 $data['lettergrade']['headers'] = "$header_cat $header_row lettergrade"; 592 } 593 594 // Rank 595 if ($this->showrank) { 596 if ($grade_grade->grade_item->needsupdate) { 597 $data['rank']['class'] = $class.' gradingerror'; 598 $data['rank']['content'] = get_string('error'); 599 } elseif ($grade_grade->is_hidden()) { 600 $data['rank']['class'] = $class.' dimmed_text'; 601 $data['rank']['content'] = '-'; 602 } else if (is_null($gradeval)) { 603 // no grade, no rank 604 $data['rank']['class'] = $class; 605 $data['rank']['content'] = '-'; 606 607 } else { 608 /// find the number of users with a higher grade 609 $sql = "SELECT COUNT(DISTINCT(userid)) 610 FROM {grade_grades} 611 WHERE finalgrade > ? 612 AND itemid = ? 613 AND hidden = 0"; 614 $rank = $DB->count_records_sql($sql, array($grade_grade->finalgrade, $grade_grade->grade_item->id)) + 1; 615 616 $data['rank']['class'] = $class; 617 $data['rank']['content'] = "$rank/".$this->get_numusers(false); // total course users 618 } 619 $data['rank']['headers'] = "$header_cat $header_row rank"; 620 } 621 622 // Average 623 if ($this->showaverage) { 624 $data['average']['class'] = $class; 625 if (!empty($this->gtree->items[$eid]->avg)) { 626 $data['average']['content'] = $this->gtree->items[$eid]->avg; 627 } else { 628 $data['average']['content'] = '-'; 629 } 630 $data['average']['headers'] = "$header_cat $header_row average"; 631 } 632 633 // Feedback 634 if ($this->showfeedback) { 635 if ($grade_grade->overridden > 0 AND ($type == 'categoryitem' OR $type == 'courseitem')) { 636 $data['feedback']['class'] = $classfeedback.' feedbacktext'; 637 $data['feedback']['content'] = get_string('overridden', 'grades').': ' . format_text($grade_grade->feedback, $grade_grade->feedbackformat); 638 } else if (empty($grade_grade->feedback) or (!$this->canviewhidden and $grade_grade->is_hidden())) { 639 $data['feedback']['class'] = $classfeedback.' feedbacktext'; 640 $data['feedback']['content'] = ' '; 641 } else { 642 $data['feedback']['class'] = $classfeedback.' feedbacktext'; 643 $data['feedback']['content'] = format_text($grade_grade->feedback, $grade_grade->feedbackformat); 644 } 645 $data['feedback']['headers'] = "$header_cat $header_row feedback"; 646 } 647 // Contribution to the course total column. 648 if ($this->showcontributiontocoursetotal) { 649 $data['contributiontocoursetotal']['class'] = $class; 650 $data['contributiontocoursetotal']['content'] = '-'; 651 $data['contributiontocoursetotal']['headers'] = "$header_cat $header_row contributiontocoursetotal"; 652 653 } 654 } 655 // We collect the aggregation hints whether they are hidden or not. 656 if ($this->showcontributiontocoursetotal) { 657 $hint['grademax'] = $grade_grade->grade_item->grademax; 658 $hint['grademin'] = $grade_grade->grade_item->grademin; 659 $hint['grade'] = $gradeval; 660 $parent = $grade_object->load_parent_category(); 661 if ($grade_object->is_category_item()) { 662 $parent = $parent->load_parent_category(); 663 } 664 $hint['parent'] = $parent->load_grade_item()->id; 665 $this->aggregationhints[$grade_grade->itemid] = $hint; 666 } 667 } 668 669 /// Category 670 if ($type == 'category') { 671 $data['leader']['class'] = $class.' '.$alter."d$depth b1t b2b b1l"; 672 $data['leader']['rowspan'] = $element['rowspan']; 673 674 if ($this->switch) { // alter style based on whether aggregation is first or last 675 $data['itemname']['class'] = $class.' '.$alter."d$depth b1b b1t"; 676 } else { 677 $data['itemname']['class'] = $class.' '.$alter."d$depth b2t"; 678 } 679 $data['itemname']['colspan'] = ($this->maxdepth - $depth + count($this->tablecolumns) - 1); 680 $data['itemname']['content'] = $fullname; 681 $data['itemname']['celltype'] = 'th'; 682 $data['itemname']['id'] = "cat_{$grade_object->id}_{$this->user->id}"; 683 } 684 685 /// Add this row to the overall system 686 foreach ($data as $key => $celldata) { 687 $data[$key]['class'] .= ' column-' . $key; 688 } 689 $this->tabledata[] = $data; 690 691 /// Recursively iterate through all child elements 692 if (isset($element['children'])) { 693 foreach ($element['children'] as $key=>$child) { 694 $this->fill_table_recursive($element['children'][$key]); 695 } 696 } 697 698 // Check we are showing this column, and we are looking at the root of the table. 699 // This should be the very last thing this fill_table_recursive function does. 700 if ($this->showcontributiontocoursetotal && ($type == 'category' && $depth == 1)) { 701 // We should have collected all the hints by now - walk the tree again and build the contributions column. 702 703 $this->fill_contributions_column($element); 704 } 705 } 706 707 /** 708 * This function is called after the table has been built and the aggregationhints 709 * have been collected. We need this info to walk up the list of parents of each 710 * grade_item. 711 * 712 * @param $element - An array containing the table data for the current row. 713 */ 714 public function fill_contributions_column($element) { 715 716 // Recursively iterate through all child elements. 717 if (isset($element['children'])) { 718 foreach ($element['children'] as $key=>$child) { 719 $this->fill_contributions_column($element['children'][$key]); 720 } 721 } else if ($element['type'] == 'item') { 722 // This is a grade item (We don't do this for categories or we would double count). 723 $grade_object = $element['object']; 724 $itemid = $grade_object->id; 725 726 // Ignore anything with no hint - e.g. a hidden row. 727 if (isset($this->aggregationhints[$itemid])) { 728 729 // Normalise the gradeval. 730 $gradecat = $grade_object->load_parent_category(); 731 if ($gradecat->aggregation == GRADE_AGGREGATE_SUM) { 732 // Natural aggregation/Sum of grades does not consider the mingrade, cannot traditionnally normalise it. 733 $graderange = $this->aggregationhints[$itemid]['grademax']; 734 $gradeval = $this->aggregationhints[$itemid]['grade'] / $graderange; 735 } else { 736 $gradeval = grade_grade::standardise_score($this->aggregationhints[$itemid]['grade'], 737 $this->aggregationhints[$itemid]['grademin'], $this->aggregationhints[$itemid]['grademax'], 0, 1); 738 } 739 740 // Multiply the normalised value by the weight 741 // of all the categories higher in the tree. 742 $parent = null; 743 do { 744 if (!is_null($this->aggregationhints[$itemid]['weight'])) { 745 $gradeval *= $this->aggregationhints[$itemid]['weight']; 746 } else if (empty($parent)) { 747 // If we are in the first loop, and the weight is null, then we cannot calculate the contribution. 748 $gradeval = null; 749 break; 750 } 751 752 // The second part of this if is to prevent infinite loops 753 // in case of crazy data. 754 if (isset($this->aggregationhints[$itemid]['parent']) && 755 $this->aggregationhints[$itemid]['parent'] != $itemid) { 756 $parent = $this->aggregationhints[$itemid]['parent']; 757 $itemid = $parent; 758 } else { 759 // We are at the top of the tree. 760 $parent = false; 761 } 762 } while ($parent); 763 764 // Finally multiply by the course grademax. 765 if (!is_null($gradeval)) { 766 // Convert to percent. 767 $gradeval *= 100; 768 } 769 770 // Now we need to loop through the "built" table data and update the 771 // contributions column for the current row. 772 $header_row = "row_{$grade_object->id}_{$this->user->id}"; 773 foreach ($this->tabledata as $key => $row) { 774 if (isset($row['itemname']) && ($row['itemname']['id'] == $header_row)) { 775 // Found it - update the column. 776 $content = '-'; 777 if (!is_null($gradeval)) { 778 $decimals = $grade_object->get_decimals(); 779 $content = format_float($gradeval, $decimals, true) . ' %'; 780 } 781 $this->tabledata[$key]['contributiontocoursetotal']['content'] = $content; 782 break; 783 } 784 } 785 } 786 } 787 } 788 789 /** 790 * Prints or returns the HTML from the flexitable. 791 * @param bool $return Whether or not to return the data instead of printing it directly. 792 * @return string 793 */ 794 public function print_table($return=false) { 795 $maxspan = $this->maxdepth; 796 797 /// Build table structure 798 $html = " 799 <table cellspacing='0' 800 cellpadding='0' 801 summary='" . s($this->get_lang_string('tablesummary', 'gradereport_user')) . "' 802 class='boxaligncenter generaltable user-grade'> 803 <thead> 804 <tr> 805 <th id='".$this->tablecolumns[0]."' class=\"header column-{$this->tablecolumns[0]}\" colspan='$maxspan'>".$this->tableheaders[0]."</th>\n"; 806 807 for ($i = 1; $i < count($this->tableheaders); $i++) { 808 $html .= "<th id='".$this->tablecolumns[$i]."' class=\"header column-{$this->tablecolumns[$i]}\">".$this->tableheaders[$i]."</th>\n"; 809 } 810 811 $html .= " 812 </tr> 813 </thead> 814 <tbody>\n"; 815 816 /// Print out the table data 817 for ($i = 0; $i < count($this->tabledata); $i++) { 818 $html .= "<tr>\n"; 819 if (isset($this->tabledata[$i]['leader'])) { 820 $rowspan = $this->tabledata[$i]['leader']['rowspan']; 821 $class = $this->tabledata[$i]['leader']['class']; 822 $html .= "<td class='$class' rowspan='$rowspan'></td>\n"; 823 } 824 for ($j = 0; $j < count($this->tablecolumns); $j++) { 825 $name = $this->tablecolumns[$j]; 826 $class = (isset($this->tabledata[$i][$name]['class'])) ? $this->tabledata[$i][$name]['class'] : ''; 827 $colspan = (isset($this->tabledata[$i][$name]['colspan'])) ? "colspan='".$this->tabledata[$i][$name]['colspan']."'" : ''; 828 $content = (isset($this->tabledata[$i][$name]['content'])) ? $this->tabledata[$i][$name]['content'] : null; 829 $celltype = (isset($this->tabledata[$i][$name]['celltype'])) ? $this->tabledata[$i][$name]['celltype'] : 'td'; 830 $id = (isset($this->tabledata[$i][$name]['id'])) ? "id='{$this->tabledata[$i][$name]['id']}'" : ''; 831 $headers = (isset($this->tabledata[$i][$name]['headers'])) ? "headers='{$this->tabledata[$i][$name]['headers']}'" : ''; 832 if (isset($content)) { 833 $html .= "<$celltype $id $headers class='$class' $colspan>$content</$celltype>\n"; 834 } 835 } 836 $html .= "</tr>\n"; 837 } 838 839 $html .= "</tbody></table>"; 840 841 if ($return) { 842 return $html; 843 } else { 844 echo $html; 845 } 846 } 847 848 /** 849 * Processes the data sent by the form (grades and feedbacks). 850 * @var array $data 851 * @return bool Success or Failure (array of errors). 852 */ 853 function process_data($data) { 854 } 855 function process_action($target, $action) { 856 } 857 858 /** 859 * Builds the grade item averages. 860 */ 861 function calculate_averages() { 862 global $USER, $DB; 863 864 if ($this->showaverage) { 865 // This settings are actually grader report settings (not user report) 866 // however we're using them as having two separate but identical settings the 867 // user would have to keep in sync would be annoying. 868 $averagesdisplaytype = $this->get_pref('averagesdisplaytype'); 869 $averagesdecimalpoints = $this->get_pref('averagesdecimalpoints'); 870 $meanselection = $this->get_pref('meanselection'); 871 $shownumberofgrades = $this->get_pref('shownumberofgrades'); 872 873 $avghtml = ''; 874 $groupsql = $this->groupsql; 875 $groupwheresql = $this->groupwheresql; 876 $totalcount = $this->get_numusers(false); 877 878 // We want to query both the current context and parent contexts. 879 list($relatedctxsql, $relatedctxparams) = $DB->get_in_or_equal($this->context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'relatedctx'); 880 881 // Limit to users with a gradeable role ie students. 882 list($gradebookrolessql, $gradebookrolesparams) = $DB->get_in_or_equal(explode(',', $this->gradebookroles), SQL_PARAMS_NAMED, 'grbr0'); 883 884 // Limit to users with an active enrolment. 885 list($enrolledsql, $enrolledparams) = get_enrolled_sql($this->context); 886 887 $params = array_merge($this->groupwheresql_params, $gradebookrolesparams, $enrolledparams, $relatedctxparams); 888 $params['courseid'] = $this->courseid; 889 890 // find sums of all grade items in course 891 $sql = "SELECT gg.itemid, SUM(gg.finalgrade) AS sum 892 FROM {grade_items} gi 893 JOIN {grade_grades} gg ON gg.itemid = gi.id 894 JOIN {user} u ON u.id = gg.userid 895 JOIN ($enrolledsql) je ON je.id = gg.userid 896 JOIN ( 897 SELECT DISTINCT ra.userid 898 FROM {role_assignments} ra 899 WHERE ra.roleid $gradebookrolessql 900 AND ra.contextid $relatedctxsql 901 ) rainner ON rainner.userid = u.id 902 $groupsql 903 WHERE gi.courseid = :courseid 904 AND u.deleted = 0 905 AND gg.finalgrade IS NOT NULL 906 AND gg.hidden = 0 907 $groupwheresql 908 GROUP BY gg.itemid"; 909 910 $sum_array = array(); 911 $sums = $DB->get_recordset_sql($sql, $params); 912 foreach ($sums as $itemid => $csum) { 913 $sum_array[$itemid] = $csum->sum; 914 } 915 $sums->close(); 916 917 $columncount=0; 918 919 // Empty grades must be evaluated as grademin, NOT always 0 920 // This query returns a count of ungraded grades (NULL finalgrade OR no matching record in grade_grades table) 921 // No join condition when joining grade_items and user to get a grade item row for every user 922 // Then left join with grade_grades and look for rows with null final grade (which includes grade items with no grade_grade) 923 $sql = "SELECT gi.id, COUNT(u.id) AS count 924 FROM {grade_items} gi 925 JOIN {user} u ON u.deleted = 0 926 JOIN ($enrolledsql) je ON je.id = u.id 927 JOIN ( 928 SELECT DISTINCT ra.userid 929 FROM {role_assignments} ra 930 WHERE ra.roleid $gradebookrolessql 931 AND ra.contextid $relatedctxsql 932 ) rainner ON rainner.userid = u.id 933 LEFT JOIN {grade_grades} gg 934 ON (gg.itemid = gi.id AND gg.userid = u.id AND gg.finalgrade IS NOT NULL AND gg.hidden = 0) 935 $groupsql 936 WHERE gi.courseid = :courseid 937 AND gg.finalgrade IS NULL 938 $groupwheresql 939 GROUP BY gi.id"; 940 941 $ungraded_counts = $DB->get_records_sql($sql, $params); 942 943 foreach ($this->gtree->items as $itemid=>$unused) { 944 if (!empty($this->gtree->items[$itemid]->avg)) { 945 continue; 946 } 947 $item = $this->gtree->items[$itemid]; 948 949 if ($item->needsupdate) { 950 $avghtml .= '<td class="cell c' . $columncount++.'"><span class="gradingerror">'.get_string('error').'</span></td>'; 951 continue; 952 } 953 954 if (empty($sum_array[$item->id])) { 955 $sum_array[$item->id] = 0; 956 } 957 958 if (empty($ungraded_counts[$itemid])) { 959 $ungraded_count = 0; 960 } else { 961 $ungraded_count = $ungraded_counts[$itemid]->count; 962 } 963 964 //do they want the averages to include all grade items 965 if ($meanselection == GRADE_REPORT_MEAN_GRADED) { 966 $mean_count = $totalcount - $ungraded_count; 967 } else { // Bump up the sum by the number of ungraded items * grademin 968 $sum_array[$item->id] += ($ungraded_count * $item->grademin); 969 $mean_count = $totalcount; 970 } 971 972 // Determine which display type to use for this average 973 if (!empty($USER->gradeediting) && $USER->gradeediting[$this->courseid]) { 974 $displaytype = GRADE_DISPLAY_TYPE_REAL; 975 976 } else if ($averagesdisplaytype == GRADE_REPORT_PREFERENCE_INHERIT) { // no ==0 here, please resave the report and user preferences 977 $displaytype = $item->get_displaytype(); 978 979 } else { 980 $displaytype = $averagesdisplaytype; 981 } 982 983 // Override grade_item setting if a display preference (not inherit) was set for the averages 984 if ($averagesdecimalpoints == GRADE_REPORT_PREFERENCE_INHERIT) { 985 $decimalpoints = $item->get_decimals(); 986 } else { 987 $decimalpoints = $averagesdecimalpoints; 988 } 989 990 if (empty($sum_array[$item->id]) || $mean_count == 0) { 991 $this->gtree->items[$itemid]->avg = '-'; 992 } else { 993 $sum = $sum_array[$item->id]; 994 $avgradeval = $sum/$mean_count; 995 $gradehtml = grade_format_gradevalue($avgradeval, $item, true, $displaytype, $decimalpoints); 996 997 $numberofgrades = ''; 998 if ($shownumberofgrades) { 999 $numberofgrades = " ($mean_count)"; 1000 } 1001 1002 $this->gtree->items[$itemid]->avg = $gradehtml.$numberofgrades; 1003 } 1004 } 1005 } 1006 } 1007 } 1008 1009 function grade_report_user_settings_definition(&$mform) { 1010 global $CFG; 1011 1012 $options = array(-1 => get_string('default', 'grades'), 1013 0 => get_string('hide'), 1014 1 => get_string('show')); 1015 1016 if (empty($CFG->grade_report_user_showrank)) { 1017 $options[-1] = get_string('defaultprev', 'grades', $options[0]); 1018 } else { 1019 $options[-1] = get_string('defaultprev', 'grades', $options[1]); 1020 } 1021 1022 $mform->addElement('select', 'report_user_showrank', get_string('showrank', 'grades'), $options); 1023 $mform->addHelpButton('report_user_showrank', 'showrank', 'grades'); 1024 1025 if (empty($CFG->grade_report_user_showpercentage)) { 1026 $options[-1] = get_string('defaultprev', 'grades', $options[0]); 1027 } else { 1028 $options[-1] = get_string('defaultprev', 'grades', $options[1]); 1029 } 1030 1031 $mform->addElement('select', 'report_user_showpercentage', get_string('showpercentage', 'grades'), $options); 1032 $mform->addHelpButton('report_user_showpercentage', 'showpercentage', 'grades'); 1033 1034 if (empty($CFG->grade_report_user_showgrade)) { 1035 $options[-1] = get_string('defaultprev', 'grades', $options[0]); 1036 } else { 1037 $options[-1] = get_string('defaultprev', 'grades', $options[1]); 1038 } 1039 1040 $mform->addElement('select', 'report_user_showgrade', get_string('showgrade', 'grades'), $options); 1041 1042 if (empty($CFG->grade_report_user_showfeedback)) { 1043 $options[-1] = get_string('defaultprev', 'grades', $options[0]); 1044 } else { 1045 $options[-1] = get_string('defaultprev', 'grades', $options[1]); 1046 } 1047 1048 $mform->addElement('select', 'report_user_showfeedback', get_string('showfeedback', 'grades'), $options); 1049 1050 if (empty($CFG->grade_report_user_showweight)) { 1051 $options[-1] = get_string('defaultprev', 'grades', $options[0]); 1052 } else { 1053 $options[-1] = get_string('defaultprev', 'grades', $options[1]); 1054 } 1055 1056 $mform->addElement('select', 'report_user_showweight', get_string('showweight', 'grades'), $options); 1057 1058 if (empty($CFG->grade_report_user_showaverage)) { 1059 $options[-1] = get_string('defaultprev', 'grades', $options[0]); 1060 } else { 1061 $options[-1] = get_string('defaultprev', 'grades', $options[1]); 1062 } 1063 1064 $mform->addElement('select', 'report_user_showaverage', get_string('showaverage', 'grades'), $options); 1065 $mform->addHelpButton('report_user_showaverage', 'showaverage', 'grades'); 1066 1067 if (empty($CFG->grade_report_user_showlettergrade)) { 1068 $options[-1] = get_string('defaultprev', 'grades', $options[0]); 1069 } else { 1070 $options[-1] = get_string('defaultprev', 'grades', $options[1]); 1071 } 1072 1073 $mform->addElement('select', 'report_user_showlettergrade', get_string('showlettergrade', 'grades'), $options); 1074 if (empty($CFG->grade_report_user_showcontributiontocoursetotal)) { 1075 $options[-1] = get_string('defaultprev', 'grades', $options[0]); 1076 } else { 1077 $options[-1] = get_string('defaultprev', 'grades', $options[$CFG->grade_report_user_showcontributiontocoursetotal]); 1078 } 1079 1080 $mform->addElement('select', 'report_user_showcontributiontocoursetotal', get_string('showcontributiontocoursetotal', 'grades'), $options); 1081 $mform->addHelpButton('report_user_showcontributiontocoursetotal', 'showcontributiontocoursetotal', 'grades'); 1082 1083 if (empty($CFG->grade_report_user_showrange)) { 1084 $options[-1] = get_string('defaultprev', 'grades', $options[0]); 1085 } else { 1086 $options[-1] = get_string('defaultprev', 'grades', $options[1]); 1087 } 1088 1089 $mform->addElement('select', 'report_user_showrange', get_string('showrange', 'grades'), $options); 1090 1091 $options = array(0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5); 1092 if (! empty($CFG->grade_report_user_rangedecimals)) { 1093 $options[-1] = $options[$CFG->grade_report_user_rangedecimals]; 1094 } 1095 $mform->addElement('select', 'report_user_rangedecimals', get_string('rangedecimals', 'grades'), $options); 1096 1097 $options = array(-1 => get_string('default', 'grades'), 1098 0 => get_string('shownohidden', 'grades'), 1099 1 => get_string('showhiddenuntilonly', 'grades'), 1100 2 => get_string('showallhidden', 'grades')); 1101 1102 if (empty($CFG->grade_report_user_showhiddenitems)) { 1103 $options[-1] = get_string('defaultprev', 'grades', $options[0]); 1104 } else { 1105 $options[-1] = get_string('defaultprev', 'grades', $options[$CFG->grade_report_user_showhiddenitems]); 1106 } 1107 1108 $mform->addElement('select', 'report_user_showhiddenitems', get_string('showhiddenitems', 'grades'), $options); 1109 $mform->addHelpButton('report_user_showhiddenitems', 'showhiddenitems', 'grades'); 1110 1111 //showtotalsifcontainhidden 1112 $options = array(-1 => get_string('default', 'grades'), 1113 GRADE_REPORT_HIDE_TOTAL_IF_CONTAINS_HIDDEN => get_string('hide'), 1114 GRADE_REPORT_SHOW_TOTAL_IF_CONTAINS_HIDDEN => get_string('hidetotalshowexhiddenitems', 'grades'), 1115 GRADE_REPORT_SHOW_REAL_TOTAL_IF_CONTAINS_HIDDEN => get_string('hidetotalshowinchiddenitems', 'grades') ); 1116 1117 if (empty($CFG->grade_report_user_showtotalsifcontainhidden)) { 1118 $options[-1] = get_string('defaultprev', 'grades', $options[0]); 1119 } else { 1120 $options[-1] = get_string('defaultprev', 'grades', $options[$CFG->grade_report_user_showtotalsifcontainhidden]); 1121 } 1122 1123 $mform->addElement('select', 'report_user_showtotalsifcontainhidden', get_string('hidetotalifhiddenitems', 'grades'), $options); 1124 $mform->addHelpButton('report_user_showtotalsifcontainhidden', 'hidetotalifhiddenitems', 'grades'); 1125 1126 } 1127 1128 /** 1129 * Profile report callback. 1130 * 1131 * @param object $course The course. 1132 * @param object $user The user. 1133 * @param boolean $viewasuser True when we are viewing this as the targetted user sees it. 1134 */ 1135 function grade_report_user_profilereport($course, $user, $viewasuser = false) { 1136 global $OUTPUT; 1137 if (!empty($course->showgrades)) { 1138 1139 $context = context_course::instance($course->id); 1140 1141 //first make sure we have proper final grades - this must be done before constructing of the grade tree 1142 grade_regrade_final_grades($course->id); 1143 1144 /// return tracking object 1145 $gpr = new grade_plugin_return(array('type'=>'report', 'plugin'=>'user', 'courseid'=>$course->id, 'userid'=>$user->id)); 1146 // Create a report instance 1147 $report = new grade_report_user($course->id, $gpr, $context, $user->id, $viewasuser); 1148 1149 // print the page 1150 echo '<div class="grade-report-user">'; // css fix to share styles with real report page 1151 echo $OUTPUT->heading(get_string('pluginname', 'gradereport_user'). ' - '.fullname($report->user)); 1152 1153 if ($report->fill_table()) { 1154 echo $report->print_table(true); 1155 } 1156 echo '</div>'; 1157 } 1158 } 1159 1160
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |