MDL-11034 Implemented the global enablepublishing and refactored the handling of...
[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 }
3565ff92 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 }
3565ff92 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 }
3565ff92 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
826c5f86 355 // First link is always 'grade'
356 $navlinks = array();
357 $navlinks[] = array('name' => $strgrades,
358 'link' => $CFG->wwwroot.'/grade/index.php?id='.$COURSE->id,
359 'type' => 'misc');
360
361 $link = '';
362 $numberofelements = 3;
363
364 // Prepare URL params string
365 $id_string = '?';
366 if (!is_null($id)) {
367 if (is_array($id)) {
368 foreach ($id as $idkey => $idvalue) {
369 $id_string .= "$idkey=$idvalue&amp;";
370 }
371 } else {
372 $id_string .= "id=$id";
373 }
374 }
375
376 $navlink4 = null;
377
0f78c4de 378 // Remove file extensions from filenames
379 foreach ($path_elements as $key => $filename) {
380 $path_elements[$key] = str_replace('.php', '', $filename);
381 }
382
826c5f86 383 // Second level links
384 switch ($path_elements[1]) {
385 case 'edit': // No link
386 if ($path_elements[3] != 'index.php') {
387 $numberofelements = 4;
388 }
389 break;
390 case 'import': // No link
391 break;
392 case 'export': // No link
393 break;
394 case 'report':
395 // $id is required for this link. Do not print it if $id isn't given
396 if (!is_null($id)) {
397 $link = $CFG->wwwroot . '/grade/report/index.php' . $id_string;
398 }
399
400 if ($path_elements[2] == 'grader') {
401 $numberofelements = 4;
402 }
403 break;
404
405 default:
406 // If this element isn't among the ones already listed above, it isn't supported, throw an error.
407 debugging("grade_build_nav() doesn't support ". $path_elements[1] . " as the second path element after 'grade'.");
408 return false;
409 }
410
411 $navlinks[] = array('name' => get_string($path_elements[1], 'grades'), 'link' => $link, 'type' => 'misc');
412
413 // Third level links
414 if (empty($pagename)) {
415 $pagename = get_string($path_elements[2], 'grades');
416 }
417
418 switch ($numberofelements) {
419 case 3:
420 $navlinks[] = array('name' => $pagename, 'link' => $link, 'type' => 'misc');
421 break;
422 case 4:
423
424 if ($path_elements[2] == 'grader' AND $path_elements[3] != 'index.php') {
3cf6a6d5 425 $navlinks[] = array('name' => get_string('modulename', 'gradereport_grader'),
826c5f86 426 'link' => "$CFG->wwwroot/grade/report/grader/index.php$id_string",
427 'type' => 'misc');
428 }
429 $navlinks[] = array('name' => $pagename, 'link' => '', 'type' => 'misc');
430 break;
431 }
432 $navigation = build_navigation($navlinks);
433
434 return $navigation;
d4795a07 435}
7a6b7acf 436
66ef0471 437/**
438 * Computes then returns the percentage value of the grade value within the given range.
439 * @param float $gradeval
440 * @param float $grademin
441 * @param float $grademx
442 * @return float $percentage
443 */
444function grade_to_percentage($gradeval, $grademin, $grademax) {
445 if ($grademin >= $grademax) {
446 debugging("The minimum grade ($grademin) was higher than or equal to the maximum grade ($grademax)!!! Cannot proceed with computation.");
447 }
33a34cd4 448
449 // If given gradevalue is lower than grademin, adjust it to grademin
450 if ($gradeval < $grademin || empty($gradeval)) {
451 $gradeval = $grademin;
452 }
453
66ef0471 454 $offset_value = $gradeval - $grademin;
455 $offset_max = $grademax - $grademin;
456 $factor = 100 / $offset_max;
457 $percentage = $offset_value * $factor;
458 return $percentage;
459}
460
7a6b7acf 461/**
462 * This class represents a complete tree of categories, grade_items and final grades,
463 * organises as an array primarily, but which can also be converted to other formats.
464 * It has simple method calls with complex implementations, allowing for easy insertion,
465 * deletion and moving of items and categories within the tree.
466 */
467class grade_tree {
468
469 /**
470 * The basic representation of the tree as a hierarchical, 3-tiered array.
471 * @var object $top_element
472 */
473 var $top_element;
474
475 /**
476 * A string of GET URL variables, namely courseid and sesskey, used in most URLs built by this class.
477 * @var string $commonvars
478 */
479 var $commonvars;
480
481 /**
482 * 2D array of grade items and categories
483 */
484 var $levels;
485
2cc773f5 486 /**
487 * Course context
488 */
489 var $context;
490
7a6b7acf 491 /**
492 * Constructor, retrieves and stores a hierarchical array of all grade_category and grade_item
493 * objects for the given courseid. Full objects are instantiated.
494 * and renumbering.
495 * @param int $courseid
496 * @param boolean $fillers include fillers and colspans, make the levels var "rectangular"
497 * @param boolean $category_grade_last category grade item is the last child
4faf5f99 498 * @param array $collapsed array of collapsed categories
7a6b7acf 499 */
aea4df41 500 function grade_tree($courseid, $fillers=true, $category_grade_last=false, $collapsed=null, $nooutcomes=false) {
7a6b7acf 501 global $USER, $CFG;
502
503 $this->courseid = $courseid;
504 $this->commonvars = "&amp;sesskey=$USER->sesskey&amp;id=$this->courseid";
505 $this->levels = array();
2cc773f5 506 $this->context = get_context_instance(CONTEXT_COURSE, $courseid);
7a6b7acf 507
508 // get course grade tree
509 $this->top_element = grade_category::fetch_course_tree($courseid, true);
510
4faf5f99 511 // collapse the categories if requested
512 if (!empty($collapsed)) {
513 grade_tree::category_collapse($this->top_element, $collapsed);
514 }
515
aea4df41 516 // no otucomes if requested
517 if (!empty($nooutcomes)) {
518 grade_tree::no_outcomes($this->top_element);
519 }
520
4faf5f99 521 // move category item to last position in category
7a6b7acf 522 if ($category_grade_last) {
523 grade_tree::category_grade_last($this->top_element);
524 }
525
526 if ($fillers) {
527 // inject fake categories == fillers
528 grade_tree::inject_fillers($this->top_element, 0);
529 // add colspans to categories and fillers
530 grade_tree::inject_colspans($this->top_element);
531 }
532
533 grade_tree::fill_levels($this->levels, $this->top_element, 0);
534 }
535
4faf5f99 536 /**
537 * Static recursive helper - removes items from collapsed categories
538 * @static
539 * @param array $element The seed of the recursion
540 * @param array $collapsed array of collapsed categories
541 * @return void
542 */
543 function category_collapse(&$element, $collapsed) {
544 if ($element['type'] != 'category') {
545 return;
546 }
547 if (empty($element['children']) or count($element['children']) < 2) {
548 return;
549 }
550
384960dd 551 if (in_array($element['object']->id, $collapsed['aggregatesonly'])) {
4faf5f99 552 $category_item = reset($element['children']); //keep only category item
553 $element['children'] = array(key($element['children'])=>$category_item);
554
555 } else {
384960dd 556 if (in_array($element['object']->id, $collapsed['gradesonly'])) { // Remove category item
557 reset($element['children']);
558 $first_key = key($element['children']);
559 unset($element['children'][$first_key]);
560 }
561 foreach ($element['children'] as $sortorder=>$child) { // Recurse through the element's children
4faf5f99 562 grade_tree::category_collapse($element['children'][$sortorder], $collapsed);
563 }
564 }
565 }
7a6b7acf 566
aea4df41 567 /**
568 * Static recursive helper - removes all outcomes
569 * @static
570 * @param array $element The seed of the recursion
571 * @return void
572 */
573 function no_outcomes(&$element) {
574 if ($element['type'] != 'category') {
575 return;
576 }
577 foreach ($element['children'] as $sortorder=>$child) {
578 if ($element['children'][$sortorder]['type'] == 'item'
579 and $element['children'][$sortorder]['object']->is_outcome_item()) {
580 unset($element['children'][$sortorder]);
581
d4795a07 582 } else if ($element['children'][$sortorder]['type'] == 'category') {
aea4df41 583 grade_tree::no_outcomes($element['children'][$sortorder]);
584 }
585 }
586 }
587
7a6b7acf 588 /**
589 * Static recursive helper - makes the grade_item for category the last children
590 * @static
591 * @param array $element The seed of the recursion
592 * @return void
593 */
594 function category_grade_last(&$element) {
595 if (empty($element['children'])) {
596 return;
597 }
598 if (count($element['children']) < 2) {
599 return;
600 }
601 $category_item = reset($element['children']);
602 $order = key($element['children']);
603 unset($element['children'][$order]);
604 $element['children'][$order] =& $category_item;
605 foreach ($element['children'] as $sortorder=>$child) {
606 grade_tree::category_grade_last($element['children'][$sortorder]);
607 }
608 }
609
610 /**
611 * Static recursive helper - fills the levels array, useful when accessing tree elements of one level
612 * @static
613 * @param int $levels
614 * @param array $element The seed of the recursion
615 * @param int $depth
616 * @return void
617 */
618 function fill_levels(&$levels, &$element, $depth) {
619 if (!array_key_exists($depth, $levels)) {
620 $levels[$depth] = array();
621 }
622
623 // prepare unique identifier
624 if ($element['type'] == 'category') {
625 $element['eid'] = 'c'.$element['object']->id;
626 } else if (in_array($element['type'], array('item', 'courseitem', 'categoryitem'))) {
627 $element['eid'] = 'i'.$element['object']->id;
628 }
629
630 $levels[$depth][] =& $element;
631 $depth++;
632 if (empty($element['children'])) {
633 return;
634 }
635 $prev = 0;
636 foreach ($element['children'] as $sortorder=>$child) {
637 grade_tree::fill_levels($levels, $element['children'][$sortorder], $depth);
638 $element['children'][$sortorder]['prev'] = $prev;
639 $element['children'][$sortorder]['next'] = 0;
640 if ($prev) {
641 $element['children'][$prev]['next'] = $sortorder;
642 }
643 $prev = $sortorder;
644 }
645 }
646
647 /**
648 * Static recursive helper - makes full tree (all leafes are at the same level)
649 */
650 function inject_fillers(&$element, $depth) {
651 $depth++;
652
653 if (empty($element['children'])) {
654 return $depth;
655 }
656 $chdepths = array();
657 $chids = array_keys($element['children']);
658 $last_child = end($chids);
659 $first_child = reset($chids);
660
661 foreach ($chids as $chid) {
662 $chdepths[$chid] = grade_tree::inject_fillers($element['children'][$chid], $depth);
663 }
664 arsort($chdepths);
665
666 $maxdepth = reset($chdepths);
667 foreach ($chdepths as $chid=>$chd) {
668 if ($chd == $maxdepth) {
669 continue;
670 }
671 for ($i=0; $i < $maxdepth-$chd; $i++) {
672 if ($chid == $first_child) {
673 $type = 'fillerfirst';
674 } else if ($chid == $last_child) {
675 $type = 'fillerlast';
676 } else {
677 $type = 'filler';
678 }
679 $oldchild =& $element['children'][$chid];
680 $element['children'][$chid] = array('object'=>'filler', 'type'=>$type, 'eid'=>'', 'depth'=>$element['object']->depth,'children'=>array($oldchild));
681 }
682 }
683
684 return $maxdepth;
685 }
686
687 /**
688 * Static recursive helper - add colspan information into categories
689 */
690 function inject_colspans(&$element) {
691 if (empty($element['children'])) {
692 return 1;
693 }
694 $count = 0;
695 foreach ($element['children'] as $key=>$child) {
696 $count += grade_tree::inject_colspans($element['children'][$key]);
697 }
698 $element['colspan'] = $count;
699 return $count;
700 }
701
702 /**
703 * Parses the array in search of a given eid and returns a element object with
704 * information about the element it has found.
705 * @param int $eid
706 * @return object element
707 */
708 function locate_element($eid) {
d3c3da1b 709 // it is a grade - construct a new object
710 if (strpos($eid, 'n') === 0) {
711 if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) {
712 return null;
713 }
714
715 $itemid = $matches[1];
716 $userid = $matches[2];
717
718 //extra security check - the grade item must be in this tree
719 if (!$item_el = $this->locate_element('i'.$itemid)) {
720 return null;
721 }
722
723 // $gradea->id may be null - means does not exist yet
724 $grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid));
725
726 $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods!
727 return array('eid'=>'n'.$itemid.'u'.$userid,'object'=>$grade, 'type'=>'grade');
728
729 } else if (strpos($eid, 'g') === 0) {
7a6b7acf 730 $id = (int)substr($eid, 1);
731 if (!$grade = grade_grade::fetch(array('id'=>$id))) {
732 return null;
733 }
734 //extra security check - the grade item must be in this tree
735 if (!$item_el = $this->locate_element('i'.$grade->itemid)) {
736 return null;
737 }
738 $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods!
739 return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade');
740 }
741
742 // it is a category or item
743 foreach ($this->levels as $row) {
744 foreach ($row as $element) {
745 if ($element['type'] == 'filler') {
746 continue;
747 }
748 if ($element['eid'] == $eid) {
749 return $element;
750 }
751 }
752 }
753
754 return null;
755 }
756
d3c3da1b 757 /**
758 * Returns the grade eid - the grade may not exist yet.
759 * @param $grade_grade object
760 * @return string eid
761 */
762 function get_grade_eid($grade_grade) {
763 if (empty($grade_grade->id)) {
764 return 'n'.$grade_grade->itemid.'u'.$grade_grade->userid;
765 } else {
766 return 'g'.$grade_grade->id;
767 }
768 }
769
7a6b7acf 770 /**
771 * Return edit icon for give element
772 * @param object $element
773 * @return string
774 */
775 function get_edit_icon($element, $gpr) {
776 global $CFG;
777
2cc773f5 778 if (!has_capability('moodle/grade:manage', $this->context)) {
27b1735b 779 if ($element['type'] == 'grade' and has_capability('moodle/grade:override', $this->context)) {
780 // oki - let them override grade
781 } else {
782 return '';
783 }
7a6b7acf 784 }
785
2cc773f5 786 static $stredit = null;
787 if (is_null($stredit)) {
788 $stredit = get_string('edit');
789 }
790
7a6b7acf 791 $object = $element['object'];
2cc773f5 792 $overlib = '';
7a6b7acf 793
794 switch ($element['type']) {
795 case 'item':
796 case 'categoryitem':
797 case 'courseitem':
9988d1b7 798 if (empty($object->outcomeid) || empty($CFG->enableoutcomes)) {
78ad5f3f 799 $url = $CFG->wwwroot.'/grade/edit/tree/item.php?courseid='.$this->courseid.'&amp;id='.$object->id;
f10fac96 800 } else {
78ad5f3f 801 $url = $CFG->wwwroot.'/grade/edit/tree/outcomeitem.php?courseid='.$this->courseid.'&amp;id='.$object->id;
f10fac96 802 }
7a6b7acf 803 $url = $gpr->add_url_params($url);
804 break;
805
806 case 'category':
78ad5f3f 807 $url = $CFG->wwwroot.'/grade/edit/tree/category.php?courseid='.$this->courseid.'&amp;id='.$object->id;
7a6b7acf 808 $url = $gpr->add_url_params($url);
809 break;
810
811 case 'grade':
d3c3da1b 812 if (empty($object->id)) {
813 $url = $CFG->wwwroot.'/grade/edit/tree/grade.php?courseid='.$this->courseid.'&amp;itemid='.$object->itemid.'&amp;userid='.$object->userid;
814 } else {
815 $url = $CFG->wwwroot.'/grade/edit/tree/grade.php?courseid='.$this->courseid.'&amp;id='.$object->id;
816 }
7a6b7acf 817 $url = $gpr->add_url_params($url);
2cc773f5 818 if (!empty($object->feedback)) {
819 $feedback = format_text($object->feedback, $object->feedbackformat);
820 $function = "return overlib('".s(ltrim($object->feedback)."', FULLHTML);");
821 $overlib = 'onmouseover="'.$function.'" onmouseout="return nd();"';
822 }
7a6b7acf 823 break;
824
825 default:
826 $url = null;
827 }
828
829 if ($url) {
2cc773f5 830 return '<a href="'.$url.'"><img '.$overlib.' src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$stredit.'" title="'.$stredit.'"/></a>';
7a6b7acf 831
832 } else {
833 return '';
834 }
835 }
836
837 /**
838 * Return hiding icon for give element
839 * @param object $element
840 * @return string
841 */
842 function get_hiding_icon($element, $gpr) {
843 global $CFG;
844
2cc773f5 845 if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:hide', $this->context)) {
7a6b7acf 846 return '';
847 }
848
2cc773f5 849 static $strshow = null;
850 static $strhide = null;
851 if (is_null($strshow)) {
7a6b7acf 852 $strshow = get_string('show');
2cc773f5 853 $strhide = get_string('hide');
854 }
855
856 if ($element['object']->is_hidden()) {
d4795a07 857 $icon = 'show';
858 $tooltip = '';
859
f60c61b1 860 if ($element['type'] != 'category' and $element['object']->get_hidden() > 1) { // Change the icon and add a tooltip showing the date
d4795a07 861 $icon = 'hiddenuntil';
c30fafbf 862 $tooltip = get_string('hiddenuntildate', 'grades', userdate($element['object']->get_hidden()));
d4795a07 863 }
864
865 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=show&amp;sesskey='.sesskey()
866 . '&amp;eid='.$element['eid'];
7a6b7acf 867 $url = $gpr->add_url_params($url);
d4795a07 868 $action = '<a href="'.$url.'"><img title="' . $tooltip . '" src="'.$CFG->pixpath.'/t/' . $icon . '.gif" class="iconsmall" alt="'
869 . $strshow.'" title="'.$strshow.'"/></a>';
7a6b7acf 870
871 } else {
d4795a07 872 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=hide&amp;sesskey='.sesskey()
873 . '&amp;eid='.$element['eid'];
7a6b7acf 874 $url = $gpr->add_url_params($url);
875 $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$strhide.'" title="'.$strhide.'"/></a>';
876 }
877 return $action;
878 }
879
880 /**
881 * Return locking icon for give element
882 * @param object $element
883 * @return string
884 */
885 function get_locking_icon($element, $gpr) {
886 global $CFG;
887
2cc773f5 888 static $strunlock = null;
889 static $strlock = null;
890 if (is_null($strunlock)) {
891 $strunlock = get_string('unlock', 'grades');
892 $strlock = get_string('lock', 'grades');
893 }
7a6b7acf 894
895 if ($element['object']->is_locked()) {
d4795a07 896 $icon = 'unlock';
897 $tooltip = '';
898
fb0e3570 899 if ($element['type'] != 'category' and $element['object']->get_locktime() > 1) { // Change the icon and add a tooltip showing the date
d4795a07 900 $icon = 'locktime';
c30fafbf 901 $tooltip = get_string('locktimedate', 'grades', userdate($element['object']->get_locktime()));
d4795a07 902 }
903
2cc773f5 904 if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:unlock', $this->context)) {
7a6b7acf 905 return '';
906 }
d4795a07 907 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=unlock&amp;sesskey='.sesskey()
908 . '&amp;eid='.$element['eid'];
7a6b7acf 909 $url = $gpr->add_url_params($url);
d4795a07 910 $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/' . $icon . '.gif" title="' . $tooltip
911 . '" class="iconsmall" alt="'.$strunlock
912 . '" title="'.$strunlock.'"/></a>';
7a6b7acf 913
914 } else {
2cc773f5 915 if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:lock', $this->context)) {
7a6b7acf 916 return '';
917 }
d4795a07 918 $url = $CFG->wwwroot.'/grade/edit/tree/action.php?id='.$this->courseid.'&amp;action=lock&amp;sesskey='.sesskey()
919 . '&amp;eid='.$element['eid'];
7a6b7acf 920 $url = $gpr->add_url_params($url);
d4795a07 921 $action = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/lock.gif" class="iconsmall" alt="'.$strlock.'" title="'
922 . $strlock.'"/></a>';
7a6b7acf 923 }
924 return $action;
925 }
926
2cc773f5 927 /**
928 * Return calculation icon for given element
929 * @param object $element
930 * @return string
931 */
932 function get_calculation_icon($element, $gpr) {
933 global $CFG;
934 if (!has_capability('moodle/grade:manage', $this->context)) {
935 return '';
936 }
937
938 $calculation_icon = '';
939
940 $type = $element['type'];
941 $object = $element['object'];
942
943 if ($type == 'item' or $type == 'courseitem' or $type == 'categoryitem') {
944 $streditcalculation = get_string('editcalculation', 'grades');
945
946 // show calculation icon only when calculation possible
9127bc5d 947 if ((!$object->is_normal_item() or $object->is_outcome_item())
948 and ($object->gradetype == GRADE_TYPE_SCALE or $object->gradetype == GRADE_TYPE_VALUE)) {
78ad5f3f 949 $url = $CFG->wwwroot.'/grade/edit/tree/calculation.php?courseid='.$this->courseid.'&amp;id='.$object->id;
2cc773f5 950 $url = $gpr->add_url_params($url);
951 $calculation_icon = '<a href="'. $url.'"><img src="'.$CFG->pixpath.'/t/calc.gif" class="iconsmall" alt="'
952 . $streditcalculation.'" title="'.$streditcalculation.'" /></a>'. "\n";
953 }
954 }
955
956 return $calculation_icon;
957 }
7a6b7acf 958}
959
e2008be2 960?>