91aaad488b1f3ea872268283fd0fcbabd88e74ce
[moodle.git] / grade / edit / tree / lib.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * A library of classes used by the grade edit pages
19  *
20  * @package   core_grades
21  * @copyright 2009 Nicolas Connault
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 class grade_edit_tree {
26     public $columns = array();
28     /**
29      * @var grade_tree $gtree   @see grade/lib.php
30      */
31     public $gtree;
33     /**
34      * @var grade_plugin_return @see grade/lib.php
35      */
36     public $gpr;
38     /**
39      * @var string              $moving The eid of the category or item being moved
40      */
41     public $moving;
43     public $deepest_level;
45     public $uses_weight = false;
47     /** @var bool indicates if tree has categories with aggregation method other than Natural. */
48     protected $uses_non_natural = false;
50     public $table;
52     public $categories = array();
54     /**
55      * Show calculator icons next to manual grade items
56      * @var bool $show_calculations
57      */
58     private $show_calculations;
60     /**
61      * Constructor
62      */
63     public function __construct($gtree, $moving=false, $gpr) {
64         global $USER, $OUTPUT, $COURSE;
66         $systemdefault = get_config('moodle', 'grade_report_showcalculations');
67         $this->show_calculations = get_user_preferences('grade_report_showcalculations', $systemdefault);
69         $this->gtree = $gtree;
70         $this->moving = $moving;
71         $this->gpr = $gpr;
72         $this->deepest_level = $this->get_deepest_level($this->gtree->top_element);
74         $this->columns = array(grade_edit_tree_column::factory('name', array('deepest_level' => $this->deepest_level)));
76         if ($this->uses_non_natural) {
77             $this->columns[] = grade_edit_tree_column::factory('aggregation', array('flag' => true));
78         }
80         if ($this->uses_weight) {
81             $this->columns[] = grade_edit_tree_column::factory('weight', array('adv' => 'weight'));
82         }
84         $this->columns[] = grade_edit_tree_column::factory('range'); // This is not a setting... How do we deal with it?
85         $this->columns[] = grade_edit_tree_column::factory('actions');
87         if ($this->deepest_level > 1) {
88             $this->columns[] = grade_edit_tree_column::factory('select');
89         }
91         $this->table = new html_table();
92         $this->table->id = "grade_edit_tree_table";
93         $this->table->attributes['class'] = 'generaltable simple';
95         foreach ($this->columns as $column) {
96             if (!($this->moving && $column->hide_when_moving)) {
97                 $this->table->head[] = $column->get_header_cell();
98             }
99         }
101         $rowcount = 0;
102         $this->table->data = $this->build_html_tree($this->gtree->top_element, true, array(), 0, $rowcount);
103     }
105     /**
106      * Recursive function for building the table holding the grade categories and items,
107      * with CSS indentation and styles.
108      *
109      * @param array   $element The current tree element being rendered
110      * @param boolean $totals Whether or not to print category grade items (category totals)
111      * @param array   $parents An array of parent categories for the current element (used for indentation and row classes)
112      *
113      * @return string HTML
114      */
115     public function build_html_tree($element, $totals, $parents, $level, &$row_count) {
116         global $CFG, $COURSE, $USER, $OUTPUT;
118         $object = $element['object'];
119         $eid    = $element['eid'];
120         $object->name = $this->gtree->get_element_header($element, true, true, false);
121         $object->stripped_name = $this->gtree->get_element_header($element, false, false, false);
123         $is_category_item = false;
124         if ($element['type'] == 'categoryitem' || $element['type'] == 'courseitem') {
125             $is_category_item = true;
126         }
128         $rowclasses = array();
129         foreach ($parents as $parent_eid) {
130             $rowclasses[] = $parent_eid;
131         }
133         $actions = '';
134         $moveaction = '';
136         if (!$is_category_item) {
137             $actions .= $this->gtree->get_edit_icon($element, $this->gpr);
138         }
140         if ($this->show_calculations) {
141             $actions .= $this->gtree->get_calculation_icon($element, $this->gpr);
142         }
144         if ($element['type'] == 'item' or ($element['type'] == 'category' and $element['depth'] > 1)) {
145             if ($this->element_deletable($element)) {
146                 $aurl = new moodle_url('index.php', array('id' => $COURSE->id, 'action' => 'delete', 'eid' => $eid, 'sesskey' => sesskey()));
147                 $actions .= $OUTPUT->action_icon($aurl, new pix_icon('t/delete', get_string('delete')));
148             }
150             $aurl = new moodle_url('index.php', array('id' => $COURSE->id, 'action' => 'moveselect', 'eid' => $eid, 'sesskey' => sesskey()));
151             $moveaction .= $OUTPUT->action_icon($aurl, new pix_icon('t/move', get_string('move')));
152         }
154         $actions .= $this->gtree->get_hiding_icon($element, $this->gpr);
156         $actions .= $this->gtree->get_reset_icon($element, $this->gpr);
158         $returnrows = array();
159         $root = false;
161         $id = required_param('id', PARAM_INT);
163         /// prepare move target if needed
164         $last = '';
166         /// print the list items now
167         if ($this->moving == $eid) {
168             // do not diplay children
169             $cell = new html_table_cell();
170             $cell->colspan = 12;
171             $cell->attributes['class'] = $element['type'] . ' moving';
172             $cell->text = $object->name.' ('.get_string('move').')';
173             return array(new html_table_row(array($cell)));
174         }
176         if ($element['type'] == 'category') {
177             $level++;
178             $this->categories[$object->id] = $object->stripped_name;
179             $category = grade_category::fetch(array('id' => $object->id));
180             $item = $category->get_grade_item();
182             // Add aggregation coef input if not a course item and if parent category has correct aggregation type
183             $dimmed = ($item->is_hidden()) ? 'dimmed' : '';
185             // Before we print the category's row, we must find out how many rows will appear below it (for the filler cell's rowspan)
186             $aggregation_position = grade_get_setting($COURSE->id, 'aggregationposition', $CFG->grade_aggregationposition);
187             $category_total_data = null; // Used if aggregationposition is set to "last", so we can print it last
189             $html_children = array();
191             $row_count = 0;
193             foreach($element['children'] as $child_el) {
194                 $moveto = null;
196                 if (empty($child_el['object']->itemtype)) {
197                     $child_el['object']->itemtype = false;
198                 }
200                 if (($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category') && !$totals) {
201                     continue;
202                 }
204                 $child_eid = $child_el['eid'];
205                 $first = '';
207                 if ($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category') {
208                     $first = array('first' => 1);
209                     $child_eid = $eid;
210                 }
212                 if ($this->moving && $this->moving != $child_eid) {
214                     $strmove     = get_string('move');
215                     $strmovehere = get_string('movehere');
216                     $actions = $moveaction = ''; // no action icons when moving
218                     $aurl = new moodle_url('index.php', array('id' => $COURSE->id, 'action' => 'move', 'eid' => $this->moving, 'moveafter' => $child_eid, 'sesskey' => sesskey()));
219                     if ($first) {
220                         $aurl->params($first);
221                     }
223                     $cell = new html_table_cell();
224                     $cell->colspan = 12;
226                     $icon = new pix_icon('movehere', $strmovehere, null, array('class'=>'movetarget'));
227                     $cell->text = $OUTPUT->action_icon($aurl, $icon);
229                     $moveto = new html_table_row(array($cell));
230                 }
232                 $newparents = $parents;
233                 $newparents[] = $eid;
235                 $row_count++;
236                 $child_row_count = 0;
238                 // If moving, do not print course and category totals, but still print the moveto target box
239                 if ($this->moving && ($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category')) {
240                     $html_children[] = $moveto;
241                 } elseif ($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category') {
242                     // We don't build the item yet because we first need to know the deepest level of categories (for category/name colspans)
243                     $category_total_item = $this->build_html_tree($child_el, $totals, $newparents, $level, $child_row_count);
244                     if (!$aggregation_position) {
245                         $html_children = array_merge($html_children, $category_total_item);
246                     }
247                 } else {
248                     $html_children = array_merge($html_children, $this->build_html_tree($child_el, $totals, $newparents, $level, $child_row_count));
249                     if (!empty($moveto)) {
250                         $html_children[] = $moveto;
251                     }
253                     if ($this->moving) {
254                         $row_count++;
255                     }
256                 }
258                 $row_count += $child_row_count;
260                 // If the child is a category, increment row_count by one more (for the extra coloured row)
261                 if ($child_el['type'] == 'category') {
262                     $row_count++;
263                 }
264             }
266             // Print category total at the end if aggregation position is "last" (1)
267             if (!empty($category_total_item) && $aggregation_position) {
268                 $html_children = array_merge($html_children, $category_total_item);
269             }
271             // Determine if we are at the root
272             if (isset($element['object']->grade_item) && $element['object']->grade_item->is_course_item()) {
273                 $root = true;
274             }
276             $levelclass = "level$level";
278             $courseclass = '';
279             if ($level == 1) {
280                 $courseclass = 'coursecategory';
281             }
283             $row = new html_table_row();
284             $row->attributes['class'] = $courseclass . ' category ' . $dimmed;
285             foreach ($rowclasses as $class) {
286                 $row->attributes['class'] .= ' ' . $class;
287             }
289             $headercell = new html_table_cell();
290             $headercell->header = true;
291             $headercell->scope = 'row';
292             $headercell->attributes['title'] = $object->stripped_name;
293             $headercell->attributes['class'] = 'cell rowspan ' . $levelclass;
294             $headercell->rowspan = $row_count + 1;
295             $row->cells[] = $headercell;
297             foreach ($this->columns as $column) {
298                 if (!($this->moving && $column->hide_when_moving)) {
299                     $row->cells[] = $column->get_category_cell($category, $levelclass, array('id' => $id,
300                         'name' => $object->name, 'level' => $level, 'actions' => $actions,
301                         'moveaction' => $moveaction, 'eid' => $eid));
302                 }
303             }
305             $returnrows[] = $row;
307             $returnrows = array_merge($returnrows, $html_children);
309             // Print a coloured row to show the end of the category across the table
310             $endcell = new html_table_cell();
311             $endcell->colspan = (19 - $level);
312             $endcell->attributes['class'] = 'colspan ' . $levelclass;
314             $returnrows[] = new html_table_row(array($endcell));
316         } else { // Dealing with a grade item
318             $item = grade_item::fetch(array('id' => $object->id));
319             $element['type'] = 'item';
320             $element['object'] = $item;
322             $categoryitemclass = '';
323             if ($item->itemtype == 'category') {
324                 $categoryitemclass = 'categoryitem';
325             }
327             $dimmed = ($item->is_hidden()) ? "dimmed_text" : "";
328             $gradeitemrow = new html_table_row();
329             $gradeitemrow->attributes['class'] = $categoryitemclass . ' item ' . $dimmed;
330             foreach ($rowclasses as $class) {
331                 $gradeitemrow->attributes['class'] .= ' ' . $class;
332             }
334             foreach ($this->columns as $column) {
335                 if (!($this->moving && $column->hide_when_moving)) {
336                     $gradeitemrow->cells[] = $column->get_item_cell($item, array('id' => $id, 'name' => $object->name,
337                         'level' => $level, 'actions' => $actions, 'element' => $element, 'eid' => $eid,
338                         'moveaction' => $moveaction, 'itemtype' => $object->itemtype));
339                 }
340             }
342             $returnrows[] = $gradeitemrow;
343         }
345         return $returnrows;
347     }
349     /**
350      * Given a grade_item object, returns a labelled input if an aggregation coefficient (weight or extra credit) applies to it.
351      * @param grade_item $item
352      * @return string HTML
353      */
354     static function get_weight_input($item) {
355         global $OUTPUT;
357         if (!is_object($item) || get_class($item) !== 'grade_item') {
358             throw new Exception('grade_edit_tree::get_weight_input($item) was given a variable that is not of the required type (grade_item object)');
359             return false;
360         }
362         if ($item->is_course_item()) {
363             return '';
364         }
366         $parent_category = $item->get_parent_category();
367         $parent_category->apply_forced_settings();
368         $aggcoef = $item->get_coefstring();
370         $itemname = $item->itemname;
371         if ($item->is_category_item()) {
372             // Remember, the parent category of a category item is the category itself.
373             $itemname = $parent_category->get_name();
374         }
375         $str = '';
377         if ($aggcoef == 'aggregationcoefweight' || $aggcoef == 'aggregationcoef' || $aggcoef == 'aggregationcoefextraweight') {
378             return '<label class="accesshide" for="weight_'.$item->id.'">'.
379                 get_string('extracreditvalue', 'grades', $itemname).'</label>'.
380                 '<input type="text" size="6" id="weight_'.$item->id.'" name="weight_'.$item->id.'"
381                 value="'.grade_edit_tree::format_number($item->aggregationcoef).'" />';
382         } else if ($aggcoef == 'aggregationcoefextraweightsum') {
384             $checkboxname = 'weightoverride_' . $item->id;
385             $checkboxlbl = html_writer::tag('label', get_string('overrideweightofa', 'grades', $itemname),
386                 array('for' => $checkboxname, 'class' => 'accesshide'));
387             $checkbox = html_writer::empty_tag('input', array('name' => $checkboxname,
388                 'type' => 'hidden', 'value' => 0));
389             $checkbox .= html_writer::empty_tag('input', array('name' => $checkboxname,
390                 'type' => 'checkbox', 'value' => 1, 'id' => $checkboxname, 'class' => 'weightoverride',
391                 'checked' => ($item->weightoverride ? 'checked' : null)));
393             $name = 'weight_' . $item->id;
394             $hiddenlabel = html_writer::tag(
395                 'label',
396                 get_string('weightofa', 'grades', $itemname),
397                 array(
398                     'class' => 'accesshide',
399                     'for' => $name
400                 )
401             );
403             $input = html_writer::empty_tag(
404                 'input',
405                 array(
406                     'type' =>   'text',
407                     'size' =>   6,
408                     'id' =>     $name,
409                     'name' =>   $name,
410                     'value' =>  grade_edit_tree::format_number($item->aggregationcoef2 * 100.0),
411                     'disabled' => ($item->weightoverride ? null : 'disabled')
412                 )
413             );
415             $str .= $checkboxlbl . $checkbox . $hiddenlabel . $input;
417             if ($item->aggregationcoef > 0) {
418                 $str .= ' ' . html_writer::tag('abbr', get_string('aggregationcoefextrasumabbr', 'grades'),
419                         array('title' => get_string('aggregationcoefextrasum', 'grades')));
420             }
421         }
423         return $str;
424     }
426     //Trims trailing zeros
427     //Used on the 'categories and items' page for grade items settings like aggregation co-efficient
428     //Grader report has its own decimal place settings so they are handled elsewhere
429     static function format_number($number) {
430         $formatted = rtrim(format_float($number, 4),'0');
431         if (substr($formatted, -1)==get_string('decsep', 'langconfig')) { //if last char is the decimal point
432             $formatted .= '0';
433         }
434         return $formatted;
435     }
437     /**
438      * Given an element of the grade tree, returns whether it is deletable or not (only manual grade items are deletable)
439      *
440      * @param array $element
441      * @return bool
442      */
443     function element_deletable($element) {
444         global $COURSE;
446         if ($element['type'] != 'item') {
447             return true;
448         }
450         $grade_item = $element['object'];
452         if ($grade_item->itemtype != 'mod' or $grade_item->is_outcome_item() or $grade_item->gradetype == GRADE_TYPE_NONE) {
453             return true;
454         }
456         $modinfo = get_fast_modinfo($COURSE);
457         if (!isset($modinfo->instances[$grade_item->itemmodule][$grade_item->iteminstance])) {
458             // module does not exist
459             return true;
460         }
462         return false;
463     }
465     /**
466      * Given the grade tree and an array of element ids (e.g. c15, i42), and expecting the 'moveafter' URL param,
467      * moves the selected items to the requested location. Then redirects the user to the given $returnurl
468      *
469      * @param object $gtree The grade tree (a recursive representation of the grade categories and grade items)
470      * @param array $eids
471      * @param string $returnurl
472      */
473     function move_elements($eids, $returnurl) {
474         $moveafter = required_param('moveafter', PARAM_INT);
476         if (!is_array($eids)) {
477             $eids = array($eids);
478         }
480         if(!$after_el = $this->gtree->locate_element("cg$moveafter")) {
481             print_error('invalidelementid', '', $returnurl);
482         }
484         $after = $after_el['object'];
485         $parent = $after;
486         $sortorder = $after->get_sortorder();
488         foreach ($eids as $eid) {
489             if (!$element = $this->gtree->locate_element($eid)) {
490                 print_error('invalidelementid', '', $returnurl);
491             }
492             $object = $element['object'];
494             $object->set_parent($parent->id);
495             $object->move_after_sortorder($sortorder);
496             $sortorder++;
497         }
499         redirect($returnurl, '', 0);
500     }
502     /**
503      * Recurses through the entire grade tree to find and return the maximum depth of the tree.
504      * This should be run only once from the root element (course category), and is used for the
505      * indentation of the Name column's cells (colspan)
506      *
507      * @param array $element An array of values representing a grade tree's element (all grade items in this case)
508      * @param int $level The level of the current recursion
509      * @param int $deepest_level A value passed to each subsequent level of recursion and incremented if $level > $deepest_level
510      * @return int Deepest level
511      */
512     function get_deepest_level($element, $level=0, $deepest_level=1) {
513         $object = $element['object'];
515         $level++;
516         $coefstring = $element['object']->get_coefstring();
517         if ($element['type'] == 'category') {
518             if ($element['object']->aggregation != GRADE_AGGREGATE_SUM) {
519                 $this->uses_non_natural = true;
520             }
522             if ($coefstring == 'aggregationcoefweight' || $coefstring == 'aggregationcoefextraweightsum' ||
523                     $coefstring == 'aggregationcoefextraweight') {
524                 $this->uses_weight = true;
525             }
527             foreach($element['children'] as $child_el) {
528                 if ($level > $deepest_level) {
529                     $deepest_level = $level;
530                 }
531                 $deepest_level = $this->get_deepest_level($child_el, $level, $deepest_level);
532             }
533         }
535         return $deepest_level;
536     }
539 /**
540  * Class grade_edit_tree_column
541  *
542  * @package   core_grades
543  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
544  */
545 abstract class grade_edit_tree_column {
546     public $forced;
547     public $hidden;
548     public $forced_hidden;
549     public $advanced_hidden;
550     public $hide_when_moving = true;
551     /**
552      * html_table_cell object used as a template for header cells in all categories.
553      * It must be cloned before being used.
554      * @var html_table_cell $headercell
555      */
556     public $headercell;
557     /**
558      * html_table_cell object used as a template for category cells in all categories.
559      * It must be cloned before being used.
560      * @var html_table_cell $categorycell
561      */
562     public $categorycell;
563     /**
564      * html_table_cell object used as a template for item cells in all categories.
565      * It must be cloned before being used.
566      * @var html_table_cell $itemcell
567      */
568     public $itemcell;
570     public static function factory($name, $params=array()) {
571         $class_name = "grade_edit_tree_column_$name";
572         if (class_exists($class_name)) {
573             return new $class_name($params);
574         }
575     }
577     public abstract function get_header_cell();
579     public abstract function get_category_cell($category, $levelclass, $params);
581     public abstract function get_item_cell($item, $params);
583     public function __construct() {
584         $this->headercell = new html_table_cell();
585         $this->headercell->header = true;
586         $this->headercell->style = 'whitespace: normal;';
587         $this->headercell->attributes['class'] = 'header';
589         $this->categorycell = new html_table_cell();
590         $this->categorycell->attributes['class']  = 'cell';
592         $this->itemcell = new html_table_cell();
593         $this->itemcell->attributes['class'] = 'cell';
594     }
597 /**
598  * Class grade_edit_tree_column_name
599  *
600  * @package   core_grades
601  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
602  */
603 class grade_edit_tree_column_name extends grade_edit_tree_column {
604     public $forced = false;
605     public $hidden = false;
606     public $forced_hidden = false;
607     public $advanced_hidden = false;
608     public $deepest_level = 1;
609     public $hide_when_moving = false;
611     public function __construct($params) {
612         if (empty($params['deepest_level'])) {
613             throw new Exception('Tried to instantiate a grade_edit_tree_column_name object without the "deepest_level" param!');
614         }
616         $this->deepest_level = $params['deepest_level'];
617         parent::__construct();
618     }
620     public function get_header_cell() {
621         $headercell = clone($this->headercell);
622         $headercell->attributes['class'] .= ' name';
623         $headercell->colspan = $this->deepest_level + 1;
624         $headercell->text = get_string('name');
625         return $headercell;
626     }
628     public function get_category_cell($category, $levelclass, $params) {
629         global $OUTPUT;
630         if (empty($params['name']) || empty($params['level'])) {
631             throw new Exception('Array key (name or level) missing from 3rd param of grade_edit_tree_column_name::get_category_cell($category, $levelclass, $params)');
632         }
633         $moveaction = isset($params['moveaction']) ? $params['moveaction'] : '';
634         $categorycell = clone($this->categorycell);
635         $categorycell->attributes['class'] .= ' name ' . $levelclass;
636         $categorycell->colspan = ($this->deepest_level +1) - $params['level'];
637         $categorycell->text = $OUTPUT->heading($moveaction . $params['name'], 4);
638         return $categorycell;
639     }
641     public function get_item_cell($item, $params) {
642         global $CFG;
644         if (empty($params['element']) || empty($params['name']) || empty($params['level'])) {
645             throw new Exception('Array key (name, level or element) missing from 2nd param of grade_edit_tree_column_name::get_item_cell($item, $params)');
646         }
648         $name = $params['name'];
649         $moveaction = isset($params['moveaction']) ? $params['moveaction'] : '';
651         $itemcell = clone($this->itemcell);
652         $itemcell->attributes['class'] .= ' name';
653         $itemcell->colspan = ($this->deepest_level + 1) - $params['level'];
654         $itemcell->text = $moveaction . $name;
655         return $itemcell;
656     }
659 /**
660  * Class grade_edit_tree_column_aggregation
661  *
662  * @package   core_grades
663  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
664  */
665 class grade_edit_tree_column_aggregation extends grade_edit_tree_column {
667     public function __construct($params) {
668         parent::__construct('aggregation');
669     }
671     public function get_header_cell() {
672         global $OUTPUT;
673         $headercell = clone($this->headercell);
674         $headercell->text = get_string('aggregation', 'grades').$OUTPUT->help_icon('aggregation', 'grades');
675         return $headercell;
676     }
678     public function get_category_cell($category, $levelclass, $params) {
679         global $CFG, $OUTPUT;
680         if (empty($params['id'])) {
681             throw new Exception('Array key (id) missing from 3rd param of grade_edit_tree_column_aggregation::get_category_cell($category, $levelclass, $params)');
682         }
684         $options = grade_helper::get_aggregation_strings();
685         $aggregation = $options[$category->aggregation];
687         $categorycell = clone($this->categorycell);
688         $categorycell->attributes['class'] .= ' ' . $levelclass;
689         $categorycell->text = $aggregation;
690         return $categorycell;
692     }
694     public function get_item_cell($item, $params) {
695         $itemcell = clone($this->itemcell);
696         $itemcell->text = ' - ';
697         return $itemcell;
698     }
701 /**
702  * Class grade_edit_tree_column_weight
703  *
704  * @package   core_grades
705  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
706  */
707 class grade_edit_tree_column_weight extends grade_edit_tree_column {
709     public function get_header_cell() {
710         global $OUTPUT;
711         $headercell = clone($this->headercell);
712         $headercell->text = get_string('weights', 'grades').$OUTPUT->help_icon('aggregationcoefweight', 'grades');
713         return $headercell;
714     }
716     public function get_category_cell($category, $levelclass, $params) {
718         $item = $category->get_grade_item();
719         $categorycell = clone($this->categorycell);
720         $categorycell->attributes['class']  .= ' ' . $levelclass;
721         $categorycell->text = grade_edit_tree::get_weight_input($item);
722         return $categorycell;
723     }
725     public function get_item_cell($item, $params) {
726         if (empty($params['element'])) {
727             throw new Exception('Array key (element) missing from 2nd param of grade_edit_tree_column_weightorextracredit::get_item_cell($item, $params)');
728         }
729         $itemcell = clone($this->itemcell);
730         $itemcell->text = '&nbsp;';
732         if (!in_array($params['element']['object']->itemtype, array('courseitem', 'categoryitem', 'category'))) {
733             $itemcell->text = grade_edit_tree::get_weight_input($item);
734         }
736         return $itemcell;
737     }
740 /**
741  * Class grade_edit_tree_column_range
742  *
743  * @package   core_grades
744  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
745  */
746 class grade_edit_tree_column_range extends grade_edit_tree_column {
748     public function get_header_cell() {
749         $headercell = clone($this->headercell);
750         $headercell->text = get_string('maxgrade', 'grades');
751         return $headercell;
752     }
754     public function get_category_cell($category, $levelclass, $params) {
755         $categorycell = clone($this->categorycell);
756         $categorycell->attributes['class'] .= ' range ' . $levelclass;
757         $categorycell->text = ' - ';
758         return $categorycell;
759     }
761     public function get_item_cell($item, $params) {
762         global $DB, $OUTPUT;
764         // If the parent aggregation is Natural, we should show the number, even for scales, as that value is used...
765         // ...in the computation. For text grades, the grademax is not used, so we can still show the no value string.
766         $parent_cat = $item->get_parent_category();
767         if ($item->gradetype == GRADE_TYPE_TEXT) {
768             $grademax = ' - ';
769         } else if ($parent_cat->aggregation == GRADE_AGGREGATE_SUM) {
770             $grademax = format_float($item->grademax, $item->get_decimals());
771         } else if ($item->gradetype == GRADE_TYPE_SCALE) {
772             $scale = $DB->get_record('scale', array('id' => $item->scaleid));
773             $scale_items = null;
774             if (empty($scale)) { //if the item is using a scale that's been removed
775                 $scale_items = array();
776             } else {
777                 $scale_items = explode(',', $scale->scale);
778             }
779             $grademax = end($scale_items) . ' (' . count($scale_items) . ')';
780         } else {
781             $grademax = format_float($item->grademax, $item->get_decimals());
782         }
784         $itemcell = clone($this->itemcell);
785         $itemcell->text = $grademax;
786         return $itemcell;
787     }
790 /**
791  * Class grade_edit_tree_column_actions
792  *
793  * @package   core_grades
794  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
795  */
796 class grade_edit_tree_column_actions extends grade_edit_tree_column {
798     public function __construct($params) {
799         parent::__construct();
800     }
802     public function get_header_cell() {
803         $headercell = clone($this->headercell);
804         $headercell->attributes['class'] .= ' actions';
805         $headercell->text = get_string('actions');
806         return $headercell;
807     }
809     public function get_category_cell($category, $levelclass, $params) {
811         if (empty($params['actions'])) {
812             throw new Exception('Array key (actions) missing from 3rd param of grade_edit_tree_column_actions::get_category_actions($category, $levelclass, $params)');
813         }
815         $categorycell = clone($this->categorycell);
816         $categorycell->attributes['class'] .= ' ' . $levelclass;
817         $categorycell->text = $params['actions'];
818         return $categorycell;
819     }
821     public function get_item_cell($item, $params) {
822         if (empty($params['actions'])) {
823             throw new Exception('Array key (actions) missing from 2nd param of grade_edit_tree_column_actions::get_item_cell($item, $params)');
824         }
825         $itemcell = clone($this->itemcell);
826         $itemcell->attributes['class'] .= ' actions';
827         $itemcell->text = $params['actions'];
828         return $itemcell;
829     }
832 /**
833  * Class grade_edit_tree_column_select
834  *
835  * @package   core_grades
836  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
837  */
838 class grade_edit_tree_column_select extends grade_edit_tree_column {
840     public function get_header_cell() {
841         $headercell = clone($this->headercell);
842         $headercell->attributes['class'] .= ' selection';
843         $headercell->text = get_string('select');
844         return $headercell;
845     }
847     public function get_category_cell($category, $levelclass, $params) {
848         global $OUTPUT;
849         if (empty($params['eid'])) {
850             throw new Exception('Array key (eid) missing from 3rd param of grade_edit_tree_column_select::get_category_cell($category, $levelclass, $params)');
851         }
852         $selectall  = new action_link(new moodle_url('#'), get_string('all'), new component_action('click', 'togglecheckboxes', array('eid' => $params['eid'], 'check' => true)));
853         $selectnone = new action_link(new moodle_url('#'), get_string('none'), new component_action('click', 'togglecheckboxes', array('eid' => $params['eid'], 'check' => false)));
855         $categorycell = clone($this->categorycell);
856         $categorycell->attributes['class'] .= ' selection ' . $levelclass;
857         $categorycell->text = $OUTPUT->render($selectall) . '<br />' . $OUTPUT->render($selectnone);
858         return $categorycell;
859     }
861     public function get_item_cell($item, $params) {
862         if (empty($params['itemtype']) || empty($params['eid'])) {
863             error('Array key (itemtype or eid) missing from 2nd param of grade_edit_tree_column_select::get_item_cell($item, $params)');
864         }
865         $itemcell = clone($this->itemcell);
866         $itemcell->attributes['class'] .= ' selection';
867         $itemcell->text = '';
869         if ($params['itemtype'] != 'course' && $params['itemtype'] != 'category') {
870             $itemcell->text = '<label class="accesshide" for="select_'.$params['eid'].'">'.
871                 get_string('select', 'grades', $item->itemname).'</label>
872                 <input class="itemselect ignoredirty" type="checkbox" name="select_'.$params['eid'].'" id="select_'.$params['eid'].
873                 '" onchange="toggleCategorySelector();"/>'; // TODO: convert to YUI handler
874         }
875         //html_writer::table() will wrap the item cell contents in a <TD> so don't do it here
876         return $itemcell;
877     }