Commit | Line | Data |
---|---|---|
cf72e2dd | 1 | <?php |
2 | ||
3 | // This file is part of Moodle - http://moodle.org/ | |
4 | // | |
5 | // Moodle is free software: you can redistribute it and/or modify | |
6 | // it under the terms of the GNU General Public License as published by | |
7 | // the Free Software Foundation, either version 3 of the License, or | |
8 | // (at your option) any later version. | |
9 | // | |
10 | // Moodle is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | // GNU General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU General Public License | |
16 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
cf72e2dd | 18 | /** |
19 | * Functions used by gradebook plugins and reports. | |
20 | * | |
0287fe7e | 21 | * @package moodlecore |
cf72e2dd | 22 | * @copyright 2009 Petr Skoda and Nicolas Connault |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | */ | |
8ad36f4c | 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. |
cf72e2dd | 31 | * |
32 | * @author Petr Skoda <skodak@moodle.org> | |
33 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
0f5660f7 | 34 | */ |
35 | class graded_users_iterator { | |
d24832f9 | 36 | public $course; |
37 | public $grade_items; | |
38 | public $groupid; | |
39 | public $users_rs; | |
40 | public $grades_rs; | |
41 | public $gradestack; | |
42 | public $sortfield1; | |
43 | public $sortorder1; | |
44 | public $sortfield2; | |
45 | public $sortorder2; | |
0f5660f7 | 46 | |
47 | /** | |
48 | * Constructor | |
cf72e2dd | 49 | * |
50 | * @param object $course A course object | |
51 | * @param array $grade_items array of grade items, if not specified only user info returned | |
52 | * @param int $groupid iterate only group users if present | |
d08bba83 | 53 | * @param string $sortfield1 The first field of the users table by which the array of users will be sorted |
54 | * @param string $sortorder1 The order in which the first sorting field will be sorted (ASC or DESC) | |
55 | * @param string $sortfield2 The second field of the users table by which the array of users will be sorted | |
56 | * @param string $sortorder2 The order in which the second sorting field will be sorted (ASC or DESC) | |
0f5660f7 | 57 | */ |
cf72e2dd | 58 | public function graded_users_iterator($course, $grade_items=null, $groupid=0, |
59 | $sortfield1='lastname', $sortorder1='ASC', | |
60 | $sortfield2='firstname', $sortorder2='ASC') { | |
0f5660f7 | 61 | $this->course = $course; |
62 | $this->grade_items = $grade_items; | |
63 | $this->groupid = $groupid; | |
d08bba83 | 64 | $this->sortfield1 = $sortfield1; |
65 | $this->sortorder1 = $sortorder1; | |
66 | $this->sortfield2 = $sortfield2; | |
67 | $this->sortorder2 = $sortorder2; | |
0f5660f7 | 68 | |
69 | $this->gradestack = array(); | |
70 | } | |
71 | ||
72 | /** | |
73 | * Initialise the iterator | |
74 | * @return boolean success | |
75 | */ | |
d24832f9 | 76 | public function init() { |
77 | global $CFG, $DB; | |
0f5660f7 | 78 | |
79 | $this->close(); | |
80 | ||
81 | grade_regrade_final_grades($this->course->id); | |
82 | $course_item = grade_item::fetch_course_item($this->course->id); | |
83 | if ($course_item->needsupdate) { | |
84 | // can not calculate all final grades - sorry | |
85 | return false; | |
86 | } | |
87 | ||
656d17c2 AD |
88 | $coursecontext = get_context_instance(CONTEXT_COURSE, $this->course->id); |
89 | $relatedcontexts = get_related_contexts_string($coursecontext); | |
90 | ||
cf72e2dd | 91 | list($gradebookroles_sql, $params) = |
cf717dc2 | 92 | $DB->get_in_or_equal(explode(',', $CFG->gradebookroles), SQL_PARAMS_NAMED, 'grbr'); |
0f5660f7 | 93 | |
656d17c2 AD |
94 | //limit to users with an active enrolment |
95 | list($enrolledsql, $enrolledparams) = get_enrolled_sql($coursecontext); | |
96 | ||
97 | $params = array_merge($params, $enrolledparams); | |
0f5660f7 | 98 | |
99 | if ($this->groupid) { | |
d24832f9 | 100 | $groupsql = "INNER JOIN {groups_members} gm ON gm.userid = u.id"; |
e58fcb35 | 101 | $groupwheresql = "AND gm.groupid = :groupid"; |
d24832f9 | 102 | // $params contents: gradebookroles |
e58fcb35 | 103 | $params['groupid'] = $this->groupid; |
0f5660f7 | 104 | } else { |
105 | $groupsql = ""; | |
106 | $groupwheresql = ""; | |
107 | } | |
108 | ||
345674ca | 109 | if (empty($this->sortfield1)) { |
110 | // we must do some sorting even if not specified | |
111 | $ofields = ", u.id AS usrt"; | |
112 | $order = "usrt ASC"; | |
113 | ||
114 | } else { | |
115 | $ofields = ", u.$this->sortfield1 AS usrt1"; | |
116 | $order = "usrt1 $this->sortorder1"; | |
117 | if (!empty($this->sortfield2)) { | |
0febb12d | 118 | $ofields .= ", u.$this->sortfield2 AS usrt2"; |
345674ca | 119 | $order .= ", usrt2 $this->sortorder2"; |
120 | } | |
121 | if ($this->sortfield1 != 'id' and $this->sortfield2 != 'id') { | |
cf72e2dd | 122 | // user order MUST be the same in both queries, |
123 | // must include the only unique user->id if not already present | |
345674ca | 124 | $ofields .= ", u.id AS usrt"; |
125 | $order .= ", usrt ASC"; | |
126 | } | |
127 | } | |
128 | ||
d24832f9 | 129 | // $params contents: gradebookroles and groupid (for $groupwheresql) |
345674ca | 130 | $users_sql = "SELECT u.* $ofields |
d24832f9 | 131 | FROM {user} u |
656d17c2 | 132 | JOIN ($enrolledsql) je ON je.id = u.id |
0f5660f7 | 133 | $groupsql |
656d17c2 AD |
134 | JOIN ( |
135 | SELECT DISTINCT ra.userid | |
136 | FROM {role_assignments} ra | |
137 | WHERE ra.roleid $gradebookroles_sql | |
138 | AND ra.contextid $relatedcontexts | |
139 | ) rainner ON rainner.userid = u.id | |
140 | WHERE u.deleted = 0 | |
345674ca | 141 | $groupwheresql |
142 | ORDER BY $order"; | |
d24832f9 | 143 | $this->users_rs = $DB->get_recordset_sql($users_sql, $params); |
0f5660f7 | 144 | |
145 | if (!empty($this->grade_items)) { | |
146 | $itemids = array_keys($this->grade_items); | |
cf717dc2 | 147 | list($itemidsql, $grades_params) = $DB->get_in_or_equal($itemids, SQL_PARAMS_NAMED, 'items'); |
d24832f9 | 148 | $params = array_merge($params, $grades_params); |
656d17c2 | 149 | // $params contents: gradebookroles, enrolledparams, groupid (for $groupwheresql) and itemids |
0f5660f7 | 150 | |
345674ca | 151 | $grades_sql = "SELECT g.* $ofields |
d24832f9 | 152 | FROM {grade_grades} g |
656d17c2 AD |
153 | JOIN {user} u ON g.userid = u.id |
154 | JOIN ($enrolledsql) je ON je.id = u.id | |
0f5660f7 | 155 | $groupsql |
656d17c2 AD |
156 | JOIN ( |
157 | SELECT DISTINCT ra.userid | |
158 | FROM {role_assignments} ra | |
159 | WHERE ra.roleid $gradebookroles_sql | |
160 | AND ra.contextid $relatedcontexts | |
4f82032a | 161 | ) rainner ON rainner.userid = u.id |
656d17c2 AD |
162 | WHERE u.deleted = 0 |
163 | AND g.itemid $itemidsql | |
164 | $groupwheresql | |
345674ca | 165 | ORDER BY $order, g.itemid ASC"; |
d24832f9 | 166 | $this->grades_rs = $DB->get_recordset_sql($grades_sql, $params); |
345674ca | 167 | } else { |
168 | $this->grades_rs = false; | |
0f5660f7 | 169 | } |
345674ca | 170 | |
0f5660f7 | 171 | return true; |
172 | } | |
173 | ||
174 | /** | |
175 | * Returns information about the next user | |
176 | * @return mixed array of user info, all grades and feedback or null when no more users found | |
177 | */ | |
178 | function next_user() { | |
03cedd62 | 179 | if (!$this->users_rs) { |
0f5660f7 | 180 | return false; // no users present |
181 | } | |
182 | ||
5c75a0a3 | 183 | if (!$this->users_rs->valid()) { |
345674ca | 184 | if ($current = $this->_pop()) { |
185 | // this is not good - user or grades updated between the two reads above :-( | |
186 | } | |
187 | ||
0f5660f7 | 188 | return false; // no more users |
5c75a0a3 | 189 | } else { |
190 | $user = $this->users_rs->current(); | |
191 | $this->users_rs->next(); | |
0f5660f7 | 192 | } |
193 | ||
345674ca | 194 | // find grades of this user |
0f5660f7 | 195 | $grade_records = array(); |
196 | while (true) { | |
197 | if (!$current = $this->_pop()) { | |
198 | break; // no more grades | |
199 | } | |
200 | ||
5c75a0a3 | 201 | if (empty($current->userid)) { |
202 | break; | |
203 | } | |
204 | ||
345674ca | 205 | if ($current->userid != $user->id) { |
206 | // grade of the next user, we have all for this user | |
0f5660f7 | 207 | $this->_push($current); |
208 | break; | |
209 | } | |
210 | ||
211 | $grade_records[$current->itemid] = $current; | |
212 | } | |
213 | ||
214 | $grades = array(); | |
215 | $feedbacks = array(); | |
216 | ||
d08bba83 | 217 | if (!empty($this->grade_items)) { |
345674ca | 218 | foreach ($this->grade_items as $grade_item) { |
219 | if (array_key_exists($grade_item->id, $grade_records)) { | |
220 | $feedbacks[$grade_item->id]->feedback = $grade_records[$grade_item->id]->feedback; | |
221 | $feedbacks[$grade_item->id]->feedbackformat = $grade_records[$grade_item->id]->feedbackformat; | |
222 | unset($grade_records[$grade_item->id]->feedback); | |
223 | unset($grade_records[$grade_item->id]->feedbackformat); | |
224 | $grades[$grade_item->id] = new grade_grade($grade_records[$grade_item->id], false); | |
225 | } else { | |
226 | $feedbacks[$grade_item->id]->feedback = ''; | |
227 | $feedbacks[$grade_item->id]->feedbackformat = FORMAT_MOODLE; | |
cf72e2dd | 228 | $grades[$grade_item->id] = |
229 | new grade_grade(array('userid'=>$user->id, 'itemid'=>$grade_item->id), false); | |
345674ca | 230 | } |
0f5660f7 | 231 | } |
232 | } | |
233 | ||
ace9051c | 234 | $result = new stdClass(); |
0f5660f7 | 235 | $result->user = $user; |
236 | $result->grades = $grades; | |
237 | $result->feedbacks = $feedbacks; | |
0f5660f7 | 238 | return $result; |
239 | } | |
240 | ||
241 | /** | |
242 | * Close the iterator, do not forget to call this function. | |
243 | * @return void | |
244 | */ | |
245 | function close() { | |
caffc55a | 246 | if ($this->users_rs) { |
d24832f9 | 247 | $this->users_rs->close(); |
caffc55a | 248 | $this->users_rs = null; |
0f5660f7 | 249 | } |
caffc55a | 250 | if ($this->grades_rs) { |
d24832f9 | 251 | $this->grades_rs->close(); |
caffc55a | 252 | $this->grades_rs = null; |
0f5660f7 | 253 | } |
254 | $this->gradestack = array(); | |
255 | } | |
256 | ||
cf72e2dd | 257 | |
0f5660f7 | 258 | /** |
cf72e2dd | 259 | * _push |
260 | * | |
261 | * @param grade_grade $grade Grade object | |
262 | * | |
263 | * @return void | |
0f5660f7 | 264 | */ |
265 | function _push($grade) { | |
266 | array_push($this->gradestack, $grade); | |
267 | } | |
268 | ||
cf72e2dd | 269 | |
0f5660f7 | 270 | /** |
cf72e2dd | 271 | * _pop |
272 | * | |
7ce5df86 | 273 | * @return object current grade object |
0f5660f7 | 274 | */ |
275 | function _pop() { | |
d24832f9 | 276 | global $DB; |
0f5660f7 | 277 | if (empty($this->gradestack)) { |
8de7c8a7 | 278 | if (empty($this->grades_rs) || !$this->grades_rs->valid()) { |
cf72e2dd | 279 | return null; // no grades present |
0f5660f7 | 280 | } |
281 | ||
7ce5df86 AD |
282 | $current = $this->grades_rs->current(); |
283 | ||
284 | $this->grades_rs->next(); | |
0f5660f7 | 285 | |
7ce5df86 | 286 | return $current; |
0f5660f7 | 287 | } else { |
288 | return array_pop($this->gradestack); | |
289 | } | |
290 | } | |
291 | } | |
292 | ||
d08bba83 | 293 | /** |
294 | * Print a selection popup form of the graded users in a course. | |
295 | * | |
4d5059d4 SH |
296 | * @deprecated since 2.0 |
297 | * | |
cf72e2dd | 298 | * @param int $course id of the course |
d08bba83 | 299 | * @param string $actionpage The page receiving the data from the popoup form |
cf72e2dd | 300 | * @param int $userid id of the currently selected user (or 'all' if they are all selected) |
301 | * @param int $groupid id of requested group, 0 means all | |
302 | * @param int $includeall bool include all option | |
303 | * @param bool $return If true, will return the HTML, otherwise, will print directly | |
d08bba83 | 304 | * @return null |
305 | */ | |
7ac88172 | 306 | function print_graded_users_selector($course, $actionpage, $userid=0, $groupid=0, $includeall=true, $return=false) { |
714b4745 | 307 | global $CFG, $USER, $OUTPUT; |
4d5059d4 SH |
308 | return $OUTPUT->render(grade_get_graded_users_select(substr($actionpage, 0, strpos($actionpage, '/')), $course, $userid, $groupid, $includeall)); |
309 | } | |
345674ca | 310 | |
4d5059d4 | 311 | function grade_get_graded_users_select($report, $course, $userid, $groupid, $includeall) { |
f9510667 PS |
312 | global $USER; |
313 | ||
879c99bb | 314 | if (is_null($userid)) { |
315 | $userid = $USER->id; | |
316 | } | |
d08bba83 | 317 | |
d08bba83 | 318 | $menu = array(); // Will be a list of userid => user name |
772229f3 | 319 | $gui = new graded_users_iterator($course, null, $groupid); |
d08bba83 | 320 | $gui->init(); |
10f5c046 | 321 | $label = get_string('selectauser', 'grades'); |
7ac88172 | 322 | if ($includeall) { |
879c99bb | 323 | $menu[0] = get_string('allusers', 'grades'); |
10f5c046 | 324 | $label = get_string('selectalloroneuser', 'grades'); |
d08bba83 | 325 | } |
d08bba83 | 326 | while ($userdata = $gui->next_user()) { |
327 | $user = $userdata->user; | |
328 | $menu[$user->id] = fullname($user); | |
329 | } | |
d08bba83 | 330 | $gui->close(); |
331 | ||
7ac88172 | 332 | if ($includeall) { |
879c99bb | 333 | $menu[0] .= " (" . (count($menu) - 1) . ")"; |
334 | } | |
4d5059d4 | 335 | $select = new single_select(new moodle_url('/grade/report/'.$report.'/index.php', array('id'=>$course->id)), 'userid', $menu, $userid); |
f8dab966 PS |
336 | $select->label = $label; |
337 | $select->formid = 'choosegradeuser'; | |
4d5059d4 | 338 | return $select; |
d08bba83 | 339 | } |
340 | ||
0610812a | 341 | /** |
342 | * Print grading plugin selection popup form. | |
343 | * | |
cf72e2dd | 344 | * @param array $plugin_info An array of plugins containing information for the selector |
0610812a | 345 | * @param boolean $return return as string |
cf72e2dd | 346 | * |
0610812a | 347 | * @return nothing or string if $return true |
348 | */ | |
f1a3e072 | 349 | function print_grade_plugin_selector($plugin_info, $active_type, $active_plugin, $return=false) { |
714b4745 | 350 | global $CFG, $OUTPUT, $PAGE; |
dc482cfa | 351 | |
352 | $menu = array(); | |
353 | $count = 0; | |
354 | $active = ''; | |
355 | ||
356 | foreach ($plugin_info as $plugin_type => $plugins) { | |
357 | if ($plugin_type == 'strings') { | |
358 | continue; | |
359 | } | |
360 | ||
7981d537 | 361 | $first_plugin = reset($plugins); |
dc482cfa | 362 | |
f1a3e072 PS |
363 | $sectionname = $plugin_info['strings'][$plugin_type]; |
364 | $section = array(); | |
d4dcfc6b | 365 | |
f1a3e072 | 366 | foreach ($plugins as $plugin) { |
d4dcfc6b | 367 | $link = $plugin->link->out(false); |
f1a3e072 PS |
368 | $section[$link] = $plugin->string; |
369 | $count++; | |
370 | if ($plugin_type === $active_type and $plugin->id === $active_plugin) { | |
371 | $active = $link; | |
dc482cfa | 372 | } |
373 | } | |
f1a3e072 PS |
374 | |
375 | if ($section) { | |
376 | $menu[] = array($sectionname=>$section); | |
377 | } | |
dc482cfa | 378 | } |
379 | ||
cf72e2dd | 380 | // finally print/return the popup form |
dc482cfa | 381 | if ($count > 1) { |
f1a3e072 | 382 | $select = new url_select($menu, $active, null, 'choosepluginreport'); |
1c1f64a2 | 383 | |
7981d537 | 384 | if ($return) { |
f1a3e072 | 385 | return $OUTPUT->render($select); |
7981d537 | 386 | } else { |
f1a3e072 | 387 | echo $OUTPUT->render($select); |
7981d537 | 388 | } |
dc482cfa | 389 | } else { |
390 | // only one option - no plugin selector needed | |
391 | return ''; | |
392 | } | |
393 | } | |
394 | ||
395 | /** | |
396 | * Print grading plugin selection tab-based navigation. | |
397 | * | |
cf72e2dd | 398 | * @param string $active_type type of plugin on current page - import, export, report or edit |
399 | * @param string $active_plugin active plugin type - grader, user, cvs, ... | |
400 | * @param array $plugin_info Array of plugins | |
dc482cfa | 401 | * @param boolean $return return as string |
cf72e2dd | 402 | * |
dc482cfa | 403 | * @return nothing or string if $return true |
404 | */ | |
b827784e | 405 | function grade_print_tabs($active_type, $active_plugin, $plugin_info, $return=false) { |
dc482cfa | 406 | global $CFG, $COURSE; |
1c1f64a2 | 407 | |
e1513ca9 | 408 | if (!isset($currenttab)) { //TODO: this is weird |
dc482cfa | 409 | $currenttab = ''; |
410 | } | |
411 | ||
412 | $tabs = array(); | |
413 | $top_row = array(); | |
414 | $bottom_row = array(); | |
415 | $inactive = array($active_plugin); | |
416 | $activated = array(); | |
417 | ||
418 | $count = 0; | |
419 | $active = ''; | |
420 | ||
421 | foreach ($plugin_info as $plugin_type => $plugins) { | |
422 | if ($plugin_type == 'strings') { | |
423 | continue; | |
424 | } | |
425 | ||
426 | // If $plugins is actually the definition of a child-less parent link: | |
cf72e2dd | 427 | if (!empty($plugins->id)) { |
428 | $string = $plugins->string; | |
429 | if (!empty($plugin_info[$active_type]->parent)) { | |
430 | $string = $plugin_info[$active_type]->parent->string; | |
27eef3bb | 431 | } |
432 | ||
cf72e2dd | 433 | $top_row[] = new tabobject($plugin_type, $plugins->link, $string); |
dc482cfa | 434 | continue; |
435 | } | |
436 | ||
437 | $first_plugin = reset($plugins); | |
cf72e2dd | 438 | $url = $first_plugin->link; |
dc482cfa | 439 | |
440 | if ($plugin_type == 'report') { | |
441 | $url = $CFG->wwwroot.'/grade/report/index.php?id='.$COURSE->id; | |
442 | } | |
443 | ||
444 | $top_row[] = new tabobject($plugin_type, $url, $plugin_info['strings'][$plugin_type]); | |
445 | ||
446 | if ($active_type == $plugin_type) { | |
447 | foreach ($plugins as $plugin) { | |
cf72e2dd | 448 | $bottom_row[] = new tabobject($plugin->id, $plugin->link, $plugin->string); |
449 | if ($plugin->id == $active_plugin) { | |
450 | $inactive = array($plugin->id); | |
dc482cfa | 451 | } |
452 | } | |
453 | } | |
454 | } | |
455 | ||
456 | $tabs[] = $top_row; | |
457 | $tabs[] = $bottom_row; | |
458 | ||
459 | if ($return) { | |
460 | return print_tabs($tabs, $active_type, $inactive, $activated, true); | |
461 | } else { | |
462 | print_tabs($tabs, $active_type, $inactive, $activated); | |
463 | } | |
464 | } | |
465 | ||
cf72e2dd | 466 | /** |
467 | * grade_get_plugin_info | |
468 | * | |
469 | * @param int $courseid The course id | |
470 | * @param string $active_type type of plugin on current page - import, export, report or edit | |
471 | * @param string $active_plugin active plugin type - grader, user, cvs, ... | |
472 | * | |
473 | * @return array | |
474 | */ | |
dc482cfa | 475 | function grade_get_plugin_info($courseid, $active_type, $active_plugin) { |
f7fcf4cd | 476 | global $CFG, $SITE; |
cbff94ba | 477 | |
3af29899 | 478 | $context = get_context_instance(CONTEXT_COURSE, $courseid); |
cbff94ba | 479 | |
dc482cfa | 480 | $plugin_info = array(); |
6e2f3121 | 481 | $count = 0; |
3af29899 | 482 | $active = ''; |
dc482cfa | 483 | $url_prefix = $CFG->wwwroot . '/grade/'; |
484 | ||
485 | // Language strings | |
4d5059d4 | 486 | $plugin_info['strings'] = grade_helper::get_plugin_strings(); |
b827784e | 487 | |
4d5059d4 SH |
488 | if ($reports = grade_helper::get_plugins_reports($courseid)) { |
489 | $plugin_info['report'] = $reports; | |
b827784e | 490 | } |
c46aeeab | 491 | |
f7fcf4cd AD |
492 | //showing grade categories and items make no sense if we're not within a course |
493 | if ($courseid!=$SITE->id) { | |
494 | if ($edittree = grade_helper::get_info_edit_structure($courseid)) { | |
495 | $plugin_info['edittree'] = $edittree; | |
496 | } | |
04678d8e | 497 | } |
b827784e | 498 | |
4d5059d4 SH |
499 | if ($scale = grade_helper::get_info_scales($courseid)) { |
500 | $plugin_info['scale'] = array('view'=>$scale); | |
cbff94ba | 501 | } |
dc482cfa | 502 | |
4d5059d4 SH |
503 | if ($outcomes = grade_helper::get_info_outcomes($courseid)) { |
504 | $plugin_info['outcome'] = $outcomes; | |
cbff94ba | 505 | } |
cbff94ba | 506 | |
4d5059d4 SH |
507 | if ($letters = grade_helper::get_info_letters($courseid)) { |
508 | $plugin_info['letter'] = $letters; | |
cbff94ba | 509 | } |
4d5059d4 SH |
510 | |
511 | if ($imports = grade_helper::get_plugins_import($courseid)) { | |
512 | $plugin_info['import'] = $imports; | |
281ffa4a | 513 | } |
c46aeeab | 514 | |
4d5059d4 SH |
515 | if ($exports = grade_helper::get_plugins_export($courseid)) { |
516 | $plugin_info['export'] = $exports; | |
281ffa4a | 517 | } |
281ffa4a | 518 | |
4d5059d4 SH |
519 | foreach ($plugin_info as $plugin_type => $plugins) { |
520 | if (!empty($plugins->id) && $active_plugin == $plugins->id) { | |
521 | $plugin_info['strings']['active_plugin_str'] = $plugins->string; | |
522 | break; | |
281ffa4a | 523 | } |
4d5059d4 SH |
524 | foreach ($plugins as $plugin) { |
525 | if (is_a($plugin, 'grade_plugin_info')) { | |
526 | if ($active_plugin == $plugin->id) { | |
527 | $plugin_info['strings']['active_plugin_str'] = $plugin->string; | |
528 | } | |
3af29899 | 529 | } |
281ffa4a | 530 | } |
cbff94ba | 531 | } |
c46aeeab | 532 | |
f7fcf4cd AD |
533 | //hide course settings if we're not in a course |
534 | if ($courseid!=$SITE->id) { | |
535 | if ($setting = grade_helper::get_info_manage_settings($courseid)) { | |
536 | $plugin_info['settings'] = array('course'=>$setting); | |
537 | } | |
281ffa4a | 538 | } |
cbff94ba | 539 | |
4d5059d4 SH |
540 | // Put preferences last |
541 | if ($preferences = grade_helper::get_plugins_report_preferences($courseid)) { | |
542 | $plugin_info['preferences'] = $preferences; | |
27eef3bb | 543 | } |
544 | ||
dc482cfa | 545 | foreach ($plugin_info as $plugin_type => $plugins) { |
cf72e2dd | 546 | if (!empty($plugins->id) && $active_plugin == $plugins->id) { |
547 | $plugin_info['strings']['active_plugin_str'] = $plugins->string; | |
dc482cfa | 548 | break; |
78ad5f3f | 549 | } |
dc482cfa | 550 | foreach ($plugins as $plugin) { |
cf72e2dd | 551 | if (is_a($plugin, 'grade_plugin_info')) { |
552 | if ($active_plugin == $plugin->id) { | |
553 | $plugin_info['strings']['active_plugin_str'] = $plugin->string; | |
554 | } | |
78ad5f3f | 555 | } |
78ad5f3f | 556 | } |
dc482cfa | 557 | } |
78ad5f3f | 558 | |
dc482cfa | 559 | return $plugin_info; |
560 | } | |
284abb09 | 561 | |
cf72e2dd | 562 | /** |
563 | * A simple class containing info about grade plugins. | |
564 | * Can be subclassed for special rules | |
565 | * | |
566 | * @package moodlecore | |
567 | * @copyright 2009 Nicolas Connault | |
568 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
569 | */ | |
570 | class grade_plugin_info { | |
571 | /** | |
572 | * A unique id for this plugin | |
573 | * | |
574 | * @var mixed | |
575 | */ | |
576 | public $id; | |
577 | /** | |
578 | * A URL to access this plugin | |
579 | * | |
580 | * @var mixed | |
581 | */ | |
582 | public $link; | |
583 | /** | |
584 | * The name of this plugin | |
585 | * | |
586 | * @var mixed | |
587 | */ | |
588 | public $string; | |
589 | /** | |
590 | * Another grade_plugin_info object, parent of the current one | |
591 | * | |
592 | * @var mixed | |
593 | */ | |
594 | public $parent; | |
595 | ||
596 | /** | |
597 | * Constructor | |
598 | * | |
599 | * @param int $id A unique id for this plugin | |
600 | * @param string $link A URL to access this plugin | |
601 | * @param string $string The name of this plugin | |
602 | * @param object $parent Another grade_plugin_info object, parent of the current one | |
603 | * | |
604 | * @return void | |
605 | */ | |
606 | public function __construct($id, $link, $string, $parent=null) { | |
607 | $this->id = $id; | |
608 | $this->link = $link; | |
609 | $this->string = $string; | |
610 | $this->parent = $parent; | |
611 | } | |
612 | } | |
613 | ||
de0300ea | 614 | /** |
615 | * Prints the page headers, breadcrumb trail, page heading, (optional) dropdown navigation menu and | |
616 | * (optional) navigation tabs for any gradebook page. All gradebook pages MUST use these functions | |
617 | * in favour of the usual print_header(), print_header_simple(), print_heading() etc. | |
618 | * !IMPORTANT! Use of tabs.php file in gradebook pages is forbidden unless tabs are switched off at | |
619 | * the site level for the gradebook ($CFG->grade_navmethod = GRADE_NAVMETHOD_DROPDOWN). | |
620 | * | |
cf72e2dd | 621 | * @param int $courseid Course id |
622 | * @param string $active_type The type of the current page (report, settings, | |
623 | * import, export, scales, outcomes, letters) | |
624 | * @param string $active_plugin The plugin of the current page (grader, fullview etc...) | |
625 | * @param string $heading The heading of the page. Tries to guess if none is given | |
de0300ea | 626 | * @param boolean $return Whether to return (true) or echo (false) the HTML generated by this function |
cf72e2dd | 627 | * @param string $bodytags Additional attributes that will be added to the <body> tag |
628 | * @param string $buttons Additional buttons to display on the page | |
de0300ea | 629 | * |
630 | * @return string HTML code or nothing if $return == false | |
631 | */ | |
cf72e2dd | 632 | function print_grade_page_head($courseid, $active_type, $active_plugin=null, |
34a2777c | 633 | $heading = false, $return=false, |
2821c495 | 634 | $buttons=false, $shownavigation=true) { |
4d5059d4 | 635 | global $CFG, $OUTPUT, $PAGE; |
c46aeeab | 636 | |
dc482cfa | 637 | $plugin_info = grade_get_plugin_info($courseid, $active_type, $active_plugin); |
c46aeeab | 638 | |
dc482cfa | 639 | // Determine the string of the active plugin |
640 | $stractive_plugin = ($active_plugin) ? $plugin_info['strings']['active_plugin_str'] : $heading; | |
641 | $stractive_type = $plugin_info['strings'][$active_type]; | |
e0724506 | 642 | |
cf72e2dd | 643 | if (empty($plugin_info[$active_type]->id) || !empty($plugin_info[$active_type]->parent)) { |
4d5059d4 SH |
644 | $title = $PAGE->course->fullname.': ' . $stractive_type . ': ' . $stractive_plugin; |
645 | } else { | |
646 | $title = $PAGE->course->fullname.': ' . $stractive_plugin; | |
c4c97a6d | 647 | } |
648 | ||
367a75fa SH |
649 | if ($active_type == 'report') { |
650 | $PAGE->set_pagelayout('report'); | |
651 | } else { | |
652 | $PAGE->set_pagelayout('admin'); | |
653 | } | |
4d5059d4 | 654 | $PAGE->set_title(get_string('grades') . ': ' . $stractive_type); |
f3df5e14 | 655 | $PAGE->set_heading($title); |
4d5059d4 SH |
656 | if ($buttons instanceof single_button) { |
657 | $buttons = $OUTPUT->render($buttons); | |
658 | } | |
f3df5e14 | 659 | $PAGE->set_button($buttons); |
4d5059d4 SH |
660 | grade_extend_settings($plugin_info, $courseid); |
661 | ||
f3df5e14 | 662 | $returnval = $OUTPUT->header(); |
663 | if (!$return) { | |
664 | echo $returnval; | |
665 | } | |
dc482cfa | 666 | |
667 | // Guess heading if not given explicitly | |
668 | if (!$heading) { | |
669 | $heading = $stractive_plugin; | |
670 | } | |
671 | ||
2821c495 AD |
672 | if ($shownavigation) { |
673 | if ($CFG->grade_navmethod == GRADE_NAVMETHOD_COMBO || $CFG->grade_navmethod == GRADE_NAVMETHOD_DROPDOWN) { | |
674 | $returnval .= print_grade_plugin_selector($plugin_info, $active_type, $active_plugin, $return); | |
675 | } | |
676 | $returnval .= $OUTPUT->heading($heading); | |
677 | if ($CFG->grade_navmethod == GRADE_NAVMETHOD_COMBO || $CFG->grade_navmethod == GRADE_NAVMETHOD_TABS) { | |
678 | $returnval .= grade_print_tabs($active_type, $active_plugin, $plugin_info, $return); | |
679 | } | |
8354e716 | 680 | } |
dc482cfa | 681 | |
682 | if ($return) { | |
683 | return $returnval; | |
6e2f3121 | 684 | } |
cbff94ba | 685 | } |
686 | ||
0610812a | 687 | /** |
7a6b7acf | 688 | * Utility class used for return tracking when using edit and other forms in grade plugins |
cf72e2dd | 689 | * |
690 | * @package moodlecore | |
691 | * @copyright 2009 Nicolas Connault | |
692 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
0610812a | 693 | */ |
3af29899 | 694 | class grade_plugin_return { |
d24832f9 | 695 | public $type; |
696 | public $plugin; | |
697 | public $courseid; | |
698 | public $userid; | |
699 | public $page; | |
281ffa4a | 700 | |
0610812a | 701 | /** |
702 | * Constructor | |
cf72e2dd | 703 | * |
0610812a | 704 | * @param array $params - associative array with return parameters, if null parameter are taken from _GET or _POST |
705 | */ | |
cf72e2dd | 706 | public function grade_plugin_return($params = null) { |
3af29899 | 707 | if (empty($params)) { |
708 | $this->type = optional_param('gpr_type', null, PARAM_SAFEDIR); | |
aff24313 | 709 | $this->plugin = optional_param('gpr_plugin', null, PARAM_PLUGIN); |
3af29899 | 710 | $this->courseid = optional_param('gpr_courseid', null, PARAM_INT); |
711 | $this->userid = optional_param('gpr_userid', null, PARAM_INT); | |
712 | $this->page = optional_param('gpr_page', null, PARAM_INT); | |
a983b6ec | 713 | |
a983b6ec | 714 | } else { |
3af29899 | 715 | foreach ($params as $key=>$value) { |
4cc977a6 | 716 | if (property_exists($this, $key)) { |
3af29899 | 717 | $this->$key = $value; |
718 | } | |
cbff94ba | 719 | } |
720 | } | |
6cd8c592 | 721 | } |
722 | ||
0610812a | 723 | /** |
724 | * Returns return parameters as options array suitable for buttons. | |
725 | * @return array options | |
726 | */ | |
d24832f9 | 727 | public function get_options() { |
7a6b7acf | 728 | if (empty($this->type)) { |
3af29899 | 729 | return array(); |
865e9a82 | 730 | } |
6cd8c592 | 731 | |
3af29899 | 732 | $params = array(); |
6cd8c592 | 733 | |
7a6b7acf | 734 | if (!empty($this->plugin)) { |
735 | $params['plugin'] = $this->plugin; | |
736 | } | |
6cd8c592 | 737 | |
3af29899 | 738 | if (!empty($this->courseid)) { |
739 | $params['id'] = $this->courseid; | |
6cd8c592 | 740 | } |
9c61ba4d | 741 | |
3af29899 | 742 | if (!empty($this->userid)) { |
743 | $params['userid'] = $this->userid; | |
9c61ba4d | 744 | } |
9c61ba4d | 745 | |
3af29899 | 746 | if (!empty($this->page)) { |
747 | $params['page'] = $this->page; | |
cbff94ba | 748 | } |
865e9a82 | 749 | |
3af29899 | 750 | return $params; |
cbff94ba | 751 | } |
cbff94ba | 752 | |
0610812a | 753 | /** |
754 | * Returns return url | |
cf72e2dd | 755 | * |
0610812a | 756 | * @param string $default default url when params not set |
cf72e2dd | 757 | * @param array $extras Extra URL parameters |
758 | * | |
0610812a | 759 | * @return string url |
760 | */ | |
d24832f9 | 761 | public function get_return_url($default, $extras=null) { |
3af29899 | 762 | global $CFG; |
cbff94ba | 763 | |
3af29899 | 764 | if (empty($this->type) or empty($this->plugin)) { |
765 | return $default; | |
cbff94ba | 766 | } |
767 | ||
65dd61bd | 768 | $url = $CFG->wwwroot.'/grade/'.$this->type.'/'.$this->plugin.'/index.php'; |
769 | $glue = '?'; | |
cbff94ba | 770 | |
3af29899 | 771 | if (!empty($this->courseid)) { |
772 | $url .= $glue.'id='.$this->courseid; | |
773 | $glue = '&'; | |
cbff94ba | 774 | } |
cbff94ba | 775 | |
3af29899 | 776 | if (!empty($this->userid)) { |
777 | $url .= $glue.'userid='.$this->userid; | |
778 | $glue = '&'; | |
cbff94ba | 779 | } |
7e2d7c92 | 780 | |
3af29899 | 781 | if (!empty($this->page)) { |
782 | $url .= $glue.'page='.$this->page; | |
65dd61bd | 783 | $glue = '&'; |
784 | } | |
785 | ||
786 | if (!empty($extras)) { | |
cf72e2dd | 787 | foreach ($extras as $key=>$value) { |
65dd61bd | 788 | $url .= $glue.$key.'='.$value; |
789 | $glue = '&'; | |
7a6b7acf | 790 | } |
cbff94ba | 791 | } |
cbff94ba | 792 | |
3af29899 | 793 | return $url; |
cbff94ba | 794 | } |
cbff94ba | 795 | |
0610812a | 796 | /** |
797 | * Returns string with hidden return tracking form elements. | |
798 | * @return string | |
799 | */ | |
d24832f9 | 800 | public function get_form_fields() { |
7a6b7acf | 801 | if (empty($this->type)) { |
3af29899 | 802 | return ''; |
cbff94ba | 803 | } |
cbff94ba | 804 | |
3af29899 | 805 | $result = '<input type="hidden" name="gpr_type" value="'.$this->type.'" />'; |
7a6b7acf | 806 | |
807 | if (!empty($this->plugin)) { | |
808 | $result .= '<input type="hidden" name="gpr_plugin" value="'.$this->plugin.'" />'; | |
809 | } | |
0ca5abd6 | 810 | |
3af29899 | 811 | if (!empty($this->courseid)) { |
812 | $result .= '<input type="hidden" name="gpr_courseid" value="'.$this->courseid.'" />'; | |
cbff94ba | 813 | } |
cbff94ba | 814 | |
3af29899 | 815 | if (!empty($this->userid)) { |
816 | $result .= '<input type="hidden" name="gpr_userid" value="'.$this->userid.'" />'; | |
cbff94ba | 817 | } |
cbff94ba | 818 | |
3af29899 | 819 | if (!empty($this->page)) { |
820 | $result .= '<input type="hidden" name="gpr_page" value="'.$this->page.'" />'; | |
cbff94ba | 821 | } |
822 | } | |
cbff94ba | 823 | |
0610812a | 824 | /** |
825 | * Add hidden elements into mform | |
cf72e2dd | 826 | * |
827 | * @param object &$mform moodle form object | |
828 | * | |
0610812a | 829 | * @return void |
830 | */ | |
d24832f9 | 831 | public function add_mform_elements(&$mform) { |
7a6b7acf | 832 | if (empty($this->type)) { |
3af29899 | 833 | return; |
cbff94ba | 834 | } |
cbff94ba | 835 | |
3af29899 | 836 | $mform->addElement('hidden', 'gpr_type', $this->type); |
837 | $mform->setType('gpr_type', PARAM_SAFEDIR); | |
cbff94ba | 838 | |
7a6b7acf | 839 | if (!empty($this->plugin)) { |
840 | $mform->addElement('hidden', 'gpr_plugin', $this->plugin); | |
aff24313 | 841 | $mform->setType('gpr_plugin', PARAM_PLUGIN); |
7a6b7acf | 842 | } |
97033c86 | 843 | |
3af29899 | 844 | if (!empty($this->courseid)) { |
845 | $mform->addElement('hidden', 'gpr_courseid', $this->courseid); | |
846 | $mform->setType('gpr_courseid', PARAM_INT); | |
cbff94ba | 847 | } |
cbff94ba | 848 | |
3af29899 | 849 | if (!empty($this->userid)) { |
850 | $mform->addElement('hidden', 'gpr_userid', $this->userid); | |
851 | $mform->setType('gpr_userid', PARAM_INT); | |
cbff94ba | 852 | } |
cbff94ba | 853 | |
3af29899 | 854 | if (!empty($this->page)) { |
855 | $mform->addElement('hidden', 'gpr_page', $this->page); | |
856 | $mform->setType('gpr_page', PARAM_INT); | |
cbff94ba | 857 | } |
858 | } | |
281ffa4a | 859 | |
0610812a | 860 | /** |
861 | * Add return tracking params into url | |
cf72e2dd | 862 | * |
1c1f64a2 | 863 | * @param moodle_url $url A URL |
cf72e2dd | 864 | * |
6ef4878b | 865 | * @return string $url with return tracking params |
0610812a | 866 | */ |
1c1f64a2 | 867 | public function add_url_params(moodle_url $url) { |
7a6b7acf | 868 | if (empty($this->type)) { |
3af29899 | 869 | return $url; |
cbff94ba | 870 | } |
5609f9e6 | 871 | |
1c1f64a2 | 872 | $url->param('gpr_type', $this->type); |
cbff94ba | 873 | |
7a6b7acf | 874 | if (!empty($this->plugin)) { |
1c1f64a2 | 875 | $url->param('gpr_plugin', $this->plugin); |
7a6b7acf | 876 | } |
cbff94ba | 877 | |
3af29899 | 878 | if (!empty($this->courseid)) { |
1c1f64a2 | 879 | $url->param('gpr_courseid' ,$this->courseid); |
cbff94ba | 880 | } |
cbff94ba | 881 | |
3af29899 | 882 | if (!empty($this->userid)) { |
1c1f64a2 | 883 | $url->param('gpr_userid', $this->userid); |
cbff94ba | 884 | } |
0a8a95c9 | 885 | |
3af29899 | 886 | if (!empty($this->page)) { |
1c1f64a2 | 887 | $url->param('gpr_page', $this->page); |
0a8a95c9 | 888 | } |
5a412dbf | 889 | |
3af29899 | 890 | return $url; |
5a412dbf | 891 | } |
5a412dbf | 892 | } |
7a6b7acf | 893 | |
826c5f86 | 894 | /** |
895 | * Function central to gradebook for building and printing the navigation (breadcrumb trail). | |
cf72e2dd | 896 | * |
826c5f86 | 897 | * @param string $path The path of the calling script (using __FILE__?) |
898 | * @param string $pagename The language string to use as the last part of the navigation (non-link) | |
cf72e2dd | 899 | * @param mixed $id Either a plain integer (assuming the key is 'id') or |
900 | * an array of keys and values (e.g courseid => $courseid, itemid...) | |
901 | * | |
826c5f86 | 902 | * @return string |
903 | */ | |
904 | function grade_build_nav($path, $pagename=null, $id=null) { | |
f3df5e14 | 905 | global $CFG, $COURSE, $PAGE; |
826c5f86 | 906 | |
907 | $strgrades = get_string('grades', 'grades'); | |
908 | ||
909 | // Parse the path and build navlinks from its elements | |
910 | $dirroot_length = strlen($CFG->dirroot) + 1; // Add 1 for the first slash | |
911 | $path = substr($path, $dirroot_length); | |
912 | $path = str_replace('\\', '/', $path); | |
913 | ||
914 | $path_elements = explode('/', $path); | |
915 | ||
916 | $path_elements_count = count($path_elements); | |
917 | ||
826c5f86 | 918 | // First link is always 'grade' |
a6855934 | 919 | $PAGE->navbar->add($strgrades, new moodle_url('/grade/index.php', array('id'=>$COURSE->id))); |
826c5f86 | 920 | |
f3df5e14 | 921 | $link = null; |
826c5f86 | 922 | $numberofelements = 3; |
923 | ||
924 | // Prepare URL params string | |
f3df5e14 | 925 | $linkparams = array(); |
826c5f86 | 926 | if (!is_null($id)) { |
927 | if (is_array($id)) { | |
928 | foreach ($id as $idkey => $idvalue) { | |
f3df5e14 | 929 | $linkparams[$idkey] = $idvalue; |
826c5f86 | 930 | } |
931 | } else { | |
f3df5e14 | 932 | $linkparams['id'] = $id; |
826c5f86 | 933 | } |
934 | } | |
935 | ||
936 | $navlink4 = null; | |
937 | ||
0f78c4de | 938 | // Remove file extensions from filenames |
939 | foreach ($path_elements as $key => $filename) { | |
940 | $path_elements[$key] = str_replace('.php', '', $filename); | |
941 | } | |
942 | ||
826c5f86 | 943 | // Second level links |
944 | switch ($path_elements[1]) { | |
945 | case 'edit': // No link | |
946 | if ($path_elements[3] != 'index.php') { | |
947 | $numberofelements = 4; | |
948 | } | |
949 | break; | |
950 | case 'import': // No link | |
951 | break; | |
952 | case 'export': // No link | |
953 | break; | |
954 | case 'report': | |
955 | // $id is required for this link. Do not print it if $id isn't given | |
956 | if (!is_null($id)) { | |
a6855934 | 957 | $link = new moodle_url('/grade/report/index.php', $linkparams); |
826c5f86 | 958 | } |
959 | ||
960 | if ($path_elements[2] == 'grader') { | |
961 | $numberofelements = 4; | |
962 | } | |
963 | break; | |
964 | ||
965 | default: | |
966 | // If this element isn't among the ones already listed above, it isn't supported, throw an error. | |
cf72e2dd | 967 | debugging("grade_build_nav() doesn't support ". $path_elements[1] . |
968 | " as the second path element after 'grade'."); | |
826c5f86 | 969 | return false; |
970 | } | |
f3df5e14 | 971 | $PAGE->navbar->add(get_string($path_elements[1], 'grades'), $link); |
826c5f86 | 972 | |
973 | // Third level links | |
974 | if (empty($pagename)) { | |
975 | $pagename = get_string($path_elements[2], 'grades'); | |
976 | } | |
977 | ||
978 | switch ($numberofelements) { | |
979 | case 3: | |
f3df5e14 | 980 | $PAGE->navbar->add($pagename, $link); |
826c5f86 | 981 | break; |
982 | case 4: | |
826c5f86 | 983 | if ($path_elements[2] == 'grader' AND $path_elements[3] != 'index.php') { |
b5e7b2bf | 984 | $PAGE->navbar->add(get_string('pluginname', 'gradereport_grader'), new moodle_url('/grade/report/grader/index.php', $linkparams)); |
826c5f86 | 985 | } |
f3df5e14 | 986 | $PAGE->navbar->add($pagename); |
826c5f86 | 987 | break; |
988 | } | |
826c5f86 | 989 | |
f3df5e14 | 990 | return ''; |
d4795a07 | 991 | } |
7a6b7acf | 992 | |
e98871a2 | 993 | /** |
6cc3e350 | 994 | * General structure representing grade items in course |
cf72e2dd | 995 | * |
996 | * @package moodlecore | |
997 | * @copyright 2009 Nicolas Connault | |
998 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
e98871a2 | 999 | */ |
6cc3e350 | 1000 | class grade_structure { |
d24832f9 | 1001 | public $context; |
e98871a2 | 1002 | |
d24832f9 | 1003 | public $courseid; |
e98871a2 | 1004 | |
1005 | /** | |
1006 | * 1D array of grade items only | |
1007 | */ | |
d24832f9 | 1008 | public $items; |
e98871a2 | 1009 | |
6391ebe7 | 1010 | /** |
6cc3e350 | 1011 | * Returns icon of element |
cf72e2dd | 1012 | * |
1013 | * @param array &$element An array representing an element in the grade_tree | |
1014 | * @param bool $spacerifnone return spacer if no icon found | |
1015 | * | |
6cc3e350 | 1016 | * @return string icon or spacer |
6391ebe7 | 1017 | */ |
d24832f9 | 1018 | public function get_element_icon(&$element, $spacerifnone=false) { |
6b608f8f | 1019 | global $CFG, $OUTPUT; |
6cc3e350 | 1020 | |
1021 | switch ($element['type']) { | |
1022 | case 'item': | |
1023 | case 'courseitem': | |
1024 | case 'categoryitem': | |
cf72e2dd | 1025 | $is_course = $element['object']->is_course_item(); |
1026 | $is_category = $element['object']->is_category_item(); | |
1027 | $is_scale = $element['object']->gradetype == GRADE_TYPE_SCALE; | |
1028 | $is_value = $element['object']->gradetype == GRADE_TYPE_VALUE; | |
0077f571 | 1029 | $is_outcome = !empty($element['object']->outcomeid); |
cf72e2dd | 1030 | |
6cc3e350 | 1031 | if ($element['object']->is_calculated()) { |
dc482cfa | 1032 | $strcalc = get_string('calculatedgrade', 'grades'); |
b5d0cafc | 1033 | return '<img src="'.$OUTPUT->pix_url('i/calc') . '" class="icon itemicon" title="'. |
cf72e2dd | 1034 | s($strcalc).'" alt="'.s($strcalc).'"/>'; |
6cc3e350 | 1035 | |
cf72e2dd | 1036 | } else if (($is_course or $is_category) and ($is_scale or $is_value)) { |
6cc3e350 | 1037 | if ($category = $element['object']->get_item_category()) { |
1038 | switch ($category->aggregation) { | |
1039 | case GRADE_AGGREGATE_MEAN: | |
1040 | case GRADE_AGGREGATE_MEDIAN: | |
1041 | case GRADE_AGGREGATE_WEIGHTED_MEAN: | |
1426edac | 1042 | case GRADE_AGGREGATE_WEIGHTED_MEAN2: |
6cc3e350 | 1043 | case GRADE_AGGREGATE_EXTRACREDIT_MEAN: |
dc482cfa | 1044 | $stragg = get_string('aggregation', 'grades'); |
b5d0cafc | 1045 | return '<img src="'.$OUTPUT->pix_url('i/agg_mean') . '" ' . |
cf72e2dd | 1046 | 'class="icon itemicon" title="'.s($stragg).'" alt="'.s($stragg).'"/>'; |
0758a08e | 1047 | case GRADE_AGGREGATE_SUM: |
dc482cfa | 1048 | $stragg = get_string('aggregation', 'grades'); |
b5d0cafc | 1049 | return '<img src="'.$OUTPUT->pix_url('i/agg_sum') . '" ' . |
cf72e2dd | 1050 | 'class="icon itemicon" title="'.s($stragg).'" alt="'.s($stragg).'"/>'; |
6cc3e350 | 1051 | } |
1052 | } | |
1053 | ||
1054 | } else if ($element['object']->itemtype == 'mod') { | |
0077f571 AD |
1055 | //prevent outcomes being displaying the same icon as the activity they are attached to |
1056 | if ($is_outcome) { | |
1057 | $stroutcome = s(get_string('outcome', 'grades')); | |
1058 | return '<img src="'.$OUTPUT->pix_url('i/outcomes') . '" ' . | |
1059 | 'class="icon itemicon" title="'.$stroutcome. | |
1060 | '" alt="'.$stroutcome.'"/>'; | |
1061 | } else { | |
1062 | $strmodname = get_string('modulename', $element['object']->itemmodule); | |
1063 | return '<img src="'.$OUTPUT->pix_url('icon', | |
e63f88c9 | 1064 | $element['object']->itemmodule) . '" ' . |
cf72e2dd | 1065 | 'class="icon itemicon" title="' .s($strmodname). |
1066 | '" alt="' .s($strmodname).'"/>'; | |
0077f571 | 1067 | } |
6cc3e350 | 1068 | } else if ($element['object']->itemtype == 'manual') { |
1069 | if ($element['object']->is_outcome_item()) { | |
dc482cfa | 1070 | $stroutcome = get_string('outcome', 'grades'); |
b5d0cafc | 1071 | return '<img src="'.$OUTPUT->pix_url('i/outcomes') . '" ' . |
cf72e2dd | 1072 | 'class="icon itemicon" title="'.s($stroutcome). |
1073 | '" alt="'.s($stroutcome).'"/>'; | |
6cc3e350 | 1074 | } else { |
dc482cfa | 1075 | $strmanual = get_string('manualitem', 'grades'); |
b5d0cafc | 1076 | return '<img src="'.$OUTPUT->pix_url('t/manual_item') . '" '. |
cf72e2dd | 1077 | 'class="icon itemicon" title="'.s($strmanual). |
1078 | '" alt="'.s($strmanual).'"/>'; | |
6cc3e350 | 1079 | } |
1080 | } | |
1081 | break; | |
1082 | ||
1083 | case 'category': | |
dc482cfa | 1084 | $strcat = get_string('category', 'grades'); |
b5d0cafc | 1085 | return '<img src="'.$OUTPUT->pix_url('f/folder') . '" class="icon itemicon" ' . |
cf72e2dd | 1086 | 'title="'.s($strcat).'" alt="'.s($strcat).'" />'; |
6cc3e350 | 1087 | } |
1088 | ||
1089 | if ($spacerifnone) { | |
b11681e0 | 1090 | return $OUTPUT->spacer().' '; |
6cc3e350 | 1091 | } else { |
1092 | return ''; | |
1093 | } | |
1094 | } | |
6391ebe7 | 1095 | |
e98871a2 | 1096 | /** |
6cc3e350 | 1097 | * Returns name of element optionally with icon and link |
cf72e2dd | 1098 | * |
1099 | * @param array &$element An array representing an element in the grade_tree | |
1100 | * @param bool $withlink Whether or not this header has a link | |
1101 | * @param bool $icon Whether or not to display an icon with this header | |
1102 | * @param bool $spacerifnone return spacer if no icon found | |
1103 | * | |
1104 | * @return string header | |
e98871a2 | 1105 | */ |
d24832f9 | 1106 | public function get_element_header(&$element, $withlink=false, $icon=true, $spacerifnone=false) { |
6cc3e350 | 1107 | global $CFG; |
1108 | ||
1109 | $header = ''; | |
1110 | ||
1111 | if ($icon) { | |
1112 | $header .= $this->get_element_icon($element, $spacerifnone); | |
1113 | } | |
1114 | ||
dc482cfa | 1115 | $header .= $element['object']->get_name(); |
6cc3e350 | 1116 | |
cf72e2dd | 1117 | if ($element['type'] != 'item' and $element['type'] != 'categoryitem' and |
1118 | $element['type'] != 'courseitem') { | |
6cc3e350 | 1119 | return $header; |
1120 | } | |
1121 | ||
1122 | $itemtype = $element['object']->itemtype; | |
1123 | $itemmodule = $element['object']->itemmodule; | |
1124 | $iteminstance = $element['object']->iteminstance; | |
1125 | ||
1126 | if ($withlink and $itemtype=='mod' and $iteminstance and $itemmodule) { | |
46d1af82 | 1127 | if ($cm = get_coursemodule_from_instance($itemmodule, $iteminstance, $this->courseid)) { |
6cc3e350 | 1128 | |
ace9051c | 1129 | $a = new stdClass(); |
45a639d5 | 1130 | $a->name = get_string('modulename', $element['object']->itemmodule); |
dc482cfa | 1131 | $title = get_string('linktoactivity', 'grades', $a); |
46d1af82 | 1132 | $dir = $CFG->dirroot.'/mod/'.$itemmodule; |
6cc3e350 | 1133 | |
46d1af82 | 1134 | if (file_exists($dir.'/grade.php')) { |
1135 | $url = $CFG->wwwroot.'/mod/'.$itemmodule.'/grade.php?id='.$cm->id; | |
1136 | } else { | |
1137 | $url = $CFG->wwwroot.'/mod/'.$itemmodule.'/view.php?id='.$cm->id; | |
1138 | } | |
6cc3e350 | 1139 | |
1edd08ab | 1140 | $header = '<a href="'.$url.'" title="'.s($title).'">'.$header.'</a>'; |
46d1af82 | 1141 | } |
6cc3e350 | 1142 | } |
1143 | ||
1144 | return $header; | |
1145 | } | |
1146 | ||
1147 | /** | |
1148 | * Returns the grade eid - the grade may not exist yet. | |
cf72e2dd | 1149 | * |
1150 | * @param grade_grade $grade_grade A grade_grade object | |
1151 | * | |
6cc3e350 | 1152 | * @return string eid |
1153 | */ | |
d24832f9 | 1154 | public function get_grade_eid($grade_grade) { |
6cc3e350 | 1155 | if (empty($grade_grade->id)) { |
1156 | return 'n'.$grade_grade->itemid.'u'.$grade_grade->userid; | |
1157 | } else { | |
1158 | return 'g'.$grade_grade->id; | |
1159 | } | |
1160 | } | |
1161 | ||
1162 | /** | |
1163 | * Returns the grade_item eid | |
cf72e2dd | 1164 | * @param grade_item $grade_item A grade_item object |
6cc3e350 | 1165 | * @return string eid |
1166 | */ | |
d24832f9 | 1167 | public function get_item_eid($grade_item) { |
6cc3e350 | 1168 | return 'i'.$grade_item->id; |
1169 | } | |
1170 | ||
cf72e2dd | 1171 | /** |
1172 | * Given a grade_tree element, returns an array of parameters | |
1173 | * used to build an icon for that element. | |
1174 | * | |
1175 | * @param array $element An array representing an element in the grade_tree | |
1176 | * | |
1177 | * @return array | |
1178 | */ | |
1179 | public function get_params_for_iconstr($element) { | |
9ecd4386 | 1180 | $strparams = new stdClass(); |
1181 | $strparams->category = ''; | |
1182 | $strparams->itemname = ''; | |
1183 | $strparams->itemmodule = ''; | |
cf72e2dd | 1184 | |
9ecd4386 | 1185 | if (!method_exists($element['object'], 'get_name')) { |
1186 | return $strparams; | |
1187 | } | |
345674ca | 1188 | |
b285c5aa | 1189 | $strparams->itemname = html_to_text($element['object']->get_name()); |
9ecd4386 | 1190 | |
1191 | // If element name is categorytotal, get the name of the parent category | |
1192 | if ($strparams->itemname == get_string('categorytotal', 'grades')) { | |
1193 | $parent = $element['object']->get_parent_category(); | |
1194 | $strparams->category = $parent->get_name() . ' '; | |
1195 | } else { | |
1196 | $strparams->category = ''; | |
1197 | } | |
1198 | ||
1199 | $strparams->itemmodule = null; | |
1200 | if (isset($element['object']->itemmodule)) { | |
1201 | $strparams->itemmodule = $element['object']->itemmodule; | |
345674ca | 1202 | } |
9ecd4386 | 1203 | return $strparams; |
1204 | } | |
1205 | ||
6cc3e350 | 1206 | /** |
1207 | * Return edit icon for give element | |
cf72e2dd | 1208 | * |
1209 | * @param array $element An array representing an element in the grade_tree | |
1210 | * @param object $gpr A grade_plugin_return object | |
1211 | * | |
6cc3e350 | 1212 | * @return string |
1213 | */ | |
d24832f9 | 1214 | public function get_edit_icon($element, $gpr) { |
6b608f8f | 1215 | global $CFG, $OUTPUT; |
6cc3e350 | 1216 | |
1217 | if (!has_capability('moodle/grade:manage', $this->context)) { | |
1218 | if ($element['type'] == 'grade' and has_capability('moodle/grade:edit', $this->context)) { | |
1219 | // oki - let them override grade | |
1220 | } else { | |
1221 | return ''; | |
1222 | } | |
1223 | } | |
1224 | ||
05aba805 | 1225 | static $strfeedback = null; |
1226 | static $streditgrade = null; | |
1227 | if (is_null($streditgrade)) { | |
1228 | $streditgrade = get_string('editgrade', 'grades'); | |
1229 | $strfeedback = get_string('feedback'); | |
6cc3e350 | 1230 | } |
1231 | ||
9ecd4386 | 1232 | $strparams = $this->get_params_for_iconstr($element); |
6ef4878b | 1233 | |
6cc3e350 | 1234 | $object = $element['object']; |
6cc3e350 | 1235 | |
1236 | switch ($element['type']) { | |
1237 | case 'item': | |
1238 | case 'categoryitem': | |
1239 | case 'courseitem': | |
9ecd4386 | 1240 | $stredit = get_string('editverbose', 'grades', $strparams); |
6cc3e350 | 1241 | if (empty($object->outcomeid) || empty($CFG->enableoutcomes)) { |
a6855934 | 1242 | $url = new moodle_url('/grade/edit/tree/item.php', |
1c1f64a2 | 1243 | array('courseid' => $this->courseid, 'id' => $object->id)); |
6cc3e350 | 1244 | } else { |
a6855934 | 1245 | $url = new moodle_url('/grade/edit/tree/outcomeitem.php', |
1c1f64a2 | 1246 | array('courseid' => $this->courseid, 'id' => $object->id)); |
6cc3e350 | 1247 | } |
6cc3e350 | 1248 | break; |
1249 | ||
1250 | case 'category': | |
9ecd4386 | 1251 | $stredit = get_string('editverbose', 'grades', $strparams); |
a6855934 | 1252 | $url = new moodle_url('/grade/edit/tree/category.php', |
1c1f64a2 | 1253 | array('courseid' => $this->courseid, 'id' => $object->id)); |
6cc3e350 | 1254 | break; |
1255 | ||
1256 | case 'grade': | |
05aba805 | 1257 | $stredit = $streditgrade; |
6cc3e350 | 1258 | if (empty($object->id)) { |
a6855934 | 1259 | $url = new moodle_url('/grade/edit/tree/grade.php', |
1c1f64a2 | 1260 | array('courseid' => $this->courseid, 'itemid' => $object->itemid, 'userid' => $object->userid)); |
6cc3e350 | 1261 | } else { |
a6855934 | 1262 | $url = new moodle_url('/grade/edit/tree/grade.php', |
1c1f64a2 | 1263 | array('courseid' => $this->courseid, 'id' => $object->id)); |
6cc3e350 | 1264 | } |
6cc3e350 | 1265 | if (!empty($object->feedback)) { |
1266 | $feedback = addslashes_js(trim(format_string($object->feedback, $object->feedbackformat))); | |
6cc3e350 | 1267 | } |
1268 | break; | |
1269 | ||
1270 | default: | |
1271 | $url = null; | |
1272 | } | |
1273 | ||
1274 | if ($url) { | |
c63923bd | 1275 | return $OUTPUT->action_icon($gpr->add_url_params($url), new pix_icon('t/edit', $stredit)); |
6cc3e350 | 1276 | |
1277 | } else { | |
1278 | return ''; | |
1279 | } | |
1280 | } | |
1281 | ||
1282 | /** | |
1283 | * Return hiding icon for give element | |
cf72e2dd | 1284 | * |
1285 | * @param array $element An array representing an element in the grade_tree | |
1286 | * @param object $gpr A grade_plugin_return object | |
1287 | * | |
6cc3e350 | 1288 | * @return string |
1289 | */ | |
d24832f9 | 1290 | public function get_hiding_icon($element, $gpr) { |
5d3b9994 | 1291 | global $CFG, $OUTPUT; |
6cc3e350 | 1292 | |
cf72e2dd | 1293 | if (!has_capability('moodle/grade:manage', $this->context) and |
1294 | !has_capability('moodle/grade:hide', $this->context)) { | |
6cc3e350 | 1295 | return ''; |
1296 | } | |
1297 | ||
345674ca | 1298 | $strparams = $this->get_params_for_iconstr($element); |
9ecd4386 | 1299 | $strshow = get_string('showverbose', 'grades', $strparams); |
345674ca | 1300 | $strhide = get_string('hideverbose', 'grades', $strparams); |
6cc3e350 | 1301 | |
a6855934 | 1302 | $url = new moodle_url('/grade/edit/tree/action.php', array('id' => $this->courseid, 'sesskey' => sesskey(), 'eid' => $element['eid'])); |
8ae8bf8a | 1303 | $url = $gpr->add_url_params($url); |
1c1f64a2 | 1304 | |
6cc3e350 | 1305 | if ($element['object']->is_hidden()) { |
8ae8bf8a | 1306 | $type = 'show'; |
6cc3e350 | 1307 | $tooltip = $strshow; |
1308 | ||
cf72e2dd | 1309 | // Change the icon and add a tooltip showing the date |
1310 | if ($element['type'] != 'category' and $element['object']->get_hidden() > 1) { | |
8ae8bf8a | 1311 | $type = 'hiddenuntil'; |
cf72e2dd | 1312 | $tooltip = get_string('hiddenuntildate', 'grades', |
1313 | userdate($element['object']->get_hidden())); | |
6cc3e350 | 1314 | } |
1315 | ||
8ae8bf8a | 1316 | $url->param('action', 'show'); |
b9cc756e | 1317 | |
c63923bd | 1318 | $hideicon = $OUTPUT->action_icon($url, new pix_icon('t/'.$type, $tooltip, 'moodle', array('alt'=>$strshow, 'class'=>'iconsmall'))); |
6cc3e350 | 1319 | |
1320 | } else { | |
8ae8bf8a | 1321 | $url->param('action', 'hide'); |
c63923bd | 1322 | $hideicon = $OUTPUT->action_icon($url, new pix_icon('t/hide', $strhide)); |
6cc3e350 | 1323 | } |
1c1f64a2 | 1324 | |
8ae8bf8a | 1325 | return $hideicon; |
6cc3e350 | 1326 | } |
1327 | ||
1328 | /** | |
2673c733 | 1329 | * Return locking icon for given element |
cf72e2dd | 1330 | * |
1331 | * @param array $element An array representing an element in the grade_tree | |
1332 | * @param object $gpr A grade_plugin_return object | |
1333 | * | |
6cc3e350 | 1334 | * @return string |
1335 | */ | |
d24832f9 | 1336 | public function get_locking_icon($element, $gpr) { |
6b608f8f | 1337 | global $CFG, $OUTPUT; |
6cc3e350 | 1338 | |
345674ca | 1339 | $strparams = $this->get_params_for_iconstr($element); |
9ecd4386 | 1340 | $strunlock = get_string('unlockverbose', 'grades', $strparams); |
1341 | $strlock = get_string('lockverbose', 'grades', $strparams); | |
d24832f9 | 1342 | |
a6855934 | 1343 | $url = new moodle_url('/grade/edit/tree/action.php', array('id' => $this->courseid, 'sesskey' => sesskey(), 'eid' => $element['eid'])); |
8ae8bf8a | 1344 | $url = $gpr->add_url_params($url); |
1c1f64a2 | 1345 | |
2673c733 | 1346 | // Don't allow an unlocking action for a grade whose grade item is locked: just print a state icon |
1347 | if ($element['type'] == 'grade' && $element['object']->grade_item->is_locked()) { | |
1348 | $strparamobj = new stdClass(); | |
1349 | $strparamobj->itemname = $element['object']->grade_item->itemname; | |
1350 | $strnonunlockable = get_string('nonunlockableverbose', 'grades', $strparamobj); | |
1c1f64a2 | 1351 | |
000c278c | 1352 | $action = $OUTPUT->pix_icon('t/unlock_gray', $strnonunlockable); |
8ae8bf8a | 1353 | |
cf72e2dd | 1354 | } else if ($element['object']->is_locked()) { |
8ae8bf8a | 1355 | $type = 'unlock'; |
6cc3e350 | 1356 | $tooltip = $strunlock; |
1357 | ||
cf72e2dd | 1358 | // Change the icon and add a tooltip showing the date |
1359 | if ($element['type'] != 'category' and $element['object']->get_locktime() > 1) { | |
8ae8bf8a | 1360 | $type = 'locktime'; |
cf72e2dd | 1361 | $tooltip = get_string('locktimedate', 'grades', |
1362 | userdate($element['object']->get_locktime())); | |
6cc3e350 | 1363 | } |
1364 | ||
8ae8bf8a PS |
1365 | if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:unlock', $this->context)) { |
1366 | $action = ''; | |
1367 | } else { | |
1368 | $url->param('action', 'unlock'); | |
c63923bd | 1369 | $action = $OUTPUT->action_icon($url, new pix_icon('t/'.$type, $tooltip, 'moodle', array('alt'=>$strunlock, 'class'=>'smallicon'))); |
6cc3e350 | 1370 | } |
6cc3e350 | 1371 | |
1372 | } else { | |
8ae8bf8a PS |
1373 | if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:lock', $this->context)) { |
1374 | $action = ''; | |
1375 | } else { | |
1376 | $url->param('action', 'lock'); | |
c63923bd | 1377 | $action = $OUTPUT->action_icon($url, new pix_icon('t/lock', $strlock)); |
6cc3e350 | 1378 | } |
6cc3e350 | 1379 | } |
8ae8bf8a | 1380 | |
6cc3e350 | 1381 | return $action; |
1382 | } | |
1383 | ||
1384 | /** | |
1385 | * Return calculation icon for given element | |
cf72e2dd | 1386 | * |
1387 | * @param array $element An array representing an element in the grade_tree | |
1388 | * @param object $gpr A grade_plugin_return object | |
1389 | * | |
6cc3e350 | 1390 | * @return string |
1391 | */ | |
d24832f9 | 1392 | public function get_calculation_icon($element, $gpr) { |
666e8458 | 1393 | global $CFG, $OUTPUT; |
6cc3e350 | 1394 | if (!has_capability('moodle/grade:manage', $this->context)) { |
1395 | return ''; | |
1396 | } | |
1397 | ||
6cc3e350 | 1398 | $type = $element['type']; |
1399 | $object = $element['object']; | |
1400 | ||
1401 | if ($type == 'item' or $type == 'courseitem' or $type == 'categoryitem') { | |
345674ca | 1402 | $strparams = $this->get_params_for_iconstr($element); |
9ecd4386 | 1403 | $streditcalculation = get_string('editcalculationverbose', 'grades', $strparams); |
6cc3e350 | 1404 | |
cf72e2dd | 1405 | $is_scale = $object->gradetype == GRADE_TYPE_SCALE; |
1406 | $is_value = $object->gradetype == GRADE_TYPE_VALUE; | |
1407 | ||
6cc3e350 | 1408 | // show calculation icon only when calculation possible |
cf72e2dd | 1409 | if (!$object->is_external_item() and ($is_scale or $is_value)) { |
6cc3e350 | 1410 | if ($object->is_calculated()) { |
666e8458 | 1411 | $icon = 't/calc'; |
6cc3e350 | 1412 | } else { |
666e8458 | 1413 | $icon = 't/calc_off'; |
6cc3e350 | 1414 | } |
cf72e2dd | 1415 | |
a6855934 | 1416 | $url = new moodle_url('/grade/edit/tree/calculation.php', array('courseid' => $this->courseid, 'id' => $object->id)); |
8ae8bf8a | 1417 | $url = $gpr->add_url_params($url); |
c63923bd | 1418 | return $OUTPUT->action_icon($url, new pix_icon($icon, $streditcalculation)) . "\n"; |
6cc3e350 | 1419 | } |
1420 | } | |
1421 | ||
1c1f64a2 | 1422 | return ''; |
6cc3e350 | 1423 | } |
1424 | } | |
1425 | ||
1426 | /** | |
1427 | * Flat structure similar to grade tree. | |
cf72e2dd | 1428 | * |
1429 | * @uses grade_structure | |
1430 | * @package moodlecore | |
1431 | * @copyright 2009 Nicolas Connault | |
1432 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
6cc3e350 | 1433 | */ |
1434 | class grade_seq extends grade_structure { | |
1435 | ||
6cc3e350 | 1436 | /** |
1437 | * 1D array of elements | |
1438 | */ | |
d24832f9 | 1439 | public $elements; |
e98871a2 | 1440 | |
1441 | /** | |
1442 | * Constructor, retrieves and stores array of all grade_category and grade_item | |
1443 | * objects for the given courseid. Full objects are instantiated. Ordering sequence is fixed if needed. | |
cf72e2dd | 1444 | * |
1445 | * @param int $courseid The course id | |
1446 | * @param bool $category_grade_last category grade item is the last child | |
1447 | * @param bool $nooutcomes Whether or not outcomes should be included | |
e98871a2 | 1448 | */ |
d24832f9 | 1449 | public function grade_seq($courseid, $category_grade_last=false, $nooutcomes=false) { |
e98871a2 | 1450 | global $USER, $CFG; |
1451 | ||
1452 | $this->courseid = $courseid; | |
e98871a2 | 1453 | $this->context = get_context_instance(CONTEXT_COURSE, $courseid); |
1454 | ||
1455 | // get course grade tree | |
1456 | $top_element = grade_category::fetch_course_tree($courseid, true); | |
1457 | ||
6391ebe7 | 1458 | $this->elements = grade_seq::flatten($top_element, $category_grade_last, $nooutcomes); |
1459 | ||
1460 | foreach ($this->elements as $key=>$unused) { | |
b89a70ce | 1461 | $this->items[$this->elements[$key]['object']->id] =& $this->elements[$key]['object']; |
6391ebe7 | 1462 | } |
e98871a2 | 1463 | } |
1464 | ||
1465 | /** | |
1466 | * Static recursive helper - makes the grade_item for category the last children | |
cf72e2dd | 1467 | * |
1468 | * @param array &$element The seed of the recursion | |
1469 | * @param bool $category_grade_last category grade item is the last child | |
1470 | * @param bool $nooutcomes Whether or not outcomes should be included | |
1471 | * | |
1472 | * @return array | |
e98871a2 | 1473 | */ |
d24832f9 | 1474 | public function flatten(&$element, $category_grade_last, $nooutcomes) { |
e98871a2 | 1475 | if (empty($element['children'])) { |
1476 | return array(); | |
1477 | } | |
1478 | $children = array(); | |
1479 | ||
1480 | foreach ($element['children'] as $sortorder=>$unused) { | |
cf72e2dd | 1481 | if ($nooutcomes and $element['type'] != 'category' and |
1482 | $element['children'][$sortorder]['object']->is_outcome_item()) { | |
e98871a2 | 1483 | continue; |
1484 | } | |
1485 | $children[] = $element['children'][$sortorder]; | |
1486 | } | |
1487 | unset($element['children']); | |
1488 | ||
1489 | if ($category_grade_last and count($children) > 1) { | |
1490 | $cat_item = array_shift($children); | |
1491 | array_push($children, $cat_item); | |
1492 | } | |
1493 | ||
1494 | $result = array(); | |
1495 | foreach ($children as $child) { | |
e0724506 | 1496 | if ($child['type'] == 'category') { |
6391ebe7 | 1497 | $result = $result + grade_seq::flatten($child, $category_grade_last, $nooutcomes); |
e98871a2 | 1498 | } else { |
1499 | $child['eid'] = 'i'.$child['object']->id; | |
6391ebe7 | 1500 | $result[$child['object']->id] = $child; |
e98871a2 | 1501 | } |
1502 | } | |
1503 | ||
1504 | return $result; | |
1505 | } | |
1506 | ||
1507 | /** | |
1508 | * Parses the array in search of a given eid and returns a element object with | |
1509 | * information about the element it has found. | |
cf72e2dd | 1510 | * |
1511 | * @param int $eid Gradetree Element ID | |
1512 | * | |
e98871a2 | 1513 | * @return object element |
1514 | */ | |
d24832f9 | 1515 | public function locate_element($eid) { |
e98871a2 | 1516 | // it is a grade - construct a new object |
1517 | if (strpos($eid, 'n') === 0) { | |
1518 | if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) { | |
1519 | return null; | |
1520 | } | |
1521 | ||
1522 | $itemid = $matches[1]; | |
1523 | $userid = $matches[2]; | |
1524 | ||
1525 | //extra security check - the grade item must be in this tree | |
1526 | if (!$item_el = $this->locate_element('i'.$itemid)) { | |
1527 | return null; | |
1528 | } | |
1529 | ||
1530 | // $gradea->id may be null - means does not exist yet | |
1531 | $grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid)); | |
1532 | ||
1533 | $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods! | |
1534 | return array('eid'=>'n'.$itemid.'u'.$userid,'object'=>$grade, 'type'=>'grade'); | |
1535 | ||
1536 | } else if (strpos($eid, 'g') === 0) { | |
cf72e2dd | 1537 | $id = (int) substr($eid, 1); |
e98871a2 | 1538 | if (!$grade = grade_grade::fetch(array('id'=>$id))) { |
1539 | return null; | |
1540 | } | |
1541 | //extra security check - the grade item must be in this tree | |
1542 | if (!$item_el = $this->locate_element('i'.$grade->itemid)) { | |
1543 | return null; | |
1544 | } | |
1545 | $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods! | |
1546 | return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade'); | |
1547 | } | |
1548 | ||
1549 | // it is a category or item | |
6391ebe7 | 1550 | foreach ($this->elements as $element) { |
6cc3e350 | 1551 | if ($element['eid'] == $eid) { |
1552 | return $element; | |
1553 | } | |
e98871a2 | 1554 | } |
6cc3e350 | 1555 | |
1556 | return null; | |
e98871a2 | 1557 | } |
e98871a2 | 1558 | } |
1559 | ||
7a6b7acf | 1560 | /** |
1561 | * This class represents a complete tree of categories, grade_items and final grades, | |
1562 | * organises as an array primarily, but which can also be converted to other formats. | |
1563 | * It has simple method calls with complex implementations, allowing for easy insertion, | |
1564 | * deletion and moving of items and categories within the tree. | |
cf72e2dd | 1565 | * |
1566 | * @uses grade_structure | |
1567 | * @package moodlecore | |
1568 | * @copyright 2009 Nicolas Connault | |
1569 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
7a6b7acf | 1570 | */ |
6cc3e350 | 1571 | class grade_tree extends grade_structure { |
7a6b7acf | 1572 | |
1573 | /** | |
1574 | * The basic representation of the tree as a hierarchical, 3-tiered array. | |
1575 | * @var object $top_element | |
1576 | */ | |
d24832f9 | 1577 | public $top_element; |
7a6b7acf | 1578 | |
7a6b7acf | 1579 | /** |
1580 | * 2D array of grade items and categories | |
cf72e2dd | 1581 | * @var array $levels |
7a6b7acf | 1582 | */ |
d24832f9 | 1583 | public $levels; |
7a6b7acf | 1584 | |
b89a70ce | 1585 | /** |
1586 | * Grade items | |
cf72e2dd | 1587 | * @var array $items |
b89a70ce | 1588 | */ |
d24832f9 | 1589 | public $items; |
b89a70ce | 1590 | |
7a6b7acf | 1591 | /** |
1592 | * Constructor, retrieves and stores a hierarchical array of all grade_category and grade_item | |
e98871a2 | 1593 | * objects for the given courseid. Full objects are instantiated. Ordering sequence is fixed if needed. |
cf72e2dd | 1594 | * |
1595 | * @param int $courseid The Course ID | |
1596 | * @param bool $fillers include fillers and colspans, make the levels var "rectangular" | |
1597 | * @param bool $category_grade_last category grade item is the last child | |
4faf5f99 | 1598 | * @param array $collapsed array of collapsed categories |
cf72e2dd | 1599 | * @param bool $nooutcomes Whether or not outcomes should be included |
7a6b7acf | 1600 | */ |
cf72e2dd | 1601 | public function grade_tree($courseid, $fillers=true, $category_grade_last=false, |
1602 | $collapsed=null, $nooutcomes=false) { | |
7a6b7acf | 1603 | global $USER, $CFG; |
1604 | ||
1605 | $this->courseid = $courseid; | |
7a6b7acf | 1606 | $this->levels = array(); |
2cc773f5 | 1607 | $this->context = get_context_instance(CONTEXT_COURSE, $courseid); |
7a6b7acf | 1608 | |
1609 | // get course grade tree | |
1610 | $this->top_element = grade_category::fetch_course_tree($courseid, true); | |
1611 | ||
4faf5f99 | 1612 | // collapse the categories if requested |
1613 | if (!empty($collapsed)) { | |
1614 | grade_tree::category_collapse($this->top_element, $collapsed); | |
1615 | } | |
1616 | ||
aea4df41 | 1617 | // no otucomes if requested |
1618 | if (!empty($nooutcomes)) { | |
1619 | grade_tree::no_outcomes($this->top_element); | |
1620 | } | |
1621 | ||
4faf5f99 | 1622 | // move category item to last position in category |
7a6b7acf | 1623 | if ($category_grade_last) { |
1624 | grade_tree::category_grade_last($this->top_element); | |
1625 | } | |
1626 | ||
1627 | if ($fillers) { | |
1628 | // inject fake categories == fillers | |
1629 | grade_tree::inject_fillers($this->top_element, 0); | |
1630 | // add colspans to categories and fillers | |
1631 | grade_tree::inject_colspans($this->top_element); | |
1632 | } | |
1633 | ||
1634 | grade_tree::fill_levels($this->levels, $this->top_element, 0); | |
d297269d | 1635 | |
7a6b7acf | 1636 | } |
1637 | ||
4faf5f99 | 1638 | /** |
1639 | * Static recursive helper - removes items from collapsed categories | |
cf72e2dd | 1640 | * |
1641 | * @param array &$element The seed of the recursion | |
4faf5f99 | 1642 | * @param array $collapsed array of collapsed categories |
cf72e2dd | 1643 | * |
4faf5f99 | 1644 | * @return void |
1645 | */ | |
d24832f9 | 1646 | public function category_collapse(&$element, $collapsed) { |
4faf5f99 | 1647 | if ($element['type'] != 'category') { |
1648 | return; | |
1649 | } | |
1650 | if (empty($element['children']) or count($element['children']) < 2) { | |
1651 | return; | |
1652 | } | |
1653 | ||
384960dd | 1654 | if (in_array($element['object']->id, $collapsed['aggregatesonly'])) { |
4faf5f99 | 1655 | $category_item = reset($element['children']); //keep only category item |
1656 | $element['children'] = array(key($element['children'])=>$category_item); | |
1657 | ||
1658 | } else { | |
384960dd | 1659 | if (in_array($element['object']->id, $collapsed['gradesonly'])) { // Remove category item |
1660 | reset($element['children']); | |
1661 | $first_key = key($element['children']); | |
1662 | unset($element['children'][$first_key]); | |
1663 | } | |
1664 | foreach ($element['children'] as $sortorder=>$child) { // Recurse through the element's children | |
4faf5f99 | 1665 | grade_tree::category_collapse($element['children'][$sortorder], $collapsed); |
1666 | } | |
1667 | } | |
1668 | } | |
7a6b7acf | 1669 | |
aea4df41 | 1670 | /** |
1671 | * Static recursive helper - removes all outcomes | |
cf72e2dd | 1672 | * |
1673 | * @param array &$element The seed of the recursion | |
1674 | * | |
aea4df41 | 1675 | * @return void |
1676 | */ | |
d24832f9 | 1677 | public function no_outcomes(&$element) { |
aea4df41 | 1678 | if ($element['type'] != 'category') { |
1679 | return; | |
1680 | } | |
1681 | foreach ($element['children'] as $sortorder=>$child) { | |
1682 | if ($element['children'][$sortorder]['type'] == 'item' | |
1683 | and $element['children'][$sortorder]['object']->is_outcome_item()) { | |
1684 | unset($element['children'][$sortorder]); | |
1685 | ||
d4795a07 | 1686 | } else if ($element['children'][$sortorder]['type'] == 'category') { |
aea4df41 | 1687 | grade_tree::no_outcomes($element['children'][$sortorder]); |
1688 | } | |
1689 | } | |
1690 | } | |
1691 | ||
7a6b7acf | 1692 | /** |
1693 | * Static recursive helper - makes the grade_item for category the last children | |
cf72e2dd | 1694 | * |
1695 | * @param array &$element The seed of the recursion | |
1696 | * | |
7a6b7acf | 1697 | * @return void |
1698 | */ | |
d24832f9 | 1699 | public function category_grade_last(&$element) { |
7a6b7acf | 1700 | if (empty($element['children'])) { |
1701 | return; | |
1702 | } | |
1703 | if (count($element['children']) < 2) { | |
1704 | return; | |
1705 | } | |
3e0e2436 | 1706 | $first_item = reset($element['children']); |
4a3dfd9a | 1707 | if ($first_item['type'] == 'categoryitem' or $first_item['type'] == 'courseitem') { |
3e0e2436 | 1708 | // the category item might have been already removed |
1709 | $order = key($element['children']); | |
1710 | unset($element['children'][$order]); | |
1711 | $element['children'][$order] =& $first_item; | |
1712 | } | |
206f9953 | 1713 | foreach ($element['children'] as $sortorder => $child) { |
7a6b7acf | 1714 | grade_tree::category_grade_last($element['children'][$sortorder]); |
1715 | } | |
1716 | } | |
1717 | ||
1718 | /** | |
1719 | * Static recursive helper - fills the levels array, useful when accessing tree elements of one level | |
cf72e2dd | 1720 | * |
1721 | * @param array &$levels The levels of the grade tree through which to recurse | |
1722 | * @param array &$element The seed of the recursion | |
1723 | * @param int $depth How deep are we? | |
7a6b7acf | 1724 | * @return void |
1725 | */ | |
d24832f9 | 1726 | public function fill_levels(&$levels, &$element, $depth) { |
7a6b7acf | 1727 | if (!array_key_exists($depth, $levels)) { |
1728 | $levels[$depth] = array(); | |
1729 | } | |
1730 | ||
1731 | // prepare unique identifier | |
1732 | if ($element['type'] == 'category') { | |
1733 | $element['eid'] = 'c'.$element['object']->id; | |
1734 | } else if (in_array($element['type'], array('item', 'courseitem', 'categoryitem'))) { | |
1735 | $element['eid'] = 'i'.$element['object']->id; | |
b89a70ce | 1736 | $this->items[$element['object']->id] =& $element['object']; |
7a6b7acf | 1737 | } |
1738 | ||
1739 | $levels[$depth][] =& $element; | |
1740 | $depth++; | |
1741 | if (empty($element['children'])) { | |
1742 | return; | |
1743 | } | |
1744 | $prev = 0; | |
1745 | foreach ($element['children'] as $sortorder=>$child) { | |
1746 | grade_tree::fill_levels($levels, $element['children'][$sortorder], $depth); | |
1747 | $element['children'][$sortorder]['prev'] = $prev; | |
1748 | $element['children'][$sortorder]['next'] = 0; | |
1749 | if ($prev) { | |
1750 | $element['children'][$prev]['next'] = $sortorder; | |
1751 | } | |
1752 | $prev = $sortorder; | |
1753 | } | |
1754 | } | |
1755 | ||
1756 | /** | |
1757 | * Static recursive helper - makes full tree (all leafes are at the same level) | |
cf72e2dd | 1758 | * |
1759 | * @param array &$element The seed of the recursion | |
1760 | * @param int $depth How deep are we? | |
1761 | * | |
1762 | * @return int | |
7a6b7acf | 1763 | */ |
d24832f9 | 1764 | public function inject_fillers(&$element, $depth) { |
7a6b7acf | 1765 | $depth++; |
1766 | ||
1767 | if (empty($element['children'])) { | |
1768 | return $depth; | |
1769 | } | |
1770 | $chdepths = array(); | |
1771 | $chids = array_keys($element['children']); | |
1772 | $last_child = end($chids); | |
1773 | $first_child = reset($chids); | |
1774 | ||
1775 | foreach ($chids as $chid) { | |
1776 | $chdepths[$chid] = grade_tree::inject_fillers($element['children'][$chid], $depth); | |
1777 | } | |
1778 | arsort($chdepths); | |
1779 | ||
1780 | $maxdepth = reset($chdepths); | |
1781 | foreach ($chdepths as $chid=>$chd) { | |
1782 | if ($chd == $maxdepth) { | |
1783 | continue; | |
1784 | } | |
1785 | for ($i=0; $i < $maxdepth-$chd; $i++) { | |
1786 | if ($chid == $first_child) { | |
1787 | $type = 'fillerfirst'; | |
1788 | } else if ($chid == $last_child) { | |
1789 | $type = 'fillerlast'; | |
1790 | } else { | |
1791 | $type = 'filler'; | |
1792 | } | |
1793 | $oldchild =& $element['children'][$chid]; | |
cf72e2dd | 1794 | $element['children'][$chid] = array('object'=>'filler', 'type'=>$type, |
1795 | 'eid'=>'', 'depth'=>$element['object']->depth, | |
1796 | 'children'=>array($oldchild)); | |
7a6b7acf | 1797 | } |
1798 | } | |
1799 | ||
1800 | return $maxdepth; | |
1801 | } | |
1802 | ||
1803 | /** | |
1804 | * Static recursive helper - add colspan information into categories | |
cf72e2dd | 1805 | * |
1806 | * @param array &$element The seed of the recursion | |
1807 | * | |
1808 | * @return int | |
7a6b7acf | 1809 | */ |
d24832f9 | 1810 | public function inject_colspans(&$element) { |
7a6b7acf | 1811 | if (empty($element['children'])) { |
1812 | return 1; | |
1813 | } | |
1814 | $count = 0; | |
1815 | foreach ($element['children'] as $key=>$child) { | |
1816 | $count += grade_tree::inject_colspans($element['children'][$key]); | |
1817 | } | |
1818 | $element['colspan'] = $count; | |
1819 | return $count; | |
1820 | } | |
1821 | ||
1822 | /** | |
1823 | * Parses the array in search of a given eid and returns a element object with | |
1824 | * information about the element it has found. | |
cf72e2dd | 1825 | * @param int $eid Gradetree Element ID |
7a6b7acf | 1826 | * @return object element |
1827 | */ | |
d24832f9 | 1828 | public function locate_element($eid) { |
d3c3da1b | 1829 | // it is a grade - construct a new object |
1830 | if (strpos($eid, 'n') === 0) { | |
1831 | if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) { | |
1832 | return null; | |
1833 | } | |
1834 | ||
1835 | $itemid = $matches[1]; | |
1836 | $userid = $matches[2]; | |
1837 | ||
1838 | //extra security check - the grade item must be in this tree | |
1839 | if (!$item_el = $this->locate_element('i'.$itemid)) { | |
1840 | return null; | |
1841 | } | |
1842 | ||
1843 | // $gradea->id may be null - means does not exist yet | |
1844 | $grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid)); | |
1845 | ||
1846 | $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods! | |
1847 | return array('eid'=>'n'.$itemid.'u'.$userid,'object'=>$grade, 'type'=>'grade'); | |
1848 | ||
1849 | } else if (strpos($eid, 'g') === 0) { | |
cf72e2dd | 1850 | $id = (int) substr($eid, 1); |
7a6b7acf | 1851 | if (!$grade = grade_grade::fetch(array('id'=>$id))) { |
1852 | return null; | |
1853 | } | |
1854 | //extra security check - the grade item must be in this tree | |
1855 | if (!$item_el = $this->locate_element('i'.$grade->itemid)) { | |
1856 | return null; | |
1857 | } | |
1858 | $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods! | |
1859 | return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade'); | |
1860 | } | |
1861 | ||
1862 | // it is a category or item | |
1863 | foreach ($this->levels as $row) { | |
1864 | foreach ($row as $element) { | |
1865 | if ($element['type'] == 'filler') { | |
1866 | continue; | |
1867 | } | |
1868 | if ($element['eid'] == $eid) { | |
1869 | return $element; | |
1870 | } | |
1871 | } | |
1872 | } | |
1873 | ||
1874 | return null; | |
1875 | } | |
d24832f9 | 1876 | |
b244b9b7 | 1877 | /** |
1878 | * Returns a well-formed XML representation of the grade-tree using recursion. | |
cf72e2dd | 1879 | * |
1880 | * @param array $root The current element in the recursion. If null, starts at the top of the tree. | |
1881 | * @param string $tabs The control character to use for tabs | |
1882 | * | |
b244b9b7 | 1883 | * @return string $xml |
1884 | */ | |
cf72e2dd | 1885 | public function exporttoxml($root=null, $tabs="\t") { |
b244b9b7 | 1886 | $xml = null; |
1887 | $first = false; | |
1888 | if (is_null($root)) { | |
1889 | $root = $this->top_element; | |
1890 | $xml = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n"; | |
1891 | $xml .= "<gradetree>\n"; | |
1892 | $first = true; | |
1893 | } | |
d24832f9 | 1894 | |
b244b9b7 | 1895 | $type = 'undefined'; |
1896 | if (strpos($root['object']->table, 'grade_categories') !== false) { | |
1897 | $type = 'category'; | |
cf72e2dd | 1898 | } else if (strpos($root['object']->table, 'grade_items') !== false) { |
b244b9b7 | 1899 | $type = 'item'; |
cf72e2dd | 1900 | } else if (strpos($root['object']->table, 'grade_outcomes') !== false) { |
b244b9b7 | 1901 | $type = 'outcome'; |
1902 | } | |
d24832f9 | 1903 | |
b244b9b7 | 1904 | $xml .= "$tabs<element type=\"$type\">\n"; |
1905 | foreach ($root['object'] as $var => $value) { | |
1906 | if (!is_object($value) && !is_array($value) && !empty($value)) { | |
1907 | $xml .= "$tabs\t<$var>$value</$var>\n"; | |
1908 | } | |
1909 | } | |
1910 | ||
1911 | if (!empty($root['children'])) { | |
1912 | $xml .= "$tabs\t<children>\n"; | |
1913 | foreach ($root['children'] as $sortorder => $child) { | |
1914 | $xml .= $this->exportToXML($child, $tabs."\t\t"); | |
1915 | } | |
1916 | $xml .= "$tabs\t</children>\n"; | |
1917 | } | |
d24832f9 | 1918 | |
b244b9b7 | 1919 | $xml .= "$tabs</element>\n"; |
1920 | ||
1921 | if ($first) { | |
1922 | $xml .= "</gradetree>"; | |
1923 | } | |
d24832f9 | 1924 | |
b244b9b7 | 1925 | return $xml; |
1926 | } | |
d24832f9 | 1927 | |
b244b9b7 | 1928 | /** |
1929 | * Returns a JSON representation of the grade-tree using recursion. | |
cf72e2dd | 1930 | * |
b244b9b7 | 1931 | * @param array $root The current element in the recursion. If null, starts at the top of the tree. |
1932 | * @param string $tabs Tab characters used to indent the string nicely for humans to enjoy | |
cf72e2dd | 1933 | * |
1934 | * @return string | |
b244b9b7 | 1935 | */ |
cf72e2dd | 1936 | public function exporttojson($root=null, $tabs="\t") { |
b244b9b7 | 1937 | $json = null; |
1938 | $first = false; | |
1939 | if (is_null($root)) { | |
1940 | $root = $this->top_element; | |
1941 | $first = true; | |
1942 | } | |
d24832f9 | 1943 | |
b244b9b7 | 1944 | $name = ''; |
1945 | ||
1946 | ||
1947 | if (strpos($root['object']->table, 'grade_categories') !== false) { | |
1948 | $name = $root['object']->fullname; | |
1949 | if ($name == '?') { | |
d24832f9 | 1950 | $name = $root['object']->get_name(); |
b244b9b7 | 1951 | } |
cf72e2dd | 1952 | } else if (strpos($root['object']->table, 'grade_items') !== false) { |
b244b9b7 | 1953 | $name = $root['object']->itemname; |
cf72e2dd | 1954 | } else if (strpos($root['object']->table, 'grade_outcomes') !== false) { |
b244b9b7 | 1955 | $name = $root['object']->itemname; |
1956 | } | |
d24832f9 | 1957 | |
b244b9b7 | 1958 | $json .= "$tabs {\n"; |
1959 | $json .= "$tabs\t \"type\": \"{$root['type']}\",\n"; | |
1960 | $json .= "$tabs\t \"name\": \"$name\",\n"; | |
1961 | ||
1962 | foreach ($root['object'] as $var => $value) { | |
1963 | if (!is_object($value) && !is_array($value) && !empty($value)) { | |
1964 | $json .= "$tabs\t \"$var\": \"$value\",\n"; | |
1965 | } | |
1966 | } | |
d24832f9 | 1967 | |
b244b9b7 | 1968 | $json = substr($json, 0, strrpos($json, ',')); |
d24832f9 | 1969 | |
b244b9b7 | 1970 | if (!empty($root['children'])) { |
1971 | $json .= ",\n$tabs\t\"children\": [\n"; | |
1972 | foreach ($root['children'] as $sortorder => $child) { | |
1973 | $json .= $this->exportToJSON($child, $tabs."\t\t"); | |
1974 | } | |
1975 | $json = substr($json, 0, strrpos($json, ',')); | |
1976 | $json .= "\n$tabs\t]\n"; | |
d24832f9 | 1977 | } |
b244b9b7 | 1978 | |
1979 | if ($first) { | |
1980 | $json .= "\n}"; | |
1981 | } else { | |
1982 | $json .= "\n$tabs},\n"; | |
1983 | } | |
d24832f9 | 1984 | |
b244b9b7 | 1985 | return $json; |
1986 | } | |
d24832f9 | 1987 | |
cf72e2dd | 1988 | /** |
1989 | * Returns the array of levels | |
1990 | * | |
1991 | * @return array | |
1992 | */ | |
d24832f9 | 1993 | public function get_levels() { |
1994 | return $this->levels; | |
1995 | } | |
1996 | ||
cf72e2dd | 1997 | /** |
1998 | * Returns the array of grade items | |
1999 | * | |
2000 | * @return array | |
2001 | */ | |
d24832f9 | 2002 | public function get_items() { |
2003 | return $this->items; | |
2004 | } | |
2005 | ||
cf72e2dd | 2006 | /** |
2007 | * Returns a specific Grade Item | |
2008 | * | |
2009 | * @param int $itemid The ID of the grade_item object | |
2010 | * | |
2011 | * @return grade_item | |
2012 | */ | |
d24832f9 | 2013 | public function get_item($itemid) { |
2014 | if (array_key_exists($itemid, $this->items)) { | |
2015 | return $this->items[$itemid]; | |
2016 | } else { | |
2017 | return false; | |
2018 | } | |
2019 | } | |
7a6b7acf | 2020 | } |
eef00ade | 2021 | |
2022 | /** | |
2023 | * Local shortcut function for creating an edit/delete button for a grade_* object. | |
4d27bc79 | 2024 | * @param string $type 'edit' or 'delete' |
eef00ade | 2025 | * @param int $courseid The Course ID |
2026 | * @param grade_* $object The grade_* object | |
2027 | * @return string html | |
2028 | */ | |
2029 | function grade_button($type, $courseid, $object) { | |
2030 | global $CFG, $OUTPUT; | |
2031 | if (preg_match('/grade_(.*)/', get_class($object), $matches)) { | |
2032 | $objectidstring = $matches[1] . 'id'; | |
2033 | } else { | |
2034 | throw new coding_exception('grade_button() only accepts grade_* objects as third parameter!'); | |
2035 | } | |
2036 | ||
2037 | $strdelete = get_string('delete'); | |
2038 | $stredit = get_string('edit'); | |
2039 | ||
eef00ade | 2040 | if ($type == 'delete') { |
8ae8bf8a | 2041 | $url = new moodle_url('index.php', array('id' => $courseid, $objectidstring => $object->id, 'action' => 'delete', 'sesskey' => sesskey())); |
eef00ade | 2042 | } else if ($type == 'edit') { |
8ae8bf8a | 2043 | $url = new moodle_url('edit.php', array('courseid' => $courseid, 'id' => $object->id)); |
eef00ade | 2044 | } |
2045 | ||
c63923bd | 2046 | return $OUTPUT->action_icon($url, new pix_icon('t/'.$type, ${'str'.$type})); |
eef00ade | 2047 | |
2048 | } | |
4d5059d4 SH |
2049 | |
2050 | /** | |
2051 | * This method adds settings to the settings block for the grade system and its | |
2052 | * plugins | |
2053 | * | |
2054 | * @global moodle_page $PAGE | |
2055 | */ | |
2056 | function grade_extend_settings($plugininfo, $courseid) { | |
2057 | global $PAGE; | |
2058 | ||
2059 | $gradenode = $PAGE->settingsnav->prepend(get_string('gradeadministration', 'grades'), null, navigation_node::TYPE_CONTAINER); | |
2060 | ||
2061 | $strings = array_shift($plugininfo); | |
2062 | ||
2063 | if ($reports = grade_helper::get_plugins_reports($courseid)) { | |
2064 | foreach ($reports as $report) { | |
2065 | $gradenode->add($report->string, $report->link, navigation_node::TYPE_SETTING, null, $report->id, new pix_icon('i/report', '')); | |
2066 | } | |
2067 | } | |
2068 | ||
2069 | if ($imports = grade_helper::get_plugins_import($courseid)) { | |
2070 | $importnode = $gradenode->add($strings['import'], null, navigation_node::TYPE_CONTAINER); | |
2071 | foreach ($imports as $import) { | |
2072 | $importnode->add($import->string, $import->link, navigation_node::TYPE_SETTING, null, $import->id, new pix_icon('i/restore', '')); | |
2073 | } | |
2074 | } | |
2075 | ||
2076 | if ($exports = grade_helper::get_plugins_export($courseid)) { | |
2077 | $exportnode = $gradenode->add($strings['export'], null, navigation_node::TYPE_CONTAINER); | |
2078 | foreach ($exports as $export) { | |
2079 | $exportnode->add($export->string, $export->link, navigation_node::TYPE_SETTING, null, $export->id, new pix_icon('i/backup', '')); | |
2080 | } | |
2081 | } | |
2082 | ||
2083 | if ($setting = grade_helper::get_info_manage_settings($courseid)) { | |
2084 | $gradenode->add(get_string('coursegradesettings', 'grades'), $setting->link, navigation_node::TYPE_SETTING, null, $setting->id, new pix_icon('i/settings', '')); | |
2085 | } | |
2086 | ||
2087 | if ($preferences = grade_helper::get_plugins_report_preferences($courseid)) { | |
2088 | $preferencesnode = $gradenode->add(get_string('myreportpreferences', 'grades'), null, navigation_node::TYPE_CONTAINER); | |
2089 | foreach ($preferences as $preference) { | |
2090 | $preferencesnode->add($preference->string, $preference->link, navigation_node::TYPE_SETTING, null, $preference->id, new pix_icon('i/settings', '')); | |
2091 | } | |
2092 | } | |
2093 | ||
2094 | if ($letters = grade_helper::get_info_letters($courseid)) { | |
2095 | $letters = array_shift($letters); | |
2096 | $gradenode->add($strings['letter'], $letters->link, navigation_node::TYPE_SETTING, null, $letters->id, new pix_icon('i/settings', '')); | |
2097 | } | |
2098 | ||
2099 | if ($outcomes = grade_helper::get_info_outcomes($courseid)) { | |
2100 | $outcomes = array_shift($outcomes); | |
2101 | $gradenode->add($strings['outcome'], $outcomes->link, navigation_node::TYPE_SETTING, null, $outcomes->id, new pix_icon('i/outcomes', '')); | |
2102 | } | |
2103 | ||
2104 | if ($scales = grade_helper::get_info_scales($courseid)) { | |
2105 | $gradenode->add($strings['scale'], $scales->link, navigation_node::TYPE_SETTING, null, $scales->id, new pix_icon('i/scales', '')); | |
2106 | } | |
2107 | ||
2108 | if ($categories = grade_helper::get_info_edit_structure($courseid)) { | |
2109 | $categoriesnode = $gradenode->add(get_string('categoriesanditems','grades'), null, navigation_node::TYPE_CONTAINER); | |
2110 | foreach ($categories as $category) { | |
2111 | $categoriesnode->add($category->string, $category->link, navigation_node::TYPE_SETTING, null, $category->id, new pix_icon('i/report', '')); | |
2112 | } | |
2113 | } | |
2114 | ||
2115 | if ($gradenode->contains_active_node()) { | |
2116 | // If the gradenode is active include the settings base node (gradeadministration) in | |
2117 | // the navbar, typcially this is ignored. | |
2118 | $PAGE->navbar->includesettingsbase = true; | |
2119 | ||
2120 | // If we can get the course admin node make sure it is closed by default | |
2121 | // as in this case the gradenode will be opened | |
2122 | if ($coursenode = $PAGE->settingsnav->get('courseadmin', navigation_node::TYPE_COURSE)){ | |
2123 | $coursenode->make_inactive(); | |
2124 | $coursenode->forceopen = false; | |
2125 | } | |
2126 | } | |
2127 | } | |
2128 | ||
2129 | /** | |
2130 | * Grade helper class | |
2131 | * | |
2132 | * This class provides several helpful functions that work irrespective of any | |
2133 | * current state. | |
2134 | * | |
2135 | * @copyright 2010 Sam Hemelryk | |
2136 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
2137 | */ | |
2138 | abstract class grade_helper { | |
2139 | /** | |
2140 | * Cached manage settings info {@see get_info_settings} | |
2141 | * @var grade_plugin_info|false | |
2142 | */ | |
2143 | protected static $managesetting = null; | |
2144 | /** | |
2145 | * Cached grade report plugins {@see get_plugins_reports} | |
2146 | * @var array|false | |
2147 | */ | |
2148 | protected static $gradereports = null; | |
2149 | /** | |
2150 | * Cached grade report plugins preferences {@see get_info_scales} | |
2151 | * @var array|false | |
2152 | */ | |
2153 | protected static $gradereportpreferences = null; | |
2154 | /** | |
2155 | * Cached scale info {@see get_info_scales} | |
2156 | * @var grade_plugin_info|false | |
2157 | */ | |
2158 | protected static $scaleinfo = null; | |
2159 | /** | |
2160 | * Cached outcome info {@see get_info_outcomes} | |
2161 | * @var grade_plugin_info|false | |
2162 | */ | |
2163 | protected static $outcomeinfo = null; | |
2164 | /** | |
2165 | * Cached info on edit structure {@see get_info_edit_structure} | |
2166 | * @var array|false | |
2167 | */ | |
2168 | protected static $edittree = null; | |
2169 | /** | |
2170 | * Cached leftter info {@see get_info_letters} | |
2171 | * @var grade_plugin_info|false | |
2172 | */ | |
2173 | protected static $letterinfo = null; | |
2174 | /** | |
2175 | * Cached grade import plugins {@see get_plugins_import} | |
2176 | * @var array|false | |
2177 | */ | |
2178 | protected static $importplugins = null; | |
2179 | /** | |
2180 | * Cached grade export plugins {@see get_plugins_export} | |
2181 | * @var array|false | |
2182 | */ | |
2183 | protected static $exportplugins = null; | |
2184 | /** | |
2185 | * Cached grade plugin strings | |
2186 | * @var array | |
2187 | */ | |
2188 | protected static $pluginstrings = null; | |
2189 | ||
2190 | /** | |
2191 | * Gets strings commonly used by the describe plugins | |
2192 | * | |
2193 | * report => get_string('view'), | |
2194 | * edittree => get_string('edittree', 'grades'), | |
2195 | * scale => get_string('scales'), | |
2196 | * outcome => get_string('outcomes', 'grades'), | |
2197 | * letter => get_string('letters', 'grades'), | |
2198 | * export => get_string('export', 'grades'), | |
2199 | * import => get_string('import'), | |
2200 | * preferences => get_string('mypreferences', 'grades'), | |
2201 | * settings => get_string('settings') | |
2202 | * | |
2203 | * @return array | |
2204 | */ | |
2205 | public static function get_plugin_strings() { | |
2206 | if (self::$pluginstrings === null) { | |
2207 | self::$pluginstrings = array( | |
2208 | 'report' => get_string('view'), | |
2209 | 'edittree' => get_string('edittree', 'grades'), | |
2210 | 'scale' => get_string('scales'), | |
2211 | 'outcome' => get_string('outcomes', 'grades'), | |
2212 | 'letter' => get_string('letters', 'grades'), | |
2213 | 'export' => get_string('export', 'grades'), | |
2214 | 'import' => get_string('import'), | |
2215 | 'preferences' => get_string('mypreferences', 'grades'), | |
2216 | 'settings' => get_string('settings') | |
2217 | ); | |
2218 | } | |
2219 | return self::$pluginstrings; | |
2220 | } | |
2221 | /** | |
2222 | * Get grade_plugin_info object for managing settings if the user can | |
2223 | * | |
2224 | * @param int $courseid | |
2225 | * @return grade_plugin_info | |
2226 | */ | |
2227 | public static function get_info_manage_settings($courseid) { | |
2228 | if (self::$managesetting !== null) { | |
2229 | return self::$managesetting; | |
2230 | } | |
2231 | $context = get_context_instance(CONTEXT_COURSE, $courseid); | |
2232 | if (has_capability('moodle/course:update', $context)) { | |
2233 | self::$managesetting = new grade_plugin_info('coursesettings', new moodle_url('/grade/edit/settings/index.php', array('id'=>$courseid)), get_string('course')); | |
2234 | } else { | |
2235 | self::$managesetting = false; | |
2236 | } | |
2237 | return self::$managesetting; | |
2238 | } | |
2239 | /** | |
2240 | * Returns an array of plugin reports as grade_plugin_info objects | |
2241 | * | |
2242 | * @param int $courseid | |
2243 | * @return array | |
2244 | */ | |
2245 | public static function get_plugins_reports($courseid) { | |
f7fcf4cd | 2246 | global $SITE; |
cf717dc2 | 2247 | |
4d5059d4 SH |
2248 | if (self::$gradereports !== null) { |
2249 | return self::$gradereports; | |
2250 | } | |
2251 | $context = get_context_instance(CONTEXT_COURSE, $courseid); | |
2252 | $gradereports = array(); | |
2253 | $gradepreferences = array(); | |
2254 | foreach (get_plugin_list('gradereport') as $plugin => $plugindir) { | |
f7fcf4cd AD |
2255 | //some reports make no sense if we're not within a course |
2256 | if ($courseid==$SITE->id && ($plugin=='grader' || $plugin=='user')) { | |
2257 | continue; | |
2258 | } | |
2259 | ||
4d5059d4 SH |
2260 | // Remove ones we can't see |
2261 | if (!has_capability('gradereport/'.$plugin.':view', $context)) { | |
2262 | continue; | |
2263 | } | |
2264 | ||
b5e7b2bf | 2265 | $pluginstr = get_string('pluginname', 'gradereport_'.$plugin); |
4d5059d4 SH |
2266 | $url = new moodle_url('/grade/report/'.$plugin.'/index.php', array('id'=>$courseid)); |
2267 | $gradereports[$plugin] = new grade_plugin_info($plugin, $url, $pluginstr); | |
2268 | ||
2269 | // Add link to preferences tab if such a page exists | |
2270 | if (file_exists($plugindir.'/preferences.php')) { | |
2271 | $url = new moodle_url('/grade/report/'.$plugin.'/preferences.php', array('id'=>$courseid)); | |
2272 | $gradepreferences[$plugin] = new grade_plugin_info($plugin, $url, $pluginstr); | |
2273 | } | |
2274 | } | |
2275 | if (count($gradereports) == 0) { | |
2276 | $gradereports = false; | |
2277 | $gradepreferences = false; | |
2278 | } else if (count($gradepreferences) == 0) { | |
2279 | $gradepreferences = false; | |
2280 | asort($gradereports); | |
2281 | } else { | |
2282 | asort($gradereports); | |
2283 | asort($gradepreferences); | |
2284 | } | |
2285 | self::$gradereports = $gradereports; | |
2286 | self::$gradereportpreferences = $gradepreferences; | |
2287 | return self::$gradereports; | |
2288 | } | |
2289 | /** | |
2290 | * Returns an array of grade plugin report preferences for plugin reports that | |
2291 | * support preferences | |
2292 | * @param int $courseid | |
2293 | * @return array | |
2294 | */ | |
2295 | public static function get_plugins_report_preferences($courseid) { | |
2296 | if (self::$gradereportpreferences !== null) { | |
2297 | return self::$gradereportpreferences; | |
2298 | } | |
2299 | self::get_plugins_reports($courseid); | |
2300 | return self::$gradereportpreferences; | |
2301 | } | |
2302 | /** | |
2303 | * Get information on scales | |
2304 | * @param int $courseid | |
2305 | * @return grade_plugin_info | |
2306 | */ | |
2307 | public static function get_info_scales($courseid) { | |
2308 | if (self::$scaleinfo !== null) { | |
2309 | return self::$scaleinfo; | |
2310 | } | |
2311 | if (has_capability('moodle/course:managescales', get_context_instance(CONTEXT_COURSE, $courseid))) { | |
2312 | $url = new moodle_url('/grade/edit/scale/index.php', array('id'=>$courseid)); | |
2313 | self::$scaleinfo = new grade_plugin_info('scale', $url, get_string('view')); | |
2314 | } else { | |
2315 | self::$scaleinfo = false; | |
2316 | } | |
2317 | return self::$scaleinfo; | |
2318 | } | |
2319 | /** | |
2320 | * Get information on outcomes | |
2321 | * @param int $courseid | |
2322 | * @return grade_plugin_info | |
2323 | */ | |
2324 | public static function get_info_outcomes($courseid) { | |
f7fcf4cd | 2325 | global $CFG, $SITE; |
4d5059d4 SH |
2326 | |
2327 | if (self::$outcomeinfo !== null) { | |
2328 | return self::$outcomeinfo; | |
2329 | } | |
2330 | $context = get_context_instance(CONTEXT_COURSE, $courseid); | |
2331 | $canmanage = has_capability('moodle/grade:manage', $context); | |
2332 | $canupdate = has_capability('moodle/course:update', $context); | |
2333 | if (!empty($CFG->enableoutcomes) && ($canmanage || $canupdate)) { | |
2334 | $outcomes = array(); | |
2335 | if ($canupdate) { | |
f7fcf4cd AD |
2336 | if ($courseid!=$SITE->id) { |
2337 | $url = new moodle_url('/grade/edit/outcome/course.php', array('id'=>$courseid)); | |
2338 | $outcomes['course'] = new grade_plugin_info('course', $url, get_string('outcomescourse', 'grades')); | |
2339 | } | |
4d5059d4 SH |
2340 | $url = new moodle_url('/grade/edit/outcome/index.php', array('id'=>$courseid)); |
2341 | $outcomes['edit'] = new grade_plugin_info('edit', $url, get_string('editoutcomes', 'grades')); | |
c46aeeab DC |
2342 | $url = new moodle_url('/grade/edit/outcome/import.php', array('courseid'=>$courseid)); |
2343 | $outcomes['import'] = new grade_plugin_info('import', $url, get_string('importoutcomes', 'grades')); | |
4d5059d4 | 2344 | } else { |
f7fcf4cd AD |
2345 | if ($courseid!=$SITE->id) { |
2346 | $url = new moodle_url('/grade/edit/outcome/course.php', array('id'=>$courseid)); | |
2347 | $outcomes['edit'] = new grade_plugin_info('edit', $url, get_string('outcomescourse', 'grades')); | |
2348 | } | |
4d5059d4 SH |
2349 | } |
2350 | self::$outcomeinfo = $outcomes; | |
2351 | } else { | |
2352 | self::$outcomeinfo = false; | |
2353 | } | |
2354 | return self::$outcomeinfo; | |
2355 | } | |
2356 | /** | |
2357 | * Get information on editing structures | |
2358 | * @param int $courseid | |
2359 | * @return array | |
2360 | */ | |
2361 | public static function get_info_edit_structure($courseid) { | |
2362 | if (self::$edittree !== null) { | |
2363 | return self::$edittree; | |
2364 | } | |
2365 | if (has_capability('moodle/grade:manage', get_context_instance(CONTEXT_COURSE, $courseid))) { | |
2366 | $url = new moodle_url('/grade/edit/tree/index.php', array('sesskey'=>sesskey(), 'showadvanced'=>'0', 'id'=>$courseid)); | |
2367 | self::$edittree = array( | |
2368 | 'simpleview' => new grade_plugin_info('simpleview', $url, get_string('simpleview', 'grades')), | |
2369 | 'fullview' => new grade_plugin_info('fullview', new moodle_url($url, array('showadvanced'=>'1')), get_string('fullview', 'grades')) | |
2370 | ); | |
2371 | } else { | |
2372 | self::$edittree = false; | |
2373 | } | |
2374 | return self::$edittree; | |
2375 | } | |
2376 | /** | |
2377 | * Get information on letters | |
2378 | * @param int $courseid | |
4d27bc79 | 2379 | * @return array |
4d5059d4 SH |
2380 | */ |
2381 | public static function get_info_letters($courseid) { | |
2382 | if (self::$letterinfo !== null) { | |
2383 | return self::$letterinfo; | |
2384 | } | |
2385 | $context = get_context_instance(CONTEXT_COURSE, $courseid); | |
2386 | $canmanage = has_capability('moodle/grade:manage', $context); | |
2387 | $canmanageletters = has_capability('moodle/grade:manageletters', $context); | |
2388 | if ($canmanage || $canmanageletters) { | |
2389 | self::$letterinfo = array( | |
54caa598 AD |
2390 | 'view' => new grade_plugin_info('view', new moodle_url('/grade/edit/letter/index.php', array('id'=>$context->id)), get_string('view')), |
2391 | 'edit' => new grade_plugin_info('edit', new moodle_url('/grade/edit/letter/index.php', array('edit'=>1,'id'=>$context->id)), get_string('edit')) | |
4d5059d4 SH |
2392 | ); |
2393 | } else { | |
2394 | self::$letterinfo = false; | |
2395 | } | |
2396 | return self::$letterinfo; | |
2397 | } | |
2398 | /** | |
2399 | * Get information import plugins | |
2400 | * @param int $courseid | |
2401 | * @return array | |
2402 | */ | |
2403 | public static function get_plugins_import($courseid) { | |
2404 | global $CFG; | |
2405 | ||
2406 | if (self::$importplugins !== null) { | |
2407 | return self::$importplugins; | |
2408 | } | |
2409 | $importplugins = array(); | |
2410 | $context = get_context_instance(CONTEXT_COURSE, $courseid); | |
2411 | ||
2412 | if (has_capability('moodle/grade:import', $context)) { | |
2413 | foreach (get_plugin_list('gradeimport') as $plugin => $plugindir) { | |
2414 | if (!has_capability('gradeimport/'.$plugin.':view', $context)) { | |
2415 | continue; | |
2416 | } | |
b5e7b2bf | 2417 | $pluginstr = get_string('pluginname', 'gradeimport_'.$plugin); |
4d5059d4 SH |
2418 | $url = new moodle_url('/grade/import/'.$plugin.'/index.php', array('id'=>$courseid)); |
2419 | $importplugins[$plugin] = new grade_plugin_info($plugin, $url, $pluginstr); | |
2420 | } | |
2421 | ||
2422 | ||
2423 | if ($CFG->gradepublishing) { | |
2424 | $url = new moodle_url('/grade/import/keymanager.php', array('id'=>$courseid)); | |
2425 | $importplugins['keymanager'] = new grade_plugin_info('keymanager', $url, get_string('keymanager', 'grades')); | |
2426 | } | |
2427 | } | |
2428 | ||
2429 | if (count($importplugins) > 0) { | |
2430 | asort($importplugins); | |
2431 | self::$importplugins = $importplugins; | |
2432 | } else { | |
2433 | self::$importplugins = false; | |
2434 | } | |
2435 | return self::$importplugins; | |
2436 | } | |
2437 | /** | |
2438 | * Get information export plugins | |
2439 | * @param int $courseid | |
2440 | * @return array | |
2441 | */ | |
2442 | public static function get_plugins_export($courseid) { | |
2443 | global $CFG; | |
2444 | ||
2445 | if (self::$exportplugins !== null) { | |
2446 | return self::$exportplugins; | |
2447 | } | |
2448 | $context = get_context_instance(CONTEXT_COURSE, $courseid); | |
2449 | $exportplugins = array(); | |
2450 | if (has_capability('moodle/grade:export', $context)) { | |
2451 | foreach (get_plugin_list('gradeexport') as $plugin => $plugindir) { | |
2452 | if (!has_capability('gradeexport/'.$plugin.':view', $context)) { | |
2453 | continue; | |
2454 | } | |
b5e7b2bf | 2455 | $pluginstr = get_string('pluginname', 'gradeexport_'.$plugin); |
4d5059d4 SH |
2456 | $url = new moodle_url('/grade/export/'.$plugin.'/index.php', array('id'=>$courseid)); |
2457 | $exportplugins[$plugin] = new grade_plugin_info($plugin, $url, $pluginstr); | |
2458 | } | |
2459 | ||
2460 | if ($CFG->gradepublishing) { | |
2461 | $url = new moodle_url('/grade/export/keymanager.php', array('id'=>$courseid)); | |
2462 | $exportplugins['keymanager'] = new grade_plugin_info('keymanager', $url, get_string('keymanager', 'grades')); | |
2463 | } | |
2464 | } | |
2465 | if (count($exportplugins) > 0) { | |
2466 | asort($exportplugins); | |
2467 | self::$exportplugins = $exportplugins; | |
2468 | } else { | |
2469 | self::$exportplugins = false; | |
2470 | } | |
2471 | return self::$exportplugins; | |
2472 | } | |
d4dcfc6b | 2473 | } |