|
By dservos:
CONTRIB-496
Cleaned up code, added more documentation and added the copy right notice to the new files.
Also made the statistics classes be loaded in dynamically so new statistics can be added to the report with out touching any of the plug-ins code.
|
| 1 |
<?php |
<?php |
| 2 |
|
/////////////////////////////////////////////////////////////////////////// |
| 3 |
|
// // |
| 4 |
|
// NOTICE OF COPYRIGHT // |
| 5 |
|
// // |
| 6 |
|
// Moodle - Modular Object-Oriented Dynamic Learning Environment // |
| 7 |
|
// http://moodle.org // |
| 8 |
|
// // |
| 9 |
|
// Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com // |
| 10 |
|
// // |
| 11 |
|
// This program is free software; you can redistribute it and/or modify // |
| 12 |
|
// it under the terms of the GNU General Public License as published by // |
| 13 |
|
// the Free Software Foundation; either version 2 of the License, or // |
| 14 |
|
// (at your option) any later version. // |
| 15 |
|
// // |
| 16 |
|
// This program is distributed in the hope that it will be useful, // |
| 17 |
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of // |
| 18 |
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
| 19 |
|
// GNU General Public License for more details: // |
| 20 |
|
// // |
| 21 |
|
// http://www.gnu.org/copyleft/gpl.html // |
| 22 |
|
// // |
| 23 |
|
/////////////////////////////////////////////////////////////////////////// |
| 24 |
|
|
| 25 |
/** |
/** |
| 26 |
* File in which the grade_report_stats class is defined. |
* File in which the grade_report_stats class is defined. |
| 27 |
|
* @package gradebook |
| 28 |
*/ |
*/ |
| 29 |
|
|
| 30 |
require_once($CFG->dirroot . '/grade/report/lib.php'); |
require_once($CFG->dirroot . '/grade/report/lib.php'); |
| 31 |
require_once($CFG->libdir.'/tablelib.php'); |
require_once($CFG->libdir.'/tablelib.php'); |
| 32 |
require_once($CFG->dirroot . '/grade/report/stats/statistics/load.php'); |
|
| 33 |
|
foreach (glob($CFG->dirroot . '/grade/report/stats/statistics/stat_*.php') as $filename) { |
| 34 |
|
require_once($filename); |
| 35 |
|
} |
| 36 |
|
|
| 37 |
/** |
/** |
| 38 |
* Class providing the API for the stats report, including harvesters, |
* Class providing the API for the stats report, including harvesters, |
| 44 |
/** |
/** |
| 45 |
* Capability to view hidden items. |
* Capability to view hidden items. |
| 46 |
* TODO: Add a check for hidden items and grades. |
* TODO: Add a check for hidden items and grades. |
| 47 |
* @var boolean $canviewhidden |
* @var bool $canviewhidden |
| 48 |
*/ |
*/ |
| 49 |
private $canviewhidden; |
private $canviewhidden; |
| 50 |
|
|
| 52 |
* Categories to be displayed based on report setting. |
* Categories to be displayed based on report setting. |
| 53 |
* @var array $collapsed |
* @var array $collapsed |
| 54 |
*/ |
*/ |
| 55 |
private $collapsed; |
private $collapsed = array(); |
| 56 |
|
|
| 57 |
/** |
/** |
| 58 |
* Grade objects of users in the course |
* Grade objects of users in the course |
| 59 |
* @var array $grades |
* @var array $grades |
| 60 |
*/ |
*/ |
| 61 |
private $grades; |
private $grades = array(); |
| 62 |
|
|
| 63 |
/** |
/** |
| 64 |
* Array of users final grades affter filtered based on |
* Array of users final grades affter filtered based on |
| 65 |
* settings. |
* settings. |
| 66 |
* @var array $finalgrades |
* @var array $finalgrades |
| 67 |
*/ |
*/ |
| 68 |
private $finalgrades; |
private $finalgrades = array(); |
| 69 |
|
|
| 70 |
/** |
/** |
| 71 |
* The value returned from each statistic. |
* The value returned from each statistic. |
| 72 |
* @var array $reportedstats |
* @var array $reportedstats |
| 73 |
*/ |
*/ |
| 74 |
private $reportedstats; |
private $reportedstats = array(); |
| 75 |
|
|
| 76 |
/** |
/** |
| 77 |
* The html of the report to output. |
* The html of the report to output. |
| 86 |
private $table; |
private $table; |
| 87 |
|
|
| 88 |
/** |
/** |
| 89 |
|
* Array of clases that extend stats witch have the logic to |
| 90 |
|
* generate the statstics. |
| 91 |
|
* @var array $stats |
| 92 |
|
*/ |
| 93 |
|
private $stats = array(); |
| 94 |
|
|
| 95 |
|
/** |
| 96 |
* Constructor. Initialises grade_tree, sets up group, baseurl |
* Constructor. Initialises grade_tree, sets up group, baseurl |
| 97 |
* and pbarurl. |
* and pbarurl. |
| 98 |
* @param int $courseid the coures id for the report |
* @param int $courseid the coures id for the report |
| 137 |
$this->local_setup_groups(); |
$this->local_setup_groups(); |
| 138 |
} |
} |
| 139 |
} |
} |
| 140 |
|
|
| 141 |
|
$this->load_stats(); |
| 142 |
|
} |
| 143 |
|
|
| 144 |
|
/** |
| 145 |
|
* Load all the stats classes into $stats. |
| 146 |
|
* Looks in /statistics and trys to make an instence of any |
| 147 |
|
* class that is in a file that starts with stats_ and extends |
| 148 |
|
* stats directly. |
| 149 |
|
* @param bool $return if true return stats array, else store in $this->stats |
| 150 |
|
* @returns array array of clases that extend stats |
| 151 |
|
*/ |
| 152 |
|
private function load_stats($return=false) { |
| 153 |
|
global $CFG; |
| 154 |
|
|
| 155 |
|
$stats = array(); |
| 156 |
|
|
| 157 |
|
foreach (glob($CFG->dirroot . '/grade/report/stats/statistics/stat_*.php') as $path) { |
| 158 |
|
$filename = substr(basename($path, '.php'), 5); |
| 159 |
|
|
| 160 |
|
if(class_exists($filename) && get_parent_class($class = new $filename) == 'stats' ) { |
| 161 |
|
$stats[$filename] = $class; |
| 162 |
|
} |
| 163 |
|
} |
| 164 |
|
|
| 165 |
|
if($return) { |
| 166 |
|
return $stats; |
| 167 |
|
} else { |
| 168 |
|
$this->stats = $stats; |
| 169 |
|
} |
| 170 |
} |
} |
| 171 |
|
|
| 172 |
|
/** |
| 173 |
|
* Returns the current stats being used in the report or if no stats are |
| 174 |
|
* set, it returns the stats as whould be loaded by load_stats. |
| 175 |
|
* @returns array array of classes that extend stats |
| 176 |
|
*/ |
| 177 |
|
public function get_stats() { |
| 178 |
|
if(!isset($this->stats) || is_null($this->stats) || empty($this->stats)) { |
| 179 |
|
return grade_report_stats::load_stats(true); |
| 180 |
|
} else { |
| 181 |
|
return $this->stats; |
| 182 |
|
} |
| 183 |
|
} |
| 184 |
|
|
| 185 |
|
/// Added to keep grade_report happy |
| 186 |
public function process_data($data){} |
public function process_data($data){} |
| 187 |
public function process_action($target, $action){} |
public function process_action($target, $action){} |
| 188 |
|
|
| 189 |
|
/// Tempary fix intill issue MDL-15253 is fixed |
| 190 |
private function local_setup_groups() { |
private function local_setup_groups() { |
| 191 |
global $CFG; |
global $CFG; |
| 192 |
|
|
| 259 |
* @param object $stat stats object to use to format stats |
* @param object $stat stats object to use to format stats |
| 260 |
* @return string encoded string |
* @return string encoded string |
| 261 |
*/ |
*/ |
| 262 |
private function encode_array($array, $item, $stat) { |
private function encode_array(array $array, $item, $stat) { |
| 263 |
$string = ''; |
$string = ''; |
| 264 |
|
|
| 265 |
/// Encode each elment by puting a delimiter and base64 encoding it. |
/// Encode each elment by puting a delimiter and base64 encoding it. |
| 277 |
public function harvest_data() { |
public function harvest_data() { |
| 278 |
global $CFG, $DB; |
global $CFG, $DB; |
| 279 |
|
|
| 280 |
|
$params = array(); |
| 281 |
|
|
| 282 |
if(isset($DB) && !is_null($DB)) { |
if(isset($DB) && !is_null($DB)) { |
| 283 |
$params = array_merge(array($this->courseid), $this->userselect_params); |
$params = array_merge(array($this->courseid), $this->userselect_params); |
| 284 |
|
|
| 356 |
* TODO: Do not report on hidden items if the user can not see them. |
* TODO: Do not report on hidden items if the user can not see them. |
| 357 |
*/ |
*/ |
| 358 |
public function report_data() { |
public function report_data() { |
|
global $stats; |
|
| 359 |
$this->reportedstats = array(); |
$this->reportedstats = array(); |
| 360 |
|
|
| 361 |
foreach($stats as $name=>$stat) { |
foreach($this->stats as $name=>$stat) { |
| 362 |
if($this->get_pref('stats', $name) || is_null($this->get_pref('stats', $name))) { |
if($this->get_pref('stats', $name) || is_null($this->get_pref('stats', $name))) { |
| 363 |
$this->reportedstats[$name] = array(); |
$this->reportedstats[$name] = array(); |
| 364 |
|
|
| 381 |
* TODO: Make it look nice. |
* TODO: Make it look nice. |
| 382 |
*/ |
*/ |
| 383 |
public function adapt_data() { |
public function adapt_data() { |
| 384 |
global $stats; |
global $CFG; |
| 385 |
|
|
| 386 |
/// Set up table arrays |
/// Set up table arrays |
| 387 |
$tablecolumns = array('statistic'); |
$tablecolumns = array('statistic'); |
| 388 |
$tableheaders = array($this->get_lang_string('statistic', 'gradereport_stats')); |
$tableheaders = array($this->get_lang_string('statistic', 'gradereport_stats')); |
| 389 |
|
|
| 390 |
/// Set up range column and number of grades column |
/// Set up range column and number of grades column |
| 391 |
$ranges = array('<strong>' . $this->get_lang_string('range', 'gradereport_stats') . '</strong>'); |
$ranges = array(format_text('<strong>' . $this->get_lang_string('range', 'gradereport_stats') . '</strong>', FORMAT_HTML)); |
| 392 |
$numgrades = array('<strong>' . $this->get_lang_string('num_grades', 'gradereport_stats') . '</strong>'); |
$numgrades = array(format_text('<strong>' . $this->get_lang_string('num_grades', 'gradereport_stats') . '</strong>', FORMAT_HTML)); |
| 393 |
|
|
| 394 |
/// Loop threw items and build arrays |
/// Loop threw items and build arrays |
| 395 |
foreach($this->finalgrades as $itemid=>$item) { |
foreach($this->finalgrades as $itemid=>$item) { |
| 396 |
array_push($tablecolumns, $itemid); |
array_push($tablecolumns, $itemid); |
| 397 |
array_push($tableheaders, $this->gtree->items[$itemid]->get_name()); |
array_push($tableheaders, format_text($this->gtree->items[$itemid]->get_name(), FORMAT_HTML)); |
| 398 |
array_push($ranges, '<strong>' . grade_format_gradevalue($this->gtree->items[$itemid]->grademin, $this->gtree->items[$itemid], true) . '-' . grade_format_gradevalue($this->gtree->items[$itemid]->grademax, $this->gtree->items[$itemid], true) . '<strong>' ); |
array_push($ranges, format_text('<strong>' . grade_format_gradevalue($this->gtree->items[$itemid]->grademin, $this->gtree->items[$itemid], true) . '-' . grade_format_gradevalue($this->gtree->items[$itemid]->grademax, $this->gtree->items[$itemid], true) . '<strong>' , FORMAT_HTML)); |
| 399 |
array_push($numgrades, count($item)); |
array_push($numgrades, format_text(count($item), FORMAT_HTML)); |
| 400 |
} |
} |
| 401 |
|
|
| 402 |
/// Set up flexible table |
/// Set up flexible table |
| 420 |
/// If stat retured an array of values display the elements or |
/// If stat retured an array of values display the elements or |
| 421 |
/// make a link to a popup with the data in it. |
/// make a link to a popup with the data in it. |
| 422 |
foreach($this->reportedstats as $name=>$data) { |
foreach($this->reportedstats as $name=>$data) { |
| 423 |
$row = array('<strong>' . $stats[$name]->name . '</strong>'); |
$row = array(format_text('<strong>' . $this->stats[$name]->name . '</strong>', FORMAT_HTML)); |
| 424 |
|
|
| 425 |
foreach($data as $itemid=>$stat) { |
foreach($data as $itemid=>$stat) { |
| 426 |
if(!is_array($stat)) { |
if(!is_array($stat)) { |
| 427 |
array_push($row, grade_format_gradevalue($stat, $this->gtree->items[$itemid], true, $stats[$name]->displaytype, $stats[$name]->decimals)); |
array_push($row, format_text(grade_format_gradevalue($stat, $this->gtree->items[$itemid], true, $this->stats[$name]->displaytype, $this->stats[$name]->decimals), FORMAT_HTML)); |
| 428 |
} else { |
} else { |
| 429 |
$statstring = ""; |
$statstring = ""; |
| 430 |
|
|
| 432 |
if($i >= count($stat)) { |
if($i >= count($stat)) { |
| 433 |
break; |
break; |
| 434 |
} |
} |
| 435 |
$statstring .= grade_format_gradevalue($stat[$i], $this->gtree->items[$itemid], true, $stats[$name]->displaytype, $stats[$name]->decimals) . ', '; |
$statstring .= grade_format_gradevalue($stat[$i], $this->gtree->items[$itemid], true, $this->stats[$name]->displaytype, $this->stats[$name]->decimals) . ', '; |
| 436 |
} |
} |
| 437 |
|
|
| 438 |
if($i < count($stat)) { |
if($i < count($stat)) { |
| 439 |
$statstring = "<a href=\"#\" onClick=\"javascript:window.open('arrayview.php?data={$this->encode_array($stat, $this->gtree->items[$itemid], $stats[$name])}','{$this->get_lang_string('moredata', 'gradereport_stats')}','width=300,height=500,menubar=no,status=no,location=no,directories=no,toolbar=no,scrollbars=yes');\">". $statstring . '....</a>'; |
$statstring = "<a href=\"#\" onClick=\"javascript:window.open('{$CFG->wwwroot}/grade/report/stats/arrayview.php?id={$this->courseid}&data={$this->encode_array($stat, $this->gtree->items[$itemid], $this->stats[$name])}','{$this->get_lang_string('moredata', 'gradereport_stats')}','width=300,height=500,menubar=no,status=no,location=no,directories=no,toolbar=no,scrollbars=yes');\">". format_text($statstring, FORMAT_HTML) . '....</a>'; |
| 440 |
} else { |
} else { |
| 441 |
$statstring = substr($statstring, 0, strlen($statstring) - 2); |
$statstring = substr($statstring, 0, strlen($statstring) - 2); |
| 442 |
} |
} |
| 455 |
/// Build html |
/// Build html |
| 456 |
ob_start(); |
ob_start(); |
| 457 |
if($this->currentgroup == 0) { |
if($this->currentgroup == 0) { |
| 458 |
echo '<strong>Group:</strong> All participants'; |
echo format_text('<strong>Group:</strong> All participants', FORMAT_HTML); |
| 459 |
} else { |
} else { |
| 460 |
echo '<strong>Group:</strong> ' . groups_get_group_name($this->currentgroup); |
echo format_text('<strong>Group:</strong> ' . groups_get_group_name($this->currentgroup), FORMAT_HTML); |
| 461 |
} |
} |
| 462 |
$this->table->print_html(); |
$this->table->print_html(); |
| 463 |
$this->html = ob_get_clean(); |
$this->html = ob_get_clean(); |
| 527 |
.$string.'" title="'.$string.'" />'. "\n"; |
.$string.'" title="'.$string.'" />'. "\n"; |
| 528 |
|
|
| 529 |
$retval = $img . '<a href="' . $this->baseurl . "&toggle=$toggle_action&toggle_type=$type\">" |
$retval = $img . '<a href="' . $this->baseurl . "&toggle=$toggle_action&toggle_type=$type\">" |
| 530 |
. $string . '</a> '; |
. format_text($string, FORMAT_HTML) . '</a> '; |
| 531 |
|
|
| 532 |
if ($return) { |
if ($return) { |
| 533 |
return $retval; |
return $retval; |