[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.2 - (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 :     class grade_report_visual extends grade_report {
30 :     /**
31 :     * Capability to view hidden items.
32 :     * TODO: Add a check for hidden items and grades.
33 :     * @var bool $canviewhidden
34 :     */
35 :     private $canviewhidden;
36 :    
37 :    
38 :     /**
39 :     * Grade objects of users in the course
40 :     * @var array $grades
41 :     */
42 :     private $grades = array();
43 :    
44 :     private $visdata = array();
45 :    
46 :    
47 :     /**
48 :     * The html of the report to output.
49 :     * @var string $html
50 :     */
51 :     public $html;
52 :    
53 :    
54 :     /**
55 :     * Constructor. Initialises grade_tree, sets up group, baseurl
56 :     * and pbarurl.
57 :     * @param int $courseid the coures id for the report
58 :     * @param object $gpr grade plugin tracking object
59 :     * @context string $context
60 :     */
61 :     public function __construct($courseid, $gpr, $context) {
62 :     global $CFG;
63 :     parent::__construct($courseid, $gpr, $context, null);
64 :    
65 :     $this->canviewhidden = has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $this->course->id));
66 :    
67 :     /// Set up urls
68 :     $this->baseurl = 'index.php?id=' . $this->courseid;
69 :     $this->pbarurl = 'index.php?id=' . $this->courseid;
70 :    
71 :     /// Build grade tree
72 :     $this->gtree = new grade_tree($this->courseid, false);
73 :    
74 :     /// Set up Groups
75 :     /*if ($this->get_pref('stats', 'showgroups') || is_null($this->get_pref('stats', 'showgroups'))) {
76 :     if(is_callable(array($this, 'setup_groups'))) {
77 :     $this->setup_groups();
78 :     } else {
79 :     $this->local_setup_groups();
80 :     }
81 :     }*/
82 :     }
83 :    
84 :     /// Added to keep grade_report happy
85 :     public function process_data($data){}
86 :     public function process_action($target, $action){}
87 :    
88 :     /**
89 :     * Based on load user function from grader report.
90 :     * Pulls out the userids of the users to be used in the stats.
91 :     * @return array array of user ids to use in stats
92 :     */
93 :     public function load_users() {
94 :     global $CFG, $DB;
95 :    
96 :     if(isset($DB) && !is_null($DB)) {
97 :     $params = array();
98 :     list($usql, $gbr_params) = $DB->get_in_or_equal(explode(',', $this->gradebookroles));
99 :    
100 :     $sql = "SELECT u.id, u.firstname, u.lastname, u.imagealt, u.picture, u.idnumber, u.username
101 :     FROM {user} u
102 :     JOIN {role_assignments} ra ON u.id = ra.userid
103 :     $this->groupsql
104 :     WHERE ra.roleid $usql
105 :     $this->groupwheresql
106 :     AND ra.contextid ".get_related_contexts_string($this->context);
107 :    
108 :     $params = array_merge($gbr_params, $this->groupwheresql_params);
109 :     $this->users = $DB->get_records_sql($sql, $params);
110 :     } else {
111 :     $sql = "SELECT u.id, u.firstname, u.lastname, u.imagealt, u.picture, u.idnumber
112 :     FROM {$CFG->prefix}user u
113 :     JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
114 :     $this->groupsql
115 :     WHERE ra.roleid in ($this->gradebookroles)
116 :     $this->groupwheresql
117 :     AND ra.contextid ".get_related_contexts_string($this->context);
118 :    
119 :     $this->users = get_records_sql($sql);
120 :     }
121 :    
122 :     if (empty($this->users)) {
123 :     $this->userselect = '';
124 :     $this->users = array();
125 :     $this->userselect_params = array();
126 :     } else {
127 :     if(isset($DB) && !is_null($DB)) {
128 :     list($usql, $params) = $DB->get_in_or_equal(array_keys($this->users));
129 :     $this->userselect = "AND g.userid $usql";
130 :     $this->userselect_params = $params;
131 :     }else{
132 :     $this->userselect = 'AND g.userid in ('.implode(',', array_keys($this->users)).')';
133 :     }
134 :     }
135 :    
136 :     return $this->users;
137 :     }
138 :    
139 :     /**
140 :     * Harvest the grades from the data base and build the finalgrades array.
141 :     * Filters out hidden, locked and null grades based on users settings.
142 :     * Partly based on grader reports load_final_grades function.
143 :     */
144 :     public function harvest_data() {
145 :     global $CFG, $DB;
146 :    
147 :     $params = array();
148 :    
149 :     if(isset($DB) && !is_null($DB)) {
150 :     $params = array_merge(array($this->courseid), $this->userselect_params);
151 :    
152 :     /// please note that we must fetch all grade_grades fields if we want to contruct grade_grade object from it!
153 :     $sql = "SELECT g.*
154 :     FROM {grade_items} gi,
155 :     {grade_grades} g
156 :     WHERE g.itemid = gi.id AND gi.courseid = ? {$this->userselect}";
157 :    
158 :     $grades = $DB->get_records_sql($sql, $params);
159 :     } else {
160 :     /// please note that we must fetch all grade_grades fields if we want to contruct grade_grade object from it!
161 :     $sql = "SELECT g.*
162 :     FROM {$CFG->prefix}grade_items gi,
163 :     {$CFG->prefix}grade_grades g
164 :     WHERE g.itemid = gi.id AND gi.courseid = {$this->courseid} {$this->userselect}";
165 :    
166 :     $grades = get_records_sql($sql);
167 :     }
168 :    
169 :     $userids = array_keys($this->users);
170 :    
171 :     if ($grades) {
172 :     foreach ($grades as $graderec) {
173 :     if (in_array($graderec->userid, $userids) and array_key_exists($graderec->itemid, $this->gtree->items)) { // some items may not be present!!
174 :     $this->grades[$graderec->itemid][$graderec->userid] = new grade_grade($graderec, false);
175 :     $this->grades[$graderec->itemid][$graderec->userid]->grade_item =& $this->gtree->items[$graderec->itemid]; // db caching
176 :     }
177 :     }
178 :     }
179 :    
180 :     /// prefil grades that do not exist yet
181 :     foreach ($userids as $userid) {
182 :     foreach ($this->gtree->items as $itemid=>$unused) {
183 :     if (!isset($this->grades[$itemid][$userid])) {
184 :     $this->grades[$itemid][$userid] = new grade_grade();
185 :     $this->grades[$itemid][$userid]->itemid = $itemid;
186 :     $this->grades[$itemid][$userid]->userid = $userid;
187 :     $this->grades[$itemid][$userid]->grade_item =& $this->gtree->items[$itemid]; // db caching
188 :     }
189 :     }
190 :     }
191 :    
192 :     /*$this->finalgrades = array();
193 :    
194 :     /// Build finalgrades array and filliter out unwanted grades.
195 :     foreach ($this->gtree->items as $id=>$item) {
196 :     if(($item->gradetype == GRADE_TYPE_SCALE && ($this->get_pref('stats', 'showscaleitems') || is_null($this->get_pref('stats', 'showscaleitems'))))
197 :     || ($item->gradetype == GRADE_TYPE_VALUE && ($this->get_pref('stats', 'showvalueitems') || is_null($this->get_pref('stats', 'showvalueitems'))))) {
198 :     $this->finalgrades[$id] = array();
199 :     $i = 0;
200 :    
201 :     if(isset($this->grades[$id]) && !is_null($this->grades[$id])) {
202 :     foreach ($this->grades[$id] as $grade) {
203 :     if( (($grade->is_hidden() && ($this->get_pref('stats', 'usehidden') || is_null($this->get_pref('stats', 'usehidden')))) || !$grade->is_hidden())
204 :     && (($grade->is_locked() && ($this->get_pref('stats', 'uselocked') || is_null($this->get_pref('stats', 'uselocked')))) || !$grade->is_locked())) {
205 :     if($this->get_pref('stats', 'incompleasmin') && is_null($grade->finalgrade)) {
206 :     $this->finalgrades[$id][$i] = $item->grademin;
207 :     $i++;
208 :     } elseif(!is_null($grade->finalgrade)) {
209 :     $this->finalgrades[$id][$i] = $grade->finalgrade;
210 :     $i++;
211 :     }
212 :     }
213 :     }
214 :     }
215 :     }
216 :     }*/
217 :     }
218 :    
219 : dservos 1.2 /**
220 :     * TODO: have this make the data depending on what kind of visulsation
221 :     * is needed rather then a singal visulasztion witch is currently hardcoded.
222 :     */
223 : dservos 1.1 public function report_data() {
224 :     $this->visdata['test2'] = array();
225 :     $this->visdata['test2']['header'] = array();
226 :     $this->visdata['test2']['header']['student'] = 'student';
227 :     $this->visdata['test2']['header']['grade'] = 'grade';
228 :     $this->visdata['test2']['header']['item'] = 'item';
229 :     $this->visdata['test2']['header']['group'] = 'group';
230 :    
231 :     foreach($this->grades as $itemkey=>$itemgrades) {
232 :     foreach($itemgrades as $studentkey=>$studentdata) {
233 :     if($studentdata != null && $studentdata->finalgrade != null) {
234 :     foreach(groups_get_user_groups($this->courseid, $studentkey) as $grouping) {
235 :     foreach($grouping as $group) {
236 :     $this->visdata['test2'][$studentkey . '-' . $itemkey . '-' . $group]['student'] = $this->users[$studentkey]->firstname . ' ' . $this->users[$studentkey]->lastname;
237 :     $this->visdata['test2'][$studentkey . '-' . $itemkey . '-' . $group]['grade'] = $studentdata->standardise_score($studentdata->finalgrade, $this->gtree->items[$itemkey]->grademin, $this->gtree->items[$itemkey]->grademax, 0, 100);
238 :     $this->visdata['test2'][$studentkey . '-' . $itemkey . '-' . $group]['item'] = $this->gtree->items[$itemkey]->get_name();
239 :     $this->visdata['test2'][$studentkey . '-' . $itemkey . '-' . $group]['group'] = groups_get_group_name($group);
240 :     }
241 :     }
242 :     }
243 :     }
244 :     }
245 :    
246 :    
247 :     $this->visdata['test'] = array();
248 :     $this->visdata['test']['header'] = array();
249 :     $this->visdata['test']['header']['student'] = 'student';
250 :    
251 :     foreach($this->gtree->items as $itemid=>$item) {
252 :     $this->visdata['test']['header']['i' . $itemid] = $this->gtree->items[$itemid]->get_name();
253 :     }
254 :    
255 :     foreach($this->users as $studentid=>$student) {
256 :     $this->visdata['test']['s' . $studentid]['student'] = $student->firstname . ' ' . $student->lastname;
257 :     }
258 :    
259 :     foreach($this->grades as $itemkey=>$itemgrades) {
260 :     foreach($itemgrades as $studentkey=>$studentdata) {
261 :     $this->visdata['test']['s' . $studentkey]['i' . $itemkey] = $studentdata->finalgrade;
262 :     }
263 :     }
264 :     }
265 :    
266 : dservos 1.2 /**
267 :     *TODO: Have this call on flex.php to generate html for the report.,
268 :     *rather then having it hardcoded as it is now.
269 :     */
270 : dservos 1.1 public function adapt_data($printerversion = false) {
271 :    
272 :     }
273 :    
274 : dservos 1.2 /**
275 :     * Returns the data for a visulazation in json format.
276 :     * TODO: Use moodle's json encoding functions rather then phps.
277 :     * @param boolean $return if true return a string, other wise echo the data.
278 :     * @return string If return is set to true, returns a string of the data in json format.
279 :     */
280 : dservos 1.1 public function get_json($return = true) {
281 :     if ($return) {
282 :     return json_encode($this->visdata['test']);
283 :     } else {
284 :     echo json_encode($this->visdata['test']);
285 :     }
286 :     }
287 :    
288 : dservos 1.2
289 :     /**
290 :     * Returns the data for a visulasation in tab format.
291 :     * @param boolean $return if true return a string, other wise echo the data.
292 :     * @return string If return is set to true, returns a string of the data in tab format.
293 :     */
294 : dservos 1.1 public function get_tab($return = true) {
295 :     if ($return) {
296 :     return $this->tab_encode($this->visdata['test2']);
297 :     } else {
298 :     echo $this->tab_encode($this->visdata['test2']);
299 :     }
300 :     }
301 :    
302 : dservos 1.2 /**
303 :     * Encodes an array to a string using the tab format.
304 :     *@param array $data the array to encode.
305 :     *@return string a string encoded in tab format based on the given array.
306 :     */
307 : dservos 1.1 private function tab_encode(array $data) {
308 :     $outstring = '';
309 :    
310 :     foreach($data as $row) {
311 :     foreach($row as $col) {
312 :     $outstring .= $col . "\t";
313 :     }
314 :    
315 :     $outstring[strlen($outstring) - 1] = "\n";
316 :     }
317 :    
318 :     return $outstring;
319 :     }
320 :     }
321 :     ?>

Moodle CVS Admin
ViewVC Help
Powered by ViewVC 1.0.7