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

Moodle CVS Admin
ViewVC Help
Powered by ViewVC 1.0.7