removing an extra include
[moodle.git] / grade / lib.php
CommitLineData
3af29899 1<?php //$Id$
cbff94ba 2
7a6b7acf 3require_once $CFG->libdir.'/gradelib.php';
4
0610812a 5/**
6 * Print grading plugin selection popup form.
7 *
8 * @param int $courseid id of course
9 * @param string $active_type type of plugin on current page - import, export, report or edit
10 * @param string $active_plugin active plugin type - grader, user, cvs, ...
11 * @param boolean $return return as string
12 * @return nothing or string if $return true
13 */
3af29899 14function print_grade_plugin_selector($courseid, $active_type, $active_plugin, $return=false) {
cbff94ba 15 global $CFG;
cbff94ba 16
3af29899 17 $context = get_context_instance(CONTEXT_COURSE, $courseid);
cbff94ba 18
3af29899 19 $menu = array();
cbff94ba 20
3af29899 21 $active = '';
cbff94ba 22
3af29899 23/// report plugins with its special structure
24 if ($reports = get_list_of_plugins('grade/report', 'CVS')) { // Get all installed reports
25 foreach ($reports as $key => $plugin) { // Remove ones we can't see
26 if (!has_capability('gradereport/'.$plugin.':view', $context)) {
27 unset($reports[$key]);
cbff94ba 28 }
29 }
04678d8e 30 }
3af29899 31 $reportnames = array();
32 if (!empty($reports)) {
33 foreach ($reports as $plugin) {
65dd61bd 34 $url = 'report/'.$plugin.'/index.php?id='.$courseid;
3af29899 35 if ($active_type == 'report' and $active_plugin == $plugin ) {
36 $active = $url;
cbff94ba 37 }
e2008be2 38 $reportnames[$url] = get_string('modulename', 'gradereport_'.$plugin, NULL, $CFG->dirroot.'/grade/report/'.$plugin.'lang/');
cbff94ba 39 }
3af29899 40 asort($reportnames);
cbff94ba 41 }
3af29899 42 if (!empty($reportnames)) {
43 $menu['reportgroup']='--'.get_string('reportplugins', 'grades');
44 $menu = $menu+$reportnames;
cbff94ba 45 }
cbff94ba 46
3af29899 47/// standard import plugins
e2008be2 48 if ($imports = get_list_of_plugins('grade/import', 'CVS')) { // Get all installed import plugins
3af29899 49 foreach ($imports as $key => $plugin) { // Remove ones we can't see
50 if (!has_capability('gradeimport/'.$plugin.':view', $context)) {
51 unset($imports[$key]);
cbff94ba 52 }
53 }
54 }
3af29899 55 $importnames = array();
56 if (!empty($imports)) {
57 foreach ($imports as $plugin) {
58 $url = 'import/'.$plugin.'/index.php?id='.$courseid;
65dd61bd 59 if ($active_type == 'import' and $active_plugin == $plugin ) {
3af29899 60 $active = $url;
61 }
e2008be2 62 $importnames[$url] = get_string('modulename', 'gradeimport_'.$plugin, NULL, $CFG->dirroot.'/grade/import/'.$plugin.'lang/');
281ffa4a 63 }
3af29899 64 asort($importnames);
281ffa4a 65 }
3af29899 66 if (!empty($importnames)) {
67 $menu['importgroup']='--'.get_string('importplugins', 'grades');
68 $menu = $menu+$importnames;
281ffa4a 69 }
281ffa4a 70
3af29899 71/// standard export plugins
e2008be2 72 if ($exports = get_list_of_plugins('grade/export', 'CVS')) { // Get all installed export plugins
3af29899 73 foreach ($exports as $key => $plugin) { // Remove ones we can't see
74 if (!has_capability('gradeexport/'.$plugin.':view', $context)) {
75 unset($exports[$key]);
281ffa4a 76 }
77 }
cbff94ba 78 }
3af29899 79 $exportnames = array();
80 if (!empty($exports)) {
81 foreach ($exports as $plugin) {
82 $url = 'export/'.$plugin.'/index.php?id='.$courseid;
65dd61bd 83 if ($active_type == 'export' and $active_plugin == $plugin ) {
3af29899 84 $active = $url;
85 }
e2008be2 86 $exportnames[$url] = get_string('modulename', 'gradeexport_'.$plugin, NULL, $CFG->dirroot.'/grade/export/'.$plugin.'lang/');
281ffa4a 87 }
3af29899 88 asort($exportnames);
cbff94ba 89 }
3af29899 90 if (!empty($exportnames)) {
91 $menu['exportgroup']='--'.get_string('exportplugins', 'grades');
92 $menu = $menu+$exportnames;
281ffa4a 93 }
cbff94ba 94
3af29899 95/// editing scripts - not real plugins
78ad5f3f 96 if (has_capability('moodle/grade:manage', $context)
04259694 97 or has_capability('moodle/course:managescales', $context)
98 or has_capability('moodle/course:update', $context)) {
3af29899 99 $menu['edit']='--'.get_string('edit');
78ad5f3f 100
101 if (has_capability('moodle/grade:manage', $context)) {
102 $url = 'edit/tree/index.php?id='.$courseid;
103 if ($active_type == 'edit' and $active_plugin == 'tree' ) {
104 $active = $url;
105 }
106 $menu[$url] = get_string('edittree', 'grades');
107 }
108
109 if (has_capability('moodle/course:managescales', $context)) {
110 $url = 'edit/scale/index.php?id='.$courseid;
111 if ($active_type == 'edit' and $active_plugin == 'scale' ) {
112 $active = $url;
113 }
114 $menu[$url] = get_string('scales');
115 }
116
2b0f65e2 117 if (!empty($CFG->enableoutcomes) && (has_capability('moodle/grade:manage', $context) or
2a598439 118 has_capability('moodle/course:update', $context))) {
119 if (has_capability('moodle/course:update', $context)) { // Default to course assignment
04259694 120 $url = 'edit/outcome/course.php?id='.$courseid;
2a598439 121 } else {
122 $url = 'edit/outcome/index.php?id='.$courseid;
04259694 123 }
78ad5f3f 124 if ($active_type == 'edit' and $active_plugin == 'outcome' ) {
125 $active = $url;
126 }
127 $menu[$url] = get_string('outcomes', 'grades');
cbff94ba 128 }
281ffa4a 129 }
130
3af29899 131/// finally print/return the popup form
0c811aa7 132 return popup_form($CFG->wwwroot.'/grade/', $menu, 'choosepluginreport', $active, 'choose', '', '', $return, 'self', get_string('view'));
cbff94ba 133}
134
0610812a 135/**
7a6b7acf 136 * Utility class used for return tracking when using edit and other forms in grade plugins
0610812a 137 */
3af29899 138class grade_plugin_return {
139 var $type;
140 var $plugin;
141 var $courseid;
142 var $userid;
143 var $page;
281ffa4a 144
0610812a 145 /**
146 * Constructor
147 * @param array $params - associative array with return parameters, if null parameter are taken from _GET or _POST
148 */
3af29899 149 function grade_plugin_return ($params=null) {
150 if (empty($params)) {
151 $this->type = optional_param('gpr_type', null, PARAM_SAFEDIR);
152 $this->plugin = optional_param('gpr_plugin', null, PARAM_SAFEDIR);
153 $this->courseid = optional_param('gpr_courseid', null, PARAM_INT);
154 $this->userid = optional_param('gpr_userid', null, PARAM_INT);
155 $this->page = optional_param('gpr_page', null, PARAM_INT);
a983b6ec 156
a983b6ec 157 } else {
3af29899 158 foreach ($params as $key=>$value) {
159 if (array_key_exists($key, $this)) {
160 $this->$key = $value;
161 }
cbff94ba 162 }
163 }
6cd8c592 164 }
165
0610812a 166 /**
167 * Returns return parameters as options array suitable for buttons.
168 * @return array options
169 */
3af29899 170 function get_options() {
7a6b7acf 171 if (empty($this->type)) {
3af29899 172 return array();
865e9a82 173 }
6cd8c592 174
3af29899 175 $params = array();
6cd8c592 176
7a6b7acf 177 if (!empty($this->plugin)) {
178 $params['plugin'] = $this->plugin;
179 }
6cd8c592 180
3af29899 181 if (!empty($this->courseid)) {
182 $params['id'] = $this->courseid;
6cd8c592 183 }
9c61ba4d 184
3af29899 185 if (!empty($this->userid)) {
186 $params['userid'] = $this->userid;
9c61ba4d 187 }
9c61ba4d 188
3af29899 189 if (!empty($this->page)) {
190 $params['page'] = $this->page;
cbff94ba 191 }
865e9a82 192
3af29899 193 return $params;
cbff94ba 194 }
cbff94ba 195
0610812a 196 /**
197 * Returns return url
198 * @param string $default default url when params not set
199 * @return string url
200 */
65dd61bd 201 function get_return_url($default, $extras=null) {
3af29899 202 global $CFG;
cbff94ba 203
3af29899 204 if (empty($this->type) or empty($this->plugin)) {
205 return $default;
cbff94ba 206 }
207
65dd61bd 208 $url = $CFG->wwwroot.'/grade/'.$this->type.'/'.$this->plugin.'/index.php';
209 $glue = '?';
cbff94ba 210
3af29899 211 if (!empty($this->courseid)) {
212 $url .= $glue.'id='.$this->courseid;
213 $glue = '&amp;';
cbff94ba 214 }
cbff94ba 215
3af29899 216 if (!empty($this->userid)) {
217 $url .= $glue.'userid='.$this->userid;
218 $glue = '&amp;';
cbff94ba 219 }
7e2d7c92 220
3af29899 221 if (!empty($this->page)) {
222 $url .= $glue.'page='.$this->page;
65dd61bd 223 $glue = '&amp;';
224 }
225
226 if (!empty($extras)) {
227 foreach($extras as $key=>$value) {
228 $url .= $glue.$key.'='.$value;
229 $glue = '&amp;';
7a6b7acf 230 }
cbff94ba 231 }
cbff94ba 232
3af29899 233 return $url;
cbff94ba 234 }
cbff94ba 235
0610812a 236 /**
237 * Returns string with hidden return tracking form elements.
238 * @return string
239 */
3af29899 240 function get_form_fields() {
7a6b7acf 241 if (empty($this->type)) {
3af29899 242 return '';
cbff94ba 243 }
cbff94ba 244
3af29899 245 $result = '<input type="hidden" name="gpr_type" value="'.$this->type.'" />';
7a6b7acf 246
247 if (!empty($this->plugin)) {
248 $result .= '<input type="hidden" name="gpr_plugin" value="'.$this->plugin.'" />';
249 }
0ca5abd6 250
3af29899 251 if (!empty($this->courseid)) {
252 $result .= '<input type="hidden" name="gpr_courseid" value="'.$this->courseid.'" />';
cbff94ba 253 }
cbff94ba 254
3af29899 255 if (!empty($this->userid)) {
256 $result .= '<input type="hidden" name="gpr_userid" value="'.$this->userid.'" />';
cbff94ba 257 }
cbff94ba 258
3af29899 259 if (!empty($this->page)) {
260 $result .= '<input type="hidden" name="gpr_page" value="'.$this->page.'" />';
cbff94ba 261 }
262 }
cbff94ba 263
0610812a 264 /**
265 * Add hidden elements into mform
266 * @param object $mform moodle form object
267 * @return void
268 */
3af29899 269 function add_mform_elements(&$mform) {
7a6b7acf 270 if (empty($this->type)) {
3af29899 271 return;
cbff94ba 272 }
cbff94ba 273
3af29899 274 $mform->addElement('hidden', 'gpr_type', $this->type);
275 $mform->setType('gpr_type', PARAM_SAFEDIR);
cbff94ba 276
7a6b7acf 277 if (!empty($this->plugin)) {
278 $mform->addElement('hidden', 'gpr_plugin', $this->plugin);
279 $mform->setType('gpr_plugin', PARAM_SAFEDIR);
280 }
97033c86 281
3af29899 282 if (!empty($this->courseid)) {
283 $mform->addElement('hidden', 'gpr_courseid', $this->courseid);
284 $mform->setType('gpr_courseid', PARAM_INT);
cbff94ba 285 }
cbff94ba 286
3af29899 287 if (!empty($this->userid)) {
288 $mform->addElement('hidden', 'gpr_userid', $this->userid);
289 $mform->setType('gpr_userid', PARAM_INT);
cbff94ba 290 }
cbff94ba 291
3af29899 292 if (!empty($this->page)) {
293 $mform->addElement('hidden', 'gpr_page', $this->page);
294 $mform->setType('gpr_page', PARAM_INT);
cbff94ba 295 }
296 }
281ffa4a 297
0610812a 298 /**
299 * Add return tracking params into url
300 * @param string $url
301 * @return string $url with erturn tracking params
302 */
3af29899 303 function add_url_params($url) {
7a6b7acf 304 if (empty($this->type)) {
3af29899 305 return $url;
cbff94ba 306 }
5609f9e6 307
3af29899 308 if (strpos($url, '?') === false) {
309 $url .= '?gpr_type='.$this->type;
310 } else {
311 $url .= '&amp;gpr_type='.$this->type;
cbff94ba 312 }
cbff94ba 313
7a6b7acf 314 if (!empty($this->plugin)) {
315 $url .= '&amp;gpr_plugin='.$this->plugin;
316 }
cbff94ba 317
3af29899 318 if (!empty($this->courseid)) {
319 $url .= '&amp;gpr_courseid='.$this->courseid;
cbff94ba 320 }
cbff94ba 321
3af29899 322 if (!empty($this->userid)) {
323 $url .= '&amp;gpr_userid='.$this->userid;
cbff94ba 324 }
0a8a95c9 325
3af29899 326 if (!empty($this->page)) {
327 $url .= '&amp;gpr_page='.$this->page;
0a8a95c9 328 }
5a412dbf 329
3af29899 330 return $url;
5a412dbf 331 }
5a412dbf 332}
7a6b7acf 333
826c5f86 334/**
335 * Function central to gradebook for building and printing the navigation (breadcrumb trail).
336 * @param string $path The path of the calling script (using __FILE__?)
337 * @param string $pagename The language string to use as the last part of the navigation (non-link)
338 * @param mixed $id Either a plain integer (assuming the key is 'id') or an array of keys and values (e.g courseid => $courseid, itemid...)
339 * @return string
340 */
341function grade_build_nav($path, $pagename=null, $id=null) {
342 global $CFG, $COURSE;
343
344 $strgrades = get_string('grades', 'grades');
345
346 // Parse the path and build navlinks from its elements
347 $dirroot_length = strlen($CFG->dirroot) + 1; // Add 1 for the first slash
348 $path = substr($path, $dirroot_length);
349 $path = str_replace('\\', '/', $path);
350
351 $path_elements = explode('/', $path);
352
353 $path_elements_count = count($path_elements);
354
355 $last_element = $path_elements[$path_elements_count-1]; // Should be the filename (including extension)
356
357 // First link is always 'grade'
358 $navlinks = array();
359 $navlinks[] = array('name' => $strgrades,
360 'link' => $CFG->wwwroot.'/grade/index.php?id='.$COURSE->id,
361 'type' => 'misc');
362
363 $link = '';
364 $numberofelements = 3;
365
366 // Prepare URL params string
367 $id_string = '?';
368 if (!is_null($id)) {
369 if (is_array($id)) {
370 foreach ($id as $idkey => $idvalue) {
371 $id_string .= "$idkey=$idvalue&amp;";
372 }
373 } else {
374 $id_string .= "id=$id";
375 }
376 }
377
378 $navlink4 = null;
379
380 // Second level links
381 switch ($path_elements[1]) {
382 case 'edit': // No link
383 if ($path_elements[3] != 'index.php') {
384 $numberofelements = 4;
385 }
386 break;
387 case 'import': // No link
388 break;
389 case 'export': // No link
390 break;
391 case 'report':
392 // $id is required for this link. Do not print it if $id isn't given
393 if (!is_null($id)) {
394 $link = $CFG->wwwroot . '/grade/report/index.php' . $id_string;
395 }
396
397 if ($path_elements[2] == 'grader') {
398 $numberofelements = 4;
399 }
400 break;
401
402 default:
403 // If this element isn't among the ones already listed above, it isn't supported, throw an error.
404 debugging("grade_build_nav() doesn't support ". $path_elements[1] . " as the second path element after 'grade'.");
405 return false;
406 }
407
408 $navlinks[] = array('name' => get_string($path_elements[1], 'grades'), 'link' => $link, 'type' => 'misc');
409
410 // Third level links
411 if (empty($pagename)) {
412 $pagename = get_string($path_elements[2], 'grades');
413 }
414
415 switch ($numberofelements) {
416 case 3:
417 $navlinks[] = array('name' => $pagename, 'link' => $link, 'type' => 'misc');
418 break;
419 case 4:
420
421 if ($path_elements[2] == 'grader' AND $path_elements[3] != 'index.php') {
422 $navlinks[] = array('name' => get_string('grader', 'grades'),
423 'link' => "$CFG->wwwroot/grade/report/grader/index.php$id_string",
424 'type' => 'misc');
425 }
426 $navlinks[] = array('name' => $pagename, 'link' => '', 'type' => 'misc');
427 break;
428 }
429 $navigation = build_navigation($navlinks);
430
431 return $navigation;
432 }
7a6b7acf 433
434/**
435 * This class represents a complete tree of categories, grade_items and final grades,
436 * organises as an array primarily, but which can also be converted to other formats.
437 * It has simple method calls with complex implementations, allowing for easy insertion,
438 * deletion and moving of items and categories within the tree.
439 */
440class grade_tree {
441
442 /**
443 * The basic representation of the tree as a hierarchical, 3-tiered array.
444 * @var object $top_element
445 */
446 var $top_element;
447
448 /**
449 * A string of GET URL variables, namely courseid and sesskey, used in most URLs built by this class.
450 * @var string $commonvars
451 */
452 var $commonvars;
453
454 /**
455 * 2D array of grade items and categories
456 */
457 var $levels;
458
2cc773f5 459 /**
460 * Course context
461 */
462 var $context;
463
7a6b7acf 464 /**
465 * Constructor, retrieves and stores a hierarchical array of all grade_category and grade_item
466 * objects for the given courseid. Full objects are instantiated.
467 * and renumbering.
468 * @param int $courseid
469 * @param boolean $fillers include fillers and colspans, make the levels var "rectangular"
470 * @param boolean $category_grade_last category grade item is the last child
4faf5f99 471 * @param array $collapsed array of collapsed categories
7a6b7acf 472 */
4faf5f99 473 function grade_tree($courseid, $fillers=true, $category_grade_last=false, $collapsed=null) {
7a6b7acf 474 global $USER, $CFG;
475
476 $this->courseid = $courseid;
477 $this->commonvars = "&amp;sesskey=$USER->sesskey&amp;id=$this->courseid";
478 $this->levels = array();
2cc773f5 479 $this->context = get_context_instance(CONTEXT_COURSE, $courseid);
7a6b7acf 480
481 // get course grade tree
482 $this->top_element = grade_category::fetch_course_tree($courseid, true);
483
4faf5f99 484 // collapse the categories if requested
485 if (!empty($collapsed)) {
486 grade_tree::category_collapse($this->top_element, $collapsed);
487 }
488
489 // move category item to last position in category
7a6b7acf 490 if ($category_grade_last) {
491 grade_tree::category_grade_last($this->top_element);
492 }
493
494 if ($fillers) {
495 // inject fake categories == fillers
496 grade_tree::inject_fillers($this->top_element, 0);
497 // add colspans to categories and fillers
498 grade_tree::inject_colspans($this->top_element);
499 }
500
501 grade_tree::fill_levels($this->levels, $this->top_element, 0);
502 }
503
4faf5f99 504 /**
505 * Static recursive helper - removes items from collapsed categories
506 * @static
507 * @param array $element The seed of the recursion
508 * @param array $collapsed array of collapsed categories
509 * @return void
510 */
511 function category_collapse(&$element, $collapsed) {
512 if ($element['type'] != 'category') {
513 return;
514 }
515 if (empty($element['children']) or count($element['children']) < 2) {
516 return;
517 }
518
519 if (in_array($element['object']->id, $collapsed)) {
520 $category_item = reset($element['children']); //keep only category item
521 $element['children'] = array(key($element['children'])=>$category_item);
522
523 } else {
524 foreach ($element['children'] as $sortorder=>$child) {
525 grade_tree::category_collapse($element['children'][$sortorder], $collapsed);
526 }
527 }
528 }
7a6b7acf 529
530 /**
531 * Static recursive helper - makes the grade_item for category the last children
532 * @static
533 * @param array $element The seed of the recursion
534 * @return void
535 */
536 function category_grade_last(&$element) {
537 if (empty($element['children'])) {
538 return;
539 }
540 if (count($element['children']) < 2) {
541 return;
542 }
543 $category_item = reset($element['children']);
544 $order = key($element['children']);
545 unset($element['children'][$order]);
546 $element['children'][$order] =& $category_item;
547 foreach ($element['children'] as $sortorder=>$child) {
548 grade_tree::category_grade_last($element['children'][$sortorder]);
549 }
550 }
551
552 /**
553 * Static recursive helper - fills the levels array, useful when accessing tree elements of one level
554 * @static
555 * @param int $levels
556 * @param array $element The seed of the recursion
557 * @param int $depth
558 * @return void
559 */
560 function fill_levels(&$levels, &$element, $depth) {
561 if (!array_key_exists($depth, $levels)) {
562 $levels[$depth] = array();
563 }
564
565 // prepare unique identifier
566 if ($element['type'] == 'category') {
567 $element['eid'] = 'c'.$element['object']->id;
568 } else if (in_array($element['type'], array('item', 'courseitem', 'categoryitem'))) {
569 $element['eid'] = 'i'.$element['object']->id;
570 }
571
572 $levels[$depth][] =& $element;
573 $depth++;
574 if (empty($element['children'])) {
575 return;
576 }
577 $prev = 0;
578 foreach ($element['children'] as $sortorder=>$child) {
579 grade_tree::fill_levels($levels, $element['children'][$sortorder], $depth);
580 $element['children'][$sortorder]['prev'] = $prev;
581 $element['children'][$sortorder]['next'] = 0;
582 if ($prev) {
583 $element['children'][$prev]['next'] = $sortorder;
584 }
585 $prev = $sortorder;
586 }
587 }
588
589 /**
590 * Static recursive helper - makes full tree (all leafes are at the same level)
591 */
592 function inject_fillers(&$element, $depth) {
593 $depth++;
594
595 if (empty($element['children'])) {
596 return $depth;
597 }
598 $chdepths = array();
599 $chids = array_keys($element['children']);
600 $last_child = end($chids);
601 $first_child = reset($chids);
602
603 foreach ($chids as $chid) {
604 $chdepths[$chid] = grade_tree::inject_fillers($element['children'][$chid], $depth);
605 }
606 arsort($chdepths);
607
608 $maxdepth = reset($chdepths);
609 foreach ($chdepths as $chid=>$chd) {
610 if ($chd == $maxdepth) {
611 continue;
612 }
613 for ($i=0; $i < $maxdepth-$chd; $i++) {
614 if ($chid == $first_child) {
615 $type = 'fillerfirst';
616 } else if ($chid == $last_child) {
617 $type = 'fillerlast';
618 } else {
619 $type = 'filler';
620 }
621 $oldchild =& $element['children'][$chid];
622 $element['children'][$chid] = array('object'=>'filler', 'type'=>$type, 'eid'=>'', 'depth'=>$element['object']->depth,'children'=>array($oldchild));
623 }
624 }
625
626 return $maxdepth;
627 }
628
629 /**
630 * Static recursive helper - add colspan information into categories
631 */
632 function inject_colspans(&$element) {
633 if (empty($element['children'])) {
634 return 1;
635 }
636 $count = 0;
637 foreach ($element['children'] as $key=>$child) {
638 $count += grade_tree::inject_colspans($element['children'][$key]);
639 }
640 $element['colspan'] = $count;
641 return $count;
642 }
643
644 /**
645 * Parses the array in search of a given eid and returns a element object with
646 * information about the element it has found.
647 * @param int $eid
648 * @return object element
649 */
650 function locate_element($eid) {
d3c3da1b 651 // it is a grade - construct a new object
652 if (strpos($eid, 'n') === 0) {
653 if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) {
654 return null;
655 }
656
657 $itemid = $matches[1];
658 $userid = $matches[2];
659
660 //extra security check - the grade item must be in this tree
661 if (!$item_el = $this->locate_element('i'.$itemid)) {
662 return null;
663 }
664
665 // $gradea->id may be null - means does not exist yet
666 $grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid));
667
668 $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods!
669 return array('eid'=>'n'.$itemid.'u'.$userid,'object'=>$grade, 'type'=>'grade');
670
671 } else if (strpos($eid, 'g') === 0) {
7a6b7acf 672 $id = (int)substr($eid, 1);
673 if (!$grade = grade_grade::fetch(array('id'=>$id))) {
674 return null;
675 }
676 //extra security check - the grade item must be in this tree
677 if (!$item_el = $this->locate_element('i'.$grade->itemid)) {
678 return null;
679 }
680 $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods!
681 return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade');
682 }
683
684 // it is a category or item
685 foreach ($this->levels as $row) {
686 foreach ($row as $element) {
687 if ($element['type'] == 'filler') {
688 continue;
689 }
690 if ($element['eid'] == $eid) {
691 return $element;
692 }
693 }
694 }
695
696 return null;
697 }
698
d3c3da1b 699 /**
700 * Returns the grade eid - the grade may not exist yet.
701 * @param $grade_grade object
702 * @return string eid
703 */
704 function get_grade_eid($grade_grade) {
705 if (empty($grade_grade->id)) {
706 return 'n'.$grade_grade->itemid.'u'.$grade_grade->userid;
707 } else {
708 return 'g'.$grade_grade->id;
709 }
710 }
711
7a6b7acf 712 /**
713 * Return edit icon for give element
714 * @param object $element
715 * @return string
716 */
717 function get_edit_icon($element, $gpr) {
718 global $CFG;
719
2cc773f5 720 if (!has_capability('moodle/grade:manage', $this->context)) {
27b1735b 721 if ($element['type'] == 'grade' and has_capability('moodle/grade:override', $this->context)) {
722 // oki - let them override grade
723 } else {
724 return '';
725 }
7a6b7acf 726 }
727
2cc773f5 728 static $stredit = null;
729 if (is_null($stredit)) {
730 $stredit = get_string('edit');
731 }
732
7a6b7acf 733 $object = $element['object'];
2cc773f5 734 $overlib = '';
7a6b7acf 735
736 switch ($element['type']) {
737 case 'item':
738 case 'categoryitem':
739 case 'courseitem':
9988d1b7 740 if (empty($object->outcomeid) || empty($CFG->enableoutcomes)) {
78ad5f3f 741 $url = $CFG->wwwroot.'/grade/edit/tree/item.php?courseid='.$this->courseid.'&amp;id='.$object->id;
f10fac96 742 } else {
78ad5f3f 743 $url = $CFG->wwwroot.'/grade/edit/tree/outcomeitem.php?courseid='.$this->courseid.'&amp;id='.$object->id;
f10fac96 744 }
7a6b7acf 745 $url = $gpr->add_url_params($url);
746 break;
747
748 case 'category':
78ad5f3f 749 $url = $CFG->wwwroot.'/grade/edit/tree/category.php?courseid='.$this->courseid.'&amp;id='.$object->id;
7a6b7acf 750 $url = $gpr->add_url_params($url);
751 break;
752
753 case 'grade':
d3c3da1b 754 if (empty($object->id)) {
755 $url = $CFG->wwwroot.'/grade/edit/tree/grade.php?courseid='.$this->courseid.'&amp;itemid='.$object->itemid.'&amp;userid='.$object->userid;
756 } else {
757 $url = $CFG->wwwroot.'/grade/edit/tree/grade.php?courseid='.$this->courseid.'&amp;id='.$object->id;
758 }
7a6b7acf 759 $url = $gpr->add_url_params($url);
2cc773f5 760 if (!empty($object->feedback)) {
761 $feedback = format_text($object->feedback, $object->feedbackformat);
762 $function = "return overlib('".s(ltrim($object->feedback)."', FULLHTML);");
763 $overlib = 'onmouseover="'.$function.'" onmouseout="return nd();"';
764 }
7a6b7acf 765 break;
766
767 default:
768 $url = null;
769 }
770
771 if ($url) {
2cc773f5 772 return '<a href="'.$url.'"><img '.$overlib.' src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$stredit.'" title="'.$stredit.'"/></a>';
7a6b7acf 773
774 } else {
775 return '';
776 }
777 }
778
779 /**
780 * Return hiding icon for give element
781 * @param object $element
782 * @return string
783 */
784 function get_hiding_icon($element, $gpr) {
785 global $CFG;
786
2cc773f5 787 if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:hide', $this->context)) {
7a6b7acf 788 return '';
789 }
790
2cc773f5 791 static $strshow = null;
792 static $strhide = null;
793 if (is_null($strshow)) {
7a6b7acf 794 $strshow = get_string('show');
2cc773f5 795 $strhide = get_string('hide');
796 }
797
798 if ($element['object']->is_hidden()) {
78ad5f3f 799 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=show&amp;sesskey='.sesskey().'&amp;eid='.$element['eid'];
7a6b7acf 800 $url = $gpr->add_url_params($url);
801 $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.$strshow.'" title="'.$strshow.'"/></a>';
802
803 } else {
78ad5f3f 804 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=hide&amp;sesskey='.sesskey().'&amp;eid='.$element['eid'];
7a6b7acf 805 $url = $gpr->add_url_params($url);
806 $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$strhide.'" title="'.$strhide.'"/></a>';
807 }
808 return $action;
809 }
810
811 /**
812 * Return locking icon for give element
813 * @param object $element
814 * @return string
815 */
816 function get_locking_icon($element, $gpr) {
817 global $CFG;
818
2cc773f5 819 static $strunlock = null;
820 static $strlock = null;
821 if (is_null($strunlock)) {
822 $strunlock = get_string('unlock', 'grades');
823 $strlock = get_string('lock', 'grades');
824 }
7a6b7acf 825
826 if ($element['object']->is_locked()) {
2cc773f5 827 if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:unlock', $this->context)) {
7a6b7acf 828 return '';
829 }
78ad5f3f 830 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=unlock&amp;sesskey='.sesskey().'&amp;eid='.$element['eid'];
7a6b7acf 831 $url = $gpr->add_url_params($url);
832 $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/unlock.gif" class="iconsmall" alt="'.$strunlock.'" title="'.$strunlock.'"/></a>';
833
834 } else {
2cc773f5 835 if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:lock', $this->context)) {
7a6b7acf 836 return '';
837 }
78ad5f3f 838 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=lock&amp;sesskey='.sesskey().'&amp;eid='.$element['eid'];
7a6b7acf 839 $url = $gpr->add_url_params($url);
840 $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/lock.gif" class="iconsmall" alt="'.$strlock.'" title="'.$strlock.'"/></a>';
841 }
842 return $action;
843 }
844
2cc773f5 845 /**
846 * Return calculation icon for given element
847 * @param object $element
848 * @return string
849 */
850 function get_calculation_icon($element, $gpr) {
851 global $CFG;
852 if (!has_capability('moodle/grade:manage', $this->context)) {
853 return '';
854 }
855
856 $calculation_icon = '';
857
858 $type = $element['type'];
859 $object = $element['object'];
860
861 if ($type == 'item' or $type == 'courseitem' or $type == 'categoryitem') {
862 $streditcalculation = get_string('editcalculation', 'grades');
863
864 // show calculation icon only when calculation possible
9127bc5d 865 if ((!$object->is_normal_item() or $object->is_outcome_item())
866 and ($object->gradetype == GRADE_TYPE_SCALE or $object->gradetype == GRADE_TYPE_VALUE)) {
78ad5f3f 867 $url = $CFG->wwwroot.'/grade/edit/tree/calculation.php?courseid='.$this->courseid.'&amp;id='.$object->id;
2cc773f5 868 $url = $gpr->add_url_params($url);
869 $calculation_icon = '<a href="'. $url.'"><img src="'.$CFG->pixpath.'/t/calc.gif" class="iconsmall" alt="'
870 . $streditcalculation.'" title="'.$streditcalculation.'" /></a>'. "\n";
871 }
872 }
873
874 return $calculation_icon;
875 }
7a6b7acf 876}
877
e2008be2 878?>