[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.3 - (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 :    
90 :     /// Set up Groups
91 :     /*if ($this->get_pref('stats', 'showgroups') || is_null($this->get_pref('stats', 'showgroups'))) {
92 :     if(is_callable(array($this, 'setup_groups'))) {
93 :     $this->setup_groups();
94 :     } else {
95 :     $this->local_setup_groups();
96 :     }
97 :     }*/
98 : dservos 1.3
99 :     $this->load_visualizations();
100 :     $this->set_up_flashvars();
101 : dservos 1.1 }
102 :    
103 :     /// Added to keep grade_report happy
104 :     public function process_data($data){}
105 :     public function process_action($target, $action){}
106 :    
107 :     /**
108 : dservos 1.3 * 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 : dservos 1.1 * Based on load user function from grader report.
167 :     * Pulls out the userids of the users to be used in the stats.
168 :     * @return array array of user ids to use in stats
169 :     */
170 :     public function load_users() {
171 :     global $CFG, $DB;
172 :    
173 :     if(isset($DB) && !is_null($DB)) {
174 :     $params = array();
175 :     list($usql, $gbr_params) = $DB->get_in_or_equal(explode(',', $this->gradebookroles));
176 :    
177 :     $sql = "SELECT u.id, u.firstname, u.lastname, u.imagealt, u.picture, u.idnumber, u.username
178 :     FROM {user} u
179 :     JOIN {role_assignments} ra ON u.id = ra.userid
180 :     $this->groupsql
181 :     WHERE ra.roleid $usql
182 :     $this->groupwheresql
183 :     AND ra.contextid ".get_related_contexts_string($this->context);
184 :    
185 :     $params = array_merge($gbr_params, $this->groupwheresql_params);
186 :     $this->users = $DB->get_records_sql($sql, $params);
187 :     } else {
188 :     $sql = "SELECT u.id, u.firstname, u.lastname, u.imagealt, u.picture, u.idnumber
189 :     FROM {$CFG->prefix}user u
190 :     JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
191 :     $this->groupsql
192 :     WHERE ra.roleid in ($this->gradebookroles)
193 :     $this->groupwheresql
194 :     AND ra.contextid ".get_related_contexts_string($this->context);
195 :    
196 :     $this->users = get_records_sql($sql);
197 :     }
198 :    
199 :     if (empty($this->users)) {
200 :     $this->userselect = '';
201 :     $this->users = array();
202 :     $this->userselect_params = array();
203 :     } else {
204 :     if(isset($DB) && !is_null($DB)) {
205 :     list($usql, $params) = $DB->get_in_or_equal(array_keys($this->users));
206 :     $this->userselect = "AND g.userid $usql";
207 :     $this->userselect_params = $params;
208 :     }else{
209 :     $this->userselect = 'AND g.userid in ('.implode(',', array_keys($this->users)).')';
210 :     }
211 :     }
212 :    
213 :     return $this->users;
214 :     }
215 :    
216 :     /**
217 :     * Harvest the grades from the data base and build the finalgrades array.
218 :     * Filters out hidden, locked and null grades based on users settings.
219 :     * Partly based on grader reports load_final_grades function.
220 :     */
221 :     public function harvest_data() {
222 :     global $CFG, $DB;
223 :    
224 :     $params = array();
225 :    
226 :     if(isset($DB) && !is_null($DB)) {
227 :     $params = array_merge(array($this->courseid), $this->userselect_params);
228 :    
229 :     /// please note that we must fetch all grade_grades fields if we want to contruct grade_grade object from it!
230 :     $sql = "SELECT g.*
231 :     FROM {grade_items} gi,
232 :     {grade_grades} g
233 :     WHERE g.itemid = gi.id AND gi.courseid = ? {$this->userselect}";
234 :    
235 :     $grades = $DB->get_records_sql($sql, $params);
236 :     } else {
237 :     /// please note that we must fetch all grade_grades fields if we want to contruct grade_grade object from it!
238 :     $sql = "SELECT g.*
239 :     FROM {$CFG->prefix}grade_items gi,
240 :     {$CFG->prefix}grade_grades g
241 :     WHERE g.itemid = gi.id AND gi.courseid = {$this->courseid} {$this->userselect}";
242 :    
243 :     $grades = get_records_sql($sql);
244 :     }
245 :    
246 :     $userids = array_keys($this->users);
247 :    
248 :     if ($grades) {
249 :     foreach ($grades as $graderec) {
250 :     if (in_array($graderec->userid, $userids) and array_key_exists($graderec->itemid, $this->gtree->items)) { // some items may not be present!!
251 :     $this->grades[$graderec->itemid][$graderec->userid] = new grade_grade($graderec, false);
252 :     $this->grades[$graderec->itemid][$graderec->userid]->grade_item =& $this->gtree->items[$graderec->itemid]; // db caching
253 :     }
254 :     }
255 :     }
256 :    
257 : dservos 1.3 if($this->nullgradesasmin) {
258 : dservos 1.1 /// prefil grades that do not exist yet
259 : dservos 1.3 foreach ($userids as $userid) {
260 :     foreach ($this->gtree->items as $itemid=>$unused) {
261 :     if (!isset($this->grades[$itemid][$userid])) {
262 :     $this->grades[$itemid][$userid] = new grade_grade();
263 :     $this->grades[$itemid][$userid]->itemid = $itemid;
264 :     $this->grades[$itemid][$userid]->userid = $userid;
265 :     $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 : dservos 1.1 }
269 :     }
270 :     }
271 :    
272 :     /*$this->finalgrades = array();
273 :    
274 :     /// Build finalgrades array and filliter out unwanted grades.
275 :     foreach ($this->gtree->items as $id=>$item) {
276 :     if(($item->gradetype == GRADE_TYPE_SCALE && ($this->get_pref('stats', 'showscaleitems') || is_null($this->get_pref('stats', 'showscaleitems'))))
277 :     || ($item->gradetype == GRADE_TYPE_VALUE && ($this->get_pref('stats', 'showvalueitems') || is_null($this->get_pref('stats', 'showvalueitems'))))) {
278 :     $this->finalgrades[$id] = array();
279 :     $i = 0;
280 :    
281 :     if(isset($this->grades[$id]) && !is_null($this->grades[$id])) {
282 :     foreach ($this->grades[$id] as $grade) {
283 :     if( (($grade->is_hidden() && ($this->get_pref('stats', 'usehidden') || is_null($this->get_pref('stats', 'usehidden')))) || !$grade->is_hidden())
284 :     && (($grade->is_locked() && ($this->get_pref('stats', 'uselocked') || is_null($this->get_pref('stats', 'uselocked')))) || !$grade->is_locked())) {
285 :     if($this->get_pref('stats', 'incompleasmin') && is_null($grade->finalgrade)) {
286 :     $this->finalgrades[$id][$i] = $item->grademin;
287 :     $i++;
288 :     } elseif(!is_null($grade->finalgrade)) {
289 :     $this->finalgrades[$id][$i] = $grade->finalgrade;
290 :     $i++;
291 :     }
292 :     }
293 :     }
294 :     }
295 :     }
296 :     }*/
297 :     }
298 :    
299 : dservos 1.2 /**
300 :     * TODO: have this make the data depending on what kind of visulsation
301 :     * is needed rather then a singal visulasztion witch is currently hardcoded.
302 :     */
303 : dservos 1.3 public function report_data($all = false) {
304 :     if($all) {
305 :     $visuals = $this->get_visualizations();
306 :    
307 :     foreach($visuals as $key=>$visual) {
308 :     $this->visdata[$key] = $visual->report_data($this);
309 : dservos 1.1 }
310 : dservos 1.3 } else {
311 :     $visual = $this->get_visualization($this->visid);
312 :     $this->visdata[$this->visid] = $visual->report_data($this);
313 : dservos 1.1 }
314 : dservos 1.3 }
315 : dservos 1.1
316 : dservos 1.3 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 : dservos 1.1
332 : dservos 1.3 foreach($flashvars as $key=>$val) {
333 :     $this->flashvarshtml .= $key. '=' . addslashes(urlencode(strip_tags($val))) . '&';
334 : dservos 1.1 }
335 : dservos 1.3 $this->flashvarshtml = substr($this->flashvarshtml, 0, strlen($this->flashvarshtml) - 1);
336 : dservos 1.1 }
337 :    
338 : dservos 1.2 /**
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 : dservos 1.3 public function adapt_html($printerversion = false, $return = false) {
343 :     global $CFG;
344 :    
345 :     $flashvarshtml = $this->flashvarshtml;
346 :     $visual = $this->get_visualization($this->visid);
347 :    
348 :     ob_start();
349 :     require($CFG->dirroot.'/grade/report/visual/flex.php');
350 :     $this->html = ob_get_clean();
351 :    
352 :     if($return) {
353 :     return $this->html;
354 :     } else {
355 :     echo $this->html;
356 :     }
357 :     }
358 :    
359 :     public function adapt_data($return = false) {
360 :     if($return) {
361 :     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 : dservos 1.1
372 : dservos 1.3 foreach($visuals as $key=>$visual) {
373 :     $visualmenu[$key] = format_string($visual->name);
374 :     }
375 :    
376 :     if(count($visualmenu) > 1) {
377 :     $selectorhtml = popup_form($this->pbarurl . '&amp;visid=', $visualmenu, 'selectvisual', $this->visid, '', '', '', true, 'self', $vislabel);
378 :     } else {
379 :     $selectorhtml = '';
380 :     }
381 :    
382 :     if($return) {
383 :     return $selectorhtml;
384 :     } else {
385 :     echo $selectorhtml;
386 :     }
387 : dservos 1.1 }
388 :    
389 : dservos 1.2 /**
390 :     * Returns the data for a visulazation in json format.
391 :     * TODO: Use moodle's json encoding functions rather then phps.
392 :     * @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.
394 :     */
395 : dservos 1.3 private function get_json($return = true) {
396 : dservos 1.1 if ($return) {
397 : dservos 1.3 return json_encode($this->visdata[$this->visid]);
398 : dservos 1.1 } else {
399 : dservos 1.3 echo json_encode($this->visdata[$this->visid]);
400 : dservos 1.1 }
401 :     }
402 :    
403 : dservos 1.2
404 :     /**
405 :     * Returns the data for a visulasation in tab format.
406 :     * @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.
408 :     */
409 : dservos 1.3 private function get_tab($return = true) {
410 : dservos 1.1 if ($return) {
411 : dservos 1.3 return $this->tab_encode($this->visdata[$this->visid]);
412 : dservos 1.1 } else {
413 : dservos 1.3 echo $this->tab_encode($this->visdata[$this->visid]);
414 : dservos 1.1 }
415 :     }
416 :    
417 : dservos 1.2 /**
418 :     * Encodes an array to a string using the tab format.
419 :     *@param array $data the array to encode.
420 :     *@return string a string encoded in tab format based on the given array.
421 :     */
422 : dservos 1.3 public static function tab_encode(array $data, $useindexes = false) {
423 : dservos 1.1 $outstring = '';
424 :    
425 : dservos 1.3 foreach($data as $key=>$row) {
426 :     if(is_array($row)) {
427 :     if($useindexes) {
428 :     $outstring .= $key . "\t";
429 :     }
430 :    
431 :     foreach($row as $col) {
432 :     $outstring .= $col . "\t";
433 :     }
434 :    
435 :     $outstring[strlen($outstring) - 1] = "\n";
436 :     } else {
437 :     if($useindexes) {
438 :     $outstring .= $key . "\t" . $row . "\n";
439 :     } else {
440 :     $outstring .= $row . "\n";
441 :     }
442 : dservos 1.1 }
443 :     }
444 :    
445 :     return $outstring;
446 :     }
447 :     }
448 :     ?>

Moodle CVS Admin
ViewVC Help
Powered by ViewVC 1.0.7