|
By dservos:
CONTRIB-497
*Added new visualization, Grade Distribution
*Fixed some minor bugs
*Made abstract visualization class for creating visualizations by making classes witch extend it.
*Made visual_settings.php witch takes a visualization class and truns it in to XML witch flex can read in.
*Made flex visualization application read in XML formated settings as well as tab formated data from moodle and combind them to make a custom visualization.
*Made flex visualization application read and use langue strings from moodle.
*Added printer firendly tab
TODO:
*Add more visualizations
*Refactor some of the flex/actionscript code
*More douctenation
*More UI functions for the flex application
|
| 26 |
require_once($CFG->dirroot . '/grade/report/lib.php'); |
require_once($CFG->dirroot . '/grade/report/lib.php'); |
| 27 |
require_once($CFG->libdir.'/tablelib.php'); |
require_once($CFG->libdir.'/tablelib.php'); |
| 28 |
|
|
| 29 |
|
foreach (glob($CFG->dirroot . '/grade/report/visual/visualizations/visual_*.php') as $filename) { |
| 30 |
|
require_once($filename); |
| 31 |
|
} |
| 32 |
|
|
| 33 |
class grade_report_visual extends grade_report { |
class grade_report_visual extends grade_report { |
| 34 |
/** |
/** |
| 35 |
* Capability to view hidden items. |
* Capability to view hidden items. |
| 36 |
* TODO: Add a check for hidden items and grades. |
* TODO: Add a check for hidden items and grades. |
| 37 |
* @var bool $canviewhidden |
* @var bool $canviewhidden |
| 38 |
*/ |
*/ |
| 39 |
private $canviewhidden; |
public $canviewhidden; |
| 40 |
|
|
| 41 |
|
public $nullgradesasmin = false; |
| 42 |
|
|
| 43 |
|
private static $visualizations = array(); |
| 44 |
|
|
| 45 |
/** |
/** |
| 46 |
* Grade objects of users in the course |
* Grade objects of users in the course |
| 47 |
* @var array $grades |
* @var array $grades |
| 48 |
*/ |
*/ |
| 49 |
private $grades = array(); |
public $grades = array(); |
| 50 |
|
|
| 51 |
private $visdata = array(); |
private $visdata = array(); |
| 52 |
|
|
| 53 |
|
private $visid = 'grades_vs_students'; |
| 54 |
|
|
| 55 |
|
private $flashvars = array(); |
| 56 |
|
|
| 57 |
|
private $flashvarshtml; |
| 58 |
|
|
| 59 |
/** |
/** |
| 60 |
* The html of the report to output. |
* The html of the report to output. |
| 70 |
* @param object $gpr grade plugin tracking object |
* @param object $gpr grade plugin tracking object |
| 71 |
* @context string $context |
* @context string $context |
| 72 |
*/ |
*/ |
| 73 |
public function __construct($courseid, $gpr, $context) { |
public function __construct($courseid, $gpr, $context, $visid=null) { |
| 74 |
global $CFG; |
global $CFG; |
| 75 |
parent::__construct($courseid, $gpr, $context, null); |
parent::__construct($courseid, $gpr, $context, null); |
| 76 |
|
|
| 77 |
$this->canviewhidden = has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $this->course->id)); |
$this->canviewhidden = has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $this->course->id)); |
| 78 |
|
|
| 79 |
|
if(!is_null($visid)) { |
| 80 |
|
$this->visid = $visid; |
| 81 |
|
} |
| 82 |
|
|
| 83 |
/// Set up urls |
/// Set up urls |
| 84 |
$this->baseurl = 'index.php?id=' . $this->courseid; |
$this->baseurl = 'index.php?id=' . $this->courseid; |
| 85 |
$this->pbarurl = 'index.php?id=' . $this->courseid; |
$this->pbarurl = 'index.php?id=' . $this->courseid; |
| 95 |
$this->local_setup_groups(); |
$this->local_setup_groups(); |
| 96 |
} |
} |
| 97 |
}*/ |
}*/ |
| 98 |
|
|
| 99 |
|
$this->load_visualizations(); |
| 100 |
|
$this->set_up_flashvars(); |
| 101 |
} |
} |
| 102 |
|
|
| 103 |
/// Added to keep grade_report happy |
/// Added to keep grade_report happy |
| 105 |
public function process_action($target, $action){} |
public function process_action($target, $action){} |
| 106 |
|
|
| 107 |
/** |
/** |
| 108 |
|
* Load all the visualization classes into $visualizations. |
| 109 |
|
* Looks in /visualizations and trys to make an instence of any |
| 110 |
|
* class that is in a file that starts with visual_ and extends |
| 111 |
|
* visualization directly. |
| 112 |
|
* @param bool $return if true return visualizations array, else store in $this->visualizations |
| 113 |
|
* @returns array array of clases that extend visualization |
| 114 |
|
*/ |
| 115 |
|
private function load_visualizations($return=false) { |
| 116 |
|
global $CFG; |
| 117 |
|
|
| 118 |
|
if(!isset(grade_report_visual::$visualizations) || is_null(grade_report_visual::$visualizations) || empty(grade_report_visual::$visualizations)) { |
| 119 |
|
$visualizations = array(); |
| 120 |
|
|
| 121 |
|
foreach (glob($CFG->dirroot . '/grade/report/visual/visualizations/visual_*.php') as $path) { |
| 122 |
|
$filename = substr(basename($path, '.php'), 7); |
| 123 |
|
|
| 124 |
|
if(class_exists($filename) && get_parent_class($class = new $filename) == 'visualization' ) { |
| 125 |
|
$visualizations[$filename] = $class; |
| 126 |
|
} |
| 127 |
|
} |
| 128 |
|
|
| 129 |
|
if($return) { |
| 130 |
|
return $visualizations; |
| 131 |
|
} else { |
| 132 |
|
grade_report_visual::$visualizations = $visualizations; |
| 133 |
|
} |
| 134 |
|
} else if($return) { |
| 135 |
|
return grade_report_visual::$visualizations; |
| 136 |
|
} |
| 137 |
|
} |
| 138 |
|
|
| 139 |
|
/** |
| 140 |
|
* Returns the current visualizations being used in the report |
| 141 |
|
* or if none are set, it returns the them as whould be loaded |
| 142 |
|
* by load_visualizations. |
| 143 |
|
* @returns array array of classes that extend visualization |
| 144 |
|
*/ |
| 145 |
|
public function get_visualizations() { |
| 146 |
|
if(!isset(grade_report_visual::$visualizations) || is_null(grade_report_visual::$visualizations) || empty(grade_report_visual::$visualizations)) { |
| 147 |
|
return grade_report_visual::load_visualizations(true); |
| 148 |
|
} else { |
| 149 |
|
return grade_report_visual::$visualizations; |
| 150 |
|
} |
| 151 |
|
} |
| 152 |
|
|
| 153 |
|
public function get_visualization($filename) { |
| 154 |
|
if(!isset(grade_report_visual::$visualizations) || is_null(grade_report_visual::$visualizations) || empty(grade_report_visual::$visualizations) || is_null(grade_report_visual::$visualizations[$filename])) { |
| 155 |
|
if(class_exists($filename) && get_parent_class($class = new $filename) == 'visualization' ) { |
| 156 |
|
return $class; |
| 157 |
|
} else { |
| 158 |
|
return null; |
| 159 |
|
} |
| 160 |
|
} else { |
| 161 |
|
return grade_report_visual::$visualizations[$filename]; |
| 162 |
|
} |
| 163 |
|
} |
| 164 |
|
|
| 165 |
|
/** |
| 166 |
* Based on load user function from grader report. |
* Based on load user function from grader report. |
| 167 |
* Pulls out the userids of the users to be used in the stats. |
* Pulls out the userids of the users to be used in the stats. |
| 168 |
* @return array array of user ids to use in stats |
* @return array array of user ids to use in stats |
| 254 |
} |
} |
| 255 |
} |
} |
| 256 |
|
|
| 257 |
|
if($this->nullgradesasmin) { |
| 258 |
/// prefil grades that do not exist yet |
/// prefil grades that do not exist yet |
| 259 |
foreach ($userids as $userid) { |
foreach ($userids as $userid) { |
| 260 |
foreach ($this->gtree->items as $itemid=>$unused) { |
foreach ($this->gtree->items as $itemid=>$unused) { |
| 263 |
$this->grades[$itemid][$userid]->itemid = $itemid; |
$this->grades[$itemid][$userid]->itemid = $itemid; |
| 264 |
$this->grades[$itemid][$userid]->userid = $userid; |
$this->grades[$itemid][$userid]->userid = $userid; |
| 265 |
$this->grades[$itemid][$userid]->grade_item =& $this->gtree->items[$itemid]; // db caching |
$this->grades[$itemid][$userid]->grade_item =& $this->gtree->items[$itemid]; // db caching |
| 266 |
|
$this->grades[$itemid][$userid]->finalgrade = $this->grades[$itemid][$userid]->grade_item->mingrade; |
| 267 |
|
} |
| 268 |
} |
} |
| 269 |
} |
} |
| 270 |
} |
} |
| 300 |
* TODO: have this make the data depending on what kind of visulsation |
* TODO: have this make the data depending on what kind of visulsation |
| 301 |
* is needed rather then a singal visulasztion witch is currently hardcoded. |
* is needed rather then a singal visulasztion witch is currently hardcoded. |
| 302 |
*/ |
*/ |
| 303 |
public function report_data() { |
public function report_data($all = false) { |
| 304 |
$this->visdata['test2'] = array(); |
if($all) { |
| 305 |
$this->visdata['test2']['header'] = array(); |
$visuals = $this->get_visualizations(); |
|
$this->visdata['test2']['header']['student'] = 'student'; |
|
|
$this->visdata['test2']['header']['grade'] = 'grade'; |
|
|
$this->visdata['test2']['header']['item'] = 'item'; |
|
|
$this->visdata['test2']['header']['group'] = 'group'; |
|
| 306 |
|
|
| 307 |
foreach($this->grades as $itemkey=>$itemgrades) { |
foreach($visuals as $key=>$visual) { |
| 308 |
foreach($itemgrades as $studentkey=>$studentdata) { |
$this->visdata[$key] = $visual->report_data($this); |
|
if($studentdata != null && $studentdata->finalgrade != null) { |
|
|
foreach(groups_get_user_groups($this->courseid, $studentkey) as $grouping) { |
|
|
foreach($grouping as $group) { |
|
|
$this->visdata['test2'][$studentkey . '-' . $itemkey . '-' . $group]['student'] = $this->users[$studentkey]->firstname . ' ' . $this->users[$studentkey]->lastname; |
|
|
$this->visdata['test2'][$studentkey . '-' . $itemkey . '-' . $group]['grade'] = $studentdata->standardise_score($studentdata->finalgrade, $this->gtree->items[$itemkey]->grademin, $this->gtree->items[$itemkey]->grademax, 0, 100); |
|
|
$this->visdata['test2'][$studentkey . '-' . $itemkey . '-' . $group]['item'] = $this->gtree->items[$itemkey]->get_name(); |
|
|
$this->visdata['test2'][$studentkey . '-' . $itemkey . '-' . $group]['group'] = groups_get_group_name($group); |
|
| 309 |
} |
} |
| 310 |
|
} else { |
| 311 |
|
$visual = $this->get_visualization($this->visid); |
| 312 |
|
$this->visdata[$this->visid] = $visual->report_data($this); |
| 313 |
} |
} |
| 314 |
} |
} |
| 315 |
|
|
| 316 |
|
private function set_up_flashvars() { |
| 317 |
|
global $USER, $SESSION, $CFG; |
| 318 |
|
|
| 319 |
|
$flashvars = array(); |
| 320 |
|
$flashvars['username'] = $USER->username; |
| 321 |
|
$flashvars['userid'] = $USER->id; |
| 322 |
|
$flashvars['courseid'] = $this->courseid; |
| 323 |
|
$flashvars['coursefullname'] = $this->course->fullname; |
| 324 |
|
$flashvars['courseshortname'] = $this->course->shortname; |
| 325 |
|
$flashvars['sessionid'] = session_id(); |
| 326 |
|
$flashvars['sessioncookie'] = $CFG->sessioncookie; |
| 327 |
|
$flashvars['sessiontest'] = $SESSION->session_test; |
| 328 |
|
$flashvars['dirroot'] = $CFG->dirroot; |
| 329 |
|
$flashvars['wwwroot'] = $CFG->wwwroot; |
| 330 |
|
$flashvars['visid'] = $this->visid; |
| 331 |
|
|
| 332 |
|
foreach($flashvars as $key=>$val) { |
| 333 |
|
$this->flashvarshtml .= $key. '=' . addslashes(urlencode(strip_tags($val))) . '&'; |
| 334 |
} |
} |
| 335 |
|
$this->flashvarshtml = substr($this->flashvarshtml, 0, strlen($this->flashvarshtml) - 1); |
| 336 |
} |
} |
| 337 |
|
|
| 338 |
|
/** |
| 339 |
|
*TODO: Have this call on flex.php to generate html for the report., |
| 340 |
|
*rather then having it hardcoded as it is now. |
| 341 |
|
*/ |
| 342 |
|
public function adapt_html($printerversion = false, $return = false) { |
| 343 |
|
global $CFG; |
| 344 |
|
|
| 345 |
$this->visdata['test'] = array(); |
$flashvarshtml = $this->flashvarshtml; |
| 346 |
$this->visdata['test']['header'] = array(); |
$visual = $this->get_visualization($this->visid); |
|
$this->visdata['test']['header']['student'] = 'student'; |
|
| 347 |
|
|
| 348 |
foreach($this->gtree->items as $itemid=>$item) { |
ob_start(); |
| 349 |
$this->visdata['test']['header']['i' . $itemid] = $this->gtree->items[$itemid]->get_name(); |
require($CFG->dirroot.'/grade/report/visual/flex.php'); |
| 350 |
} |
$this->html = ob_get_clean(); |
| 351 |
|
|
| 352 |
foreach($this->users as $studentid=>$student) { |
if($return) { |
| 353 |
$this->visdata['test']['s' . $studentid]['student'] = $student->firstname . ' ' . $student->lastname; |
return $this->html; |
| 354 |
|
} else { |
| 355 |
|
echo $this->html; |
| 356 |
|
} |
| 357 |
} |
} |
| 358 |
|
|
| 359 |
foreach($this->grades as $itemkey=>$itemgrades) { |
public function adapt_data($return = false) { |
| 360 |
foreach($itemgrades as $studentkey=>$studentdata) { |
if($return) { |
| 361 |
$this->visdata['test']['s' . $studentkey]['i' . $itemkey] = $studentdata->finalgrade; |
return $this->get_tab(true); |
| 362 |
|
} else { |
| 363 |
|
echo $this->get_tab(true); |
| 364 |
} |
} |
| 365 |
} |
} |
| 366 |
|
|
| 367 |
|
public function visualization_selector($return = false) { |
| 368 |
|
$visuals = $this->get_visualizations(); |
| 369 |
|
$visualmenu = array(); |
| 370 |
|
$vislabel = $this->get_lang_string('visualselector', 'gradereport_visual'); |
| 371 |
|
|
| 372 |
|
foreach($visuals as $key=>$visual) { |
| 373 |
|
$visualmenu[$key] = format_string($visual->name); |
| 374 |
} |
} |
| 375 |
|
|
| 376 |
/** |
if(count($visualmenu) > 1) { |
| 377 |
*TODO: Have this call on flex.php to generate html for the report., |
$selectorhtml = popup_form($this->pbarurl . '&visid=', $visualmenu, 'selectvisual', $this->visid, '', '', '', true, 'self', $vislabel); |
| 378 |
*rather then having it hardcoded as it is now. |
} else { |
| 379 |
*/ |
$selectorhtml = ''; |
| 380 |
public function adapt_data($printerversion = false) { |
} |
| 381 |
|
|
| 382 |
|
if($return) { |
| 383 |
|
return $selectorhtml; |
| 384 |
|
} else { |
| 385 |
|
echo $selectorhtml; |
| 386 |
|
} |
| 387 |
} |
} |
| 388 |
|
|
| 389 |
/** |
/** |
| 392 |
* @param boolean $return if true return a string, other wise echo the data. |
* @param boolean $return if true return a string, other wise echo the data. |
| 393 |
* @return string If return is set to true, returns a string of the data in json format. |
* @return string If return is set to true, returns a string of the data in json format. |
| 394 |
*/ |
*/ |
| 395 |
public function get_json($return = true) { |
private function get_json($return = true) { |
| 396 |
if ($return) { |
if ($return) { |
| 397 |
return json_encode($this->visdata['test']); |
return json_encode($this->visdata[$this->visid]); |
| 398 |
} else { |
} else { |
| 399 |
echo json_encode($this->visdata['test']); |
echo json_encode($this->visdata[$this->visid]); |
| 400 |
} |
} |
| 401 |
} |
} |
| 402 |
|
|
| 406 |
* @param boolean $return if true return a string, other wise echo the data. |
* @param boolean $return if true return a string, other wise echo the data. |
| 407 |
* @return string If return is set to true, returns a string of the data in tab format. |
* @return string If return is set to true, returns a string of the data in tab format. |
| 408 |
*/ |
*/ |
| 409 |
public function get_tab($return = true) { |
private function get_tab($return = true) { |
| 410 |
if ($return) { |
if ($return) { |
| 411 |
return $this->tab_encode($this->visdata['test2']); |
return $this->tab_encode($this->visdata[$this->visid]); |
| 412 |
} else { |
} else { |
| 413 |
echo $this->tab_encode($this->visdata['test2']); |
echo $this->tab_encode($this->visdata[$this->visid]); |
| 414 |
} |
} |
| 415 |
} |
} |
| 416 |
|
|
| 419 |
*@param array $data the array to encode. |
*@param array $data the array to encode. |
| 420 |
*@return string a string encoded in tab format based on the given array. |
*@return string a string encoded in tab format based on the given array. |
| 421 |
*/ |
*/ |
| 422 |
private function tab_encode(array $data) { |
public static function tab_encode(array $data, $useindexes = false) { |
| 423 |
$outstring = ''; |
$outstring = ''; |
| 424 |
|
|
| 425 |
foreach($data as $row) { |
foreach($data as $key=>$row) { |
| 426 |
|
if(is_array($row)) { |
| 427 |
|
if($useindexes) { |
| 428 |
|
$outstring .= $key . "\t"; |
| 429 |
|
} |
| 430 |
|
|
| 431 |
foreach($row as $col) { |
foreach($row as $col) { |
| 432 |
$outstring .= $col . "\t"; |
$outstring .= $col . "\t"; |
| 433 |
} |
} |
| 434 |
|
|
| 435 |
$outstring[strlen($outstring) - 1] = "\n"; |
$outstring[strlen($outstring) - 1] = "\n"; |
| 436 |
|
} else { |
| 437 |
|
if($useindexes) { |
| 438 |
|
$outstring .= $key . "\t" . $row . "\n"; |
| 439 |
|
} else { |
| 440 |
|
$outstring .= $row . "\n"; |
| 441 |
|
} |
| 442 |
|
} |
| 443 |
} |
} |
| 444 |
|
|
| 445 |
return $outstring; |
return $outstring; |