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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (view) (download)

1 : dservos 1.1 <?php
2 : dservos 1.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 : dservos 1.1
25 :     /**
26 :     * File in which the grade_report_stats class is defined.
27 : dservos 1.2 * @package gradebook
28 : dservos 1.1 */
29 :    
30 :     require_once($CFG->dirroot . '/grade/report/lib.php');
31 :     require_once($CFG->libdir.'/tablelib.php');
32 : dservos 1.2
33 :     foreach (glob($CFG->dirroot . '/grade/report/stats/statistics/stat_*.php') as $filename) {
34 :     require_once($filename);
35 :     }
36 : dservos 1.1
37 :     /**
38 :     * Class providing the API for the stats report, including harvesters,
39 :     * reports, and adaptor methods for turing grades in to statistics.
40 :     * @uses grade_report
41 :     * @package gradebook
42 :     */
43 :     class grade_report_stats extends grade_report {
44 :     /**
45 :     * Capability to view hidden items.
46 :     * TODO: Add a check for hidden items and grades.
47 : dservos 1.2 * @var bool $canviewhidden
48 : dservos 1.1 */
49 :     private $canviewhidden;
50 :    
51 :     /**
52 :     * Categories to be displayed based on report setting.
53 :     * @var array $collapsed
54 :     */
55 : dservos 1.2 private $collapsed = array();
56 : dservos 1.1
57 :     /**
58 :     * Grade objects of users in the course
59 :     * @var array $grades
60 :     */
61 : dservos 1.2 private $grades = array();
62 : dservos 1.1
63 :     /**
64 :     * Array of users final grades affter filtered based on
65 :     * settings.
66 :     * @var array $finalgrades
67 :     */
68 : dservos 1.2 private $finalgrades = array();
69 : dservos 1.1
70 :     /**
71 :     * The value returned from each statistic.
72 :     * @var array $reportedstats
73 :     */
74 : dservos 1.2 private $reportedstats = array();
75 : dservos 1.1
76 :     /**
77 :     * The html of the report to output.
78 :     * @var string $html
79 :     */
80 :     public $html;
81 :    
82 :     /**
83 :     * The table class used to make the html of the report.
84 :     * @var object $table
85 :     */
86 :     private $table;
87 : dservos 1.2
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 : dservos 1.1
95 :     /**
96 :     * Constructor. Initialises grade_tree, sets up group, baseurl
97 :     * and pbarurl.
98 :     * @param int $courseid the coures id for the report
99 :     * @param object $gpr grade plugin tracking object
100 :     * @context string $context
101 :     */
102 :     public function __construct($courseid, $gpr, $context) {
103 :     global $CFG;
104 :     parent::__construct($courseid, $gpr, $context, null);
105 :    
106 :     $this->canviewhidden = has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $this->course->id));
107 :    
108 :     /// Set categories to be displayed.
109 :     if($this->get_pref('stats', 'aggregationview') == GRADE_REPORT_PREFERENCE_DEFAULT || $this->get_pref('stats', 'aggregationview') == GRADE_REPORT_AGGREGATION_VIEW_FULL) {
110 :     $this->collapsed = array('aggregatesonly' => array(), 'gradesonly' => array());
111 :     } elseif($this->get_pref('stats', 'aggregationview') == GRADE_REPORT_AGGREGATION_VIEW_AGGREGATES_ONLY) {
112 :     $this->collapsed = array('aggregatesonly' => array());
113 :     } elseif($this->get_pref('stats', 'aggregationview') == GRADE_REPORT_AGGREGATION_VIEW_GRADES_ONLY) {
114 :     $this->collapsed = array('gradesonly' => array());
115 :     } else {
116 :     $this->collapsed = array('aggregatesonly' => array(), 'gradesonly' => array());
117 :     }
118 :    
119 :     /// Set up urls
120 :     $this->baseurl = 'index.php?id=' . $this->courseid;
121 :     $this->pbarurl = 'index.php?id=' . $this->courseid;
122 :    
123 :     /// Set the position of the aggregation categorie based on pref
124 :     $switch = $this->get_pref('stats', 'aggregationposition');
125 :     if ($switch == '' && isset($CFG->grade_aggregationposition)) {
126 :     $switch = grade_get_setting($this->courseid, 'aggregationposition', $CFG->grade_aggregationposition);
127 :     }
128 :    
129 :     /// Build grade tree
130 :     $this->gtree = new grade_tree($this->courseid, false, $switch);
131 :    
132 :     /// Set up Groups
133 :     if ($this->get_pref('stats', 'showgroups') || is_null($this->get_pref('stats', 'showgroups'))) {
134 :     if(is_callable(array($this, 'setup_groups'))) {
135 :     $this->setup_groups();
136 :     } else {
137 :     $this->local_setup_groups();
138 :     }
139 :     }
140 : dservos 1.2
141 :     $this->load_stats();
142 : dservos 1.1 }
143 :    
144 : dservos 1.2 /**
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 : dservos 1.1 public function process_data($data){}
187 :     public function process_action($target, $action){}
188 :    
189 : dservos 1.2 /// Tempary fix intill issue MDL-15253 is fixed
190 : dservos 1.1 private function local_setup_groups() {
191 :     global $CFG;
192 :    
193 :     /// find out current groups mode
194 :     $this->group_selector = groups_print_course_menu($this->course, $this->pbarurl, true);
195 :     $this->currentgroup = groups_get_course_group($this->course);
196 :    
197 :     if ($this->currentgroup) {
198 :     $this->groupsql = " LEFT JOIN {groups_members} gm ON gm.userid = u.id ";
199 :     $this->groupwheresql = " AND gm.groupid = ? ";
200 :     $this->groupwheresql_params = array($this->currentgroup);
201 :     }
202 :     }
203 :    
204 :     /**
205 :     * Based on load user function from grader report.
206 :     * Pulls out the userids of the users to be used in the stats.
207 :     * @return array array of user ids to use in stats
208 :     */
209 :     public function load_users() {
210 :     global $CFG, $DB;
211 :    
212 :     if(isset($DB) && !is_null($DB)) {
213 :     $params = array();
214 :     list($usql, $gbr_params) = $DB->get_in_or_equal(explode(',', $this->gradebookroles));
215 :    
216 :     $sql = "SELECT u.id
217 :     FROM {user} u
218 :     JOIN {role_assignments} ra ON u.id = ra.userid
219 :     $this->groupsql
220 :     WHERE ra.roleid $usql
221 :     $this->groupwheresql
222 :     AND ra.contextid ".get_related_contexts_string($this->context);
223 :    
224 :     $params = array_merge($gbr_params, $this->groupwheresql_params);
225 :     $this->users = $DB->get_records_sql($sql, $params);
226 :     } else {
227 :     $sql = "SELECT u.id, u.firstname, u.lastname, u.imagealt, u.picture, u.idnumber
228 :     FROM {$CFG->prefix}user u
229 :     JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
230 :     $this->groupsql
231 :     WHERE ra.roleid in ($this->gradebookroles)
232 :     $this->groupwheresql
233 :     AND ra.contextid ".get_related_contexts_string($this->context);
234 :    
235 :     $this->users = get_records_sql($sql);
236 :     }
237 :    
238 :     if (empty($this->users)) {
239 :     $this->userselect = '';
240 :     $this->users = array();
241 :     } else {
242 :     if(isset($DB) && !is_null($DB)) {
243 :     list($usql, $params) = $DB->get_in_or_equal(array_keys($this->users));
244 :     $this->userselect = "AND g.userid $usql";
245 :     $this->userselect_params = $params;
246 :     }else{
247 :     $this->userselect = 'AND g.userid in ('.implode(',', array_keys($this->users)).')';
248 :     }
249 :     }
250 :    
251 :     return $this->users;
252 :     }
253 :    
254 :     /**
255 :     * Encode an array of stat's data in to a stirng so it can
256 :     * be put in the get part of a url.
257 :     * @param arrray $array array of data to encode
258 :     * @param object $item grade_item to use to fromat stats
259 :     * @param object $stat stats object to use to format stats
260 :     * @return string encoded string
261 :     */
262 : dservos 1.2 private function encode_array(array $array, $item, $stat) {
263 : dservos 1.1 $string = '';
264 :    
265 :     /// Encode each elment by puting a delimiter and base64 encoding it.
266 :     foreach($array as $id=>$data) {
267 :     $string .= addslashes(grade_format_gradevalue($data, $item, true, $stat->displaytype, $stat->decimals)) . '"';
268 :     }
269 :     return strtr(base64_encode($string), '+/=', '-_,');
270 :     }
271 :    
272 :     /**
273 :     * Harvest the grades from the data base and build the finalgrades array.
274 :     * Filters out hidden, locked and null grades based on users settings.
275 :     * Partly based on grader reports load_final_grades function.
276 :     */
277 :     public function harvest_data() {
278 :     global $CFG, $DB;
279 :    
280 : dservos 1.2 $params = array();
281 :    
282 : dservos 1.1 if(isset($DB) && !is_null($DB)) {
283 :     $params = array_merge(array($this->courseid), $this->userselect_params);
284 :    
285 :     /// please note that we must fetch all grade_grades fields if we want to contruct grade_grade object from it!
286 :     $sql = "SELECT g.*
287 :     FROM {grade_items} gi,
288 :     {grade_grades} g
289 :     WHERE g.itemid = gi.id AND gi.courseid = ? {$this->userselect}";
290 :    
291 :     $grades = $DB->get_records_sql($sql, $params);
292 :     } else {
293 :     /// please note that we must fetch all grade_grades fields if we want to contruct grade_grade object from it!
294 :     $sql = "SELECT g.*
295 :     FROM {$CFG->prefix}grade_items gi,
296 :     {$CFG->prefix}grade_grades g
297 :     WHERE g.itemid = gi.id AND gi.courseid = {$this->courseid} {$this->userselect}";
298 :    
299 :     $grades = get_records_sql($sql);
300 :     }
301 :    
302 :     $userids = array_keys($this->users);
303 :    
304 :     if ($grades) {
305 :     foreach ($grades as $graderec) {
306 :     if (in_array($graderec->userid, $userids) and array_key_exists($graderec->itemid, $this->gtree->items)) { // some items may not be present!!
307 :     $this->grades[$graderec->itemid][$graderec->userid] = new grade_grade($graderec, false);
308 :     $this->grades[$graderec->itemid][$graderec->userid]->grade_item =& $this->gtree->items[$graderec->itemid]; // db caching
309 :     }
310 :     }
311 :     }
312 :    
313 :     /// prefil grades that do not exist yet
314 :     foreach ($userids as $userid) {
315 :     foreach ($this->gtree->items as $itemid=>$unused) {
316 :     if (!isset($this->grades[$itemid][$userid])) {
317 :     $this->grades[$itemid][$userid] = new grade_grade();
318 :     $this->grades[$itemid][$userid]->itemid = $itemid;
319 :     $this->grades[$itemid][$userid]->userid = $userid;
320 :     $this->grades[$itemid][$userid]->grade_item =& $this->gtree->items[$itemid]; // db caching
321 :     }
322 :     }
323 :     }
324 :    
325 :     $this->finalgrades = array();
326 :    
327 :     /// Build finalgrades array and filliter out unwanted grades.
328 :     foreach ($this->gtree->items as $id=>$item) {
329 :     if(($item->gradetype == GRADE_TYPE_SCALE && ($this->get_pref('stats', 'showscaleitems') || is_null($this->get_pref('stats', 'showscaleitems'))))
330 :     || ($item->gradetype == GRADE_TYPE_VALUE && ($this->get_pref('stats', 'showvalueitems') || is_null($this->get_pref('stats', 'showvalueitems'))))) {
331 :     $this->finalgrades[$id] = array();
332 :     $i = 0;
333 :    
334 :     if(isset($this->grades[$id]) && !is_null($this->grades[$id])) {
335 :     foreach ($this->grades[$id] as $grade) {
336 :     if( (($grade->is_hidden() && ($this->get_pref('stats', 'usehidden') || is_null($this->get_pref('stats', 'usehidden')))) || !$grade->is_hidden())
337 :     && (($grade->is_locked() && ($this->get_pref('stats', 'uselocked') || is_null($this->get_pref('stats', 'uselocked')))) || !$grade->is_locked())) {
338 :     if($this->get_pref('stats', 'incompleasmin') && is_null($grade->finalgrade)) {
339 :     $this->finalgrades[$id][$i] = $item->grademin;
340 :     $i++;
341 :     } elseif(!is_null($grade->finalgrade)) {
342 :     $this->finalgrades[$id][$i] = $grade->finalgrade;
343 :     $i++;
344 :     }
345 :     }
346 :     }
347 :     }
348 :     }
349 :     }
350 :     }
351 :    
352 :     /**
353 :     * Runs grades for each item threw the report functions
354 :     * of each stats class in $stats and stores the values in
355 :     * reportedstats.
356 :     * TODO: Do not report on hidden items if the user can not see them.
357 :     */
358 :     public function report_data() {
359 :     $this->reportedstats = array();
360 :    
361 : dservos 1.2 foreach($this->stats as $name=>$stat) {
362 : dservos 1.1 if($this->get_pref('stats', $name) || is_null($this->get_pref('stats', $name))) {
363 :     $this->reportedstats[$name] = array();
364 :    
365 :     foreach($this->finalgrades as $itemid=>$item) {
366 :     sort($item);
367 :     if(count($item) > 0) {
368 :     $this->reportedstats[$name][$itemid] = $stat->report_data($item, $this->gtree->items[$itemid]);
369 :     } else {
370 :     $this->reportedstats[$name][$itemid] = null;
371 :     }
372 :     }
373 :     }
374 :     }
375 :     }
376 :    
377 :     /**
378 :     * Take the reported data and adapt it in to HTML to output.
379 :     * HTML is stored in html.
380 :     * TODO: Deal with tables growing to wide.
381 :     * TODO: Make it look nice.
382 :     */
383 :     public function adapt_data() {
384 : dservos 1.2 global $CFG;
385 : dservos 1.1
386 :     /// Set up table arrays
387 :     $tablecolumns = array('statistic');
388 :     $tableheaders = array($this->get_lang_string('statistic', 'gradereport_stats'));
389 :    
390 :     /// Set up range column and number of grades column
391 : dservos 1.2 $ranges = array(format_text('<strong>' . $this->get_lang_string('range', 'gradereport_stats') . '</strong>', FORMAT_HTML));
392 :     $numgrades = array(format_text('<strong>' . $this->get_lang_string('num_grades', 'gradereport_stats') . '</strong>', FORMAT_HTML));
393 : dservos 1.1
394 :     /// Loop threw items and build arrays
395 :     foreach($this->finalgrades as $itemid=>$item) {
396 :     array_push($tablecolumns, $itemid);
397 : dservos 1.2 array_push($tableheaders, format_text($this->gtree->items[$itemid]->get_name(), FORMAT_HTML));
398 :     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, format_text(count($item), FORMAT_HTML));
400 : dservos 1.1 }
401 :    
402 :     /// Set up flexible table
403 :     $this->table = new flexible_table('grade-report-stats-' . $this->courseid);
404 :     $this->table->define_columns($tablecolumns);
405 :     $this->table->define_headers($tableheaders);
406 :     $this->table->define_baseurl($this->baseurl);
407 :     $this->table->collapsible(true);
408 :     $this->table->set_attribute('cellspacing', '1');
409 :     $this->table->set_attribute('id', 'stats-grade');
410 :     $this->table->set_attribute('class', 'grade-report-stats gradestable flexible');
411 :     $this->table->setup();
412 :    
413 :     /// If ranges are being shown add them to the table
414 :     if ($this->get_pref('stats', 'showranges')){
415 :     $this->table->add_data($ranges);
416 :     $this->table->add_separator();
417 :     }
418 :    
419 :     /// Loop threw all the reported data and format it in to cells
420 :     /// If stat retured an array of values display the elements or
421 :     /// make a link to a popup with the data in it.
422 :     foreach($this->reportedstats as $name=>$data) {
423 : dservos 1.2 $row = array(format_text('<strong>' . $this->stats[$name]->name . '</strong>', FORMAT_HTML));
424 : dservos 1.1
425 :     foreach($data as $itemid=>$stat) {
426 :     if(!is_array($stat)) {
427 : dservos 1.2 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 : dservos 1.1 } else {
429 :     $statstring = "";
430 :    
431 :     for($i = 0; $i < 2; $i++) {
432 :     if($i >= count($stat)) {
433 :     break;
434 :     }
435 : dservos 1.2 $statstring .= grade_format_gradevalue($stat[$i], $this->gtree->items[$itemid], true, $this->stats[$name]->displaytype, $this->stats[$name]->decimals) . ', ';
436 : dservos 1.1 }
437 :    
438 :     if($i < count($stat)) {
439 : dservos 1.2 $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 : dservos 1.1 } else {
441 :     $statstring = substr($statstring, 0, strlen($statstring) - 2);
442 :     }
443 :     array_push($row, $statstring);
444 :     }
445 :     }
446 :     $this->table->add_data($row);
447 :     }
448 :    
449 :     /// If the number of grades is being shown add it to the table.
450 :     if ($this->get_pref('stats', 'shownumgrades')){
451 :     $this->table->add_separator();
452 :     $this->table->add_data($numgrades);
453 :     }
454 :    
455 :     /// Build html
456 :     ob_start();
457 :     if($this->currentgroup == 0) {
458 : dservos 1.2 echo format_text('<strong>Group:</strong> All participants', FORMAT_HTML);
459 : dservos 1.1 } else {
460 : dservos 1.2 echo format_text('<strong>Group:</strong> ' . groups_get_group_name($this->currentgroup), FORMAT_HTML);
461 : dservos 1.1 }
462 :     $this->table->print_html();
463 :     $this->html = ob_get_clean();
464 :     }
465 :    
466 :     /**
467 :     * Builds HTML for toggles on top of report.
468 :     * Based on grader report get_toggles_html
469 :     * @return string html code for toggles.
470 :     */
471 :     public function get_toggles_html() {
472 :     global $CFG, $USER;
473 :    
474 :     $html = '<div id="stats-report-toggles" style="vertical-align: text-top; text-align: center;">';
475 :     $html .= $this->print_toggle('numgrades', true);
476 :     $html .= $this->print_toggle('groups', true);
477 :     $html .= $this->print_toggle('ranges', true);
478 :     $html .= '</div>';
479 :    
480 :     return $html;
481 :     }
482 :    
483 :     /**
484 :     * Builds HTML for each individual toggle.
485 :     * Based on grader report print_toggle
486 :     * @param string $type The toggle type.
487 :     * @param bool $return Wheather ro return the HTML or print it.
488 :     */
489 :     private function print_toggle($type, $return=false) {
490 :     global $CFG;
491 :    
492 :     $icons = array('eyecons' => 't/hide.gif',
493 :     'numgrades' => 't/grades.gif',
494 :     'calculations' => 't/calc.gif',
495 :     'locks' => 't/lock.gif',
496 :     'averages' => 't/mean.gif',
497 :     'nooutcomes' => 't/outcomes.gif');
498 :    
499 :     $pref_name = 'grade_report_statsshow' . $type;
500 :    
501 :     if (array_key_exists($pref_name, $CFG)) {
502 :     $show_pref = get_user_preferences($pref_name, $CFG->$pref_name);
503 :     } else {
504 :     $show_pref = get_user_preferences($pref_name);
505 :     }
506 :    
507 :     $strshow = $this->get_lang_string('show' . $type, 'gradereport_stats');
508 :     $strhide = $this->get_lang_string('hide' . $type, 'gradereport_stats');
509 :    
510 :     $show_hide = 'show';
511 :     $toggle_action = 1;
512 :    
513 :     if ($show_pref) {
514 :     $show_hide = 'hide';
515 :     $toggle_action = 0;
516 :     }
517 :    
518 :     if (array_key_exists($type, $icons)) {
519 :     $image_name = $icons[$type];
520 :     } else {
521 :     $image_name = "t/$type.gif";
522 :     }
523 :    
524 :     $string = ${'str' . $show_hide};
525 :    
526 :     $img = '<img src="'.$CFG->pixpath.'/'.$image_name.'" class="iconsmall" alt="'
527 :     .$string.'" title="'.$string.'" />'. "\n";
528 :    
529 :     $retval = $img . '<a href="' . $this->baseurl . "&amp;toggle=$toggle_action&amp;toggle_type=$type\">"
530 : dservos 1.2 . format_text($string, FORMAT_HTML) . '</a> ';
531 : dservos 1.1
532 :     if ($return) {
533 :     return $retval;
534 :     } else {
535 :     echo $retval;
536 :     }
537 :     }
538 :     }
539 :     ?>

Moodle CVS Admin
ViewVC Help
Powered by ViewVC 1.0.7