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