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