[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.1 - (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 :     public function report_data() {
220 :     $this->visdata['test2'] = array();
221 :     $this->visdata['test2']['header'] = array();
222 :     $this->visdata['test2']['header']['student'] = 'student';
223 :     $this->visdata['test2']['header']['grade'] = 'grade';
224 :     $this->visdata['test2']['header']['item'] = 'item';
225 :     $this->visdata['test2']['header']['group'] = 'group';
226 :    
227 :     foreach($this->grades as $itemkey=>$itemgrades) {
228 :     foreach($itemgrades as $studentkey=>$studentdata) {
229 :     if($studentdata != null && $studentdata->finalgrade != null) {
230 :     foreach(groups_get_user_groups($this->courseid, $studentkey) as $grouping) {
231 :     foreach($grouping as $group) {
232 :     $this->visdata['test2'][$studentkey . '-' . $itemkey . '-' . $group]['student'] = $this->users[$studentkey]->firstname . ' ' . $this->users[$studentkey]->lastname;
233 :     $this->visdata['test2'][$studentkey . '-' . $itemkey . '-' . $group]['grade'] = $studentdata->standardise_score($studentdata->finalgrade, $this->gtree->items[$itemkey]->grademin, $this->gtree->items[$itemkey]->grademax, 0, 100);
234 :     $this->visdata['test2'][$studentkey . '-' . $itemkey . '-' . $group]['item'] = $this->gtree->items[$itemkey]->get_name();
235 :     $this->visdata['test2'][$studentkey . '-' . $itemkey . '-' . $group]['group'] = groups_get_group_name($group);
236 :     }
237 :     }
238 :     }
239 :     }
240 :     }
241 :    
242 :    
243 :     $this->visdata['test'] = array();
244 :     $this->visdata['test']['header'] = array();
245 :     $this->visdata['test']['header']['student'] = 'student';
246 :    
247 :     foreach($this->gtree->items as $itemid=>$item) {
248 :     $this->visdata['test']['header']['i' . $itemid] = $this->gtree->items[$itemid]->get_name();
249 :     }
250 :    
251 :     foreach($this->users as $studentid=>$student) {
252 :     $this->visdata['test']['s' . $studentid]['student'] = $student->firstname . ' ' . $student->lastname;
253 :     }
254 :    
255 :     foreach($this->grades as $itemkey=>$itemgrades) {
256 :     foreach($itemgrades as $studentkey=>$studentdata) {
257 :     $this->visdata['test']['s' . $studentkey]['i' . $itemkey] = $studentdata->finalgrade;
258 :     }
259 :     }
260 :     }
261 :    
262 :     public function adapt_data($printerversion = false) {
263 :    
264 :     }
265 :    
266 :     public function get_json($return = true) {
267 :     if ($return) {
268 :     return json_encode($this->visdata['test']);
269 :     } else {
270 :     echo json_encode($this->visdata['test']);
271 :     }
272 :     }
273 :    
274 :     public function get_tab($return = true) {
275 :     if ($return) {
276 :     return $this->tab_encode($this->visdata['test2']);
277 :     } else {
278 :     echo $this->tab_encode($this->visdata['test2']);
279 :     }
280 :     }
281 :    
282 :     private function tab_encode(array $data) {
283 :     $outstring = '';
284 :    
285 :     foreach($data as $row) {
286 :     foreach($row as $col) {
287 :     $outstring .= $col . "\t";
288 :     }
289 :    
290 :     $outstring[strlen($outstring) - 1] = "\n";
291 :     }
292 :    
293 :     return $outstring;
294 :     }
295 :     }
296 :     ?>

Moodle CVS Admin
ViewVC Help
Powered by ViewVC 1.0.7