fixing scale backup/restore
[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
a2a136b8 117 if (!empty($CFG->enableoutcomes) && (has_capability('moodle/grade:manage', $context) or has_capability('course:update', $context))) {
04259694 118 if (has_capability('moodle/grade:manage', $context)) {
119 $url = 'edit/outcome/index.php?id='.$courseid;
120 } else {
121 $url = 'edit/outcome/course.php?id='.$courseid;
122 }
78ad5f3f 123 if ($active_type == 'edit' and $active_plugin == 'outcome' ) {
124 $active = $url;
125 }
126 $menu[$url] = get_string('outcomes', 'grades');
cbff94ba 127 }
281ffa4a 128 }
129
3af29899 130/// finally print/return the popup form
0c811aa7 131 return popup_form($CFG->wwwroot.'/grade/', $menu, 'choosepluginreport', $active, 'choose', '', '', $return, 'self', get_string('view'));
cbff94ba 132}
133
0610812a 134/**
7a6b7acf 135 * Utility class used for return tracking when using edit and other forms in grade plugins
0610812a 136 */
3af29899 137class grade_plugin_return {
138 var $type;
139 var $plugin;
140 var $courseid;
141 var $userid;
142 var $page;
281ffa4a 143
0610812a 144 /**
145 * Constructor
146 * @param array $params - associative array with return parameters, if null parameter are taken from _GET or _POST
147 */
3af29899 148 function grade_plugin_return ($params=null) {
149 if (empty($params)) {
150 $this->type = optional_param('gpr_type', null, PARAM_SAFEDIR);
151 $this->plugin = optional_param('gpr_plugin', null, PARAM_SAFEDIR);
152 $this->courseid = optional_param('gpr_courseid', null, PARAM_INT);
153 $this->userid = optional_param('gpr_userid', null, PARAM_INT);
154 $this->page = optional_param('gpr_page', null, PARAM_INT);
a983b6ec 155
a983b6ec 156 } else {
3af29899 157 foreach ($params as $key=>$value) {
158 if (array_key_exists($key, $this)) {
159 $this->$key = $value;
160 }
cbff94ba 161 }
162 }
6cd8c592 163 }
164
0610812a 165 /**
166 * Returns return parameters as options array suitable for buttons.
167 * @return array options
168 */
3af29899 169 function get_options() {
7a6b7acf 170 if (empty($this->type)) {
3af29899 171 return array();
865e9a82 172 }
6cd8c592 173
3af29899 174 $params = array();
6cd8c592 175
7a6b7acf 176 if (!empty($this->plugin)) {
177 $params['plugin'] = $this->plugin;
178 }
6cd8c592 179
3af29899 180 if (!empty($this->courseid)) {
181 $params['id'] = $this->courseid;
6cd8c592 182 }
9c61ba4d 183
3af29899 184 if (!empty($this->userid)) {
185 $params['userid'] = $this->userid;
9c61ba4d 186 }
9c61ba4d 187
3af29899 188 if (!empty($this->page)) {
189 $params['page'] = $this->page;
cbff94ba 190 }
865e9a82 191
3af29899 192 return $params;
cbff94ba 193 }
cbff94ba 194
0610812a 195 /**
196 * Returns return url
197 * @param string $default default url when params not set
198 * @return string url
199 */
65dd61bd 200 function get_return_url($default, $extras=null) {
3af29899 201 global $CFG;
cbff94ba 202
3af29899 203 if (empty($this->type) or empty($this->plugin)) {
204 return $default;
cbff94ba 205 }
206
65dd61bd 207 $url = $CFG->wwwroot.'/grade/'.$this->type.'/'.$this->plugin.'/index.php';
208 $glue = '?';
cbff94ba 209
3af29899 210 if (!empty($this->courseid)) {
211 $url .= $glue.'id='.$this->courseid;
212 $glue = '&amp;';
cbff94ba 213 }
cbff94ba 214
3af29899 215 if (!empty($this->userid)) {
216 $url .= $glue.'userid='.$this->userid;
217 $glue = '&amp;';
cbff94ba 218 }
7e2d7c92 219
3af29899 220 if (!empty($this->page)) {
221 $url .= $glue.'page='.$this->page;
65dd61bd 222 $glue = '&amp;';
223 }
224
225 if (!empty($extras)) {
226 foreach($extras as $key=>$value) {
227 $url .= $glue.$key.'='.$value;
228 $glue = '&amp;';
7a6b7acf 229 }
cbff94ba 230 }
cbff94ba 231
3af29899 232 return $url;
cbff94ba 233 }
cbff94ba 234
0610812a 235 /**
236 * Returns string with hidden return tracking form elements.
237 * @return string
238 */
3af29899 239 function get_form_fields() {
7a6b7acf 240 if (empty($this->type)) {
3af29899 241 return '';
cbff94ba 242 }
cbff94ba 243
3af29899 244 $result = '<input type="hidden" name="gpr_type" value="'.$this->type.'" />';
7a6b7acf 245
246 if (!empty($this->plugin)) {
247 $result .= '<input type="hidden" name="gpr_plugin" value="'.$this->plugin.'" />';
248 }
0ca5abd6 249
3af29899 250 if (!empty($this->courseid)) {
251 $result .= '<input type="hidden" name="gpr_courseid" value="'.$this->courseid.'" />';
cbff94ba 252 }
cbff94ba 253
3af29899 254 if (!empty($this->userid)) {
255 $result .= '<input type="hidden" name="gpr_userid" value="'.$this->userid.'" />';
cbff94ba 256 }
cbff94ba 257
3af29899 258 if (!empty($this->page)) {
259 $result .= '<input type="hidden" name="gpr_page" value="'.$this->page.'" />';
cbff94ba 260 }
261 }
cbff94ba 262
0610812a 263 /**
264 * Add hidden elements into mform
265 * @param object $mform moodle form object
266 * @return void
267 */
3af29899 268 function add_mform_elements(&$mform) {
7a6b7acf 269 if (empty($this->type)) {
3af29899 270 return;
cbff94ba 271 }
cbff94ba 272
3af29899 273 $mform->addElement('hidden', 'gpr_type', $this->type);
274 $mform->setType('gpr_type', PARAM_SAFEDIR);
cbff94ba 275
7a6b7acf 276 if (!empty($this->plugin)) {
277 $mform->addElement('hidden', 'gpr_plugin', $this->plugin);
278 $mform->setType('gpr_plugin', PARAM_SAFEDIR);
279 }
97033c86 280
3af29899 281 if (!empty($this->courseid)) {
282 $mform->addElement('hidden', 'gpr_courseid', $this->courseid);
283 $mform->setType('gpr_courseid', PARAM_INT);
cbff94ba 284 }
cbff94ba 285
3af29899 286 if (!empty($this->userid)) {
287 $mform->addElement('hidden', 'gpr_userid', $this->userid);
288 $mform->setType('gpr_userid', PARAM_INT);
cbff94ba 289 }
cbff94ba 290
3af29899 291 if (!empty($this->page)) {
292 $mform->addElement('hidden', 'gpr_page', $this->page);
293 $mform->setType('gpr_page', PARAM_INT);
cbff94ba 294 }
295 }
281ffa4a 296
0610812a 297 /**
298 * Add return tracking params into url
299 * @param string $url
300 * @return string $url with erturn tracking params
301 */
3af29899 302 function add_url_params($url) {
7a6b7acf 303 if (empty($this->type)) {
3af29899 304 return $url;
cbff94ba 305 }
5609f9e6 306
3af29899 307 if (strpos($url, '?') === false) {
308 $url .= '?gpr_type='.$this->type;
309 } else {
310 $url .= '&amp;gpr_type='.$this->type;
cbff94ba 311 }
cbff94ba 312
7a6b7acf 313 if (!empty($this->plugin)) {
314 $url .= '&amp;gpr_plugin='.$this->plugin;
315 }
cbff94ba 316
3af29899 317 if (!empty($this->courseid)) {
318 $url .= '&amp;gpr_courseid='.$this->courseid;
cbff94ba 319 }
cbff94ba 320
3af29899 321 if (!empty($this->userid)) {
322 $url .= '&amp;gpr_userid='.$this->userid;
cbff94ba 323 }
0a8a95c9 324
3af29899 325 if (!empty($this->page)) {
326 $url .= '&amp;gpr_page='.$this->page;
0a8a95c9 327 }
5a412dbf 328
3af29899 329 return $url;
5a412dbf 330 }
5a412dbf 331}
7a6b7acf 332
333
334/**
335 * This class represents a complete tree of categories, grade_items and final grades,
336 * organises as an array primarily, but which can also be converted to other formats.
337 * It has simple method calls with complex implementations, allowing for easy insertion,
338 * deletion and moving of items and categories within the tree.
339 */
340class grade_tree {
341
342 /**
343 * The basic representation of the tree as a hierarchical, 3-tiered array.
344 * @var object $top_element
345 */
346 var $top_element;
347
348 /**
349 * A string of GET URL variables, namely courseid and sesskey, used in most URLs built by this class.
350 * @var string $commonvars
351 */
352 var $commonvars;
353
354 /**
355 * 2D array of grade items and categories
356 */
357 var $levels;
358
2cc773f5 359 /**
360 * Course context
361 */
362 var $context;
363
7a6b7acf 364 /**
365 * Constructor, retrieves and stores a hierarchical array of all grade_category and grade_item
366 * objects for the given courseid. Full objects are instantiated.
367 * and renumbering.
368 * @param int $courseid
369 * @param boolean $fillers include fillers and colspans, make the levels var "rectangular"
370 * @param boolean $category_grade_last category grade item is the last child
4faf5f99 371 * @param array $collapsed array of collapsed categories
7a6b7acf 372 */
4faf5f99 373 function grade_tree($courseid, $fillers=true, $category_grade_last=false, $collapsed=null) {
7a6b7acf 374 global $USER, $CFG;
375
376 $this->courseid = $courseid;
377 $this->commonvars = "&amp;sesskey=$USER->sesskey&amp;id=$this->courseid";
378 $this->levels = array();
2cc773f5 379 $this->context = get_context_instance(CONTEXT_COURSE, $courseid);
7a6b7acf 380
381 // get course grade tree
382 $this->top_element = grade_category::fetch_course_tree($courseid, true);
383
4faf5f99 384 // collapse the categories if requested
385 if (!empty($collapsed)) {
386 grade_tree::category_collapse($this->top_element, $collapsed);
387 }
388
389 // move category item to last position in category
7a6b7acf 390 if ($category_grade_last) {
391 grade_tree::category_grade_last($this->top_element);
392 }
393
394 if ($fillers) {
395 // inject fake categories == fillers
396 grade_tree::inject_fillers($this->top_element, 0);
397 // add colspans to categories and fillers
398 grade_tree::inject_colspans($this->top_element);
399 }
400
401 grade_tree::fill_levels($this->levels, $this->top_element, 0);
402 }
403
4faf5f99 404 /**
405 * Static recursive helper - removes items from collapsed categories
406 * @static
407 * @param array $element The seed of the recursion
408 * @param array $collapsed array of collapsed categories
409 * @return void
410 */
411 function category_collapse(&$element, $collapsed) {
412 if ($element['type'] != 'category') {
413 return;
414 }
415 if (empty($element['children']) or count($element['children']) < 2) {
416 return;
417 }
418
419 if (in_array($element['object']->id, $collapsed)) {
420 $category_item = reset($element['children']); //keep only category item
421 $element['children'] = array(key($element['children'])=>$category_item);
422
423 } else {
424 foreach ($element['children'] as $sortorder=>$child) {
425 grade_tree::category_collapse($element['children'][$sortorder], $collapsed);
426 }
427 }
428 }
7a6b7acf 429
430 /**
431 * Static recursive helper - makes the grade_item for category the last children
432 * @static
433 * @param array $element The seed of the recursion
434 * @return void
435 */
436 function category_grade_last(&$element) {
437 if (empty($element['children'])) {
438 return;
439 }
440 if (count($element['children']) < 2) {
441 return;
442 }
443 $category_item = reset($element['children']);
444 $order = key($element['children']);
445 unset($element['children'][$order]);
446 $element['children'][$order] =& $category_item;
447 foreach ($element['children'] as $sortorder=>$child) {
448 grade_tree::category_grade_last($element['children'][$sortorder]);
449 }
450 }
451
452 /**
453 * Static recursive helper - fills the levels array, useful when accessing tree elements of one level
454 * @static
455 * @param int $levels
456 * @param array $element The seed of the recursion
457 * @param int $depth
458 * @return void
459 */
460 function fill_levels(&$levels, &$element, $depth) {
461 if (!array_key_exists($depth, $levels)) {
462 $levels[$depth] = array();
463 }
464
465 // prepare unique identifier
466 if ($element['type'] == 'category') {
467 $element['eid'] = 'c'.$element['object']->id;
468 } else if (in_array($element['type'], array('item', 'courseitem', 'categoryitem'))) {
469 $element['eid'] = 'i'.$element['object']->id;
470 }
471
472 $levels[$depth][] =& $element;
473 $depth++;
474 if (empty($element['children'])) {
475 return;
476 }
477 $prev = 0;
478 foreach ($element['children'] as $sortorder=>$child) {
479 grade_tree::fill_levels($levels, $element['children'][$sortorder], $depth);
480 $element['children'][$sortorder]['prev'] = $prev;
481 $element['children'][$sortorder]['next'] = 0;
482 if ($prev) {
483 $element['children'][$prev]['next'] = $sortorder;
484 }
485 $prev = $sortorder;
486 }
487 }
488
489 /**
490 * Static recursive helper - makes full tree (all leafes are at the same level)
491 */
492 function inject_fillers(&$element, $depth) {
493 $depth++;
494
495 if (empty($element['children'])) {
496 return $depth;
497 }
498 $chdepths = array();
499 $chids = array_keys($element['children']);
500 $last_child = end($chids);
501 $first_child = reset($chids);
502
503 foreach ($chids as $chid) {
504 $chdepths[$chid] = grade_tree::inject_fillers($element['children'][$chid], $depth);
505 }
506 arsort($chdepths);
507
508 $maxdepth = reset($chdepths);
509 foreach ($chdepths as $chid=>$chd) {
510 if ($chd == $maxdepth) {
511 continue;
512 }
513 for ($i=0; $i < $maxdepth-$chd; $i++) {
514 if ($chid == $first_child) {
515 $type = 'fillerfirst';
516 } else if ($chid == $last_child) {
517 $type = 'fillerlast';
518 } else {
519 $type = 'filler';
520 }
521 $oldchild =& $element['children'][$chid];
522 $element['children'][$chid] = array('object'=>'filler', 'type'=>$type, 'eid'=>'', 'depth'=>$element['object']->depth,'children'=>array($oldchild));
523 }
524 }
525
526 return $maxdepth;
527 }
528
529 /**
530 * Static recursive helper - add colspan information into categories
531 */
532 function inject_colspans(&$element) {
533 if (empty($element['children'])) {
534 return 1;
535 }
536 $count = 0;
537 foreach ($element['children'] as $key=>$child) {
538 $count += grade_tree::inject_colspans($element['children'][$key]);
539 }
540 $element['colspan'] = $count;
541 return $count;
542 }
543
544 /**
545 * Parses the array in search of a given eid and returns a element object with
546 * information about the element it has found.
547 * @param int $eid
548 * @return object element
549 */
550 function locate_element($eid) {
551 if (strpos($eid, 'g') === 0) {
552 // it is a grade construct a new object
553 $id = (int)substr($eid, 1);
554 if (!$grade = grade_grade::fetch(array('id'=>$id))) {
555 return null;
556 }
557 //extra security check - the grade item must be in this tree
558 if (!$item_el = $this->locate_element('i'.$grade->itemid)) {
559 return null;
560 }
561 $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods!
562 return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade');
563 }
564
565 // it is a category or item
566 foreach ($this->levels as $row) {
567 foreach ($row as $element) {
568 if ($element['type'] == 'filler') {
569 continue;
570 }
571 if ($element['eid'] == $eid) {
572 return $element;
573 }
574 }
575 }
576
577 return null;
578 }
579
580 /**
581 * Return edit icon for give element
582 * @param object $element
583 * @return string
584 */
585 function get_edit_icon($element, $gpr) {
586 global $CFG;
587
2cc773f5 588 if (!has_capability('moodle/grade:manage', $this->context)) {
7a6b7acf 589 return '';
590 }
591
2cc773f5 592 static $stredit = null;
593 if (is_null($stredit)) {
594 $stredit = get_string('edit');
595 }
596
7a6b7acf 597 $object = $element['object'];
2cc773f5 598 $overlib = '';
7a6b7acf 599
600 switch ($element['type']) {
601 case 'item':
602 case 'categoryitem':
603 case 'courseitem':
9988d1b7 604 if (empty($object->outcomeid) || empty($CFG->enableoutcomes)) {
78ad5f3f 605 $url = $CFG->wwwroot.'/grade/edit/tree/item.php?courseid='.$this->courseid.'&amp;id='.$object->id;
f10fac96 606 } else {
78ad5f3f 607 $url = $CFG->wwwroot.'/grade/edit/tree/outcomeitem.php?courseid='.$this->courseid.'&amp;id='.$object->id;
f10fac96 608 }
7a6b7acf 609 $url = $gpr->add_url_params($url);
610 break;
611
612 case 'category':
78ad5f3f 613 $url = $CFG->wwwroot.'/grade/edit/tree/category.php?courseid='.$this->courseid.'&amp;id='.$object->id;
7a6b7acf 614 $url = $gpr->add_url_params($url);
615 break;
616
617 case 'grade':
618 //TODO: improve dealing with new grades
78ad5f3f 619 $url = $CFG->wwwroot.'/grade/edit/tree/grade.php?courseid='.$this->courseid.'&amp;id='.$object->id;
7a6b7acf 620 $url = $gpr->add_url_params($url);
2cc773f5 621 if (!empty($object->feedback)) {
622 $feedback = format_text($object->feedback, $object->feedbackformat);
623 $function = "return overlib('".s(ltrim($object->feedback)."', FULLHTML);");
624 $overlib = 'onmouseover="'.$function.'" onmouseout="return nd();"';
625 }
7a6b7acf 626 break;
627
628 default:
629 $url = null;
630 }
631
632 if ($url) {
2cc773f5 633 return '<a href="'.$url.'"><img '.$overlib.' src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$stredit.'" title="'.$stredit.'"/></a>';
7a6b7acf 634
635 } else {
636 return '';
637 }
638 }
639
640 /**
641 * Return hiding icon for give element
642 * @param object $element
643 * @return string
644 */
645 function get_hiding_icon($element, $gpr) {
646 global $CFG;
647
2cc773f5 648 if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:hide', $this->context)) {
7a6b7acf 649 return '';
650 }
651
2cc773f5 652 static $strshow = null;
653 static $strhide = null;
654 if (is_null($strshow)) {
7a6b7acf 655 $strshow = get_string('show');
2cc773f5 656 $strhide = get_string('hide');
657 }
658
659 if ($element['object']->is_hidden()) {
78ad5f3f 660 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=show&amp;sesskey='.sesskey().'&amp;eid='.$element['eid'];
7a6b7acf 661 $url = $gpr->add_url_params($url);
662 $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.$strshow.'" title="'.$strshow.'"/></a>';
663
664 } else {
78ad5f3f 665 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=hide&amp;sesskey='.sesskey().'&amp;eid='.$element['eid'];
7a6b7acf 666 $url = $gpr->add_url_params($url);
667 $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$strhide.'" title="'.$strhide.'"/></a>';
668 }
669 return $action;
670 }
671
672 /**
673 * Return locking icon for give element
674 * @param object $element
675 * @return string
676 */
677 function get_locking_icon($element, $gpr) {
678 global $CFG;
679
2cc773f5 680 static $strunlock = null;
681 static $strlock = null;
682 if (is_null($strunlock)) {
683 $strunlock = get_string('unlock', 'grades');
684 $strlock = get_string('lock', 'grades');
685 }
7a6b7acf 686
687 if ($element['object']->is_locked()) {
2cc773f5 688 if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:unlock', $this->context)) {
7a6b7acf 689 return '';
690 }
78ad5f3f 691 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=unlock&amp;sesskey='.sesskey().'&amp;eid='.$element['eid'];
7a6b7acf 692 $url = $gpr->add_url_params($url);
693 $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/unlock.gif" class="iconsmall" alt="'.$strunlock.'" title="'.$strunlock.'"/></a>';
694
695 } else {
2cc773f5 696 if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:lock', $this->context)) {
7a6b7acf 697 return '';
698 }
78ad5f3f 699 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=lock&amp;sesskey='.sesskey().'&amp;eid='.$element['eid'];
7a6b7acf 700 $url = $gpr->add_url_params($url);
701 $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/lock.gif" class="iconsmall" alt="'.$strlock.'" title="'.$strlock.'"/></a>';
702 }
703 return $action;
704 }
705
2cc773f5 706 /**
707 * Return calculation icon for given element
708 * @param object $element
709 * @return string
710 */
711 function get_calculation_icon($element, $gpr) {
712 global $CFG;
713 if (!has_capability('moodle/grade:manage', $this->context)) {
714 return '';
715 }
716
717 $calculation_icon = '';
718
719 $type = $element['type'];
720 $object = $element['object'];
721
722 if ($type == 'item' or $type == 'courseitem' or $type == 'categoryitem') {
723 $streditcalculation = get_string('editcalculation', 'grades');
724
725 // show calculation icon only when calculation possible
726 if (!$object->is_normal_item() and ($object->gradetype == GRADE_TYPE_SCALE or $object->gradetype == GRADE_TYPE_VALUE)) {
78ad5f3f 727 $url = $CFG->wwwroot.'/grade/edit/tree/calculation.php?courseid='.$this->courseid.'&amp;id='.$object->id;
2cc773f5 728 $url = $gpr->add_url_params($url);
729 $calculation_icon = '<a href="'. $url.'"><img src="'.$CFG->pixpath.'/t/calc.gif" class="iconsmall" alt="'
730 . $streditcalculation.'" title="'.$streditcalculation.'" /></a>'. "\n";
731 }
732 }
733
734 return $calculation_icon;
735 }
7a6b7acf 736}
737
e2008be2 738?>