small commenting change
[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') {
3cf6a6d5 422 $navlinks[] = array('name' => get_string('modulename', 'gradereport_grader'),
826c5f86 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;
d4795a07 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
384960dd 524 if (in_array($element['object']->id, $collapsed['aggregatesonly'])) {
4faf5f99 525 $category_item = reset($element['children']); //keep only category item
526 $element['children'] = array(key($element['children'])=>$category_item);
527
528 } else {
384960dd 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]);
533 }
534 foreach ($element['children'] as $sortorder=>$child) { // Recurse through the element's children
4faf5f99 535 grade_tree::category_collapse($element['children'][$sortorder], $collapsed);
536 }
537 }
538 }
7a6b7acf 539
aea4df41 540 /**
541 * Static recursive helper - removes all outcomes
542 * @static
543 * @param array $element The seed of the recursion
544 * @return void
545 */
546 function no_outcomes(&$element) {
547 if ($element['type'] != 'category') {
548 return;
549 }
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]);
554
d4795a07 555 } else if ($element['children'][$sortorder]['type'] == 'category') {
aea4df41 556 grade_tree::no_outcomes($element['children'][$sortorder]);
557 }
558 }
559 }
560
7a6b7acf 561 /**
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
566 */
567 function category_grade_last(&$element) {
568 if (empty($element['children'])) {
569 return;
570 }
571 if (count($element['children']) < 2) {
572 return;
573 }
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]);
580 }
581 }
582
583 /**
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
590 */
591 function fill_levels(&$levels, &$element, $depth) {
592 if (!array_key_exists($depth, $levels)) {
593 $levels[$depth] = array();
594 }
595
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;
601 }
602
603 $levels[$depth][] =& $element;
604 $depth++;
605 if (empty($element['children'])) {
606 return;
607 }
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;
615 }
616 $prev = $sortorder;
617 }
618 }
619
620 /**
621 * Static recursive helper - makes full tree (all leafes are at the same level)
622 */
623 function inject_fillers(&$element, $depth) {
624 $depth++;
625
626 if (empty($element['children'])) {
627 return $depth;
628 }
629 $chdepths = array();
630 $chids = array_keys($element['children']);
631 $last_child = end($chids);
632 $first_child = reset($chids);
633
634 foreach ($chids as $chid) {
635 $chdepths[$chid] = grade_tree::inject_fillers($element['children'][$chid], $depth);
636 }
637 arsort($chdepths);
638
639 $maxdepth = reset($chdepths);
640 foreach ($chdepths as $chid=>$chd) {
641 if ($chd == $maxdepth) {
642 continue;
643 }
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';
651 }
652 $oldchild =& $element['children'][$chid];
653 $element['children'][$chid] = array('object'=>'filler', 'type'=>$type, 'eid'=>'', 'depth'=>$element['object']->depth,'children'=>array($oldchild));
654 }
655 }
656
657 return $maxdepth;
658 }
659
660 /**
661 * Static recursive helper - add colspan information into categories
662 */
663 function inject_colspans(&$element) {
664 if (empty($element['children'])) {
665 return 1;
666 }
667 $count = 0;
668 foreach ($element['children'] as $key=>$child) {
669 $count += grade_tree::inject_colspans($element['children'][$key]);
670 }
671 $element['colspan'] = $count;
672 return $count;
673 }
674
675 /**
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
680 */
681 function locate_element($eid) {
d3c3da1b 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;
686 }
687
688 $itemid = $matches[1];
689 $userid = $matches[2];
690
691 //extra security check - the grade item must be in this tree
692 if (!$item_el = $this->locate_element('i'.$itemid)) {
693 return null;
694 }
695
696 // $gradea->id may be null - means does not exist yet
697 $grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid));
698
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');
701
702 } else if (strpos($eid, 'g') === 0) {
7a6b7acf 703 $id = (int)substr($eid, 1);
704 if (!$grade = grade_grade::fetch(array('id'=>$id))) {
705 return null;
706 }
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;
710 }
711 $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods!
712 return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade');
713 }
714
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;
720 }
721 if ($element['eid'] == $eid) {
722 return $element;
723 }
724 }
725 }
726
727 return null;
728 }
729
d3c3da1b 730 /**
731 * Returns the grade eid - the grade may not exist yet.
732 * @param $grade_grade object
733 * @return string eid
734 */
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;
740 }
741 }
742
7a6b7acf 743 /**
744 * Return edit icon for give element
745 * @param object $element
746 * @return string
747 */
748 function get_edit_icon($element, $gpr) {
749 global $CFG;
750
2cc773f5 751 if (!has_capability('moodle/grade:manage', $this->context)) {
27b1735b 752 if ($element['type'] == 'grade' and has_capability('moodle/grade:override', $this->context)) {
753 // oki - let them override grade
754 } else {
755 return '';
756 }
7a6b7acf 757 }
758
2cc773f5 759 static $stredit = null;
760 if (is_null($stredit)) {
761 $stredit = get_string('edit');
762 }
763
7a6b7acf 764 $object = $element['object'];
2cc773f5 765 $overlib = '';
7a6b7acf 766
767 switch ($element['type']) {
768 case 'item':
769 case 'categoryitem':
770 case 'courseitem':
9988d1b7 771 if (empty($object->outcomeid) || empty($CFG->enableoutcomes)) {
78ad5f3f 772 $url = $CFG->wwwroot.'/grade/edit/tree/item.php?courseid='.$this->courseid.'&amp;id='.$object->id;
f10fac96 773 } else {
78ad5f3f 774 $url = $CFG->wwwroot.'/grade/edit/tree/outcomeitem.php?courseid='.$this->courseid.'&amp;id='.$object->id;
f10fac96 775 }
7a6b7acf 776 $url = $gpr->add_url_params($url);
777 break;
778
779 case 'category':
78ad5f3f 780 $url = $CFG->wwwroot.'/grade/edit/tree/category.php?courseid='.$this->courseid.'&amp;id='.$object->id;
7a6b7acf 781 $url = $gpr->add_url_params($url);
782 break;
783
784 case 'grade':
d3c3da1b 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;
789 }
7a6b7acf 790 $url = $gpr->add_url_params($url);
2cc773f5 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();"';
795 }
7a6b7acf 796 break;
797
798 default:
799 $url = null;
800 }
801
802 if ($url) {
2cc773f5 803 return '<a href="'.$url.'"><img '.$overlib.' src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$stredit.'" title="'.$stredit.'"/></a>';
7a6b7acf 804
805 } else {
806 return '';
807 }
808 }
809
810 /**
811 * Return hiding icon for give element
812 * @param object $element
813 * @return string
814 */
815 function get_hiding_icon($element, $gpr) {
816 global $CFG;
817
2cc773f5 818 if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:hide', $this->context)) {
7a6b7acf 819 return '';
820 }
821
2cc773f5 822 static $strshow = null;
823 static $strhide = null;
824 if (is_null($strshow)) {
7a6b7acf 825 $strshow = get_string('show');
2cc773f5 826 $strhide = get_string('hide');
827 }
828
829 if ($element['object']->is_hidden()) {
d4795a07 830 $icon = 'show';
831 $tooltip = '';
832
f60c61b1 833 if ($element['type'] != 'category' and $element['object']->get_hidden() > 1) { // Change the icon and add a tooltip showing the date
d4795a07 834 $icon = 'hiddenuntil';
c30fafbf 835 $tooltip = get_string('hiddenuntildate', 'grades', userdate($element['object']->get_hidden()));
d4795a07 836 }
837
838 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=show&amp;sesskey='.sesskey()
839 . '&amp;eid='.$element['eid'];
7a6b7acf 840 $url = $gpr->add_url_params($url);
d4795a07 841 $action = '<a href="'.$url.'"><img title="' . $tooltip . '" src="'.$CFG->pixpath.'/t/' . $icon . '.gif" class="iconsmall" alt="'
842 . $strshow.'" title="'.$strshow.'"/></a>';
7a6b7acf 843
844 } else {
d4795a07 845 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=hide&amp;sesskey='.sesskey()
846 . '&amp;eid='.$element['eid'];
7a6b7acf 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>';
849 }
850 return $action;
851 }
852
853 /**
854 * Return locking icon for give element
855 * @param object $element
856 * @return string
857 */
858 function get_locking_icon($element, $gpr) {
859 global $CFG;
860
2cc773f5 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');
866 }
7a6b7acf 867
868 if ($element['object']->is_locked()) {
d4795a07 869 $icon = 'unlock';
870 $tooltip = '';
871
fb0e3570 872 if ($element['type'] != 'category' and $element['object']->get_locktime() > 1) { // Change the icon and add a tooltip showing the date
d4795a07 873 $icon = 'locktime';
c30fafbf 874 $tooltip = get_string('locktimedate', 'grades', userdate($element['object']->get_locktime()));
d4795a07 875 }
876
2cc773f5 877 if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:unlock', $this->context)) {
7a6b7acf 878 return '';
879 }
d4795a07 880 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=unlock&amp;sesskey='.sesskey()
881 . '&amp;eid='.$element['eid'];
7a6b7acf 882 $url = $gpr->add_url_params($url);
d4795a07 883 $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/' . $icon . '.gif" title="' . $tooltip
884 . '" class="iconsmall" alt="'.$strunlock
885 . '" title="'.$strunlock.'"/></a>';
7a6b7acf 886
887 } else {
2cc773f5 888 if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:lock', $this->context)) {
7a6b7acf 889 return '';
890 }
d4795a07 891 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=lock&amp;sesskey='.sesskey()
892 . '&amp;eid='.$element['eid'];
7a6b7acf 893 $url = $gpr->add_url_params($url);
d4795a07 894 $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/lock.gif" class="iconsmall" alt="'.$strlock.'" title="'
895 . $strlock.'"/></a>';
7a6b7acf 896 }
897 return $action;
898 }
899
2cc773f5 900 /**
901 * Return calculation icon for given element
902 * @param object $element
903 * @return string
904 */
905 function get_calculation_icon($element, $gpr) {
906 global $CFG;
907 if (!has_capability('moodle/grade:manage', $this->context)) {
908 return '';
909 }
910
911 $calculation_icon = '';
912
913 $type = $element['type'];
914 $object = $element['object'];
915
916 if ($type == 'item' or $type == 'courseitem' or $type == 'categoryitem') {
917 $streditcalculation = get_string('editcalculation', 'grades');
918
919 // show calculation icon only when calculation possible
9127bc5d 920 if ((!$object->is_normal_item() or $object->is_outcome_item())
921 and ($object->gradetype == GRADE_TYPE_SCALE or $object->gradetype == GRADE_TYPE_VALUE)) {
78ad5f3f 922 $url = $CFG->wwwroot.'/grade/edit/tree/calculation.php?courseid='.$this->courseid.'&amp;id='.$object->id;
2cc773f5 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";
926 }
927 }
928
929 return $calculation_icon;
930 }
7a6b7acf 931}
932
e2008be2 933?>