3af29899 |
1 | <?php //$Id$ |
cbff94ba |
2 | |
8ad36f4c |
3 | /////////////////////////////////////////////////////////////////////////// |
4 | // // |
5 | // NOTICE OF COPYRIGHT // |
6 | // // |
7 | // Moodle - Modular Object-Oriented Dynamic Learning Environment // |
8 | // http://moodle.com // |
9 | // // |
10 | // Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com // |
11 | // // |
12 | // This program is free software; you can redistribute it and/or modify // |
13 | // it under the terms of the GNU General Public License as published by // |
14 | // the Free Software Foundation; either version 2 of the License, or // |
15 | // (at your option) any later version. // |
16 | // // |
17 | // This program is distributed in the hope that it will be useful, // |
18 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // |
19 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
20 | // GNU General Public License for more details: // |
21 | // // |
22 | // http://www.gnu.org/copyleft/gpl.html // |
23 | // // |
24 | /////////////////////////////////////////////////////////////////////////// |
25 | |
7a6b7acf |
26 | require_once $CFG->libdir.'/gradelib.php'; |
27 | |
0f5660f7 |
28 | |
29 | /** |
30 | * This class iterates over all users that are graded in a course. |
31 | * Returns fetailed info about users and their grades. |
32 | */ |
33 | class graded_users_iterator { |
34 | var $course; |
35 | var $grade_items; |
36 | var $groupid; |
37 | var $users_rs; |
38 | var $grades_rs; |
39 | var $gradestack; |
40 | |
41 | /** |
42 | * Constructor |
43 | * @param $coruse object |
44 | * @param array grade_items array of grade items, if not specified only user info returned |
45 | * @param int $groupid iterate only group users if present |
46 | */ |
47 | function graded_users_iterator($course, $grade_items=null, $groupid=0) { |
48 | $this->course = $course; |
49 | $this->grade_items = $grade_items; |
50 | $this->groupid = $groupid; |
51 | |
52 | $this->gradestack = array(); |
53 | } |
54 | |
55 | /** |
56 | * Initialise the iterator |
57 | * @return boolean success |
58 | */ |
59 | function init() { |
60 | global $CFG; |
61 | |
62 | $this->close(); |
63 | |
64 | grade_regrade_final_grades($this->course->id); |
65 | $course_item = grade_item::fetch_course_item($this->course->id); |
66 | if ($course_item->needsupdate) { |
67 | // can not calculate all final grades - sorry |
68 | return false; |
69 | } |
70 | |
71 | if (strpos($CFG->gradebookroles, ',') !== false) { |
72 | $gradebookroles = " = {$CFG->gradebookroles}"; |
73 | } else { |
74 | $gradebookroles = " IN ({$CFG->gradebookroles})"; |
75 | } |
76 | |
77 | $relatedcontexts = get_related_contexts_string(get_context_instance(CONTEXT_COURSE, $this->course->id)); |
78 | |
79 | if ($this->groupid) { |
80 | $groupsql = "INNER JOIN {$CFG->prefix}groups_members gm ON gm.userid = u.id"; |
81 | $groupwheresql = "AND gm.groupid = {$this->groupid}"; |
82 | } else { |
83 | $groupsql = ""; |
84 | $groupwheresql = ""; |
85 | } |
86 | |
87 | $users_sql = "SELECT u.* |
88 | FROM {$CFG->prefix}user u |
89 | INNER JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid |
90 | $groupsql |
91 | WHERE ra.roleid $gradebookroles |
92 | AND ra.contextid $relatedcontexts |
93 | $groupwheresql |
94 | ORDER BY u.id ASC"; |
caffc55a |
95 | $this->users_rs = get_recordset_sql($users_sql); |
0f5660f7 |
96 | |
97 | if (!empty($this->grade_items)) { |
98 | $itemids = array_keys($this->grade_items); |
99 | $itemids = implode(',', $itemids); |
100 | |
3f2b0c8a |
101 | $grades_sql = "SELECT g.* |
0f5660f7 |
102 | FROM {$CFG->prefix}grade_grades g |
0f5660f7 |
103 | INNER JOIN {$CFG->prefix}user u ON g.userid = u.id |
104 | INNER JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid |
105 | $groupsql |
106 | WHERE ra.roleid $gradebookroles |
107 | AND ra.contextid $relatedcontexts |
108 | AND g.itemid IN ($itemids) |
109 | $groupwheresql |
110 | ORDER BY g.userid ASC, g.itemid ASC"; |
caffc55a |
111 | $this->grades_rs = get_recordset_sql($grades_sql); |
0f5660f7 |
112 | } |
113 | |
114 | return true; |
115 | } |
116 | |
117 | /** |
118 | * Returns information about the next user |
119 | * @return mixed array of user info, all grades and feedback or null when no more users found |
120 | */ |
121 | function next_user() { |
03cedd62 |
122 | if (!$this->users_rs) { |
0f5660f7 |
123 | return false; // no users present |
124 | } |
125 | |
caffc55a |
126 | if (!$user = rs_fetch_next_record($this->users_rs)) { |
0f5660f7 |
127 | return false; // no more users |
128 | } |
129 | |
130 | //find the first grade of this user |
131 | $grade_records = array(); |
132 | while (true) { |
133 | if (!$current = $this->_pop()) { |
134 | break; // no more grades |
135 | } |
136 | |
137 | if ($current->userid < $user->id) { |
138 | // this should not happen, could be caused by concurrent updates - skip this record |
139 | continue; |
140 | |
141 | } else if ($current->userid > $user->id) { |
142 | // this user does not have any more grades |
143 | $this->_push($current); |
144 | break; |
145 | } |
146 | |
147 | $grade_records[$current->itemid] = $current; |
148 | } |
149 | |
150 | $grades = array(); |
151 | $feedbacks = array(); |
152 | |
153 | foreach ($this->grade_items as $grade_item) { |
154 | if (array_key_exists($grade_item->id, $grade_records)) { |
155 | $feedbacks[$grade_item->id]->feedback = $grade_records[$grade_item->id]->feedback; |
156 | $feedbacks[$grade_item->id]->feedbackformat = $grade_records[$grade_item->id]->feedbackformat; |
157 | unset($grade_records[$grade_item->id]->feedback); |
158 | unset($grade_records[$grade_item->id]->feedbackformat); |
159 | $grades[$grade_item->id] = new grade_grade($grade_records[$grade_item->id], false); |
160 | } else { |
161 | $feedbacks[$grade_item->id]->feedback = ''; |
162 | $feedbacks[$grade_item->id]->feedbackformat = FORMAT_MOODLE; |
163 | $grades[$grade_item->id] = new grade_grade(array('userid'=>$user->id, 'itemid'=>$grade_item->id), false); |
164 | } |
165 | } |
166 | |
167 | $result = new object(); |
168 | $result->user = $user; |
169 | $result->grades = $grades; |
170 | $result->feedbacks = $feedbacks; |
171 | |
172 | return $result; |
173 | } |
174 | |
175 | /** |
176 | * Close the iterator, do not forget to call this function. |
177 | * @return void |
178 | */ |
179 | function close() { |
caffc55a |
180 | if ($this->users_rs) { |
181 | rs_close($this->users_rs); |
182 | $this->users_rs = null; |
0f5660f7 |
183 | } |
caffc55a |
184 | if ($this->grades_rs) { |
185 | rs_close($this->grades_rs); |
186 | $this->grades_rs = null; |
0f5660f7 |
187 | } |
188 | $this->gradestack = array(); |
189 | } |
190 | |
191 | /** |
192 | * Internal function |
193 | */ |
194 | function _push($grade) { |
195 | array_push($this->gradestack, $grade); |
196 | } |
197 | |
198 | /** |
199 | * Internal function |
200 | */ |
201 | function _pop() { |
202 | if (empty($this->gradestack)) { |
03cedd62 |
203 | if (!$this->grades_rs) { |
0f5660f7 |
204 | return NULL; // no grades present |
205 | } |
206 | |
caffc55a |
207 | if (!$grade = rs_fetch_next_record($this->grades_rs)) { |
0f5660f7 |
208 | return NULL; // no more grades |
209 | } |
210 | |
211 | return $grade; |
212 | } else { |
213 | return array_pop($this->gradestack); |
214 | } |
215 | } |
216 | } |
217 | |
0610812a |
218 | /** |
219 | * Print grading plugin selection popup form. |
220 | * |
221 | * @param int $courseid id of course |
222 | * @param string $active_type type of plugin on current page - import, export, report or edit |
223 | * @param string $active_plugin active plugin type - grader, user, cvs, ... |
224 | * @param boolean $return return as string |
225 | * @return nothing or string if $return true |
226 | */ |
3af29899 |
227 | function print_grade_plugin_selector($courseid, $active_type, $active_plugin, $return=false) { |
cbff94ba |
228 | global $CFG; |
cbff94ba |
229 | |
3af29899 |
230 | $context = get_context_instance(CONTEXT_COURSE, $courseid); |
cbff94ba |
231 | |
3af29899 |
232 | $menu = array(); |
6e2f3121 |
233 | $count = 0; |
3af29899 |
234 | $active = ''; |
cbff94ba |
235 | |
3af29899 |
236 | /// report plugins with its special structure |
237 | if ($reports = get_list_of_plugins('grade/report', 'CVS')) { // Get all installed reports |
238 | foreach ($reports as $key => $plugin) { // Remove ones we can't see |
239 | if (!has_capability('gradereport/'.$plugin.':view', $context)) { |
240 | unset($reports[$key]); |
cbff94ba |
241 | } |
242 | } |
04678d8e |
243 | } |
3af29899 |
244 | $reportnames = array(); |
245 | if (!empty($reports)) { |
246 | foreach ($reports as $plugin) { |
65dd61bd |
247 | $url = 'report/'.$plugin.'/index.php?id='.$courseid; |
3af29899 |
248 | if ($active_type == 'report' and $active_plugin == $plugin ) { |
249 | $active = $url; |
cbff94ba |
250 | } |
6e2f3121 |
251 | $reportnames[$url] = get_string('modulename', 'gradereport_'.$plugin); |
252 | $count++; |
cbff94ba |
253 | } |
3af29899 |
254 | asort($reportnames); |
cbff94ba |
255 | } |
3af29899 |
256 | if (!empty($reportnames)) { |
257 | $menu['reportgroup']='--'.get_string('reportplugins', 'grades'); |
258 | $menu = $menu+$reportnames; |
cbff94ba |
259 | } |
cbff94ba |
260 | |
3af29899 |
261 | /// standard import plugins |
e2008be2 |
262 | if ($imports = get_list_of_plugins('grade/import', 'CVS')) { // Get all installed import plugins |
3af29899 |
263 | foreach ($imports as $key => $plugin) { // Remove ones we can't see |
264 | if (!has_capability('gradeimport/'.$plugin.':view', $context)) { |
265 | unset($imports[$key]); |
cbff94ba |
266 | } |
267 | } |
268 | } |
3af29899 |
269 | $importnames = array(); |
270 | if (!empty($imports)) { |
271 | foreach ($imports as $plugin) { |
272 | $url = 'import/'.$plugin.'/index.php?id='.$courseid; |
65dd61bd |
273 | if ($active_type == 'import' and $active_plugin == $plugin ) { |
3af29899 |
274 | $active = $url; |
275 | } |
6e2f3121 |
276 | $importnames[$url] = get_string('modulename', 'gradeimport_'.$plugin); |
277 | $count++; |
281ffa4a |
278 | } |
3af29899 |
279 | asort($importnames); |
281ffa4a |
280 | } |
3af29899 |
281 | if (!empty($importnames)) { |
282 | $menu['importgroup']='--'.get_string('importplugins', 'grades'); |
283 | $menu = $menu+$importnames; |
281ffa4a |
284 | } |
281ffa4a |
285 | |
3af29899 |
286 | /// standard export plugins |
e2008be2 |
287 | if ($exports = get_list_of_plugins('grade/export', 'CVS')) { // Get all installed export plugins |
3af29899 |
288 | foreach ($exports as $key => $plugin) { // Remove ones we can't see |
289 | if (!has_capability('gradeexport/'.$plugin.':view', $context)) { |
290 | unset($exports[$key]); |
281ffa4a |
291 | } |
292 | } |
cbff94ba |
293 | } |
3af29899 |
294 | $exportnames = array(); |
295 | if (!empty($exports)) { |
296 | foreach ($exports as $plugin) { |
297 | $url = 'export/'.$plugin.'/index.php?id='.$courseid; |
65dd61bd |
298 | if ($active_type == 'export' and $active_plugin == $plugin ) { |
3af29899 |
299 | $active = $url; |
300 | } |
6e2f3121 |
301 | $exportnames[$url] = get_string('modulename', 'gradeexport_'.$plugin); |
302 | $count++; |
281ffa4a |
303 | } |
3af29899 |
304 | asort($exportnames); |
cbff94ba |
305 | } |
3af29899 |
306 | if (!empty($exportnames)) { |
307 | $menu['exportgroup']='--'.get_string('exportplugins', 'grades'); |
308 | $menu = $menu+$exportnames; |
281ffa4a |
309 | } |
cbff94ba |
310 | |
3af29899 |
311 | /// editing scripts - not real plugins |
78ad5f3f |
312 | if (has_capability('moodle/grade:manage', $context) |
9376f5a6 |
313 | or has_capability('moodle/grade:manageletters', $context) |
04259694 |
314 | or has_capability('moodle/course:managescales', $context) |
315 | or has_capability('moodle/course:update', $context)) { |
3af29899 |
316 | $menu['edit']='--'.get_string('edit'); |
78ad5f3f |
317 | |
318 | if (has_capability('moodle/grade:manage', $context)) { |
319 | $url = 'edit/tree/index.php?id='.$courseid; |
320 | if ($active_type == 'edit' and $active_plugin == 'tree' ) { |
321 | $active = $url; |
322 | } |
323 | $menu[$url] = get_string('edittree', 'grades'); |
6e2f3121 |
324 | $count++; |
78ad5f3f |
325 | } |
326 | |
327 | if (has_capability('moodle/course:managescales', $context)) { |
328 | $url = 'edit/scale/index.php?id='.$courseid; |
329 | if ($active_type == 'edit' and $active_plugin == 'scale' ) { |
330 | $active = $url; |
331 | } |
332 | $menu[$url] = get_string('scales'); |
6e2f3121 |
333 | $count++; |
78ad5f3f |
334 | } |
335 | |
2b0f65e2 |
336 | if (!empty($CFG->enableoutcomes) && (has_capability('moodle/grade:manage', $context) or |
2a598439 |
337 | has_capability('moodle/course:update', $context))) { |
338 | if (has_capability('moodle/course:update', $context)) { // Default to course assignment |
04259694 |
339 | $url = 'edit/outcome/course.php?id='.$courseid; |
2a598439 |
340 | } else { |
341 | $url = 'edit/outcome/index.php?id='.$courseid; |
04259694 |
342 | } |
78ad5f3f |
343 | if ($active_type == 'edit' and $active_plugin == 'outcome' ) { |
344 | $active = $url; |
345 | } |
346 | $menu[$url] = get_string('outcomes', 'grades'); |
6e2f3121 |
347 | $count++; |
cbff94ba |
348 | } |
284abb09 |
349 | |
9376f5a6 |
350 | if (has_capability('moodle/grade:manage', $context) or has_capability('moodle/grade:manageletters', $context)) { |
284abb09 |
351 | $url = 'edit/letter/index.php?id='.$courseid; |
352 | if ($active_type == 'edit' and $active_plugin == 'letter' ) { |
353 | $active = $url; |
354 | } |
355 | $menu[$url] = get_string('letters', 'grades'); |
6e2f3121 |
356 | $count++; |
284abb09 |
357 | } |
358 | |
e0724506 |
359 | if (has_capability('moodle/grade:manage', $context)) { |
360 | $url = 'edit/settings/index.php?id='.$courseid; |
361 | if ($active_type == 'edit' and $active_plugin == 'settings' ) { |
362 | $active = $url; |
363 | } |
364 | $menu[$url] = get_string('coursesettings', 'grades'); |
6e2f3121 |
365 | $count++; |
e0724506 |
366 | } |
367 | |
281ffa4a |
368 | } |
369 | |
3af29899 |
370 | /// finally print/return the popup form |
6e2f3121 |
371 | if ($count > 1) { |
372 | return popup_form($CFG->wwwroot.'/grade/', $menu, 'choosepluginreport', $active, 'choose', '', '', $return, 'self', get_string('view')); |
373 | } else { |
374 | // only one option - no plugin selector needed |
375 | return ''; |
376 | } |
cbff94ba |
377 | } |
378 | |
0610812a |
379 | /** |
7a6b7acf |
380 | * Utility class used for return tracking when using edit and other forms in grade plugins |
0610812a |
381 | */ |
3af29899 |
382 | class grade_plugin_return { |
383 | var $type; |
384 | var $plugin; |
385 | var $courseid; |
386 | var $userid; |
387 | var $page; |
281ffa4a |
388 | |
0610812a |
389 | /** |
390 | * Constructor |
391 | * @param array $params - associative array with return parameters, if null parameter are taken from _GET or _POST |
392 | */ |
3af29899 |
393 | function grade_plugin_return ($params=null) { |
394 | if (empty($params)) { |
395 | $this->type = optional_param('gpr_type', null, PARAM_SAFEDIR); |
396 | $this->plugin = optional_param('gpr_plugin', null, PARAM_SAFEDIR); |
397 | $this->courseid = optional_param('gpr_courseid', null, PARAM_INT); |
398 | $this->userid = optional_param('gpr_userid', null, PARAM_INT); |
399 | $this->page = optional_param('gpr_page', null, PARAM_INT); |
a983b6ec |
400 | |
a983b6ec |
401 | } else { |
3af29899 |
402 | foreach ($params as $key=>$value) { |
403 | if (array_key_exists($key, $this)) { |
404 | $this->$key = $value; |
405 | } |
cbff94ba |
406 | } |
407 | } |
6cd8c592 |
408 | } |
409 | |
0610812a |
410 | /** |
411 | * Returns return parameters as options array suitable for buttons. |
412 | * @return array options |
413 | */ |
3af29899 |
414 | function get_options() { |
7a6b7acf |
415 | if (empty($this->type)) { |
3af29899 |
416 | return array(); |
865e9a82 |
417 | } |
6cd8c592 |
418 | |
3af29899 |
419 | $params = array(); |
6cd8c592 |
420 | |
7a6b7acf |
421 | if (!empty($this->plugin)) { |
422 | $params['plugin'] = $this->plugin; |
423 | } |
6cd8c592 |
424 | |
3af29899 |
425 | if (!empty($this->courseid)) { |
426 | $params['id'] = $this->courseid; |
6cd8c592 |
427 | } |
9c61ba4d |
428 | |
3af29899 |
429 | if (!empty($this->userid)) { |
430 | $params['userid'] = $this->userid; |
9c61ba4d |
431 | } |
9c61ba4d |
432 | |
3af29899 |
433 | if (!empty($this->page)) { |
434 | $params['page'] = $this->page; |
cbff94ba |
435 | } |
865e9a82 |
436 | |
3af29899 |
437 | return $params; |
cbff94ba |
438 | } |
cbff94ba |
439 | |
0610812a |
440 | /** |
441 | * Returns return url |
442 | * @param string $default default url when params not set |
443 | * @return string url |
444 | */ |
65dd61bd |
445 | function get_return_url($default, $extras=null) { |
3af29899 |
446 | global $CFG; |
cbff94ba |
447 | |
3af29899 |
448 | if (empty($this->type) or empty($this->plugin)) { |
449 | return $default; |
cbff94ba |
450 | } |
451 | |
65dd61bd |
452 | $url = $CFG->wwwroot.'/grade/'.$this->type.'/'.$this->plugin.'/index.php'; |
453 | $glue = '?'; |
cbff94ba |
454 | |
3af29899 |
455 | if (!empty($this->courseid)) { |
456 | $url .= $glue.'id='.$this->courseid; |
457 | $glue = '&'; |
cbff94ba |
458 | } |
cbff94ba |
459 | |
3af29899 |
460 | if (!empty($this->userid)) { |
461 | $url .= $glue.'userid='.$this->userid; |
462 | $glue = '&'; |
cbff94ba |
463 | } |
7e2d7c92 |
464 | |
3af29899 |
465 | if (!empty($this->page)) { |
466 | $url .= $glue.'page='.$this->page; |
65dd61bd |
467 | $glue = '&'; |
468 | } |
469 | |
470 | if (!empty($extras)) { |
471 | foreach($extras as $key=>$value) { |
472 | $url .= $glue.$key.'='.$value; |
473 | $glue = '&'; |
7a6b7acf |
474 | } |
cbff94ba |
475 | } |
cbff94ba |
476 | |
3af29899 |
477 | return $url; |
cbff94ba |
478 | } |
cbff94ba |
479 | |
0610812a |
480 | /** |
481 | * Returns string with hidden return tracking form elements. |
482 | * @return string |
483 | */ |
3af29899 |
484 | function get_form_fields() { |
7a6b7acf |
485 | if (empty($this->type)) { |
3af29899 |
486 | return ''; |
cbff94ba |
487 | } |
cbff94ba |
488 | |
3af29899 |
489 | $result = '<input type="hidden" name="gpr_type" value="'.$this->type.'" />'; |
7a6b7acf |
490 | |
491 | if (!empty($this->plugin)) { |
492 | $result .= '<input type="hidden" name="gpr_plugin" value="'.$this->plugin.'" />'; |
493 | } |
0ca5abd6 |
494 | |
3af29899 |
495 | if (!empty($this->courseid)) { |
496 | $result .= '<input type="hidden" name="gpr_courseid" value="'.$this->courseid.'" />'; |
cbff94ba |
497 | } |
cbff94ba |
498 | |
3af29899 |
499 | if (!empty($this->userid)) { |
500 | $result .= '<input type="hidden" name="gpr_userid" value="'.$this->userid.'" />'; |
cbff94ba |
501 | } |
cbff94ba |
502 | |
3af29899 |
503 | if (!empty($this->page)) { |
504 | $result .= '<input type="hidden" name="gpr_page" value="'.$this->page.'" />'; |
cbff94ba |
505 | } |
506 | } |
cbff94ba |
507 | |
0610812a |
508 | /** |
509 | * Add hidden elements into mform |
510 | * @param object $mform moodle form object |
511 | * @return void |
512 | */ |
3af29899 |
513 | function add_mform_elements(&$mform) { |
7a6b7acf |
514 | if (empty($this->type)) { |
3af29899 |
515 | return; |
cbff94ba |
516 | } |
cbff94ba |
517 | |
3af29899 |
518 | $mform->addElement('hidden', 'gpr_type', $this->type); |
519 | $mform->setType('gpr_type', PARAM_SAFEDIR); |
cbff94ba |
520 | |
7a6b7acf |
521 | if (!empty($this->plugin)) { |
522 | $mform->addElement('hidden', 'gpr_plugin', $this->plugin); |
523 | $mform->setType('gpr_plugin', PARAM_SAFEDIR); |
524 | } |
97033c86 |
525 | |
3af29899 |
526 | if (!empty($this->courseid)) { |
527 | $mform->addElement('hidden', 'gpr_courseid', $this->courseid); |
528 | $mform->setType('gpr_courseid', PARAM_INT); |
cbff94ba |
529 | } |
cbff94ba |
530 | |
3af29899 |
531 | if (!empty($this->userid)) { |
532 | $mform->addElement('hidden', 'gpr_userid', $this->userid); |
533 | $mform->setType('gpr_userid', PARAM_INT); |
cbff94ba |
534 | } |
cbff94ba |
535 | |
3af29899 |
536 | if (!empty($this->page)) { |
537 | $mform->addElement('hidden', 'gpr_page', $this->page); |
538 | $mform->setType('gpr_page', PARAM_INT); |
cbff94ba |
539 | } |
540 | } |
281ffa4a |
541 | |
0610812a |
542 | /** |
543 | * Add return tracking params into url |
544 | * @param string $url |
545 | * @return string $url with erturn tracking params |
546 | */ |
3af29899 |
547 | function add_url_params($url) { |
7a6b7acf |
548 | if (empty($this->type)) { |
3af29899 |
549 | return $url; |
cbff94ba |
550 | } |
5609f9e6 |
551 | |
3af29899 |
552 | if (strpos($url, '?') === false) { |
553 | $url .= '?gpr_type='.$this->type; |
554 | } else { |
555 | $url .= '&gpr_type='.$this->type; |
cbff94ba |
556 | } |
cbff94ba |
557 | |
7a6b7acf |
558 | if (!empty($this->plugin)) { |
559 | $url .= '&gpr_plugin='.$this->plugin; |
560 | } |
cbff94ba |
561 | |
3af29899 |
562 | if (!empty($this->courseid)) { |
563 | $url .= '&gpr_courseid='.$this->courseid; |
cbff94ba |
564 | } |
cbff94ba |
565 | |
3af29899 |
566 | if (!empty($this->userid)) { |
567 | $url .= '&gpr_userid='.$this->userid; |
cbff94ba |
568 | } |
0a8a95c9 |
569 | |
3af29899 |
570 | if (!empty($this->page)) { |
571 | $url .= '&gpr_page='.$this->page; |
0a8a95c9 |
572 | } |
5a412dbf |
573 | |
3af29899 |
574 | return $url; |
5a412dbf |
575 | } |
5a412dbf |
576 | } |
7a6b7acf |
577 | |
826c5f86 |
578 | /** |
579 | * Function central to gradebook for building and printing the navigation (breadcrumb trail). |
580 | * @param string $path The path of the calling script (using __FILE__?) |
581 | * @param string $pagename The language string to use as the last part of the navigation (non-link) |
582 | * @param mixed $id Either a plain integer (assuming the key is 'id') or an array of keys and values (e.g courseid => $courseid, itemid...) |
583 | * @return string |
584 | */ |
585 | function grade_build_nav($path, $pagename=null, $id=null) { |
586 | global $CFG, $COURSE; |
587 | |
588 | $strgrades = get_string('grades', 'grades'); |
589 | |
590 | // Parse the path and build navlinks from its elements |
591 | $dirroot_length = strlen($CFG->dirroot) + 1; // Add 1 for the first slash |
592 | $path = substr($path, $dirroot_length); |
593 | $path = str_replace('\\', '/', $path); |
594 | |
595 | $path_elements = explode('/', $path); |
596 | |
597 | $path_elements_count = count($path_elements); |
598 | |
826c5f86 |
599 | // First link is always 'grade' |
600 | $navlinks = array(); |
601 | $navlinks[] = array('name' => $strgrades, |
602 | 'link' => $CFG->wwwroot.'/grade/index.php?id='.$COURSE->id, |
603 | 'type' => 'misc'); |
604 | |
605 | $link = ''; |
606 | $numberofelements = 3; |
607 | |
608 | // Prepare URL params string |
609 | $id_string = '?'; |
610 | if (!is_null($id)) { |
611 | if (is_array($id)) { |
612 | foreach ($id as $idkey => $idvalue) { |
613 | $id_string .= "$idkey=$idvalue&"; |
614 | } |
615 | } else { |
616 | $id_string .= "id=$id"; |
617 | } |
618 | } |
619 | |
620 | $navlink4 = null; |
621 | |
0f78c4de |
622 | // Remove file extensions from filenames |
623 | foreach ($path_elements as $key => $filename) { |
624 | $path_elements[$key] = str_replace('.php', '', $filename); |
625 | } |
626 | |
826c5f86 |
627 | // Second level links |
628 | switch ($path_elements[1]) { |
629 | case 'edit': // No link |
630 | if ($path_elements[3] != 'index.php') { |
631 | $numberofelements = 4; |
632 | } |
633 | break; |
634 | case 'import': // No link |
635 | break; |
636 | case 'export': // No link |
637 | break; |
638 | case 'report': |
639 | // $id is required for this link. Do not print it if $id isn't given |
640 | if (!is_null($id)) { |
641 | $link = $CFG->wwwroot . '/grade/report/index.php' . $id_string; |
642 | } |
643 | |
644 | if ($path_elements[2] == 'grader') { |
645 | $numberofelements = 4; |
646 | } |
647 | break; |
648 | |
649 | default: |
650 | // If this element isn't among the ones already listed above, it isn't supported, throw an error. |
651 | debugging("grade_build_nav() doesn't support ". $path_elements[1] . " as the second path element after 'grade'."); |
652 | return false; |
653 | } |
654 | |
655 | $navlinks[] = array('name' => get_string($path_elements[1], 'grades'), 'link' => $link, 'type' => 'misc'); |
656 | |
657 | // Third level links |
658 | if (empty($pagename)) { |
659 | $pagename = get_string($path_elements[2], 'grades'); |
660 | } |
661 | |
662 | switch ($numberofelements) { |
663 | case 3: |
664 | $navlinks[] = array('name' => $pagename, 'link' => $link, 'type' => 'misc'); |
665 | break; |
666 | case 4: |
667 | |
668 | if ($path_elements[2] == 'grader' AND $path_elements[3] != 'index.php') { |
3cf6a6d5 |
669 | $navlinks[] = array('name' => get_string('modulename', 'gradereport_grader'), |
826c5f86 |
670 | 'link' => "$CFG->wwwroot/grade/report/grader/index.php$id_string", |
671 | 'type' => 'misc'); |
672 | } |
673 | $navlinks[] = array('name' => $pagename, 'link' => '', 'type' => 'misc'); |
674 | break; |
675 | } |
676 | $navigation = build_navigation($navlinks); |
677 | |
678 | return $navigation; |
d4795a07 |
679 | } |
7a6b7acf |
680 | |
66ef0471 |
681 | /** |
682 | * Computes then returns the percentage value of the grade value within the given range. |
683 | * @param float $gradeval |
684 | * @param float $grademin |
685 | * @param float $grademx |
686 | * @return float $percentage |
687 | */ |
688 | function grade_to_percentage($gradeval, $grademin, $grademax) { |
689 | if ($grademin >= $grademax) { |
690 | debugging("The minimum grade ($grademin) was higher than or equal to the maximum grade ($grademax)!!! Cannot proceed with computation."); |
691 | } |
33a34cd4 |
692 | |
693 | // If given gradevalue is lower than grademin, adjust it to grademin |
694 | if ($gradeval < $grademin || empty($gradeval)) { |
695 | $gradeval = $grademin; |
696 | } |
697 | |
66ef0471 |
698 | $offset_value = $gradeval - $grademin; |
699 | $offset_max = $grademax - $grademin; |
700 | $factor = 100 / $offset_max; |
701 | $percentage = $offset_value * $factor; |
702 | return $percentage; |
703 | } |
704 | |
e98871a2 |
705 | /** |
6cc3e350 |
706 | * General structure representing grade items in course |
e98871a2 |
707 | */ |
6cc3e350 |
708 | class grade_structure { |
709 | var $context; |
e98871a2 |
710 | |
6cc3e350 |
711 | var $courseid; |
e98871a2 |
712 | |
713 | /** |
714 | * 1D array of grade items only |
715 | */ |
716 | var $items; |
717 | |
6391ebe7 |
718 | /** |
6cc3e350 |
719 | * Returns icon of element |
720 | * @param object $element |
721 | * @param bool $spacerifnone return spacer if no icon found |
722 | * @return string icon or spacer |
6391ebe7 |
723 | */ |
6cc3e350 |
724 | function get_element_icon(&$element, $spacerifnone=false) { |
725 | global $CFG; |
726 | |
727 | switch ($element['type']) { |
728 | case 'item': |
729 | case 'courseitem': |
730 | case 'categoryitem': |
731 | if ($element['object']->is_calculated()) { |
732 | return '<img src="'.$CFG->pixpath.'/i/calc.gif" class="icon itemicon" alt="'.get_string('calculation', 'grades').'"/>'; |
733 | |
734 | } else if (($element['object']->is_course_item() or $element['object']->is_category_item()) |
735 | and ($element['object']->gradetype == GRADE_TYPE_SCALE or $element['object']->gradetype == GRADE_TYPE_VALUE)) { |
736 | if ($category = $element['object']->get_item_category()) { |
737 | switch ($category->aggregation) { |
738 | case GRADE_AGGREGATE_MEAN: |
739 | case GRADE_AGGREGATE_MEDIAN: |
740 | case GRADE_AGGREGATE_WEIGHTED_MEAN: |
1426edac |
741 | case GRADE_AGGREGATE_WEIGHTED_MEAN2: |
6cc3e350 |
742 | case GRADE_AGGREGATE_EXTRACREDIT_MEAN: |
743 | return '<img src="'.$CFG->pixpath.'/i/agg_mean.gif" class="icon itemicon" alt="'.get_string('aggregation', 'grades').'"/>'; |
0758a08e |
744 | case GRADE_AGGREGATE_SUM: |
745 | return '<img src="'.$CFG->pixpath.'/i/agg_sum.gif" class="icon itemicon" alt="'.get_string('aggregation', 'grades').'"/>'; |
6cc3e350 |
746 | } |
747 | } |
748 | |
749 | } else if ($element['object']->itemtype == 'mod') { |
750 | return '<img src="'.$CFG->modpixpath.'/'.$element['object']->itemmodule.'/icon.gif" class="icon itemicon" alt="' |
751 | .get_string('modulename', $element['object']->itemmodule).'"/>'; |
752 | |
753 | } else if ($element['object']->itemtype == 'manual') { |
754 | if ($element['object']->is_outcome_item()) { |
755 | return '<img src="'.$CFG->pixpath.'/i/outcomes.gif" class="icon itemicon" alt="'.get_string('outcome', 'grades').'"/>'; |
756 | } else { |
757 | //TODO: add better icon |
758 | return '<img src="'.$CFG->pixpath.'/t/edit.gif" class="icon itemicon" alt="'.get_string('manualitem', 'grades').'"/>'; |
759 | } |
760 | } |
761 | break; |
762 | |
763 | case 'category': |
764 | return '<img src="'.$CFG->pixpath.'/f/folder.gif" class="icon itemicon" alt="'.get_string('category', 'grades').'"/>'; |
765 | } |
766 | |
767 | if ($spacerifnone) { |
768 | return '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="icon itemicon" alt=""/>'; |
769 | } else { |
770 | return ''; |
771 | } |
772 | } |
6391ebe7 |
773 | |
e98871a2 |
774 | /** |
6cc3e350 |
775 | * Returns name of element optionally with icon and link |
776 | * @param object $element |
777 | * @param bool $withlinks |
778 | * @param bool $icons |
779 | * @param bool $spacerifnone return spacer if no icon found |
780 | * @return header string |
e98871a2 |
781 | */ |
6cc3e350 |
782 | function get_element_header(&$element, $withlink=false, $icon=true, $spacerifnone=false) { |
783 | global $CFG; |
784 | |
785 | $header = ''; |
786 | |
787 | if ($icon) { |
788 | $header .= $this->get_element_icon($element, $spacerifnone); |
789 | } |
790 | |
791 | $header .= $element['object']->get_name(); |
792 | |
793 | if ($element['type'] != 'item' and $element['type'] != 'categoryitem' and $element['type'] != 'courseitem') { |
794 | return $header; |
795 | } |
796 | |
797 | $itemtype = $element['object']->itemtype; |
798 | $itemmodule = $element['object']->itemmodule; |
799 | $iteminstance = $element['object']->iteminstance; |
800 | |
801 | if ($withlink and $itemtype=='mod' and $iteminstance and $itemmodule) { |
802 | $cm = get_coursemodule_from_instance($itemmodule, $iteminstance, $this->courseid); |
803 | |
804 | $dir = $CFG->dirroot.'/mod/'.$itemmodule; |
805 | |
806 | if (file_exists($dir.'/grade.php')) { |
807 | $url = $CFG->wwwroot.'/mod/'.$itemmodule.'/grade.php?id='.$cm->id; |
808 | } else { |
809 | $url = $CFG->wwwroot.'/mod/'.$itemmodule.'/view.php?id='.$cm->id; |
810 | } |
811 | |
812 | $header = '<a href="'.$url.'">'.$header.'</a>'; |
813 | } |
814 | |
815 | return $header; |
816 | } |
817 | |
818 | /** |
819 | * Returns the grade eid - the grade may not exist yet. |
820 | * @param $grade_grade object |
821 | * @return string eid |
822 | */ |
823 | function get_grade_eid($grade_grade) { |
824 | if (empty($grade_grade->id)) { |
825 | return 'n'.$grade_grade->itemid.'u'.$grade_grade->userid; |
826 | } else { |
827 | return 'g'.$grade_grade->id; |
828 | } |
829 | } |
830 | |
831 | /** |
832 | * Returns the grade_item eid |
833 | * @param $grade_item object |
834 | * @return string eid |
835 | */ |
836 | function get_item_eid($grade_item) { |
837 | return 'i'.$grade_item->id; |
838 | } |
839 | |
840 | /** |
841 | * Return edit icon for give element |
842 | * @param object $element |
843 | * @return string |
844 | */ |
845 | function get_edit_icon($element, $gpr) { |
846 | global $CFG; |
847 | |
848 | if (!has_capability('moodle/grade:manage', $this->context)) { |
849 | if ($element['type'] == 'grade' and has_capability('moodle/grade:edit', $this->context)) { |
850 | // oki - let them override grade |
851 | } else { |
852 | return ''; |
853 | } |
854 | } |
855 | |
856 | static $stredit = null; |
857 | static $strfeedback = null; |
858 | if (is_null($stredit)) { |
859 | $stredit = get_string('edit'); |
860 | $strfeedback = get_string('feedback'); |
861 | } |
862 | |
863 | $object = $element['object']; |
864 | $overlib = ''; |
865 | |
866 | switch ($element['type']) { |
867 | case 'item': |
868 | case 'categoryitem': |
869 | case 'courseitem': |
870 | if (empty($object->outcomeid) || empty($CFG->enableoutcomes)) { |
871 | $url = $CFG->wwwroot.'/grade/edit/tree/item.php?courseid='.$this->courseid.'&id='.$object->id; |
872 | } else { |
873 | $url = $CFG->wwwroot.'/grade/edit/tree/outcomeitem.php?courseid='.$this->courseid.'&id='.$object->id; |
874 | } |
875 | $url = $gpr->add_url_params($url); |
876 | break; |
877 | |
878 | case 'category': |
879 | $url = $CFG->wwwroot.'/grade/edit/tree/category.php?courseid='.$this->courseid.'&id='.$object->id; |
880 | $url = $gpr->add_url_params($url); |
881 | break; |
882 | |
883 | case 'grade': |
884 | if (empty($object->id)) { |
885 | $url = $CFG->wwwroot.'/grade/edit/tree/grade.php?courseid='.$this->courseid.'&itemid='.$object->itemid.'&userid='.$object->userid; |
886 | } else { |
887 | $url = $CFG->wwwroot.'/grade/edit/tree/grade.php?courseid='.$this->courseid.'&id='.$object->id; |
888 | } |
889 | $url = $gpr->add_url_params($url); |
890 | if (!empty($object->feedback)) { |
891 | $feedback = addslashes_js(trim(format_string($object->feedback, $object->feedbackformat))); |
892 | $function = "return overlib('$feedback', BORDER, 0, FGCLASS, 'feedback', " |
893 | ."CAPTIONFONTCLASS, 'caption', CAPTION, '$strfeedback');"; |
894 | $overlib = 'onmouseover="'.s($function).'" onmouseout="return nd();"'; |
895 | } |
896 | break; |
897 | |
898 | default: |
899 | $url = null; |
900 | } |
901 | |
902 | if ($url) { |
903 | return '<a href="'.$url.'"><img '.$overlib.' src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$stredit.'" title="'.$stredit.'"/></a>'; |
904 | |
905 | } else { |
906 | return ''; |
907 | } |
908 | } |
909 | |
910 | /** |
911 | * Return hiding icon for give element |
912 | * @param object $element |
913 | * @return string |
914 | */ |
915 | function get_hiding_icon($element, $gpr) { |
916 | global $CFG; |
917 | |
918 | if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:hide', $this->context)) { |
919 | return ''; |
920 | } |
921 | |
922 | static $strshow = null; |
923 | static $strhide = null; |
924 | if (is_null($strshow)) { |
925 | $strshow = get_string('show'); |
926 | $strhide = get_string('hide'); |
927 | } |
928 | |
929 | if ($element['object']->is_hidden()) { |
930 | $icon = 'show'; |
931 | $tooltip = $strshow; |
932 | |
933 | if ($element['type'] != 'category' and $element['object']->get_hidden() > 1) { // Change the icon and add a tooltip showing the date |
934 | $icon = 'hiddenuntil'; |
935 | $tooltip = get_string('hiddenuntildate', 'grades', userdate($element['object']->get_hidden())); |
936 | } |
937 | |
938 | $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&action=show&sesskey='.sesskey() |
939 | . '&eid='.$element['eid']; |
940 | $url = $gpr->add_url_params($url); |
941 | $action = '<a href="'.$url.'"><img alt="'.$strshow.'" src="'.$CFG->pixpath.'/t/'.$icon.'.gif" class="iconsmall" title="'.$tooltip.'"/></a>'; |
942 | |
943 | } else { |
944 | $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&action=hide&sesskey='.sesskey() |
945 | . '&eid='.$element['eid']; |
946 | $url = $gpr->add_url_params($url); |
947 | $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$strhide.'" title="'.$strhide.'"/></a>'; |
948 | } |
949 | return $action; |
950 | } |
951 | |
952 | /** |
953 | * Return locking icon for give element |
954 | * @param object $element |
955 | * @return string |
956 | */ |
957 | function get_locking_icon($element, $gpr) { |
958 | global $CFG; |
959 | |
960 | static $strunlock = null; |
961 | static $strlock = null; |
962 | if (is_null($strunlock)) { |
963 | $strunlock = get_string('unlock', 'grades'); |
964 | $strlock = get_string('lock', 'grades'); |
965 | } |
966 | |
967 | if ($element['object']->is_locked()) { |
968 | $icon = 'unlock'; |
969 | $tooltip = $strunlock; |
970 | |
971 | if ($element['type'] != 'category' and $element['object']->get_locktime() > 1) { // Change the icon and add a tooltip showing the date |
972 | $icon = 'locktime'; |
973 | $tooltip = get_string('locktimedate', 'grades', userdate($element['object']->get_locktime())); |
974 | } |
975 | |
976 | if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:unlock', $this->context)) { |
977 | return ''; |
978 | } |
979 | $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&action=unlock&sesskey='.sesskey() |
980 | . '&eid='.$element['eid']; |
981 | $url = $gpr->add_url_params($url); |
982 | $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/'.$icon.'.gif" alt="'.$strunlock.'" class="iconsmall" title="'.$tooltip.'"/></a>'; |
983 | |
984 | } else { |
985 | if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:lock', $this->context)) { |
986 | return ''; |
987 | } |
988 | $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&action=lock&sesskey='.sesskey() |
989 | . '&eid='.$element['eid']; |
990 | $url = $gpr->add_url_params($url); |
991 | $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/lock.gif" class="iconsmall" alt="'.$strlock.'" title="' |
992 | . $strlock.'"/></a>'; |
993 | } |
994 | return $action; |
995 | } |
996 | |
997 | /** |
998 | * Return calculation icon for given element |
999 | * @param object $element |
1000 | * @return string |
1001 | */ |
1002 | function get_calculation_icon($element, $gpr) { |
1003 | global $CFG; |
1004 | if (!has_capability('moodle/grade:manage', $this->context)) { |
1005 | return ''; |
1006 | } |
1007 | |
1008 | $calculation_icon = ''; |
1009 | |
1010 | $type = $element['type']; |
1011 | $object = $element['object']; |
1012 | |
1013 | if ($type == 'item' or $type == 'courseitem' or $type == 'categoryitem') { |
1014 | $streditcalculation = get_string('editcalculation', 'grades'); |
1015 | |
1016 | // show calculation icon only when calculation possible |
1017 | if ((!$object->is_external_item() or $object->is_outcome_item()) |
1018 | and ($object->gradetype == GRADE_TYPE_SCALE or $object->gradetype == GRADE_TYPE_VALUE)) { |
1019 | if ($object->is_calculated()) { |
1020 | $icon = 'calc.gif'; |
1021 | } else { |
1022 | $icon = 'calc_off.gif'; |
1023 | } |
1024 | $url = $CFG->wwwroot.'/grade/edit/tree/calculation.php?courseid='.$this->courseid.'&id='.$object->id; |
1025 | $url = $gpr->add_url_params($url); |
1026 | $calculation_icon = '<a href="'. $url.'"><img src="'.$CFG->pixpath.'/t/'.$icon.'" class="iconsmall" alt="' |
1027 | . $streditcalculation.'" title="'.$streditcalculation.'" /></a>'. "\n"; |
1028 | } |
1029 | } |
1030 | |
1031 | return $calculation_icon; |
1032 | } |
1033 | } |
1034 | |
1035 | /** |
1036 | * Flat structure similar to grade tree. |
1037 | */ |
1038 | class grade_seq extends grade_structure { |
1039 | |
1040 | /** |
1041 | * A string of GET URL variables, namely courseid and sesskey, used in most URLs built by this class. |
1042 | * @var string $commonvars |
1043 | */ |
1044 | var $commonvars; |
1045 | |
1046 | /** |
1047 | * 1D array of elements |
1048 | */ |
1049 | var $elements; |
e98871a2 |
1050 | |
1051 | /** |
1052 | * Constructor, retrieves and stores array of all grade_category and grade_item |
1053 | * objects for the given courseid. Full objects are instantiated. Ordering sequence is fixed if needed. |
1054 | * @param int $courseid |
1055 | * @param boolean $category_grade_last category grade item is the last child |
1056 | * @param array $collapsed array of collapsed categories |
1057 | */ |
1058 | function grade_seq($courseid, $category_grade_last=false, $nooutcomes=false) { |
1059 | global $USER, $CFG; |
1060 | |
1061 | $this->courseid = $courseid; |
1062 | $this->commonvars = "&sesskey=$USER->sesskey&id=$this->courseid"; |
1063 | $this->context = get_context_instance(CONTEXT_COURSE, $courseid); |
1064 | |
1065 | // get course grade tree |
1066 | $top_element = grade_category::fetch_course_tree($courseid, true); |
1067 | |
6391ebe7 |
1068 | $this->elements = grade_seq::flatten($top_element, $category_grade_last, $nooutcomes); |
1069 | |
1070 | foreach ($this->elements as $key=>$unused) { |
b89a70ce |
1071 | $this->items[$this->elements[$key]['object']->id] =& $this->elements[$key]['object']; |
6391ebe7 |
1072 | } |
e98871a2 |
1073 | } |
1074 | |
1075 | /** |
1076 | * Static recursive helper - makes the grade_item for category the last children |
1077 | * @static |
1078 | * @param array $element The seed of the recursion |
1079 | * @return void |
1080 | */ |
1081 | function flatten(&$element, $category_grade_last, $nooutcomes) { |
1082 | if (empty($element['children'])) { |
1083 | return array(); |
1084 | } |
1085 | $children = array(); |
1086 | |
1087 | foreach ($element['children'] as $sortorder=>$unused) { |
1088 | if ($nooutcomes and $element['type'] != 'category' and $element['children'][$sortorder]['object']->is_outcome_item()) { |
1089 | continue; |
1090 | } |
1091 | $children[] = $element['children'][$sortorder]; |
1092 | } |
1093 | unset($element['children']); |
1094 | |
1095 | if ($category_grade_last and count($children) > 1) { |
1096 | $cat_item = array_shift($children); |
1097 | array_push($children, $cat_item); |
1098 | } |
1099 | |
1100 | $result = array(); |
1101 | foreach ($children as $child) { |
e0724506 |
1102 | if ($child['type'] == 'category') { |
6391ebe7 |
1103 | $result = $result + grade_seq::flatten($child, $category_grade_last, $nooutcomes); |
e98871a2 |
1104 | } else { |
1105 | $child['eid'] = 'i'.$child['object']->id; |
6391ebe7 |
1106 | $result[$child['object']->id] = $child; |
e98871a2 |
1107 | } |
1108 | } |
1109 | |
1110 | return $result; |
1111 | } |
1112 | |
1113 | /** |
1114 | * Parses the array in search of a given eid and returns a element object with |
1115 | * information about the element it has found. |
1116 | * @param int $eid |
1117 | * @return object element |
1118 | */ |
1119 | function locate_element($eid) { |
1120 | // it is a grade - construct a new object |
1121 | if (strpos($eid, 'n') === 0) { |
1122 | if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) { |
1123 | return null; |
1124 | } |
1125 | |
1126 | $itemid = $matches[1]; |
1127 | $userid = $matches[2]; |
1128 | |
1129 | //extra security check - the grade item must be in this tree |
1130 | if (!$item_el = $this->locate_element('i'.$itemid)) { |
1131 | return null; |
1132 | } |
1133 | |
1134 | // $gradea->id may be null - means does not exist yet |
1135 | $grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid)); |
1136 | |
1137 | $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods! |
1138 | return array('eid'=>'n'.$itemid.'u'.$userid,'object'=>$grade, 'type'=>'grade'); |
1139 | |
1140 | } else if (strpos($eid, 'g') === 0) { |
1141 | $id = (int)substr($eid, 1); |
1142 | if (!$grade = grade_grade::fetch(array('id'=>$id))) { |
1143 | return null; |
1144 | } |
1145 | //extra security check - the grade item must be in this tree |
1146 | if (!$item_el = $this->locate_element('i'.$grade->itemid)) { |
1147 | return null; |
1148 | } |
1149 | $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods! |
1150 | return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade'); |
1151 | } |
1152 | |
1153 | // it is a category or item |
6391ebe7 |
1154 | foreach ($this->elements as $element) { |
6cc3e350 |
1155 | if ($element['eid'] == $eid) { |
1156 | return $element; |
1157 | } |
e98871a2 |
1158 | } |
6cc3e350 |
1159 | |
1160 | return null; |
e98871a2 |
1161 | } |
e98871a2 |
1162 | } |
1163 | |
7a6b7acf |
1164 | /** |
1165 | * This class represents a complete tree of categories, grade_items and final grades, |
1166 | * organises as an array primarily, but which can also be converted to other formats. |
1167 | * It has simple method calls with complex implementations, allowing for easy insertion, |
1168 | * deletion and moving of items and categories within the tree. |
1169 | */ |
6cc3e350 |
1170 | class grade_tree extends grade_structure { |
7a6b7acf |
1171 | |
1172 | /** |
1173 | * The basic representation of the tree as a hierarchical, 3-tiered array. |
1174 | * @var object $top_element |
1175 | */ |
1176 | var $top_element; |
1177 | |
1178 | /** |
1179 | * A string of GET URL variables, namely courseid and sesskey, used in most URLs built by this class. |
1180 | * @var string $commonvars |
1181 | */ |
1182 | var $commonvars; |
1183 | |
1184 | /** |
1185 | * 2D array of grade items and categories |
1186 | */ |
1187 | var $levels; |
1188 | |
b89a70ce |
1189 | /** |
1190 | * Grade items |
1191 | */ |
1192 | var $items; |
1193 | |
7a6b7acf |
1194 | /** |
1195 | * Constructor, retrieves and stores a hierarchical array of all grade_category and grade_item |
e98871a2 |
1196 | * objects for the given courseid. Full objects are instantiated. Ordering sequence is fixed if needed. |
7a6b7acf |
1197 | * @param int $courseid |
1198 | * @param boolean $fillers include fillers and colspans, make the levels var "rectangular" |
1199 | * @param boolean $category_grade_last category grade item is the last child |
4faf5f99 |
1200 | * @param array $collapsed array of collapsed categories |
7a6b7acf |
1201 | */ |
aea4df41 |
1202 | function grade_tree($courseid, $fillers=true, $category_grade_last=false, $collapsed=null, $nooutcomes=false) { |
7a6b7acf |
1203 | global $USER, $CFG; |
1204 | |
1205 | $this->courseid = $courseid; |
1206 | $this->commonvars = "&sesskey=$USER->sesskey&id=$this->courseid"; |
1207 | $this->levels = array(); |
2cc773f5 |
1208 | $this->context = get_context_instance(CONTEXT_COURSE, $courseid); |
7a6b7acf |
1209 | |
1210 | // get course grade tree |
1211 | $this->top_element = grade_category::fetch_course_tree($courseid, true); |
1212 | |
4faf5f99 |
1213 | // collapse the categories if requested |
1214 | if (!empty($collapsed)) { |
1215 | grade_tree::category_collapse($this->top_element, $collapsed); |
1216 | } |
1217 | |
aea4df41 |
1218 | // no otucomes if requested |
1219 | if (!empty($nooutcomes)) { |
1220 | grade_tree::no_outcomes($this->top_element); |
1221 | } |
1222 | |
4faf5f99 |
1223 | // move category item to last position in category |
7a6b7acf |
1224 | if ($category_grade_last) { |
1225 | grade_tree::category_grade_last($this->top_element); |
1226 | } |
1227 | |
1228 | if ($fillers) { |
1229 | // inject fake categories == fillers |
1230 | grade_tree::inject_fillers($this->top_element, 0); |
1231 | // add colspans to categories and fillers |
1232 | grade_tree::inject_colspans($this->top_element); |
1233 | } |
1234 | |
1235 | grade_tree::fill_levels($this->levels, $this->top_element, 0); |
d297269d |
1236 | |
7a6b7acf |
1237 | } |
1238 | |
4faf5f99 |
1239 | /** |
1240 | * Static recursive helper - removes items from collapsed categories |
1241 | * @static |
1242 | * @param array $element The seed of the recursion |
1243 | * @param array $collapsed array of collapsed categories |
1244 | * @return void |
1245 | */ |
1246 | function category_collapse(&$element, $collapsed) { |
1247 | if ($element['type'] != 'category') { |
1248 | return; |
1249 | } |
1250 | if (empty($element['children']) or count($element['children']) < 2) { |
1251 | return; |
1252 | } |
1253 | |
384960dd |
1254 | if (in_array($element['object']->id, $collapsed['aggregatesonly'])) { |
4faf5f99 |
1255 | $category_item = reset($element['children']); //keep only category item |
1256 | $element['children'] = array(key($element['children'])=>$category_item); |
1257 | |
1258 | } else { |
384960dd |
1259 | if (in_array($element['object']->id, $collapsed['gradesonly'])) { // Remove category item |
1260 | reset($element['children']); |
1261 | $first_key = key($element['children']); |
1262 | unset($element['children'][$first_key]); |
1263 | } |
1264 | foreach ($element['children'] as $sortorder=>$child) { // Recurse through the element's children |
4faf5f99 |
1265 | grade_tree::category_collapse($element['children'][$sortorder], $collapsed); |
1266 | } |
1267 | } |
1268 | } |
7a6b7acf |
1269 | |
aea4df41 |
1270 | /** |
1271 | * Static recursive helper - removes all outcomes |
1272 | * @static |
1273 | * @param array $element The seed of the recursion |
1274 | * @return void |
1275 | */ |
1276 | function no_outcomes(&$element) { |
1277 | if ($element['type'] != 'category') { |
1278 | return; |
1279 | } |
1280 | foreach ($element['children'] as $sortorder=>$child) { |
1281 | if ($element['children'][$sortorder]['type'] == 'item' |
1282 | and $element['children'][$sortorder]['object']->is_outcome_item()) { |
1283 | unset($element['children'][$sortorder]); |
1284 | |
d4795a07 |
1285 | } else if ($element['children'][$sortorder]['type'] == 'category') { |
aea4df41 |
1286 | grade_tree::no_outcomes($element['children'][$sortorder]); |
1287 | } |
1288 | } |
1289 | } |
1290 | |
7a6b7acf |
1291 | /** |
1292 | * Static recursive helper - makes the grade_item for category the last children |
1293 | * @static |
1294 | * @param array $element The seed of the recursion |
1295 | * @return void |
1296 | */ |
1297 | function category_grade_last(&$element) { |
1298 | if (empty($element['children'])) { |
1299 | return; |
1300 | } |
1301 | if (count($element['children']) < 2) { |
1302 | return; |
1303 | } |
3e0e2436 |
1304 | $first_item = reset($element['children']); |
4a3dfd9a |
1305 | if ($first_item['type'] == 'categoryitem' or $first_item['type'] == 'courseitem') { |
3e0e2436 |
1306 | // the category item might have been already removed |
1307 | $order = key($element['children']); |
1308 | unset($element['children'][$order]); |
1309 | $element['children'][$order] =& $first_item; |
1310 | } |
206f9953 |
1311 | foreach ($element['children'] as $sortorder => $child) { |
7a6b7acf |
1312 | grade_tree::category_grade_last($element['children'][$sortorder]); |
1313 | } |
1314 | } |
1315 | |
1316 | /** |
1317 | * Static recursive helper - fills the levels array, useful when accessing tree elements of one level |
1318 | * @static |
1319 | * @param int $levels |
1320 | * @param array $element The seed of the recursion |
1321 | * @param int $depth |
1322 | * @return void |
1323 | */ |
1324 | function fill_levels(&$levels, &$element, $depth) { |
1325 | if (!array_key_exists($depth, $levels)) { |
1326 | $levels[$depth] = array(); |
1327 | } |
1328 | |
1329 | // prepare unique identifier |
1330 | if ($element['type'] == 'category') { |
1331 | $element['eid'] = 'c'.$element['object']->id; |
1332 | } else if (in_array($element['type'], array('item', 'courseitem', 'categoryitem'))) { |
1333 | $element['eid'] = 'i'.$element['object']->id; |
b89a70ce |
1334 | $this->items[$element['object']->id] =& $element['object']; |
7a6b7acf |
1335 | } |
1336 | |
1337 | $levels[$depth][] =& $element; |
1338 | $depth++; |
1339 | if (empty($element['children'])) { |
1340 | return; |
1341 | } |
1342 | $prev = 0; |
1343 | foreach ($element['children'] as $sortorder=>$child) { |
1344 | grade_tree::fill_levels($levels, $element['children'][$sortorder], $depth); |
1345 | $element['children'][$sortorder]['prev'] = $prev; |
1346 | $element['children'][$sortorder]['next'] = 0; |
1347 | if ($prev) { |
1348 | $element['children'][$prev]['next'] = $sortorder; |
1349 | } |
1350 | $prev = $sortorder; |
1351 | } |
1352 | } |
1353 | |
1354 | /** |
1355 | * Static recursive helper - makes full tree (all leafes are at the same level) |
1356 | */ |
1357 | function inject_fillers(&$element, $depth) { |
1358 | $depth++; |
1359 | |
1360 | if (empty($element['children'])) { |
1361 | return $depth; |
1362 | } |
1363 | $chdepths = array(); |
1364 | $chids = array_keys($element['children']); |
1365 | $last_child = end($chids); |
1366 | $first_child = reset($chids); |
1367 | |
1368 | foreach ($chids as $chid) { |
1369 | $chdepths[$chid] = grade_tree::inject_fillers($element['children'][$chid], $depth); |
1370 | } |
1371 | arsort($chdepths); |
1372 | |
1373 | $maxdepth = reset($chdepths); |
1374 | foreach ($chdepths as $chid=>$chd) { |
1375 | if ($chd == $maxdepth) { |
1376 | continue; |
1377 | } |
1378 | for ($i=0; $i < $maxdepth-$chd; $i++) { |
1379 | if ($chid == $first_child) { |
1380 | $type = 'fillerfirst'; |
1381 | } else if ($chid == $last_child) { |
1382 | $type = 'fillerlast'; |
1383 | } else { |
1384 | $type = 'filler'; |
1385 | } |
1386 | $oldchild =& $element['children'][$chid]; |
1387 | $element['children'][$chid] = array('object'=>'filler', 'type'=>$type, 'eid'=>'', 'depth'=>$element['object']->depth,'children'=>array($oldchild)); |
1388 | } |
1389 | } |
1390 | |
1391 | return $maxdepth; |
1392 | } |
1393 | |
1394 | /** |
1395 | * Static recursive helper - add colspan information into categories |
1396 | */ |
1397 | function inject_colspans(&$element) { |
1398 | if (empty($element['children'])) { |
1399 | return 1; |
1400 | } |
1401 | $count = 0; |
1402 | foreach ($element['children'] as $key=>$child) { |
1403 | $count += grade_tree::inject_colspans($element['children'][$key]); |
1404 | } |
1405 | $element['colspan'] = $count; |
1406 | return $count; |
1407 | } |
1408 | |
1409 | /** |
1410 | * Parses the array in search of a given eid and returns a element object with |
1411 | * information about the element it has found. |
1412 | * @param int $eid |
1413 | * @return object element |
1414 | */ |
1415 | function locate_element($eid) { |
d3c3da1b |
1416 | // it is a grade - construct a new object |
1417 | if (strpos($eid, 'n') === 0) { |
1418 | if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) { |
1419 | return null; |
1420 | } |
1421 | |
1422 | $itemid = $matches[1]; |
1423 | $userid = $matches[2]; |
1424 | |
1425 | //extra security check - the grade item must be in this tree |
1426 | if (!$item_el = $this->locate_element('i'.$itemid)) { |
1427 | return null; |
1428 | } |
1429 | |
1430 | // $gradea->id may be null - means does not exist yet |
1431 | $grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid)); |
1432 | |
1433 | $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods! |
1434 | return array('eid'=>'n'.$itemid.'u'.$userid,'object'=>$grade, 'type'=>'grade'); |
1435 | |
1436 | } else if (strpos($eid, 'g') === 0) { |
7a6b7acf |
1437 | $id = (int)substr($eid, 1); |
1438 | if (!$grade = grade_grade::fetch(array('id'=>$id))) { |
1439 | return null; |
1440 | } |
1441 | //extra security check - the grade item must be in this tree |
1442 | if (!$item_el = $this->locate_element('i'.$grade->itemid)) { |
1443 | return null; |
1444 | } |
1445 | $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods! |
1446 | return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade'); |
1447 | } |
1448 | |
1449 | // it is a category or item |
1450 | foreach ($this->levels as $row) { |
1451 | foreach ($row as $element) { |
1452 | if ($element['type'] == 'filler') { |
1453 | continue; |
1454 | } |
1455 | if ($element['eid'] == $eid) { |
1456 | return $element; |
1457 | } |
1458 | } |
1459 | } |
1460 | |
1461 | return null; |
1462 | } |
7a6b7acf |
1463 | } |
1464 | |
e2008be2 |
1465 | ?> |