MDL-19799 Adding missing echo
[moodle.git] / grade / edit / tree / lib.php
CommitLineData
e060e33d 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
dc482cfa 17
18class grade_edit_tree {
19 public $columns = array();
20
21 /**
22 * @var object $gtree @see grade/lib.php
23 */
24 public $gtree;
25
26 /**
27 * @var grade_plugin_return @see grade/lib.php
28 */
29 public $gpr;
30
31 /**
32 * @var string $moving The eid of the category or item being moved
33 */
34 public $moving;
35
36 public $deepest_level;
37
653a8648 38 public $uses_extra_credit = false;
39
40 public $uses_weight = false;
41
dc482cfa 42 /**
43 * Constructor
44 */
45 public function __construct($gtree, $moving=false, $gpr) {
46 $this->gtree = $gtree;
47 $this->moving = $moving;
48 $this->gpr = $gpr;
49 $this->deepest_level = $this->get_deepest_level($this->gtree->top_element);
50
51 $this->columns = array(grade_edit_tree_column::factory('name', array('deepest_level' => $this->deepest_level)),
653a8648 52 grade_edit_tree_column::factory('aggregation', array('flag' => true)));
53
54 if ($this->uses_weight) {
55 $this->columns[] = grade_edit_tree_column::factory('weight', array('adv' => 'aggregationcoef'));
56 }
57 if ($this->uses_extra_credit) {
58 $this->columns[] = grade_edit_tree_column::factory('extracredit', array('adv' => 'aggregationcoef'));
59 }
60
61 $this->columns[] = grade_edit_tree_column::factory('range'); // This is not a setting... How do we deal with it?
62 $this->columns[] = grade_edit_tree_column::factory('aggregateonlygraded', array('flag' => true));
63 $this->columns[] = grade_edit_tree_column::factory('aggregatesubcats', array('flag' => true));
64 $this->columns[] = grade_edit_tree_column::factory('aggregateoutcomes', array('flag' => true));
65 $this->columns[] = grade_edit_tree_column::factory('droplow', array('flag' => true));
66 $this->columns[] = grade_edit_tree_column::factory('keephigh', array('flag' => true));
67 $this->columns[] = grade_edit_tree_column::factory('multfactor', array('adv' => true));
68 $this->columns[] = grade_edit_tree_column::factory('plusfactor', array('adv' => true));
69 $this->columns[] = grade_edit_tree_column::factory('actions');
70 $this->columns[] = grade_edit_tree_column::factory('select');
dc482cfa 71 }
72
73 /**
74 * Recursive function for building the table holding the grade categories and items,
75 * with CSS indentation and styles.
76 *
77 * @param array $element The current tree element being rendered
78 * @param boolean $totals Whether or not to print category grade items (category totals)
79 * @param array $parents An array of parent categories for the current element (used for indentation and row classes)
80 *
81 * @return string HTML
82 */
e8048b1d 83 public function build_html_tree($element, $totals, $parents, &$categories, $level, &$row_count) {
6b608f8f 84 global $CFG, $COURSE, $USER, $OUTPUT;
dc482cfa 85
86 $object = $element['object'];
87 $eid = $element['eid'];
88 $object->name = $this->gtree->get_element_header($element, true, true, false);
89 $object->stripped_name = $this->gtree->get_element_header($element, false, false, false);
90
91 $is_category_item = false;
92 if ($element['type'] == 'categoryitem' || $element['type'] == 'courseitem') {
93 $is_category_item = true;
94 }
95
96 $rowclasses = '';
97 foreach ($parents as $parent_eid) {
98 $rowclasses .= " $parent_eid ";
99 }
100
101 $actions = '';
102
103 if (!$is_category_item) {
104 $actions .= $this->gtree->get_edit_icon($element, $this->gpr);
105 }
106
107 $actions .= $this->gtree->get_calculation_icon($element, $this->gpr);
108
109 if ($element['type'] == 'item' or ($element['type'] == 'category' and $element['depth'] > 1)) {
110 if ($this->element_deletable($element)) {
111 $actions .= '<a href="index.php?id='.$COURSE->id.'&amp;action=delete&amp;eid='
6b608f8f 112 . $eid.'&amp;sesskey='.sesskey().'"><img src="'.$OUTPUT->old_icon_url('t/delete') . '" class="iconsmall" alt="'
dc482cfa 113 . get_string('delete').'" title="'.get_string('delete').'"/></a>';
114 }
115 $actions .= '<a href="index.php?id='.$COURSE->id.'&amp;action=moveselect&amp;eid='
6b608f8f 116 . $eid.'&amp;sesskey='.sesskey().'"><img src="'.$OUTPUT->old_icon_url('t/move') . '" class="iconsmall" alt="'
dc482cfa 117 . get_string('move').'" title="'.get_string('move').'"/></a>';
118 }
119
120 $actions .= $this->gtree->get_hiding_icon($element, $this->gpr);
121 $actions .= $this->gtree->get_locking_icon($element, $this->gpr);
122
123 $mode = ($USER->gradeediting[$COURSE->id]) ? 'advanced' : 'simple';
124
125 $html = '';
126 $root = false;
127
128
129 $id = required_param('id', PARAM_INT);
130
131 /// prepare move target if needed
132 $last = '';
133
134 /// print the list items now
135 if ($this->moving == $eid) {
136
137 // do not diplay children
138 return '<tr><td colspan="12" class="'.$element['type'].' moving">'.$object->name.' ('.get_string('move').')</td></tr>';
139
140 }
141
142 if ($element['type'] == 'category') {
143 $level++;
144 $categories[$object->id] = $object->stripped_name;
145 $category = grade_category::fetch(array('id' => $object->id));
146 $item = $category->get_grade_item();
147
148 // Add aggregation coef input if not a course item and if parent category has correct aggregation type
fa5ac78b 149 $dimmed = ($item->is_hidden()) ? " dimmed " : "";
dc482cfa 150
151 // Before we print the category's row, we must find out how many rows will appear below it (for the filler cell's rowspan)
152 $aggregation_position = grade_get_setting($COURSE->id, 'aggregationposition', $CFG->grade_aggregationposition);
153 $category_total_data = null; // Used if aggregationposition is set to "last", so we can print it last
154
155 $html_children = '';
156
157 $row_count = 0;
158
159 foreach($element['children'] as $child_el) {
160 $moveto = '';
161
162 if (empty($child_el['object']->itemtype)) {
163 $child_el['object']->itemtype = false;
164 }
165
166 if (($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category') && !$totals) {
167 continue;
168 }
169
170 $child_eid = $child_el['eid'];
171 $first = '';
172
173 if ($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category') {
174 $first = '&amp;first=1';
175 $child_eid = $eid;
176 }
177
178 if ($this->moving && $this->moving != $child_eid) {
179
180 $strmove = get_string('move');
181 $strmovehere = get_string('movehere');
182 $actions = ''; // no action icons when moving
183
184 $moveto = '<tr><td colspan="12"><a href="index.php?id='.$COURSE->id.'&amp;action=move&amp;eid='.$this->moving.'&amp;moveafter='
185 . $child_eid.'&amp;sesskey='.sesskey().$first.'"><img class="movetarget" src="'.$CFG->wwwroot.'/pix/movehere.gif" alt="'
1edd08ab 186 . s($strmovehere).'" title="'.s($strmovehere).'" /></a></td></tr>';
dc482cfa 187 }
188
189 $newparents = $parents;
190 $newparents[] = $eid;
191
192 $row_count++;
193 $child_row_count = 0;
194
195 // If moving, do not print course and category totals, but still print the moveto target box
196 if ($this->moving && ($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category')) {
197 $html_children .= $moveto;
198 } elseif ($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category') {
199 // We don't build the item yet because we first need to know the deepest level of categories (for category/name colspans)
200 $category_total_item = $this->build_html_tree($child_el, $totals, $newparents, $categories, $level, $child_row_count);
201 if (!$aggregation_position) {
202 $html_children .= $category_total_item;
203 }
204 } else {
205 $html_children .= $this->build_html_tree($child_el, $totals, $newparents, $categories, $level, $child_row_count) . $moveto;
206
207 if ($this->moving) {
208 $row_count++;
209 }
210 }
211
212 $row_count += $child_row_count;
213
214 // If the child is a category, increment row_count by one more (for the extra coloured row)
215 if ($child_el['type'] == 'category') {
216 $row_count++;
217 }
218 }
219
220 // Print category total at the end if aggregation position is "last" (1)
221 if (!empty($category_total_item) && $aggregation_position) {
222 $html_children .= $category_total_item;
223 }
224
225 // now build the header
226 if (isset($element['object']->grade_item) && $element['object']->grade_item->is_course_item()) {
227 // Reduce width if advanced elements are not shown
228 $width_style = '';
229
230 if ($mode == 'simple') {
231 $width_style = ' style="width:auto;" ';
232 }
233
b189277f 234 $html .= '<table id="grade_edit_tree_table" cellpadding="5" class="generaltable" '.$width_style.'>
dc482cfa 235 <tr>';
236
237 foreach ($this->columns as $column) {
238 if (!($this->moving && $column->hide_when_moving) && !$column->is_hidden($mode)) {
239 $html .= $column->get_header_cell();
240 }
241 }
242
243 $html .= '</tr>';
244 $root = true;
245 }
246
247 $row_count_offset = 0;
248
249 if (empty($category_total_item) && !$this->moving) {
250 $row_count_offset = -1;
251 }
252
253 $levelclass = " level$level ";
2be6ca78 254
b189277f 255 $courseclass = '';
256 if ($level == 1) {
257 $courseclass = 'coursecategory';
258 }
dc482cfa 259
260 $html .= '
b189277f 261 <tr class="'.$courseclass.' category '.$dimmed.$rowclasses.'">
1edd08ab 262 <th scope="row" title="'.s($object->stripped_name).'" class="cell rowspan '.$levelclass.'" rowspan="'.($row_count+1+$row_count_offset).'"></th>';
dc482cfa 263
264 foreach ($this->columns as $column) {
265 if (!($this->moving && $column->hide_when_moving) && !$column->is_hidden($mode)) {
266 $html .= $column->get_category_cell($category, $levelclass, array('id' => $id, 'name' => $object->name, 'level' => $level, 'actions' => $actions, 'eid' => $eid));
267 }
268 }
269
270 $html .= '</tr>';
271
272 $html .= $html_children;
273
274 // Print a coloured row to show the end of the category accross the table
275 $html .= '<tr><td colspan="'.(19 - $level).'" class="colspan '.$levelclass.'"></td></tr>';
276
277 } else { // Dealing with a grade item
278
279 $item = grade_item::fetch(array('id' => $object->id));
280 $element['type'] = 'item';
281 $element['object'] = $item;
2be6ca78 282
b189277f 283 $categoryitemclass = '';
284 if ($item->itemtype == 'category') {
285 $categoryitemclass = 'categoryitem';
286 }
dc482cfa 287
fa5ac78b 288 $dimmed = ($item->is_hidden()) ? " dimmed_text " : "";
b189277f 289 $html .= '<tr class="'.$categoryitemclass.' item'.$dimmed.$rowclasses.'">';
dc482cfa 290
291 foreach ($this->columns as $column) {
292 if (!($this->moving && $column->hide_when_moving) && !$column->is_hidden($mode)) {
293 $html .= $column->get_item_cell($item, array('id' => $id, 'name' => $object->name, 'level' => $level, 'actions' => $actions,
294 'element' => $element, 'eid' => $eid, 'itemtype' => $object->itemtype));
295 }
296 }
297
298 $html .= '</tr>';
299 }
300
301
302 if ($root) {
303 $html .= "</table>\n";
304 }
305
306 return $html;
307
308 }
309
310 /**
311 * Given a grade_item object, returns a labelled input if an aggregation coefficient (weight or extra credit) applies to it.
312 * @param grade_item $item
653a8648 313 * @param string type "extra" or "weight": the type of the column hosting the weight input
dc482cfa 314 * @return string HTML
315 */
653a8648 316 function get_weight_input($item, $type) {
dc482cfa 317 if (!is_object($item) || get_class($item) !== 'grade_item') {
318 throw new Exception('grade_edit_tree::get_weight_input($item) was given a variable that is not of the required type (grade_item object)');
319 return false;
320 }
321
322 if ($item->is_course_item()) {
323 return '';
324 }
325
326 $parent_category = $item->get_parent_category();
dc482cfa 327 $parent_category->apply_forced_settings();
653a8648 328 $aggcoef = $item->get_coefstring();
dc482cfa 329
653a8648 330 if ((($aggcoef == 'aggregationcoefweight' || $aggcoef == 'aggregationcoef') && $type == 'weight') ||
331 ($aggcoef == 'aggregationcoefextra' && $type == 'extra')) {
cd3a391c 332 return '<input type="text" size="6" id="aggregationcoef_'.$item->id.'" name="aggregationcoef_'.$item->id.'"
333 value="'.format_float($item->aggregationcoef, 4).'" />';
653a8648 334 } elseif ($aggcoef == 'aggregationcoefextrasum' && $type == 'extra') {
cd3a391c 335 $checked = ($item->aggregationcoef > 0) ? 'checked="checked"' : '';
336 return '<input type="hidden" name="extracredit_'.$item->id.'" value="0" />
337 <input type="checkbox" id="extracredit_'.$item->id.'" name="extracredit_'.$item->id.'" value="1" '."$checked />\n";
338 } else {
339 return '';
dc482cfa 340 }
cd3a391c 341 }
dc482cfa 342
343 /**
344 * Given an element of the grade tree, returns whether it is deletable or not (only manual grade items are deletable)
345 *
346 * @param array $element
347 * @return bool
348 */
349 function element_deletable($element) {
350 global $COURSE;
351
352 if ($element['type'] != 'item') {
353 return true;
354 }
355
356 $grade_item = $element['object'];
357
358 if ($grade_item->itemtype != 'mod' or $grade_item->is_outcome_item() or $grade_item->gradetype == GRADE_TYPE_NONE) {
359 return true;
360 }
361
362 $modinfo = get_fast_modinfo($COURSE);
363 if (!isset($modinfo->instances[$grade_item->itemmodule][$grade_item->iteminstance])) {
364 // module does not exist
365 return true;
366 }
367
368 return false;
369 }
370
371 /**
372 * Given the grade tree and an array of element ids (e.g. c15, i42), and expecting the 'moveafter' URL param,
373 * moves the selected items to the requested location. Then redirects the user to the given $returnurl
374 *
375 * @param object $gtree The grade tree (a recursive representation of the grade categories and grade items)
376 * @param array $eids
377 * @param string $returnurl
378 */
379 function move_elements($eids, $returnurl) {
380 $moveafter = required_param('moveafter', PARAM_INT);
381
382 if (!is_array($eids)) {
383 $eids = array($eids);
384 }
385
386 if(!$after_el = $this->gtree->locate_element("c$moveafter")) {
387 print_error('invalidelementid', '', $returnurl);
388 }
389
390 $after = $after_el['object'];
391 $parent = $after;
392 $sortorder = $after->get_sortorder();
393
394 foreach ($eids as $eid) {
395 if (!$element = $this->gtree->locate_element($eid)) {
396 print_error('invalidelementid', '', $returnurl);
397 }
398 $object = $element['object'];
399
400 $object->set_parent($parent->id);
401 $object->move_after_sortorder($sortorder);
4d2f7923 402 $sortorder++;
dc482cfa 403 }
404
405 redirect($returnurl, '', 0);
406 }
407
408 /**
409 * Recurses through the entire grade tree to find and return the maximum depth of the tree.
410 * This should be run only once from the root element (course category), and is used for the
411 * indentation of the Name column's cells (colspan)
412 *
413 * @param array $element An array of values representing a grade tree's element (all grade items in this case)
414 * @param int $level The level of the current recursion
415 * @param int $deepest_level A value passed to each subsequent level of recursion and incremented if $level > $deepest_level
416 * @return int Deepest level
417 */
418 function get_deepest_level($element, $level=0, $deepest_level=1) {
419 $object = $element['object'];
420
421 $level++;
653a8648 422 $coefstring = $element['object']->get_coefstring();
dc482cfa 423 if ($element['type'] == 'category') {
653a8648 424 if ($coefstring == 'aggregationcoefweight') {
425 $this->uses_weight = true;
426 } elseif ($coefstring == 'aggregationcoefextra' || $coefstring == 'aggregationcoefextrasum') {
427 $this->uses_extra_credit = true;
428 }
429
dc482cfa 430 foreach($element['children'] as $child_el) {
431 if ($level > $deepest_level) {
432 $deepest_level = $level;
433 }
434 $deepest_level = $this->get_deepest_level($child_el, $level, $deepest_level);
435 }
436 }
437
438 return $deepest_level;
439 }
440}
441
442abstract class grade_edit_tree_column {
443 public $forced;
444 public $hidden;
445 public $forced_hidden;
446 public $advanced_hidden;
447 public $hide_when_moving = true;
448
449 public static function factory($name, $params=array()) {
450 $class_name = "grade_edit_tree_column_$name";
451 if (class_exists($class_name)) {
452 return new $class_name($params);
453 }
454 }
455
456 public abstract function get_header_cell();
457
458 public abstract function get_category_cell($category, $levelclass, $params);
459
460 public abstract function get_item_cell($item, $params);
461
462 public abstract function is_hidden($mode='simple');
463}
464
465abstract class grade_edit_tree_column_category extends grade_edit_tree_column {
466
467 public $forced;
468 public $advanced;
469
470 public function __construct($name) {
471 global $CFG;
472 $this->forced = (int)$CFG->{"grade_$name"."_flag"} & 1;
473 $this->advanced = (int)$CFG->{"grade_$name"."_flag"} & 2;
474 }
475
476 public function is_hidden($mode='simple') {
477 global $CFG;
478 if ($mode == 'simple') {
479 return $this->advanced;
480 } elseif ($mode == 'advanced') {
481 if ($this->forced && $CFG->grade_hideforcedsettings) {
482 return true;
483 } else {
484 return false;
485 }
486 }
487 }
488}
489
490class grade_edit_tree_column_name extends grade_edit_tree_column {
491 public $forced = false;
492 public $hidden = false;
493 public $forced_hidden = false;
494 public $advanced_hidden = false;
495 public $deepest_level = 1;
496 public $hide_when_moving = false;
497
498 public function __construct($params) {
499 if (empty($params['deepest_level'])) {
500 throw new Exception('Tried to instantiate a grade_edit_tree_column_name object without the "deepest_level" param!');
501 }
502
503 $this->deepest_level = $params['deepest_level'];
504 }
505
506 public function get_header_cell() {
507 return '<th class="header name" colspan="'.($this->deepest_level + 1).'" scope="col">'.get_string('name').'</th>';
508 }
509
510 public function get_category_cell($category, $levelclass, $params) {
511 if (empty($params['name']) || empty($params['level'])) {
512 throw new Exception('Array key (name or level) missing from 3rd param of grade_edit_tree_column_name::get_category_cell($category, $levelclass, $params)');
513 }
514
515 return '<td class="cell name '.$levelclass.'" colspan="'.(($this->deepest_level +1) - $params['level']).'"><h4>' . $params['name'] . "</h4></td>\n";
516 }
517
518 public function get_item_cell($item, $params) {
519 global $CFG;
520
521 if (empty($params['element']) || empty($params['name']) || empty($params['level'])) {
522 throw new Exception('Array key (name, level or element) missing from 2nd param of grade_edit_tree_column_name::get_item_cell($item, $params)');
523 }
524
525 $name = $params['name'];
526
527 return '<td class="cell name" colspan="'.(($this->deepest_level + 1) - $params['level']).'">' . $name . '</td>';
528 }
529
530 public function is_hidden($mode='simple') {
531 return false;
532 }
533}
534
535class grade_edit_tree_column_aggregation extends grade_edit_tree_column_category {
536
537 public function __construct($params) {
538 parent::__construct('aggregation');
539 }
540
541 public function get_header_cell() {
c3b834b4 542 global $OUTPUT;
543 return '<th class="header" scope="col">'.get_string('aggregation', 'grades').$OUTPUT->help_icon(moodle_help_icon::make('aggregation', 'aggregation', 'grade')).'</th>';
dc482cfa 544 }
545
546 public function get_category_cell($category, $levelclass, $params) {
0a95bebf 547 global $CFG, $OUTPUT;
dc482cfa 548 if (empty($params['id'])) {
549 throw new Exception('Array key (id) missing from 3rd param of grade_edit_tree_column_aggregation::get_category_cell($category, $levelclass, $params)');
550 }
551
552 $options = array(GRADE_AGGREGATE_MEAN => get_string('aggregatemean', 'grades'),
553 GRADE_AGGREGATE_WEIGHTED_MEAN => get_string('aggregateweightedmean', 'grades'),
554 GRADE_AGGREGATE_WEIGHTED_MEAN2 => get_string('aggregateweightedmean2', 'grades'),
555 GRADE_AGGREGATE_EXTRACREDIT_MEAN => get_string('aggregateextracreditmean', 'grades'),
556 GRADE_AGGREGATE_MEDIAN => get_string('aggregatemedian', 'grades'),
557 GRADE_AGGREGATE_MIN => get_string('aggregatemin', 'grades'),
558 GRADE_AGGREGATE_MAX => get_string('aggregatemax', 'grades'),
559 GRADE_AGGREGATE_MODE => get_string('aggregatemode', 'grades'),
560 GRADE_AGGREGATE_SUM => get_string('aggregatesum', 'grades'));
561
653a8648 562 $visible = explode(',', $CFG->grade_aggregations_visible);
563 foreach ($options as $constant => $string) {
564 if (!in_array($constant, $visible) && $constant != $category->aggregation) {
565 unset($options[$constant]);
566 }
567 }
568
7b1f2c82 569 $select = new html_select();
49c8c8d2 570 $select->options = $options;
571 $select->name = 'aggregation_'.$category->id;
572 $select->selectedvalue = $category->aggregation;
573 $select->add_action('change', 'update_category_aggregation', array('courseid' => $params['id'], 'category' => $category->id, 'sesskey' => sesskey()));
574 $aggregation = $OUTPUT->select($select);
dc482cfa 575
576 if ($this->forced) {
577 $aggregation = $options[$category->aggregation];
578 }
579
580 return '<td class="cell '.$levelclass.'">' . $aggregation . '</td>';
581
582 }
583
584 public function get_item_cell($item, $params) {
585 return '<td class="cell"> - </td>';
586 }
587}
588
653a8648 589class grade_edit_tree_column_extracredit extends grade_edit_tree_column {
590
591 public function get_header_cell() {
c3b834b4 592 global $OUTPUT;
593 return '<th class="header" scope="col">'.get_string('extracredit', 'grades').$OUTPUT->help_icon(moodle_help_icon::make('aggregationcoefcombo', 'aggregationcoefcombo', 'grade')).'</th>';
653a8648 594 }
595
596 public function get_category_cell($category, $levelclass, $params) {
597
598 $item = $category->get_grade_item();
599 $aggcoef_input = grade_edit_tree::get_weight_input($item, 'extra');
600 return '<td class="cell '.$levelclass.'">' . $aggcoef_input . '</td>';
601 }
602
603 public function get_item_cell($item, $params) {
604 if (empty($params['element'])) {
605 throw new Exception('Array key (element) missing from 2nd param of grade_edit_tree_column_weightorextracredit::get_item_cell($item, $params)');
606 }
607
608 $html = '<td class="cell">';
609
610 if (!in_array($params['element']['object']->itemtype, array('courseitem', 'categoryitem', 'category'))) {
611 $html .= grade_edit_tree::get_weight_input($item, 'extra');
612 }
613
614 return $html.'</td>';
615 }
616
617 public function is_hidden($mode='simple') {
618 global $CFG;
619 if ($mode == 'simple') {
620 return strstr($CFG->grade_item_advanced, 'aggregationcoef');
621 } elseif ($mode == 'advanced') {
622 return false;
623 }
624 }
625}
626
627class grade_edit_tree_column_weight extends grade_edit_tree_column {
dc482cfa 628
629 public function get_header_cell() {
c3b834b4 630 global $OUTPUT;
631 return '<th class="header" scope="col">'.get_string('weightuc', 'grades').$OUTPUT->help_icon(moodle_help_icon::make('aggregationcoefweight', 'aggregationcoefweight', 'grade')).'</th>';
dc482cfa 632 }
633
634 public function get_category_cell($category, $levelclass, $params) {
635
636 $item = $category->get_grade_item();
653a8648 637 $aggcoef_input = grade_edit_tree::get_weight_input($item, 'weight');
dc482cfa 638 return '<td class="cell '.$levelclass.'">' . $aggcoef_input . '</td>';
639 }
640
641 public function get_item_cell($item, $params) {
642 if (empty($params['element'])) {
643 throw new Exception('Array key (element) missing from 2nd param of grade_edit_tree_column_weightorextracredit::get_item_cell($item, $params)');
644 }
645
dc482cfa 646 $html = '<td class="cell">';
647
b8436ccb 648 if (!in_array($params['element']['object']->itemtype, array('courseitem', 'categoryitem', 'category'))) {
653a8648 649 $html .= grade_edit_tree::get_weight_input($item, 'weight');
dc482cfa 650 }
651
652 return $html.'</td>';
653 }
654
655 public function is_hidden($mode='simple') {
656 global $CFG;
657 if ($mode == 'simple') {
658 return strstr($CFG->grade_item_advanced, 'aggregationcoef');
659 } elseif ($mode == 'advanced') {
660 return false;
661 }
662 }
663}
664
665class grade_edit_tree_column_range extends grade_edit_tree_column {
666
667 public function get_header_cell() {
668 return '<th class="header" scope="col">'.get_string('maxgrade', 'grades').'</th>';
669 }
670
671 public function get_category_cell($category, $levelclass, $params) {
672 return '<td class="cell range '.$levelclass.'"> - </td>';
673 }
674
675 public function get_item_cell($item, $params) {
3998cc5c 676 global $DB;
46409b20 677
678 // If the parent aggregation is Sum of Grades, this cannot be changed
679 $parent_cat = $item->get_parent_category();
680 if ($parent_cat->aggregation == GRADE_AGGREGATE_SUM) {
681 $grademax = format_float($item->grademax, $item->get_decimals());
682 } elseif ($item->gradetype == GRADE_TYPE_SCALE) {
3998cc5c 683 $scale = $DB->get_record('scale', array('id' => $item->scaleid));
dc482cfa 684 $scale_items = explode(',', $scale->scale);
685 $grademax = end($scale_items) . ' (' . count($scale_items) . ')';
686 } elseif ($item->is_external_item()) {
687 $grademax = format_float($item->grademax, $item->get_decimals());
688 } else {
cd3a391c 689 $grademax = '<input type="text" size="4" id="grademax'.$item->id.'" name="grademax_'.$item->id.'" value="'.format_float($item->grademax, $item->get_decimals()).'" />';
dc482cfa 690 }
691
692 return '<td class="cell">'.$grademax.'</td>';
693 }
694
695 public function is_hidden($mode='simple') {
696 global $CFG;
697 if ($mode == 'simple') {
698 return strstr($CFG->grade_item_advanced, 'grademax');
699 } elseif ($mode == 'advanced') {
700 return false;
701 }
702 }
703}
704
705class grade_edit_tree_column_aggregateonlygraded extends grade_edit_tree_column_category {
706
707 public function __construct($params) {
708 parent::__construct('aggregateonlygraded');
709 }
710
711 public function get_header_cell() {
c3b834b4 712 global $OUTPUT;
dc482cfa 713 return '<th class="header" style="width: 40px" scope="col">'.get_string('aggregateonlygraded', 'grades')
c3b834b4 714 .$OUTPUT->help_icon(moodle_help_icon::make('aggregateonlygraded', 'aggregateonlygraded', 'grade')).'</th>';
dc482cfa 715 }
716
717 public function get_category_cell($category, $levelclass, $params) {
718 $onlygradedcheck = ($category->aggregateonlygraded == 1) ? 'checked="checked"' : '';
cd3a391c 719 $hidden = '<input type="hidden" name="aggregateonlygraded_'.$category->id.'" value="0" />';
720 $aggregateonlygraded ='<input type="checkbox" id="aggregateonlygraded_'.$category->id.'" name="aggregateonlygraded_'.$category->id.'" value="1" '.$onlygradedcheck . ' />';
dc482cfa 721
722 if ($this->forced) {
723 $aggregateonlygraded = ($category->aggregateonlygraded) ? get_string('yes') : get_string('no');
724 }
725
cd3a391c 726 return '<td class="cell '.$levelclass.'">'.$hidden.$aggregateonlygraded.'</td>';
dc482cfa 727 }
728
729 public function get_item_cell($item, $params) {
730 return '<td class="cell"> - </td>';
731 }
732}
733
734class grade_edit_tree_column_aggregatesubcats extends grade_edit_tree_column_category {
735
736 public function __construct($params) {
737 parent::__construct('aggregatesubcats');
738 }
739
740 public function get_header_cell() {
c3b834b4 741 global $OUTPUT;
dc482cfa 742 return '<th class="header" style="width: 40px" scope="col">'.get_string('aggregatesubcats', 'grades')
c3b834b4 743 .$OUTPUT->help_icon(moodle_help_icon::make('aggregatesubcats', 'aggregatesubcats', 'grade')).'</th>';
dc482cfa 744 }
745
746 public function get_category_cell($category, $levelclass, $params) {
747 $subcatscheck = ($category->aggregatesubcats == 1) ? 'checked="checked"' : '';
cd3a391c 748 $hidden = '<input type="hidden" name="aggregatesubcats_'.$category->id.'" value="0" />';
749 $aggregatesubcats = '<input type="checkbox" id="aggregatesubcats_'.$category->id.'" name="aggregatesubcats_'.$category->id.'" value="1" ' . $subcatscheck.' />';
dc482cfa 750
751 if ($this->forced) {
752 $aggregatesubcats = ($category->aggregatesubcats) ? get_string('yes') : get_string('no');
753 }
754
cd3a391c 755 return '<td class="cell '.$levelclass.'">'.$hidden.$aggregatesubcats.'</td>';
dc482cfa 756
757 }
758
759 public function get_item_cell($item, $params) {
760 return '<td class="cell"> - </td>';
761 }
762}
763
764class grade_edit_tree_column_aggregateoutcomes extends grade_edit_tree_column_category {
765
766 public function __construct($params) {
767 parent::__construct('aggregateoutcomes');
768 }
769
770 public function get_header_cell() {
c3b834b4 771 global $OUTPUT;
dc482cfa 772 return '<th class="header" style="width: 40px" scope="col">'.get_string('aggregateoutcomes', 'grades')
c3b834b4 773 .$OUTPUT->help_icon(moodle_help_icon::make('aggregateoutcomes', 'aggregateoutcomes', 'grade')).'</th>';
dc482cfa 774 }
775
776 public function get_category_cell($category, $levelclass, $params) {
777
778 $outcomescheck = ($category->aggregateoutcomes == 1) ? 'checked="checked"' : '';
cd3a391c 779 $hidden = '<input type="hidden" name="aggregateoutcomes_'.$category->id.'" value="0" />';
780 $aggregateoutcomes = '<input type="checkbox" id="aggregateoutcomes_'.$category->id.'" name="aggregateoutcomes_'.$category->id.'" value="1" ' . $outcomescheck.' />';
dc482cfa 781
782 if ($this->forced) {
783 $aggregateoutcomes = ($category->aggregateoutcomes) ? get_string('yes') : get_string('no');
784 }
785
cd3a391c 786 return '<td class="cell '.$levelclass.'">'.$hidden.$aggregateoutcomes.'</td>';
dc482cfa 787 }
788
789 public function get_item_cell($item, $params) {
790 return '<td class="cell"> - </td>';
791 }
792
793 public function is_hidden($mode='simple') {
794 global $CFG;
795 if ($CFG->enableoutcomes) {
796 return parent::is_hidden($mode);
797 } else {
798 return true;
799 }
800 }
801}
802
803class grade_edit_tree_column_droplow extends grade_edit_tree_column_category {
804
805 public function __construct($params) {
806 parent::__construct('droplow');
807 }
808
809 public function get_header_cell() {
c3b834b4 810 global $OUTPUT;
811 return '<th class="header" scope="col">'.get_string('droplow', 'grades').$OUTPUT->help_icon(moodle_help_icon::make('droplow', 'droplow', 'grade')).'</th>';
dc482cfa 812 }
813
814 public function get_category_cell($category, $levelclass, $params) {
815 $droplow = '<input type="text" size="3" id="droplow_'.$category->id.'" name="droplow_'.$category->id.'" value="'.$category->droplow.'" />';
816
817 if ($this->forced) {
818 $droplow = $category->droplow;
819 }
820
821 return '<td class="cell '.$levelclass.'">' . $droplow . '</td>';
822 }
823
824 public function get_item_cell($item, $params) {
825 return '<td class="cell"> - </td>';
826 }
827}
828
829class grade_edit_tree_column_keephigh extends grade_edit_tree_column_category {
830
831 public function __construct($params) {
832 parent::__construct('keephigh');
833 }
834
835 public function get_header_cell() {
c3b834b4 836 global $OUTPUT;
837 return '<th class="header" scope="col">'.get_string('keephigh', 'grades').$OUTPUT->help_icon(moodle_help_icon::make('keephigh', 'keephigh', 'grade')).'</th>';
dc482cfa 838 }
839
840 public function get_category_cell($category, $levelclass, $params) {
841 $keephigh = '<input type="text" size="3" id="keephigh_'.$category->id.'" name="keephigh_'.$category->id.'" value="'.$category->keephigh.'" />';
842
843 if ($this->forced) {
844 $keephigh = $category->keephigh;
845 }
846
847 return '<td class="cell '.$levelclass.'">' . $keephigh . '</td>';
848 }
849
850 public function get_item_cell($item, $params) {
851 return '<td class="cell"> - </td>';
852 }
853}
854
855class grade_edit_tree_column_multfactor extends grade_edit_tree_column {
856
857 public function __construct($params) {
858
859 }
860
861 public function get_header_cell() {
c3b834b4 862 global $OUTPUT;
863 return '<th class="header" scope="col">'.get_string('multfactor', 'grades').$OUTPUT->help_icon(moodle_help_icon::make('multfactor', 'multfactor', 'grade')).'</th>';
dc482cfa 864 }
865
866 public function get_category_cell($category, $levelclass, $params) {
867
868 return '<td class="cell '.$levelclass.'"> - </td>';
869 }
870
871 public function get_item_cell($item, $params) {
a043086f 872 if (!$item->is_raw_used()) {
873 return '<td class="cell">&nbsp;</td>';
874 }
cd3a391c 875 $multfactor = '<input type="text" size="4" id="multfactor'.$item->id.'" name="multfactor_'.$item->id.'" value="'.format_float($item->multfactor, 4).'" />';
dc482cfa 876 return '<td class="cell">'.$multfactor.'</td>';
877 }
878
879 public function is_hidden($mode='simple') {
880 global $CFG;
881 if ($mode == 'simple') {
882 return strstr($CFG->grade_item_advanced, 'multfactor');
883 } elseif ($mode == 'advanced') {
884 return false;
885 }
886 }
887}
888
889class grade_edit_tree_column_plusfactor extends grade_edit_tree_column {
890
891 public function get_header_cell() {
c3b834b4 892 global $OUTPUT;
893 return '<th class="header" scope="col">'.get_string('plusfactor', 'grades').$OUTPUT->help_icon(moodle_help_icon::make('plusfactor', 'plusfactor', 'grade')).'</th>';
dc482cfa 894 }
895
896 public function get_category_cell($category, $levelclass, $params) {
897 return '<td class="cell '.$levelclass.'"> - </td>';
898
899 }
900
901 public function get_item_cell($item, $params) {
a043086f 902 if (!$item->is_raw_used()) {
903 return '<td class="cell">&nbsp;</td>';
904 }
cd3a391c 905 $plusfactor = '<input type="text" size="4" id="plusfactor_'.$item->id.'" name="plusfactor_'.$item->id.'" value="'.format_float($item->plusfactor, 4).'" />';
dc482cfa 906 return '<td class="cell">'.$plusfactor.'</td>';
907
908 }
909
910 public function is_hidden($mode='simple') {
911 global $CFG;
912 if ($mode == 'simple') {
913 return strstr($CFG->grade_item_advanced, 'plusfactor');
914 } elseif ($mode == 'advanced') {
915 return false;
916 }
917 }
918}
919
920class grade_edit_tree_column_actions extends grade_edit_tree_column {
921
922 public function __construct($params) {
923
924 }
925
926 public function get_header_cell() {
927 return '<th class="header actions" scope="col">'.get_string('actions').'</th>';
928 }
929
930 public function get_category_cell($category, $levelclass, $params) {
931
932 if (empty($params['actions'])) {
933 throw new Exception('Array key (actions) missing from 3rd param of grade_edit_tree_column_actions::get_category_actions($category, $levelclass, $params)');
934 }
935
936 return '<td class="cell actions '.$levelclass.'">' . $params['actions'] . '</td>';
937 }
938
939 public function get_item_cell($item, $params) {
940 if (empty($params['actions'])) {
941 throw new Exception('Array key (actions) missing from 2nd param of grade_edit_tree_column_actions::get_item_cell($item, $params)');
942 }
943 return '<td class="cell actions">' . $params['actions'] . '</td>';
944 }
945
946 public function is_hidden($mode='simple') {
947 return false;
948 }
949}
950
951class grade_edit_tree_column_select extends grade_edit_tree_column {
952
953 public function get_header_cell() {
954 return '<th class="header selection" scope="col">'.get_string('select').'</th>';
955 }
956
957 public function get_category_cell($category, $levelclass, $params) {
958
959 if (empty($params['eid'])) {
960 throw new Exception('Array key (eid) missing from 3rd param of grade_edit_tree_column_select::get_category_cell($category, $levelclass, $params)');
961 }
962
963 return '<td class="cell last '.$levelclass.'" style="text-align: center">
964 <span class="actionlink" onclick="togglecheckboxes(\''.$params['eid'].'\', true);">'.get_string('all').'</span><br />
965 <span class="actionlink" onclick="togglecheckboxes(\''.$params['eid'].'\', false);">'.get_string('none').'</span>
966 </td>';
967 }
968
969 public function get_item_cell($item, $params) {
970 if (empty($params['itemtype']) || empty($params['eid'])) {
971 throw new Exception('Array key (itemtype or eid) missing from 2nd param of grade_edit_tree_column_select::get_item_cell($item, $params)');
972 }
973 $itemselect = '';
974
975 if ($params['itemtype'] != 'course' && $params['itemtype'] != 'category') {
2be6ca78 976 $itemselect = '<input class="itemselect" type="checkbox" name="select_'.$params['eid'].'" onchange="toggleCategorySelector();"/>';
dc482cfa 977 }
978 return '<td class="cell last selection">' . $itemselect . '</td>';
979 }
980
981 public function is_hidden($mode='simple') {
982 return false;
983 }
984}
985?>