Was not checking error return from plugin export properly
[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 */
aea4df41 473 function grade_tree($courseid, $fillers=true, $category_grade_last=false, $collapsed=null, $nooutcomes=false) {
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
aea4df41 489 // no otucomes if requested
490 if (!empty($nooutcomes)) {
491 grade_tree::no_outcomes($this->top_element);
492 }
493
4faf5f99 494 // move category item to last position in category
7a6b7acf 495 if ($category_grade_last) {
496 grade_tree::category_grade_last($this->top_element);
497 }
498
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);
504 }
505
506 grade_tree::fill_levels($this->levels, $this->top_element, 0);
507 }
508
4faf5f99 509 /**
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
515 */
516 function category_collapse(&$element, $collapsed) {
517 if ($element['type'] != 'category') {
518 return;
519 }
520 if (empty($element['children']) or count($element['children']) < 2) {
521 return;
522 }
523
524 if (in_array($element['object']->id, $collapsed)) {
525 $category_item = reset($element['children']); //keep only category item
526 $element['children'] = array(key($element['children'])=>$category_item);
527
528 } else {
529 foreach ($element['children'] as $sortorder=>$child) {
530 grade_tree::category_collapse($element['children'][$sortorder], $collapsed);
531 }
532 }
533 }
7a6b7acf 534
aea4df41 535 /**
536 * Static recursive helper - removes all outcomes
537 * @static
538 * @param array $element The seed of the recursion
539 * @return void
540 */
541 function no_outcomes(&$element) {
542 if ($element['type'] != 'category') {
543 return;
544 }
545 foreach ($element['children'] as $sortorder=>$child) {
546 if ($element['children'][$sortorder]['type'] == 'item'
547 and $element['children'][$sortorder]['object']->is_outcome_item()) {
548 unset($element['children'][$sortorder]);
549
550 } else if ($element['children'][$sortorder]['type'] == 'category') {
551 grade_tree::no_outcomes($element['children'][$sortorder]);
552 }
553 }
554 }
555
7a6b7acf 556 /**
557 * Static recursive helper - makes the grade_item for category the last children
558 * @static
559 * @param array $element The seed of the recursion
560 * @return void
561 */
562 function category_grade_last(&$element) {
563 if (empty($element['children'])) {
564 return;
565 }
566 if (count($element['children']) < 2) {
567 return;
568 }
569 $category_item = reset($element['children']);
570 $order = key($element['children']);
571 unset($element['children'][$order]);
572 $element['children'][$order] =& $category_item;
573 foreach ($element['children'] as $sortorder=>$child) {
574 grade_tree::category_grade_last($element['children'][$sortorder]);
575 }
576 }
577
578 /**
579 * Static recursive helper - fills the levels array, useful when accessing tree elements of one level
580 * @static
581 * @param int $levels
582 * @param array $element The seed of the recursion
583 * @param int $depth
584 * @return void
585 */
586 function fill_levels(&$levels, &$element, $depth) {
587 if (!array_key_exists($depth, $levels)) {
588 $levels[$depth] = array();
589 }
590
591 // prepare unique identifier
592 if ($element['type'] == 'category') {
593 $element['eid'] = 'c'.$element['object']->id;
594 } else if (in_array($element['type'], array('item', 'courseitem', 'categoryitem'))) {
595 $element['eid'] = 'i'.$element['object']->id;
596 }
597
598 $levels[$depth][] =& $element;
599 $depth++;
600 if (empty($element['children'])) {
601 return;
602 }
603 $prev = 0;
604 foreach ($element['children'] as $sortorder=>$child) {
605 grade_tree::fill_levels($levels, $element['children'][$sortorder], $depth);
606 $element['children'][$sortorder]['prev'] = $prev;
607 $element['children'][$sortorder]['next'] = 0;
608 if ($prev) {
609 $element['children'][$prev]['next'] = $sortorder;
610 }
611 $prev = $sortorder;
612 }
613 }
614
615 /**
616 * Static recursive helper - makes full tree (all leafes are at the same level)
617 */
618 function inject_fillers(&$element, $depth) {
619 $depth++;
620
621 if (empty($element['children'])) {
622 return $depth;
623 }
624 $chdepths = array();
625 $chids = array_keys($element['children']);
626 $last_child = end($chids);
627 $first_child = reset($chids);
628
629 foreach ($chids as $chid) {
630 $chdepths[$chid] = grade_tree::inject_fillers($element['children'][$chid], $depth);
631 }
632 arsort($chdepths);
633
634 $maxdepth = reset($chdepths);
635 foreach ($chdepths as $chid=>$chd) {
636 if ($chd == $maxdepth) {
637 continue;
638 }
639 for ($i=0; $i < $maxdepth-$chd; $i++) {
640 if ($chid == $first_child) {
641 $type = 'fillerfirst';
642 } else if ($chid == $last_child) {
643 $type = 'fillerlast';
644 } else {
645 $type = 'filler';
646 }
647 $oldchild =& $element['children'][$chid];
648 $element['children'][$chid] = array('object'=>'filler', 'type'=>$type, 'eid'=>'', 'depth'=>$element['object']->depth,'children'=>array($oldchild));
649 }
650 }
651
652 return $maxdepth;
653 }
654
655 /**
656 * Static recursive helper - add colspan information into categories
657 */
658 function inject_colspans(&$element) {
659 if (empty($element['children'])) {
660 return 1;
661 }
662 $count = 0;
663 foreach ($element['children'] as $key=>$child) {
664 $count += grade_tree::inject_colspans($element['children'][$key]);
665 }
666 $element['colspan'] = $count;
667 return $count;
668 }
669
670 /**
671 * Parses the array in search of a given eid and returns a element object with
672 * information about the element it has found.
673 * @param int $eid
674 * @return object element
675 */
676 function locate_element($eid) {
d3c3da1b 677 // it is a grade - construct a new object
678 if (strpos($eid, 'n') === 0) {
679 if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) {
680 return null;
681 }
682
683 $itemid = $matches[1];
684 $userid = $matches[2];
685
686 //extra security check - the grade item must be in this tree
687 if (!$item_el = $this->locate_element('i'.$itemid)) {
688 return null;
689 }
690
691 // $gradea->id may be null - means does not exist yet
692 $grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid));
693
694 $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods!
695 return array('eid'=>'n'.$itemid.'u'.$userid,'object'=>$grade, 'type'=>'grade');
696
697 } else if (strpos($eid, 'g') === 0) {
7a6b7acf 698 $id = (int)substr($eid, 1);
699 if (!$grade = grade_grade::fetch(array('id'=>$id))) {
700 return null;
701 }
702 //extra security check - the grade item must be in this tree
703 if (!$item_el = $this->locate_element('i'.$grade->itemid)) {
704 return null;
705 }
706 $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods!
707 return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade');
708 }
709
710 // it is a category or item
711 foreach ($this->levels as $row) {
712 foreach ($row as $element) {
713 if ($element['type'] == 'filler') {
714 continue;
715 }
716 if ($element['eid'] == $eid) {
717 return $element;
718 }
719 }
720 }
721
722 return null;
723 }
724
d3c3da1b 725 /**
726 * Returns the grade eid - the grade may not exist yet.
727 * @param $grade_grade object
728 * @return string eid
729 */
730 function get_grade_eid($grade_grade) {
731 if (empty($grade_grade->id)) {
732 return 'n'.$grade_grade->itemid.'u'.$grade_grade->userid;
733 } else {
734 return 'g'.$grade_grade->id;
735 }
736 }
737
7a6b7acf 738 /**
739 * Return edit icon for give element
740 * @param object $element
741 * @return string
742 */
743 function get_edit_icon($element, $gpr) {
744 global $CFG;
745
2cc773f5 746 if (!has_capability('moodle/grade:manage', $this->context)) {
27b1735b 747 if ($element['type'] == 'grade' and has_capability('moodle/grade:override', $this->context)) {
748 // oki - let them override grade
749 } else {
750 return '';
751 }
7a6b7acf 752 }
753
2cc773f5 754 static $stredit = null;
755 if (is_null($stredit)) {
756 $stredit = get_string('edit');
757 }
758
7a6b7acf 759 $object = $element['object'];
2cc773f5 760 $overlib = '';
7a6b7acf 761
762 switch ($element['type']) {
763 case 'item':
764 case 'categoryitem':
765 case 'courseitem':
9988d1b7 766 if (empty($object->outcomeid) || empty($CFG->enableoutcomes)) {
78ad5f3f 767 $url = $CFG->wwwroot.'/grade/edit/tree/item.php?courseid='.$this->courseid.'&amp;id='.$object->id;
f10fac96 768 } else {
78ad5f3f 769 $url = $CFG->wwwroot.'/grade/edit/tree/outcomeitem.php?courseid='.$this->courseid.'&amp;id='.$object->id;
f10fac96 770 }
7a6b7acf 771 $url = $gpr->add_url_params($url);
772 break;
773
774 case 'category':
78ad5f3f 775 $url = $CFG->wwwroot.'/grade/edit/tree/category.php?courseid='.$this->courseid.'&amp;id='.$object->id;
7a6b7acf 776 $url = $gpr->add_url_params($url);
777 break;
778
779 case 'grade':
d3c3da1b 780 if (empty($object->id)) {
781 $url = $CFG->wwwroot.'/grade/edit/tree/grade.php?courseid='.$this->courseid.'&amp;itemid='.$object->itemid.'&amp;userid='.$object->userid;
782 } else {
783 $url = $CFG->wwwroot.'/grade/edit/tree/grade.php?courseid='.$this->courseid.'&amp;id='.$object->id;
784 }
7a6b7acf 785 $url = $gpr->add_url_params($url);
2cc773f5 786 if (!empty($object->feedback)) {
787 $feedback = format_text($object->feedback, $object->feedbackformat);
788 $function = "return overlib('".s(ltrim($object->feedback)."', FULLHTML);");
789 $overlib = 'onmouseover="'.$function.'" onmouseout="return nd();"';
790 }
7a6b7acf 791 break;
792
793 default:
794 $url = null;
795 }
796
797 if ($url) {
2cc773f5 798 return '<a href="'.$url.'"><img '.$overlib.' src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$stredit.'" title="'.$stredit.'"/></a>';
7a6b7acf 799
800 } else {
801 return '';
802 }
803 }
804
805 /**
806 * Return hiding icon for give element
807 * @param object $element
808 * @return string
809 */
810 function get_hiding_icon($element, $gpr) {
811 global $CFG;
812
2cc773f5 813 if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:hide', $this->context)) {
7a6b7acf 814 return '';
815 }
816
2cc773f5 817 static $strshow = null;
818 static $strhide = null;
819 if (is_null($strshow)) {
7a6b7acf 820 $strshow = get_string('show');
2cc773f5 821 $strhide = get_string('hide');
822 }
823
824 if ($element['object']->is_hidden()) {
78ad5f3f 825 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=show&amp;sesskey='.sesskey().'&amp;eid='.$element['eid'];
7a6b7acf 826 $url = $gpr->add_url_params($url);
827 $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.$strshow.'" title="'.$strshow.'"/></a>';
828
829 } else {
78ad5f3f 830 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=hide&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/hide.gif" class="iconsmall" alt="'.$strhide.'" title="'.$strhide.'"/></a>';
833 }
834 return $action;
835 }
836
837 /**
838 * Return locking icon for give element
839 * @param object $element
840 * @return string
841 */
842 function get_locking_icon($element, $gpr) {
843 global $CFG;
844
2cc773f5 845 static $strunlock = null;
846 static $strlock = null;
847 if (is_null($strunlock)) {
848 $strunlock = get_string('unlock', 'grades');
849 $strlock = get_string('lock', 'grades');
850 }
7a6b7acf 851
852 if ($element['object']->is_locked()) {
2cc773f5 853 if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:unlock', $this->context)) {
7a6b7acf 854 return '';
855 }
78ad5f3f 856 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=unlock&amp;sesskey='.sesskey().'&amp;eid='.$element['eid'];
7a6b7acf 857 $url = $gpr->add_url_params($url);
858 $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/unlock.gif" class="iconsmall" alt="'.$strunlock.'" title="'.$strunlock.'"/></a>';
859
860 } else {
2cc773f5 861 if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:lock', $this->context)) {
7a6b7acf 862 return '';
863 }
78ad5f3f 864 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=lock&amp;sesskey='.sesskey().'&amp;eid='.$element['eid'];
7a6b7acf 865 $url = $gpr->add_url_params($url);
866 $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/lock.gif" class="iconsmall" alt="'.$strlock.'" title="'.$strlock.'"/></a>';
867 }
868 return $action;
869 }
870
2cc773f5 871 /**
872 * Return calculation icon for given element
873 * @param object $element
874 * @return string
875 */
876 function get_calculation_icon($element, $gpr) {
877 global $CFG;
878 if (!has_capability('moodle/grade:manage', $this->context)) {
879 return '';
880 }
881
882 $calculation_icon = '';
883
884 $type = $element['type'];
885 $object = $element['object'];
886
887 if ($type == 'item' or $type == 'courseitem' or $type == 'categoryitem') {
888 $streditcalculation = get_string('editcalculation', 'grades');
889
890 // show calculation icon only when calculation possible
9127bc5d 891 if ((!$object->is_normal_item() or $object->is_outcome_item())
892 and ($object->gradetype == GRADE_TYPE_SCALE or $object->gradetype == GRADE_TYPE_VALUE)) {
78ad5f3f 893 $url = $CFG->wwwroot.'/grade/edit/tree/calculation.php?courseid='.$this->courseid.'&amp;id='.$object->id;
2cc773f5 894 $url = $gpr->add_url_params($url);
895 $calculation_icon = '<a href="'. $url.'"><img src="'.$CFG->pixpath.'/t/calc.gif" class="iconsmall" alt="'
896 . $streditcalculation.'" title="'.$streditcalculation.'" /></a>'. "\n";
897 }
898 }
899
900 return $calculation_icon;
901 }
7a6b7acf 902}
903
e2008be2 904?>