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