MDL-10829 Added get_grade_item() to the grade_item class, for cases when we don't...
[moodle-pu.git] / grade / lib.php
blob84d49cfda88014817a91b9c469e8330f14d6a560
1 <?php //$Id$
3 require_once $CFG->libdir.'/gradelib.php';
5 /**
6 * Print grading plugin selection popup form.
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
14 function print_grade_plugin_selector($courseid, $active_type, $active_plugin, $return=false) {
15 global $CFG;
17 $context = get_context_instance(CONTEXT_COURSE, $courseid);
19 $menu = array();
21 $active = '';
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]);
31 $reportnames = array();
32 if (!empty($reports)) {
33 foreach ($reports as $plugin) {
34 $url = 'report/'.$plugin.'/index.php?id='.$courseid;
35 if ($active_type == 'report' and $active_plugin == $plugin ) {
36 $active = $url;
38 $reportnames[$url] = get_string('modulename', 'gradereport_'.$plugin, NULL, $CFG->dirroot.'/grade/report/'.$plugin.'lang/');
40 asort($reportnames);
42 if (!empty($reportnames)) {
43 $menu['reportgroup']='--'.get_string('reportplugins', 'grades');
44 $menu = $menu+$reportnames;
47 /// standard import plugins
48 if ($imports = get_list_of_plugins('grade/import', 'CVS')) { // Get all installed import plugins
49 foreach ($imports as $key => $plugin) { // Remove ones we can't see
50 if (!has_capability('gradeimport/'.$plugin.':view', $context)) {
51 unset($imports[$key]);
55 $importnames = array();
56 if (!empty($imports)) {
57 foreach ($imports as $plugin) {
58 $url = 'import/'.$plugin.'/index.php?id='.$courseid;
59 if ($active_type == 'import' and $active_plugin == $plugin ) {
60 $active = $url;
62 $importnames[$url] = get_string('modulename', 'gradeimport_'.$plugin, NULL, $CFG->dirroot.'/grade/import/'.$plugin.'lang/');
64 asort($importnames);
66 if (!empty($importnames)) {
67 $menu['importgroup']='--'.get_string('importplugins', 'grades');
68 $menu = $menu+$importnames;
71 /// standard export plugins
72 if ($exports = get_list_of_plugins('grade/export', 'CVS')) { // Get all installed export plugins
73 foreach ($exports as $key => $plugin) { // Remove ones we can't see
74 if (!has_capability('gradeexport/'.$plugin.':view', $context)) {
75 unset($exports[$key]);
79 $exportnames = array();
80 if (!empty($exports)) {
81 foreach ($exports as $plugin) {
82 $url = 'export/'.$plugin.'/index.php?id='.$courseid;
83 if ($active_type == 'export' and $active_plugin == $plugin ) {
84 $active = $url;
86 $exportnames[$url] = get_string('modulename', 'gradeexport_'.$plugin, NULL, $CFG->dirroot.'/grade/export/'.$plugin.'lang/');
88 asort($exportnames);
90 if (!empty($exportnames)) {
91 $menu['exportgroup']='--'.get_string('exportplugins', 'grades');
92 $menu = $menu+$exportnames;
95 /// editing scripts - not real plugins
96 if (has_capability('moodle/grade:manage', $context)
97 or has_capability('moodle/course:managescales', $context)
98 or has_capability('moodle/course:update', $context)) {
99 $menu['edit']='--'.get_string('edit');
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;
106 $menu[$url] = get_string('edittree', 'grades');
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;
114 $menu[$url] = get_string('scales');
117 if (!empty($CFG->enableoutcomes) && (has_capability('moodle/grade:manage', $context) or
118 has_capability('moodle/course:update', $context))) {
119 if (has_capability('moodle/course:update', $context)) { // Default to course assignment
120 $url = 'edit/outcome/course.php?id='.$courseid;
121 } else {
122 $url = 'edit/outcome/index.php?id='.$courseid;
124 if ($active_type == 'edit' and $active_plugin == 'outcome' ) {
125 $active = $url;
127 $menu[$url] = get_string('outcomes', 'grades');
131 /// finally print/return the popup form
132 return popup_form($CFG->wwwroot.'/grade/', $menu, 'choosepluginreport', $active, 'choose', '', '', $return, 'self', get_string('view'));
136 * Utility class used for return tracking when using edit and other forms in grade plugins
138 class grade_plugin_return {
139 var $type;
140 var $plugin;
141 var $courseid;
142 var $userid;
143 var $page;
146 * Constructor
147 * @param array $params - associative array with return parameters, if null parameter are taken from _GET or _POST
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);
157 } else {
158 foreach ($params as $key=>$value) {
159 if (array_key_exists($key, $this)) {
160 $this->$key = $value;
167 * Returns return parameters as options array suitable for buttons.
168 * @return array options
170 function get_options() {
171 if (empty($this->type)) {
172 return array();
175 $params = array();
177 if (!empty($this->plugin)) {
178 $params['plugin'] = $this->plugin;
181 if (!empty($this->courseid)) {
182 $params['id'] = $this->courseid;
185 if (!empty($this->userid)) {
186 $params['userid'] = $this->userid;
189 if (!empty($this->page)) {
190 $params['page'] = $this->page;
193 return $params;
197 * Returns return url
198 * @param string $default default url when params not set
199 * @return string url
201 function get_return_url($default, $extras=null) {
202 global $CFG;
204 if (empty($this->type) or empty($this->plugin)) {
205 return $default;
208 $url = $CFG->wwwroot.'/grade/'.$this->type.'/'.$this->plugin.'/index.php';
209 $glue = '?';
211 if (!empty($this->courseid)) {
212 $url .= $glue.'id='.$this->courseid;
213 $glue = '&amp;';
216 if (!empty($this->userid)) {
217 $url .= $glue.'userid='.$this->userid;
218 $glue = '&amp;';
221 if (!empty($this->page)) {
222 $url .= $glue.'page='.$this->page;
223 $glue = '&amp;';
226 if (!empty($extras)) {
227 foreach($extras as $key=>$value) {
228 $url .= $glue.$key.'='.$value;
229 $glue = '&amp;';
233 return $url;
237 * Returns string with hidden return tracking form elements.
238 * @return string
240 function get_form_fields() {
241 if (empty($this->type)) {
242 return '';
245 $result = '<input type="hidden" name="gpr_type" value="'.$this->type.'" />';
247 if (!empty($this->plugin)) {
248 $result .= '<input type="hidden" name="gpr_plugin" value="'.$this->plugin.'" />';
251 if (!empty($this->courseid)) {
252 $result .= '<input type="hidden" name="gpr_courseid" value="'.$this->courseid.'" />';
255 if (!empty($this->userid)) {
256 $result .= '<input type="hidden" name="gpr_userid" value="'.$this->userid.'" />';
259 if (!empty($this->page)) {
260 $result .= '<input type="hidden" name="gpr_page" value="'.$this->page.'" />';
265 * Add hidden elements into mform
266 * @param object $mform moodle form object
267 * @return void
269 function add_mform_elements(&$mform) {
270 if (empty($this->type)) {
271 return;
274 $mform->addElement('hidden', 'gpr_type', $this->type);
275 $mform->setType('gpr_type', PARAM_SAFEDIR);
277 if (!empty($this->plugin)) {
278 $mform->addElement('hidden', 'gpr_plugin', $this->plugin);
279 $mform->setType('gpr_plugin', PARAM_SAFEDIR);
282 if (!empty($this->courseid)) {
283 $mform->addElement('hidden', 'gpr_courseid', $this->courseid);
284 $mform->setType('gpr_courseid', PARAM_INT);
287 if (!empty($this->userid)) {
288 $mform->addElement('hidden', 'gpr_userid', $this->userid);
289 $mform->setType('gpr_userid', PARAM_INT);
292 if (!empty($this->page)) {
293 $mform->addElement('hidden', 'gpr_page', $this->page);
294 $mform->setType('gpr_page', PARAM_INT);
299 * Add return tracking params into url
300 * @param string $url
301 * @return string $url with erturn tracking params
303 function add_url_params($url) {
304 if (empty($this->type)) {
305 return $url;
308 if (strpos($url, '?') === false) {
309 $url .= '?gpr_type='.$this->type;
310 } else {
311 $url .= '&amp;gpr_type='.$this->type;
314 if (!empty($this->plugin)) {
315 $url .= '&amp;gpr_plugin='.$this->plugin;
318 if (!empty($this->courseid)) {
319 $url .= '&amp;gpr_courseid='.$this->courseid;
322 if (!empty($this->userid)) {
323 $url .= '&amp;gpr_userid='.$this->userid;
326 if (!empty($this->page)) {
327 $url .= '&amp;gpr_page='.$this->page;
330 return $url;
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
341 function grade_build_nav($path, $pagename=null, $id=null) {
342 global $CFG, $COURSE;
344 $strgrades = get_string('grades', 'grades');
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);
351 $path_elements = explode('/', $path);
353 $path_elements_count = count($path_elements);
355 $last_element = $path_elements[$path_elements_count-1]; // Should be the filename (including extension)
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');
363 $link = '';
364 $numberofelements = 3;
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;";
373 } else {
374 $id_string .= "id=$id";
378 $navlink4 = null;
380 // Second level links
381 switch ($path_elements[1]) {
382 case 'edit': // No link
383 if ($path_elements[3] != 'index.php') {
384 $numberofelements = 4;
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;
397 if ($path_elements[2] == 'grader') {
398 $numberofelements = 4;
400 break;
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;
408 $navlinks[] = array('name' => get_string($path_elements[1], 'grades'), 'link' => $link, 'type' => 'misc');
410 // Third level links
411 if (empty($pagename)) {
412 $pagename = get_string($path_elements[2], 'grades');
415 switch ($numberofelements) {
416 case 3:
417 $navlinks[] = array('name' => $pagename, 'link' => $link, 'type' => 'misc');
418 break;
419 case 4:
421 if ($path_elements[2] == 'grader' AND $path_elements[3] != 'index.php') {
422 $navlinks[] = array('name' => get_string('modulename', 'gradereport_grader'),
423 'link' => "$CFG->wwwroot/grade/report/grader/index.php$id_string",
424 'type' => 'misc');
426 $navlinks[] = array('name' => $pagename, 'link' => '', 'type' => 'misc');
427 break;
429 $navigation = build_navigation($navlinks);
431 return $navigation;
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.
440 class grade_tree {
443 * The basic representation of the tree as a hierarchical, 3-tiered array.
444 * @var object $top_element
446 var $top_element;
449 * A string of GET URL variables, namely courseid and sesskey, used in most URLs built by this class.
450 * @var string $commonvars
452 var $commonvars;
455 * 2D array of grade items and categories
457 var $levels;
460 * Course context
462 var $context;
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
471 * @param array $collapsed array of collapsed categories
473 function grade_tree($courseid, $fillers=true, $category_grade_last=false, $collapsed=null, $nooutcomes=false) {
474 global $USER, $CFG;
476 $this->courseid = $courseid;
477 $this->commonvars = "&amp;sesskey=$USER->sesskey&amp;id=$this->courseid";
478 $this->levels = array();
479 $this->context = get_context_instance(CONTEXT_COURSE, $courseid);
481 // get course grade tree
482 $this->top_element = grade_category::fetch_course_tree($courseid, true);
484 // collapse the categories if requested
485 if (!empty($collapsed)) {
486 grade_tree::category_collapse($this->top_element, $collapsed);
489 // no otucomes if requested
490 if (!empty($nooutcomes)) {
491 grade_tree::no_outcomes($this->top_element);
494 // move category item to last position in category
495 if ($category_grade_last) {
496 grade_tree::category_grade_last($this->top_element);
499 if ($fillers) {
500 // inject fake categories == fillers
501 grade_tree::inject_fillers($this->top_element, 0);
502 // add colspans to categories and fillers
503 grade_tree::inject_colspans($this->top_element);
506 grade_tree::fill_levels($this->levels, $this->top_element, 0);
510 * Static recursive helper - removes items from collapsed categories
511 * @static
512 * @param array $element The seed of the recursion
513 * @param array $collapsed array of collapsed categories
514 * @return void
516 function category_collapse(&$element, $collapsed) {
517 if ($element['type'] != 'category') {
518 return;
520 if (empty($element['children']) or count($element['children']) < 2) {
521 return;
524 if (in_array($element['object']->id, $collapsed['aggregatesonly'])) {
525 $category_item = reset($element['children']); //keep only category item
526 $element['children'] = array(key($element['children'])=>$category_item);
528 } else {
529 if (in_array($element['object']->id, $collapsed['gradesonly'])) { // Remove category item
530 reset($element['children']);
531 $first_key = key($element['children']);
532 unset($element['children'][$first_key]);
534 foreach ($element['children'] as $sortorder=>$child) { // Recurse through the element's children
535 grade_tree::category_collapse($element['children'][$sortorder], $collapsed);
541 * Static recursive helper - removes all outcomes
542 * @static
543 * @param array $element The seed of the recursion
544 * @return void
546 function no_outcomes(&$element) {
547 if ($element['type'] != 'category') {
548 return;
550 foreach ($element['children'] as $sortorder=>$child) {
551 if ($element['children'][$sortorder]['type'] == 'item'
552 and $element['children'][$sortorder]['object']->is_outcome_item()) {
553 unset($element['children'][$sortorder]);
555 } else if ($element['children'][$sortorder]['type'] == 'category') {
556 grade_tree::no_outcomes($element['children'][$sortorder]);
562 * Static recursive helper - makes the grade_item for category the last children
563 * @static
564 * @param array $element The seed of the recursion
565 * @return void
567 function category_grade_last(&$element) {
568 if (empty($element['children'])) {
569 return;
571 if (count($element['children']) < 2) {
572 return;
574 $category_item = reset($element['children']);
575 $order = key($element['children']);
576 unset($element['children'][$order]);
577 $element['children'][$order] =& $category_item;
578 foreach ($element['children'] as $sortorder=>$child) {
579 grade_tree::category_grade_last($element['children'][$sortorder]);
584 * Static recursive helper - fills the levels array, useful when accessing tree elements of one level
585 * @static
586 * @param int $levels
587 * @param array $element The seed of the recursion
588 * @param int $depth
589 * @return void
591 function fill_levels(&$levels, &$element, $depth) {
592 if (!array_key_exists($depth, $levels)) {
593 $levels[$depth] = array();
596 // prepare unique identifier
597 if ($element['type'] == 'category') {
598 $element['eid'] = 'c'.$element['object']->id;
599 } else if (in_array($element['type'], array('item', 'courseitem', 'categoryitem'))) {
600 $element['eid'] = 'i'.$element['object']->id;
603 $levels[$depth][] =& $element;
604 $depth++;
605 if (empty($element['children'])) {
606 return;
608 $prev = 0;
609 foreach ($element['children'] as $sortorder=>$child) {
610 grade_tree::fill_levels($levels, $element['children'][$sortorder], $depth);
611 $element['children'][$sortorder]['prev'] = $prev;
612 $element['children'][$sortorder]['next'] = 0;
613 if ($prev) {
614 $element['children'][$prev]['next'] = $sortorder;
616 $prev = $sortorder;
621 * Static recursive helper - makes full tree (all leafes are at the same level)
623 function inject_fillers(&$element, $depth) {
624 $depth++;
626 if (empty($element['children'])) {
627 return $depth;
629 $chdepths = array();
630 $chids = array_keys($element['children']);
631 $last_child = end($chids);
632 $first_child = reset($chids);
634 foreach ($chids as $chid) {
635 $chdepths[$chid] = grade_tree::inject_fillers($element['children'][$chid], $depth);
637 arsort($chdepths);
639 $maxdepth = reset($chdepths);
640 foreach ($chdepths as $chid=>$chd) {
641 if ($chd == $maxdepth) {
642 continue;
644 for ($i=0; $i < $maxdepth-$chd; $i++) {
645 if ($chid == $first_child) {
646 $type = 'fillerfirst';
647 } else if ($chid == $last_child) {
648 $type = 'fillerlast';
649 } else {
650 $type = 'filler';
652 $oldchild =& $element['children'][$chid];
653 $element['children'][$chid] = array('object'=>'filler', 'type'=>$type, 'eid'=>'', 'depth'=>$element['object']->depth,'children'=>array($oldchild));
657 return $maxdepth;
661 * Static recursive helper - add colspan information into categories
663 function inject_colspans(&$element) {
664 if (empty($element['children'])) {
665 return 1;
667 $count = 0;
668 foreach ($element['children'] as $key=>$child) {
669 $count += grade_tree::inject_colspans($element['children'][$key]);
671 $element['colspan'] = $count;
672 return $count;
676 * Parses the array in search of a given eid and returns a element object with
677 * information about the element it has found.
678 * @param int $eid
679 * @return object element
681 function locate_element($eid) {
682 // it is a grade - construct a new object
683 if (strpos($eid, 'n') === 0) {
684 if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) {
685 return null;
688 $itemid = $matches[1];
689 $userid = $matches[2];
691 //extra security check - the grade item must be in this tree
692 if (!$item_el = $this->locate_element('i'.$itemid)) {
693 return null;
696 // $gradea->id may be null - means does not exist yet
697 $grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid));
699 $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods!
700 return array('eid'=>'n'.$itemid.'u'.$userid,'object'=>$grade, 'type'=>'grade');
702 } else if (strpos($eid, 'g') === 0) {
703 $id = (int)substr($eid, 1);
704 if (!$grade = grade_grade::fetch(array('id'=>$id))) {
705 return null;
707 //extra security check - the grade item must be in this tree
708 if (!$item_el = $this->locate_element('i'.$grade->itemid)) {
709 return null;
711 $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods!
712 return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade');
715 // it is a category or item
716 foreach ($this->levels as $row) {
717 foreach ($row as $element) {
718 if ($element['type'] == 'filler') {
719 continue;
721 if ($element['eid'] == $eid) {
722 return $element;
727 return null;
731 * Returns the grade eid - the grade may not exist yet.
732 * @param $grade_grade object
733 * @return string eid
735 function get_grade_eid($grade_grade) {
736 if (empty($grade_grade->id)) {
737 return 'n'.$grade_grade->itemid.'u'.$grade_grade->userid;
738 } else {
739 return 'g'.$grade_grade->id;
744 * Return edit icon for give element
745 * @param object $element
746 * @return string
748 function get_edit_icon($element, $gpr) {
749 global $CFG;
751 if (!has_capability('moodle/grade:manage', $this->context)) {
752 if ($element['type'] == 'grade' and has_capability('moodle/grade:override', $this->context)) {
753 // oki - let them override grade
754 } else {
755 return '';
759 static $stredit = null;
760 if (is_null($stredit)) {
761 $stredit = get_string('edit');
764 $object = $element['object'];
765 $overlib = '';
767 switch ($element['type']) {
768 case 'item':
769 case 'categoryitem':
770 case 'courseitem':
771 if (empty($object->outcomeid) || empty($CFG->enableoutcomes)) {
772 $url = $CFG->wwwroot.'/grade/edit/tree/item.php?courseid='.$this->courseid.'&amp;id='.$object->id;
773 } else {
774 $url = $CFG->wwwroot.'/grade/edit/tree/outcomeitem.php?courseid='.$this->courseid.'&amp;id='.$object->id;
776 $url = $gpr->add_url_params($url);
777 break;
779 case 'category':
780 $url = $CFG->wwwroot.'/grade/edit/tree/category.php?courseid='.$this->courseid.'&amp;id='.$object->id;
781 $url = $gpr->add_url_params($url);
782 break;
784 case 'grade':
785 if (empty($object->id)) {
786 $url = $CFG->wwwroot.'/grade/edit/tree/grade.php?courseid='.$this->courseid.'&amp;itemid='.$object->itemid.'&amp;userid='.$object->userid;
787 } else {
788 $url = $CFG->wwwroot.'/grade/edit/tree/grade.php?courseid='.$this->courseid.'&amp;id='.$object->id;
790 $url = $gpr->add_url_params($url);
791 if (!empty($object->feedback)) {
792 $feedback = format_text($object->feedback, $object->feedbackformat);
793 $function = "return overlib('".s(ltrim($object->feedback)."', FULLHTML);");
794 $overlib = 'onmouseover="'.$function.'" onmouseout="return nd();"';
796 break;
798 default:
799 $url = null;
802 if ($url) {
803 return '<a href="'.$url.'"><img '.$overlib.' src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$stredit.'" title="'.$stredit.'"/></a>';
805 } else {
806 return '';
811 * Return hiding icon for give element
812 * @param object $element
813 * @return string
815 function get_hiding_icon($element, $gpr) {
816 global $CFG;
818 if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:hide', $this->context)) {
819 return '';
822 static $strshow = null;
823 static $strhide = null;
824 if (is_null($strshow)) {
825 $strshow = get_string('show');
826 $strhide = get_string('hide');
829 if ($element['object']->is_hidden()) {
830 $icon = 'show';
831 $tooltip = '';
833 if ($element['type'] != 'category' and $element['object']->get_hidden() > 1) { // Change the icon and add a tooltip showing the date
834 $icon = 'hiddenuntil';
835 $tooltip = get_string('hiddenuntildate', 'grades', userdate($element['object']->get_hidden()));
838 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=show&amp;sesskey='.sesskey()
839 . '&amp;eid='.$element['eid'];
840 $url = $gpr->add_url_params($url);
841 $action = '<a href="'.$url.'"><img title="' . $tooltip . '" src="'.$CFG->pixpath.'/t/' . $icon . '.gif" class="iconsmall" alt="'
842 . $strshow.'" title="'.$strshow.'"/></a>';
844 } else {
845 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=hide&amp;sesskey='.sesskey()
846 . '&amp;eid='.$element['eid'];
847 $url = $gpr->add_url_params($url);
848 $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$strhide.'" title="'.$strhide.'"/></a>';
850 return $action;
854 * Return locking icon for give element
855 * @param object $element
856 * @return string
858 function get_locking_icon($element, $gpr) {
859 global $CFG;
861 static $strunlock = null;
862 static $strlock = null;
863 if (is_null($strunlock)) {
864 $strunlock = get_string('unlock', 'grades');
865 $strlock = get_string('lock', 'grades');
868 if ($element['object']->is_locked()) {
869 $icon = 'unlock';
870 $tooltip = '';
872 if ($element['type'] != 'category' and $element['object']->get_locktime() > 1) { // Change the icon and add a tooltip showing the date
873 $icon = 'locktime';
874 $tooltip = get_string('locktimedate', 'grades', userdate($element['object']->get_locktime()));
877 if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:unlock', $this->context)) {
878 return '';
880 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=unlock&amp;sesskey='.sesskey()
881 . '&amp;eid='.$element['eid'];
882 $url = $gpr->add_url_params($url);
883 $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/' . $icon . '.gif" title="' . $tooltip
884 . '" class="iconsmall" alt="'.$strunlock
885 . '" title="'.$strunlock.'"/></a>';
887 } else {
888 if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:lock', $this->context)) {
889 return '';
891 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=lock&amp;sesskey='.sesskey()
892 . '&amp;eid='.$element['eid'];
893 $url = $gpr->add_url_params($url);
894 $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/lock.gif" class="iconsmall" alt="'.$strlock.'" title="'
895 . $strlock.'"/></a>';
897 return $action;
901 * Return calculation icon for given element
902 * @param object $element
903 * @return string
905 function get_calculation_icon($element, $gpr) {
906 global $CFG;
907 if (!has_capability('moodle/grade:manage', $this->context)) {
908 return '';
911 $calculation_icon = '';
913 $type = $element['type'];
914 $object = $element['object'];
916 if ($type == 'item' or $type == 'courseitem' or $type == 'categoryitem') {
917 $streditcalculation = get_string('editcalculation', 'grades');
919 // show calculation icon only when calculation possible
920 if ((!$object->is_normal_item() or $object->is_outcome_item())
921 and ($object->gradetype == GRADE_TYPE_SCALE or $object->gradetype == GRADE_TYPE_VALUE)) {
922 $url = $CFG->wwwroot.'/grade/edit/tree/calculation.php?courseid='.$this->courseid.'&amp;id='.$object->id;
923 $url = $gpr->add_url_params($url);
924 $calculation_icon = '<a href="'. $url.'"><img src="'.$CFG->pixpath.'/t/calc.gif" class="iconsmall" alt="'
925 . $streditcalculation.'" title="'.$streditcalculation.'" /></a>'. "\n";
929 return $calculation_icon;