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