[moodle] / contrib / plugins / grade / report / visual / lib.php Repository:

Annotation of /contrib/plugins/grade/report/visual/lib.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (view) (download)

1 : dservos 1.1 <?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 : dservos 1.5 /**
26 :     * File in whiich the grade_report_visual class is defined.
27 :     * @package gradebook
28 :     */
29 : dservos 1.1
30 :     require_once($CFG->dirroot . '/grade/report/lib.php');
31 :     require_once($CFG->libdir.'/tablelib.php');
32 :    
33 : dservos 1.5 /// Require all visualization classes
34 : dservos 1.3 foreach (glob($CFG->dirroot . '/grade/report/visual/visualizations/visual_*.php') as $filename) {
35 :     require_once($filename);
36 :     }
37 :    
38 : dservos 1.5 /**
39 :     * Class providing the API for the visual report, including harvesters,
40 :     * reports, and adaptor methods for turing grades in to visualizations.
41 :     * @uses grade_report
42 :     * @package gradebook
43 :     */
44 : dservos 1.1 class grade_report_visual extends grade_report {
45 :     /**
46 :     * Capability to view hidden items.
47 :     * @var bool $canviewhidden
48 :     */
49 : dservos 1.3 public $canviewhidden;
50 :    
51 : dservos 1.5 /**
52 :     * Array of visualizations available.
53 :     * @var array $visualizations
54 :     */
55 : dservos 1.3 private static $visualizations = array();
56 : dservos 1.1
57 :     /**
58 :     * Grade objects of users in the course
59 :     * @var array $grades
60 :     */
61 : dservos 1.3 public $grades = array();
62 : dservos 1.1
63 : dservos 1.5 /**
64 :     * Array of data to be sent to the flash front end.
65 :     * Data is generated from grades as needed using
66 :     * report_data function and $visid.
67 :     * @var array $visdata
68 :     */
69 : dservos 1.1 private $visdata = array();
70 :    
71 : dservos 1.5 /**
72 :     * The id of the visualization that has been selected.
73 :     * @var string $visid
74 :     */
75 :     public $visid;
76 : dservos 1.3
77 : dservos 1.5 /**
78 :     * Array of flashvars to be sent to flash front end.
79 :     * @var array $flashvars
80 :     */
81 : dservos 1.3 private $flashvars = array();
82 :    
83 : dservos 1.5
84 : dservos 1.3 private $flashvarshtml;
85 : dservos 1.1
86 :     /**
87 :     * The html of the report to output.
88 :     * @var string $html
89 :     */
90 :     public $html;
91 :    
92 :    
93 :     /**
94 :     * Constructor. Initialises grade_tree, sets up group, baseurl
95 :     * and pbarurl.
96 :     * @param int $courseid the coures id for the report
97 :     * @param object $gpr grade plugin tracking object
98 :     * @context string $context
99 :     */
100 : dservos 1.3 public function __construct($courseid, $gpr, $context, $visid=null) {
101 : dservos 1.1 global $CFG;
102 :     parent::__construct($courseid, $gpr, $context, null);
103 :    
104 :     $this->canviewhidden = has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $this->course->id));
105 :    
106 :     /// Set up urls
107 :     $this->baseurl = 'index.php?id=' . $this->courseid;
108 :     $this->pbarurl = 'index.php?id=' . $this->courseid;
109 :    
110 : dservos 1.5 /// Set the position of the aggregation categorie based on pref
111 :     $switch = $this->get_pref('visual', 'aggregationposition');
112 :     if ($switch == '' && isset($CFG->grade_aggregationposition)) {
113 :     $switch = grade_get_setting($this->courseid, 'aggregationposition', $CFG->grade_aggregationposition);
114 :     }
115 :    
116 : dservos 1.1 /// Build grade tree
117 : dservos 1.5 $this->gtree = new grade_tree($this->courseid, false, $switch);
118 : dservos 1.3
119 :     $this->load_visualizations();
120 : dservos 1.5
121 :     if(!is_null($visid) && !empty($visid) && array_key_exists($visid, grade_report_visual::$visualizations)) {
122 :     $this->visid = $visid;
123 :     } else {
124 :     $keys = array_keys(grade_report_visual::$visualizations);
125 :     $this->visid = $keys[0];
126 :     }
127 :    
128 : dservos 1.3 $this->set_up_flashvars();
129 : dservos 1.4
130 :     /// Set up Groups
131 : dservos 1.5 if($this->visid && grade_report_visual::get_visualization($this->visid)->usegroups) {
132 : dservos 1.4 $this->setup_groups();
133 :     }
134 : dservos 1.1 }
135 :    
136 :     /// Added to keep grade_report happy
137 :     public function process_data($data){}
138 :     public function process_action($target, $action){}
139 :    
140 :     /**
141 : dservos 1.3 * Load all the visualization classes into $visualizations.
142 :     * Looks in /visualizations and trys to make an instence of any
143 :     * class that is in a file that starts with visual_ and extends
144 :     * visualization directly.
145 :     * @param bool $return if true return visualizations array, else store in $this->visualizations
146 : dservos 1.5 * @param object $context the context to use for checking if a user has access to view a visualization.
147 : dservos 1.3 * @returns array array of clases that extend visualization
148 :     */
149 : dservos 1.5 private function load_visualizations($return=false, $context = null) {
150 : dservos 1.3 global $CFG;
151 :    
152 : dservos 1.5 if($context == null) {
153 :     $context = $this->context;
154 :     }
155 :    
156 : dservos 1.3 if(!isset(grade_report_visual::$visualizations) || is_null(grade_report_visual::$visualizations) || empty(grade_report_visual::$visualizations)) {
157 :     $visualizations = array();
158 :    
159 :     foreach (glob($CFG->dirroot . '/grade/report/visual/visualizations/visual_*.php') as $path) {
160 :     $filename = substr(basename($path, '.php'), 7);
161 :    
162 :     if(class_exists($filename) && get_parent_class($class = new $filename) == 'visualization' ) {
163 : dservos 1.5 if($class->capability == null || has_capability($class->capability, $context)) {
164 :     $visualizations[$filename] = $class;
165 :     }
166 : dservos 1.3 }
167 :     }
168 :    
169 :     if($return) {
170 :     return $visualizations;
171 :     } else {
172 :     grade_report_visual::$visualizations = $visualizations;
173 :     }
174 :     } else if($return) {
175 :     return grade_report_visual::$visualizations;
176 :     }
177 :     }
178 :    
179 :     /**
180 :     * Returns the current visualizations being used in the report
181 :     * or if none are set, it returns the them as whould be loaded
182 :     * by load_visualizations.
183 : dservos 1.5 * @param object $context the context to use for checking if a user has access to view a visualization.
184 : dservos 1.3 * @returns array array of classes that extend visualization
185 :     */
186 : dservos 1.5 public function get_visualizations($context=null) {
187 : dservos 1.3 if(!isset(grade_report_visual::$visualizations) || is_null(grade_report_visual::$visualizations) || empty(grade_report_visual::$visualizations)) {
188 : dservos 1.5 return grade_report_visual::load_visualizations(true, $context);
189 : dservos 1.3 } else {
190 :     return grade_report_visual::$visualizations;
191 :     }
192 :     }
193 :    
194 : dservos 1.5 /**
195 :     * Returns a specified visulization object.
196 :     * @param object $context the context to use for checking if a user has access to view the visualization.
197 :     * @returns object visulization object or null if the visulization does not exist or the user does not have access to view it in the given context.
198 :     */
199 :     public function get_visualization($filename, $context=null) {
200 :     if($context == null) {
201 :     $context = $this->context;
202 :     }
203 :    
204 : dservos 1.3 if(!isset(grade_report_visual::$visualizations) || is_null(grade_report_visual::$visualizations) || empty(grade_report_visual::$visualizations) || is_null(grade_report_visual::$visualizations[$filename])) {
205 :     if(class_exists($filename) && get_parent_class($class = new $filename) == 'visualization' ) {
206 : dservos 1.5 if($class->capability == null || has_capability($class->capability, $context)) {
207 :     return $class;
208 :     } else {
209 :     return null;
210 :     }
211 : dservos 1.3 } else {
212 :     return null;
213 :     }
214 :     } else {
215 :     return grade_report_visual::$visualizations[$filename];
216 :     }
217 :     }
218 :    
219 :     /**
220 : dservos 1.1 * Based on load user function from grader report.
221 :     * Pulls out the userids of the users to be used in the stats.
222 :     * @return array array of user ids to use in stats
223 :     */
224 :     public function load_users() {
225 :     global $CFG, $DB;
226 :    
227 :     if(isset($DB) && !is_null($DB)) {
228 :     $params = array();
229 :     list($usql, $gbr_params) = $DB->get_in_or_equal(explode(',', $this->gradebookroles));
230 :    
231 :     $sql = "SELECT u.id, u.firstname, u.lastname, u.imagealt, u.picture, u.idnumber, u.username
232 :     FROM {user} u
233 :     JOIN {role_assignments} ra ON u.id = ra.userid
234 :     $this->groupsql
235 :     WHERE ra.roleid $usql
236 :     $this->groupwheresql
237 :     AND ra.contextid ".get_related_contexts_string($this->context);
238 :    
239 :     $params = array_merge($gbr_params, $this->groupwheresql_params);
240 :     $this->users = $DB->get_records_sql($sql, $params);
241 :     } else {
242 :     $sql = "SELECT u.id, u.firstname, u.lastname, u.imagealt, u.picture, u.idnumber
243 :     FROM {$CFG->prefix}user u
244 :     JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
245 :     $this->groupsql
246 :     WHERE ra.roleid in ($this->gradebookroles)
247 :     $this->groupwheresql
248 :     AND ra.contextid ".get_related_contexts_string($this->context);
249 :    
250 :     $this->users = get_records_sql($sql);
251 :     }
252 :    
253 :     if (empty($this->users)) {
254 :     $this->userselect = '';
255 :     $this->users = array();
256 :     $this->userselect_params = array();
257 :     } else {
258 :     if(isset($DB) && !is_null($DB)) {
259 :     list($usql, $params) = $DB->get_in_or_equal(array_keys($this->users));
260 :     $this->userselect = "AND g.userid $usql";
261 :     $this->userselect_params = $params;
262 :     }else{
263 :     $this->userselect = 'AND g.userid in ('.implode(',', array_keys($this->users)).')';
264 :     }
265 :     }
266 :    
267 :     return $this->users;
268 :     }
269 :    
270 :     /**
271 :     * Harvest the grades from the data base and build the finalgrades array.
272 :     * Filters out hidden, locked and null grades based on users settings.
273 :     * Partly based on grader reports load_final_grades function.
274 :     */
275 :     public function harvest_data() {
276 :     global $CFG, $DB;
277 :    
278 :     $params = array();
279 :    
280 :     if(isset($DB) && !is_null($DB)) {
281 :     $params = array_merge(array($this->courseid), $this->userselect_params);
282 :    
283 :     /// please note that we must fetch all grade_grades fields if we want to contruct grade_grade object from it!
284 :     $sql = "SELECT g.*
285 :     FROM {grade_items} gi,
286 :     {grade_grades} g
287 :     WHERE g.itemid = gi.id AND gi.courseid = ? {$this->userselect}";
288 :    
289 :     $grades = $DB->get_records_sql($sql, $params);
290 :     } else {
291 :     /// please note that we must fetch all grade_grades fields if we want to contruct grade_grade object from it!
292 :     $sql = "SELECT g.*
293 :     FROM {$CFG->prefix}grade_items gi,
294 :     {$CFG->prefix}grade_grades g
295 :     WHERE g.itemid = gi.id AND gi.courseid = {$this->courseid} {$this->userselect}";
296 :    
297 :     $grades = get_records_sql($sql);
298 :     }
299 :    
300 :     $userids = array_keys($this->users);
301 :    
302 :     if ($grades) {
303 :     foreach ($grades as $graderec) {
304 : dservos 1.5 if (in_array($graderec->userid, $userids) && array_key_exists($graderec->itemid, $this->gtree->items)) { // some items may not be present!!
305 :     $grade = new grade_grade($graderec, false);
306 :     $grade->grade_item =& $this->gtree->items[$graderec->itemid];
307 :     if((($grade->is_hidden() && $this->canviewhidden && ($this->get_pref('visual', 'usehidden') || is_null($this->get_pref('visual', 'usehidden')))) || !$grade->is_hidden())
308 :     && (($grade->is_locked() && ($this->get_pref('visual', 'uselocked') || is_null($this->get_pref('visual', 'uselocked')))) || !$grade->is_locked())) {
309 :     $this->grades[$graderec->itemid][$graderec->userid] = $grade;
310 :     }
311 : dservos 1.1 }
312 :     }
313 :     }
314 : dservos 1.5
315 :     if($this->get_pref('visual', 'incompleasmin')) {
316 : dservos 1.1 /// prefil grades that do not exist yet
317 : dservos 1.3 foreach ($userids as $userid) {
318 :     foreach ($this->gtree->items as $itemid=>$unused) {
319 :     if (!isset($this->grades[$itemid][$userid])) {
320 : dservos 1.5 $grade = new grade_grade();
321 :     $grade->itemid = $itemid;
322 :     $grade->userid = $userid;
323 :     $grade->grade_item =& $this->gtree->items[$itemid]; // db caching
324 :     $grade->finalgrade = $this->gtree->items[$itemid]->grademin;
325 :    
326 :     if((($grade->is_hidden() && $this->canviewhidden && ($this->get_pref('visual', 'usehidden') || is_null($this->get_pref('visual', 'usehidden')))) || !$grade->is_hidden())
327 :     && (($grade->is_locked() && ($this->get_pref('visual', 'uselocked') || is_null($this->get_pref('visual', 'uselocked')))) || !$grade->is_locked())) {
328 :     $this->grades[$itemid][$userid] = $grade;
329 :     }
330 : dservos 1.3 }
331 : dservos 1.1 }
332 :     }
333 :     }
334 :     }
335 :    
336 : dservos 1.2 /**
337 : dservos 1.5 * Generates the data for a visualization or all visualizations to be sent to
338 :     * the front end.
339 :     * @param bool $all if true loads the data for all visualizations into visdata, else loads only the visualization selected by visid.
340 : dservos 1.2 */
341 : dservos 1.3 public function report_data($all = false) {
342 :     if($all) {
343 :     $visuals = $this->get_visualizations();
344 :    
345 :     foreach($visuals as $key=>$visual) {
346 :     $this->visdata[$key] = $visual->report_data($this);
347 : dservos 1.1 }
348 : dservos 1.3 } else {
349 :     $visual = $this->get_visualization($this->visid);
350 :     $this->visdata[$this->visid] = $visual->report_data($this);
351 : dservos 1.1 }
352 : dservos 1.3 }
353 : dservos 1.1
354 : dservos 1.5 /**
355 :     * Generates the values of the flashvars that will be sent to the
356 :     * flash front end and encodes them as html in flashvarshtml so
357 :     * they can be droped in to the flash tag.
358 :     */
359 : dservos 1.3 private function set_up_flashvars() {
360 :     global $USER, $SESSION, $CFG;
361 :    
362 :     $flashvars = array();
363 :     $flashvars['username'] = $USER->username;
364 :     $flashvars['userid'] = $USER->id;
365 :     $flashvars['courseid'] = $this->courseid;
366 :     $flashvars['coursefullname'] = $this->course->fullname;
367 :     $flashvars['courseshortname'] = $this->course->shortname;
368 :     $flashvars['sessionid'] = session_id();
369 :     $flashvars['sessioncookie'] = $CFG->sessioncookie;
370 :     $flashvars['sessiontest'] = $SESSION->session_test;
371 :     $flashvars['dirroot'] = $CFG->dirroot;
372 :     $flashvars['wwwroot'] = $CFG->wwwroot;
373 :     $flashvars['visid'] = $this->visid;
374 : dservos 1.1
375 : dservos 1.3 foreach($flashvars as $key=>$val) {
376 :     $this->flashvarshtml .= $key. '=' . addslashes(urlencode(strip_tags($val))) . '&';
377 : dservos 1.1 }
378 : dservos 1.3 $this->flashvarshtml = substr($this->flashvarshtml, 0, strlen($this->flashvarshtml) - 1);
379 : dservos 1.1 }
380 :    
381 : dservos 1.2 /**
382 : dservos 1.5 * Returns or prints html for the report.
383 :     * HTML produced is based on flex.php and the values in this calss.
384 :     * @param bool $printerversion if true the HTML will be for the printerversion of the report.
385 :     * @param bool $return if true the HTML will be returned as a string rather then echoed.
386 :     * @returns string if $return is true a string of the HTML will be retruned.
387 :     */
388 : dservos 1.3 public function adapt_html($printerversion = false, $return = false) {
389 :     global $CFG;
390 :    
391 :     $flashvarshtml = $this->flashvarshtml;
392 :     $visual = $this->get_visualization($this->visid);
393 :    
394 : dservos 1.5 if($printerversion) {
395 :     $flashvarshtml .= '&printerversion=true';
396 :     } else {
397 :     $flashvarshtml .= '&printerversion=false';
398 :     }
399 :    
400 : dservos 1.3 ob_start();
401 :     require($CFG->dirroot.'/grade/report/visual/flex.php');
402 :     $this->html = ob_get_clean();
403 :    
404 :     if($return) {
405 :     return $this->html;
406 :     } else {
407 :     echo $this->html;
408 :     }
409 :     }
410 :    
411 : dservos 1.5 /**
412 :     * Adapts $visdata to a fromat that can be read by the flash front end.
413 :     * TODO: Have options for diffrent fromats rather then just tab format.
414 :     * @param bool $return if true the data is returned in a string, else it is echoed.
415 :     * @returns string if $return is true a string of the adapted data will be returned.
416 :     */
417 : dservos 1.3 public function adapt_data($return = false) {
418 :     if($return) {
419 :     return $this->get_tab(true);
420 :     } else {
421 :     echo $this->get_tab(true);
422 :     }
423 :     }
424 :    
425 : dservos 1.5 /**
426 :     * Generates the HTML for a selector feild witch lists the visualizations avaible
427 :     * to the current user.
428 :     * @param bool $return if true the HTML is retruned as a string, otherwise it is echoed.
429 :     * @returns string if $return is true, the HTML is returned as a string.
430 :     */
431 : dservos 1.3 public function visualization_selector($return = false) {
432 :     $visuals = $this->get_visualizations();
433 :     $visualmenu = array();
434 :     $vislabel = $this->get_lang_string('visualselector', 'gradereport_visual');
435 : dservos 1.1
436 : dservos 1.3 foreach($visuals as $key=>$visual) {
437 :     $visualmenu[$key] = format_string($visual->name);
438 :     }
439 :    
440 :     if(count($visualmenu) > 1) {
441 :     $selectorhtml = popup_form($this->pbarurl . '&amp;visid=', $visualmenu, 'selectvisual', $this->visid, '', '', '', true, 'self', $vislabel);
442 :     } else {
443 :     $selectorhtml = '';
444 :     }
445 :    
446 :     if($return) {
447 :     return $selectorhtml;
448 :     } else {
449 :     echo $selectorhtml;
450 :     }
451 : dservos 1.1 }
452 :    
453 : dservos 1.5 /**
454 :     * Truncates a string to $max chars long and adds $end to the string
455 :     * if it is more then $max chars. The resulting string will allways be at
456 :     * most $max chars long. $end must be shorter then $max long.
457 :     * @param string $string the string to truncate.
458 :     * @param int $max the maxium length of the string.
459 :     * @param string $end a string that will be appened to the end of the truncated string if it is over $max in length.
460 :     * @returns string returns the truncated string if $string is over $max in length, other wise returns $string.
461 :     */
462 : dservos 1.4 public static function truncate($string, $max = 25, $end = '...') {
463 :     if(strlen($string) <= $max) {
464 :     return $string;
465 :     }
466 :    
467 :     return substr_replace($string, $end, $max - strlen($end));
468 :     }
469 :    
470 : dservos 1.2 /**
471 :     * Returns the data for a visulasation in tab format.
472 :     * @param boolean $return if true return a string, other wise echo the data.
473 :     * @return string If return is set to true, returns a string of the data in tab format.
474 :     */
475 : dservos 1.3 private function get_tab($return = true) {
476 : dservos 1.1 if ($return) {
477 : dservos 1.3 return $this->tab_encode($this->visdata[$this->visid]);
478 : dservos 1.1 } else {
479 : dservos 1.3 echo $this->tab_encode($this->visdata[$this->visid]);
480 : dservos 1.1 }
481 :     }
482 :    
483 : dservos 1.2 /**
484 :     * Encodes an array to a string using the tab format.
485 :     *@param array $data the array to encode.
486 :     *@return string a string encoded in tab format based on the given array.
487 :     */
488 : dservos 1.3 public static function tab_encode(array $data, $useindexes = false) {
489 : dservos 1.1 $outstring = '';
490 :    
491 : dservos 1.3 foreach($data as $key=>$row) {
492 :     if(is_array($row)) {
493 :     if($useindexes) {
494 :     $outstring .= $key . "\t";
495 :     }
496 :    
497 :     foreach($row as $col) {
498 :     $outstring .= $col . "\t";
499 :     }
500 :    
501 :     $outstring[strlen($outstring) - 1] = "\n";
502 :     } else {
503 :     if($useindexes) {
504 :     $outstring .= $key . "\t" . $row . "\n";
505 :     } else {
506 :     $outstring .= $row . "\n";
507 :     }
508 : dservos 1.1 }
509 :     }
510 :    
511 :     return $outstring;
512 :     }
513 :     }
514 :     ?>

Moodle CVS Admin
ViewVC Help
Powered by ViewVC 1.0.7