MDL-55584 core_grades: Convert part of set-up screen to template
[moodle.git] / grade / edit / tree / lib.php
CommitLineData
e060e33d 1<?php
e060e33d 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/>.
dc482cfa 16
a153c9f2
AD
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 */
24
dc482cfa 25class grade_edit_tree {
26 public $columns = array();
27
28 /**
39873128 29 * @var grade_tree $gtree @see grade/lib.php
dc482cfa 30 */
31 public $gtree;
32
33 /**
34 * @var grade_plugin_return @see grade/lib.php
35 */
36 public $gpr;
37
38 /**
39 * @var string $moving The eid of the category or item being moved
40 */
41 public $moving;
42
43 public $deepest_level;
44
653a8648 45 public $uses_weight = false;
71297a3f 46
54a007e8 47 public $table;
71297a3f 48
54a007e8 49 public $categories = array();
0e999796
AD
50
51 /**
52 * Show calculator icons next to manual grade items
53 * @var bool $show_calculations
54 */
55 private $show_calculations;
56
dc482cfa 57 /**
58 * Constructor
59 */
60 public function __construct($gtree, $moving=false, $gpr) {
54a007e8 61 global $USER, $OUTPUT, $COURSE;
62
0e999796
AD
63 $systemdefault = get_config('moodle', 'grade_report_showcalculations');
64 $this->show_calculations = get_user_preferences('grade_report_showcalculations', $systemdefault);
65
dc482cfa 66 $this->gtree = $gtree;
67 $this->moving = $moving;
68 $this->gpr = $gpr;
69 $this->deepest_level = $this->get_deepest_level($this->gtree->top_element);
70
bfc8c1d4
MG
71 $this->columns = array(grade_edit_tree_column::factory('name', array('deepest_level' => $this->deepest_level)));
72
653a8648 73 if ($this->uses_weight) {
b6a1e366 74 $this->columns[] = grade_edit_tree_column::factory('weight', array('adv' => 'weight'));
653a8648 75 }
653a8648 76
77 $this->columns[] = grade_edit_tree_column::factory('range'); // This is not a setting... How do we deal with it?
653a8648 78 $this->columns[] = grade_edit_tree_column::factory('actions');
bfc8c1d4
MG
79
80 if ($this->deepest_level > 1) {
81 $this->columns[] = grade_edit_tree_column::factory('select');
82 }
54a007e8 83
54a007e8 84 $this->table = new html_table();
85 $this->table->id = "grade_edit_tree_table";
d75a2367
MG
86 $this->table->attributes['class'] = 'generaltable simple setup-grades';
87 if ($this->moving) {
88 $this->table->attributes['class'] .= ' moving';
89 }
54a007e8 90
91 foreach ($this->columns as $column) {
b79bf7a2 92 if (!($this->moving && $column->hide_when_moving)) {
54a007e8 93 $this->table->head[] = $column->get_header_cell();
94 }
95 }
96
97 $rowcount = 0;
98 $this->table->data = $this->build_html_tree($this->gtree->top_element, true, array(), 0, $rowcount);
dc482cfa 99 }
100
101 /**
102 * Recursive function for building the table holding the grade categories and items,
103 * with CSS indentation and styles.
104 *
1c1f64a2 105 * @param array $element The current tree element being rendered
106 * @param boolean $totals Whether or not to print category grade items (category totals)
107 * @param array $parents An array of parent categories for the current element (used for indentation and row classes)
dc482cfa 108 *
109 * @return string HTML
110 */
54a007e8 111 public function build_html_tree($element, $totals, $parents, $level, &$row_count) {
079b2e52 112 global $CFG, $COURSE, $PAGE, $OUTPUT;
71297a3f 113
dc482cfa 114 $object = $element['object'];
115 $eid = $element['eid'];
41e62a90 116 $object->name = $this->gtree->get_element_header($element, true, true, true, true, true);
dc482cfa 117 $object->stripped_name = $this->gtree->get_element_header($element, false, false, false);
118
119 $is_category_item = false;
120 if ($element['type'] == 'categoryitem' || $element['type'] == 'courseitem') {
121 $is_category_item = true;
122 }
123
54a007e8 124 $rowclasses = array();
dc482cfa 125 foreach ($parents as $parent_eid) {
54a007e8 126 $rowclasses[] = $parent_eid;
dc482cfa 127 }
128
2e3ec2af 129 $moveaction = '';
079b2e52
MG
130 $actionsmenu = new action_menu();
131 $actionsmenu->initialise_js($PAGE);
132 $actionsmenu->set_menu_trigger(get_string('edit'));
133 $actionsmenu->set_owner_selector('grade-item-' . $eid);
134 $actionsmenu->set_alignment(action_menu::TL, action_menu::BL);
135
136 if (!$is_category_item && ($icon = $this->gtree->get_edit_icon($element, $this->gpr, true))) {
137 $actionsmenu->add($icon);
dc482cfa 138 }
5c3b5143
MM
139 // MDL-49281 if grade_item already has calculation, it should be editable even if global setting is off.
140 $type = $element['type'];
141 $iscalculated = ($type == 'item' or $type == 'courseitem' or $type == 'categoryitem') && $object->is_calculated();
142 $icon = $this->gtree->get_calculation_icon($element, $this->gpr, true);
143 if ($iscalculated || ($this->show_calculations && $icon)) {
079b2e52 144 $actionsmenu->add($icon);
0e999796 145 }
dc482cfa 146
147 if ($element['type'] == 'item' or ($element['type'] == 'category' and $element['depth'] > 1)) {
148 if ($this->element_deletable($element)) {
8ae8bf8a 149 $aurl = new moodle_url('index.php', array('id' => $COURSE->id, 'action' => 'delete', 'eid' => $eid, 'sesskey' => sesskey()));
079b2e52
MG
150 $icon = new action_menu_link_secondary($aurl, new pix_icon('t/delete', get_string('delete')), get_string('delete'));
151 $actionsmenu->add($icon);
dc482cfa 152 }
1c1f64a2 153
8ae8bf8a 154 $aurl = new moodle_url('index.php', array('id' => $COURSE->id, 'action' => 'moveselect', 'eid' => $eid, 'sesskey' => sesskey()));
2e3ec2af 155 $moveaction .= $OUTPUT->action_icon($aurl, new pix_icon('t/move', get_string('move')));
dc482cfa 156 }
157
079b2e52
MG
158 if ($icon = $this->gtree->get_hiding_icon($element, $this->gpr, true)) {
159 $actionsmenu->add($icon);
160 }
161
162 if ($icon = $this->gtree->get_reset_icon($element, $this->gpr, true)) {
163 $actionsmenu->add($icon);
164 }
2e3ec2af 165
079b2e52 166 $actions = $OUTPUT->render($actionsmenu);
dc482cfa 167
54a007e8 168 $returnrows = array();
dc482cfa 169 $root = false;
170
dc482cfa 171 $id = required_param('id', PARAM_INT);
172
173 /// prepare move target if needed
174 $last = '';
175
176 /// print the list items now
177 if ($this->moving == $eid) {
dc482cfa 178 // do not diplay children
54a007e8 179 $cell = new html_table_cell();
180 $cell->colspan = 12;
d75a2367
MG
181 $cell->attributes['class'] = $element['type'] . ' moving column-name level' .
182 ($level + 1) . ' level' . ($level % 2 ? 'even' : 'odd');
54a007e8 183 $cell->text = $object->name.' ('.get_string('move').')';
8cea545e 184 return array(new html_table_row(array($cell)));
dc482cfa 185 }
186
187 if ($element['type'] == 'category') {
188 $level++;
54a007e8 189 $this->categories[$object->id] = $object->stripped_name;
dc482cfa 190 $category = grade_category::fetch(array('id' => $object->id));
191 $item = $category->get_grade_item();
192
193 // Add aggregation coef input if not a course item and if parent category has correct aggregation type
82c662d3 194 $dimmed = ($item->is_hidden()) ? 'dimmed_text' : '';
dc482cfa 195
196 // Before we print the category's row, we must find out how many rows will appear below it (for the filler cell's rowspan)
197 $aggregation_position = grade_get_setting($COURSE->id, 'aggregationposition', $CFG->grade_aggregationposition);
198 $category_total_data = null; // Used if aggregationposition is set to "last", so we can print it last
199
54a007e8 200 $html_children = array();
dc482cfa 201
202 $row_count = 0;
203
204 foreach($element['children'] as $child_el) {
54a007e8 205 $moveto = null;
dc482cfa 206
207 if (empty($child_el['object']->itemtype)) {
208 $child_el['object']->itemtype = false;
209 }
210
211 if (($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category') && !$totals) {
212 continue;
213 }
214
54a007e8 215 $child_eid = $child_el['eid'];
dc482cfa 216 $first = '';
217
218 if ($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category') {
1c1f64a2 219 $first = array('first' => 1);
dc482cfa 220 $child_eid = $eid;
221 }
222
223 if ($this->moving && $this->moving != $child_eid) {
224
225 $strmove = get_string('move');
226 $strmovehere = get_string('movehere');
2e3ec2af 227 $actions = $moveaction = ''; // no action icons when moving
dc482cfa 228
8ae8bf8a 229 $aurl = new moodle_url('index.php', array('id' => $COURSE->id, 'action' => 'move', 'eid' => $this->moving, 'moveafter' => $child_eid, 'sesskey' => sesskey()));
1c1f64a2 230 if ($first) {
8ae8bf8a 231 $aurl->params($first);
1c1f64a2 232 }
71297a3f 233
54a007e8 234 $cell = new html_table_cell();
235 $cell->colspan = 12;
d75a2367 236 $cell->attributes['class'] = 'movehere level' . ($level + 1) . ' level' . ($level % 2 ? 'even' : 'odd');
6ef4878b 237
84d32de3
AD
238 $icon = new pix_icon('movehere', $strmovehere, null, array('class'=>'movetarget'));
239 $cell->text = $OUTPUT->action_icon($aurl, $icon);
1c1f64a2 240
8cea545e 241 $moveto = new html_table_row(array($cell));
dc482cfa 242 }
243
244 $newparents = $parents;
245 $newparents[] = $eid;
246
247 $row_count++;
248 $child_row_count = 0;
249
250 // If moving, do not print course and category totals, but still print the moveto target box
251 if ($this->moving && ($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category')) {
54a007e8 252 $html_children[] = $moveto;
dc482cfa 253 } elseif ($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category') {
254 // We don't build the item yet because we first need to know the deepest level of categories (for category/name colspans)
54a007e8 255 $category_total_item = $this->build_html_tree($child_el, $totals, $newparents, $level, $child_row_count);
dc482cfa 256 if (!$aggregation_position) {
54a007e8 257 $html_children = array_merge($html_children, $category_total_item);
dc482cfa 258 }
259 } else {
54a007e8 260 $html_children = array_merge($html_children, $this->build_html_tree($child_el, $totals, $newparents, $level, $child_row_count));
261 if (!empty($moveto)) {
262 $html_children[] = $moveto;
263 }
dc482cfa 264
265 if ($this->moving) {
266 $row_count++;
267 }
268 }
269
270 $row_count += $child_row_count;
271
272 // If the child is a category, increment row_count by one more (for the extra coloured row)
273 if ($child_el['type'] == 'category') {
274 $row_count++;
275 }
276 }
277
278 // Print category total at the end if aggregation position is "last" (1)
279 if (!empty($category_total_item) && $aggregation_position) {
54a007e8 280 $html_children = array_merge($html_children, $category_total_item);
dc482cfa 281 }
282
54a007e8 283 // Determine if we are at the root
dc482cfa 284 if (isset($element['object']->grade_item) && $element['object']->grade_item->is_course_item()) {
dc482cfa 285 $root = true;
286 }
287
d75a2367 288 $levelclass = "level$level level" . ($level % 2 ? 'odd' : 'even');
2be6ca78 289
b189277f 290 $courseclass = '';
291 if ($level == 1) {
292 $courseclass = 'coursecategory';
293 }
71297a3f 294
54a007e8 295 $row = new html_table_row();
079b2e52 296 $row->id = 'grade-item-' . $eid;
16be8974 297 $row->attributes['class'] = $courseclass . ' category ' . $dimmed;
b5233f9b
AN
298 $row->attributes['data-category'] = $eid;
299 $row->attributes['data-itemid'] = $category->get_grade_item()->id;
54a007e8 300 foreach ($rowclasses as $class) {
16be8974 301 $row->attributes['class'] .= ' ' . $class;
54a007e8 302 }
dc482cfa 303
54a007e8 304 $headercell = new html_table_cell();
305 $headercell->header = true;
306 $headercell->scope = 'row';
16be8974 307 $headercell->attributes['title'] = $object->stripped_name;
d75a2367 308 $headercell->attributes['class'] = 'cell column-rowspan rowspan ' . $levelclass;
bd26d932 309 $headercell->rowspan = $row_count + 1;
54a007e8 310 $row->cells[] = $headercell;
dc482cfa 311
312 foreach ($this->columns as $column) {
b79bf7a2 313 if (!($this->moving && $column->hide_when_moving)) {
b5233f9b
AN
314 $row->cells[] = $column->get_category_cell($category, $levelclass, [
315 'id' => $id,
316 'name' => $object->name,
317 'level' => $level,
318 'actions' => $actions,
319 'moveaction' => $moveaction,
320 'eid' => $eid,
321 ]);
dc482cfa 322 }
323 }
71297a3f 324
54a007e8 325 $returnrows[] = $row;
dc482cfa 326
54a007e8 327 $returnrows = array_merge($returnrows, $html_children);
dc482cfa 328
6ef4878b 329 // Print a coloured row to show the end of the category across the table
54a007e8 330 $endcell = new html_table_cell();
331 $endcell->colspan = (19 - $level);
d75a2367 332 $endcell->attributes['class'] = 'emptyrow colspan ' . $levelclass;
54a007e8 333
0e35ba6f 334 $returnrows[] = new html_table_row(array($endcell));
dc482cfa 335
336 } else { // Dealing with a grade item
337
338 $item = grade_item::fetch(array('id' => $object->id));
339 $element['type'] = 'item';
340 $element['object'] = $item;
2be6ca78 341
b189277f 342 $categoryitemclass = '';
343 if ($item->itemtype == 'category') {
344 $categoryitemclass = 'categoryitem';
345 }
d75a2367
MG
346 if ($item->itemtype == 'course') {
347 $categoryitemclass = 'courseitem';
348 }
dc482cfa 349
54a007e8 350 $dimmed = ($item->is_hidden()) ? "dimmed_text" : "";
351 $gradeitemrow = new html_table_row();
079b2e52 352 $gradeitemrow->id = 'grade-item-' . $eid;
16be8974 353 $gradeitemrow->attributes['class'] = $categoryitemclass . ' item ' . $dimmed;
b5233f9b 354 $gradeitemrow->attributes['data-itemid'] = $object->id;
54a007e8 355 foreach ($rowclasses as $class) {
16be8974 356 $gradeitemrow->attributes['class'] .= ' ' . $class;
54a007e8 357 }
dc482cfa 358
359 foreach ($this->columns as $column) {
b79bf7a2 360 if (!($this->moving && $column->hide_when_moving)) {
2e3ec2af
MG
361 $gradeitemrow->cells[] = $column->get_item_cell($item, array('id' => $id, 'name' => $object->name,
362 'level' => $level, 'actions' => $actions, 'element' => $element, 'eid' => $eid,
363 'moveaction' => $moveaction, 'itemtype' => $object->itemtype));
dc482cfa 364 }
365 }
366
54a007e8 367 $returnrows[] = $gradeitemrow;
dc482cfa 368 }
369
54a007e8 370 return $returnrows;
dc482cfa 371
372 }
373
374 /**
375 * Given a grade_item object, returns a labelled input if an aggregation coefficient (weight or extra credit) applies to it.
376 * @param grade_item $item
377 * @return string HTML
378 */
bfc8c1d4 379 static function get_weight_input($item) {
1c1f64a2 380 global $OUTPUT;
381
dc482cfa 382 if (!is_object($item) || get_class($item) !== 'grade_item') {
383 throw new Exception('grade_edit_tree::get_weight_input($item) was given a variable that is not of the required type (grade_item object)');
384 return false;
385 }
386
387 if ($item->is_course_item()) {
388 return '';
389 }
390
391 $parent_category = $item->get_parent_category();
dc482cfa 392 $parent_category->apply_forced_settings();
653a8648 393 $aggcoef = $item->get_coefstring();
dc482cfa 394
b0c91d72
FM
395 $itemname = $item->itemname;
396 if ($item->is_category_item()) {
397 // Remember, the parent category of a category item is the category itself.
398 $itemname = $parent_category->get_name();
399 }
bfc8c1d4
MG
400 $str = '';
401
402 if ($aggcoef == 'aggregationcoefweight' || $aggcoef == 'aggregationcoef' || $aggcoef == 'aggregationcoefextraweight') {
b5233f9b 403
d68a9ece
FM
404 return $OUTPUT->render_from_template('core_grades/weight_field', [
405 'id' => $item->id,
406 'itemname' => $itemname,
407 'value' => self::format_number($item->aggregationcoef)
b5233f9b
AN
408 ]);
409
d68a9ece
FM
410 } else if ($aggcoef == 'aggregationcoefextraweightsum') {
411
412 $tpldata = [
413 'id' => $item->id,
414 'itemname' => $itemname,
415 'value' => grade_edit_tree::format_number($item->aggregationcoef2 * 100.0),
416 'checked' => $item->weightoverride,
417 'disabled' => !$item->weightoverride
418 ];
419 $str .= $OUTPUT->render_from_template('core_grades/weight_override_field', $tpldata);
200e5173 420
dc482cfa 421 }
bfc8c1d4
MG
422
423 return $str;
cd3a391c 424 }
dc482cfa 425
53914e44
MN
426 // Trims trailing zeros.
427 // Used on the 'Gradebook setup' page for grade items settings like aggregation co-efficient.
428 // Grader report has its own decimal place settings so they are handled elsewhere.
e2bb3c92 429 static function format_number($number) {
4592df46 430 $formatted = rtrim(format_float($number, 4),'0');
dacd5daa 431 if (substr($formatted, -1)==get_string('decsep', 'langconfig')) { //if last char is the decimal point
4592df46
AD
432 $formatted .= '0';
433 }
434 return $formatted;
3cf33f51
AD
435 }
436
dc482cfa 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;
445
446 if ($element['type'] != 'item') {
447 return true;
448 }
449
450 $grade_item = $element['object'];
451
452 if ($grade_item->itemtype != 'mod' or $grade_item->is_outcome_item() or $grade_item->gradetype == GRADE_TYPE_NONE) {
453 return true;
454 }
455
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 }
461
462 return false;
463 }
464
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);
475
476 if (!is_array($eids)) {
477 $eids = array($eids);
478 }
479
ddb9c95f 480 if(!$after_el = $this->gtree->locate_element("cg$moveafter")) {
dc482cfa 481 print_error('invalidelementid', '', $returnurl);
482 }
483
484 $after = $after_el['object'];
485 $parent = $after;
486 $sortorder = $after->get_sortorder();
487
488 foreach ($eids as $eid) {
489 if (!$element = $this->gtree->locate_element($eid)) {
490 print_error('invalidelementid', '', $returnurl);
491 }
492 $object = $element['object'];
493
494 $object->set_parent($parent->id);
495 $object->move_after_sortorder($sortorder);
4d2f7923 496 $sortorder++;
dc482cfa 497 }
498
499 redirect($returnurl, '', 0);
500 }
501
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'];
514
515 $level++;
653a8648 516 $coefstring = $element['object']->get_coefstring();
dc482cfa 517 if ($element['type'] == 'category') {
bfc8c1d4
MG
518 if ($coefstring == 'aggregationcoefweight' || $coefstring == 'aggregationcoefextraweightsum' ||
519 $coefstring == 'aggregationcoefextraweight') {
520 $this->uses_weight = true;
677bc073 521 }
653a8648 522
dc482cfa 523 foreach($element['children'] as $child_el) {
524 if ($level > $deepest_level) {
525 $deepest_level = $level;
526 }
527 $deepest_level = $this->get_deepest_level($child_el, $level, $deepest_level);
528 }
825a3fa9
JP
529
530 $category = grade_category::fetch(array('id' => $object->id));
531 $item = $category->get_grade_item();
532 if ($item->gradetype == GRADE_TYPE_NONE) {
533 // Add 1 more level for grade category that has no total.
534 $deepest_level++;
535 }
dc482cfa 536 }
537
538 return $deepest_level;
539 }
540}
541
b79bf7a2
MG
542/**
543 * Class grade_edit_tree_column
544 *
545 * @package core_grades
546 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
547 */
dc482cfa 548abstract class grade_edit_tree_column {
549 public $forced;
550 public $hidden;
551 public $forced_hidden;
552 public $advanced_hidden;
553 public $hide_when_moving = true;
54a007e8 554 /**
555 * html_table_cell object used as a template for header cells in all categories.
556 * It must be cloned before being used.
557 * @var html_table_cell $headercell
558 */
559 public $headercell;
560 /**
561 * html_table_cell object used as a template for category cells in all categories.
562 * It must be cloned before being used.
563 * @var html_table_cell $categorycell
564 */
565 public $categorycell;
566 /**
567 * html_table_cell object used as a template for item cells in all categories.
568 * It must be cloned before being used.
569 * @var html_table_cell $itemcell
570 */
571 public $itemcell;
dc482cfa 572
573 public static function factory($name, $params=array()) {
574 $class_name = "grade_edit_tree_column_$name";
575 if (class_exists($class_name)) {
576 return new $class_name($params);
577 }
578 }
579
580 public abstract function get_header_cell();
581
d75a2367
MG
582 public function get_category_cell($category, $levelclass, $params) {
583 $cell = clone($this->categorycell);
584 $cell->attributes['class'] .= ' ' . $levelclass;
585 $cell->attributes['text'] = '';
586 return $cell;
587 }
dc482cfa 588
d75a2367
MG
589 public function get_item_cell($item, $params) {
590 $cell = clone($this->itemcell);
591 $cell->attributes['text'] = '';
592 if (isset($params['level'])) {
593 $level = $params['level'] + (($item->itemtype == 'category' || $item->itemtype == 'course') ? 0 : 1);
594 $cell->attributes['class'] .= ' level' . $level;
595 $cell->attributes['class'] .= ' level' . ($level % 2 ? 'odd' : 'even');
596 }
597 return $cell;
598 }
dc482cfa 599
54a007e8 600 public function __construct() {
601 $this->headercell = new html_table_cell();
602 $this->headercell->header = true;
16be8974 603 $this->headercell->attributes['class'] = 'header';
54a007e8 604
605 $this->categorycell = new html_table_cell();
16be8974 606 $this->categorycell->attributes['class'] = 'cell';
54a007e8 607
608 $this->itemcell = new html_table_cell();
16be8974 609 $this->itemcell->attributes['class'] = 'cell';
d75a2367
MG
610
611 if (preg_match('/^grade_edit_tree_column_(\w*)$/', get_class($this), $matches)) {
612 $this->headercell->attributes['class'] .= ' column-' . $matches[1];
613 $this->categorycell->attributes['class'] .= ' column-' . $matches[1];
614 $this->itemcell->attributes['class'] .= ' column-' . $matches[1];
615 }
54a007e8 616 }
dc482cfa 617}
618
b79bf7a2
MG
619/**
620 * Class grade_edit_tree_column_name
621 *
622 * @package core_grades
623 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
624 */
dc482cfa 625class grade_edit_tree_column_name extends grade_edit_tree_column {
626 public $forced = false;
627 public $hidden = false;
628 public $forced_hidden = false;
629 public $advanced_hidden = false;
630 public $deepest_level = 1;
631 public $hide_when_moving = false;
632
633 public function __construct($params) {
634 if (empty($params['deepest_level'])) {
635 throw new Exception('Tried to instantiate a grade_edit_tree_column_name object without the "deepest_level" param!');
636 }
637
638 $this->deepest_level = $params['deepest_level'];
54a007e8 639 parent::__construct();
dc482cfa 640 }
641
642 public function get_header_cell() {
54a007e8 643 $headercell = clone($this->headercell);
54a007e8 644 $headercell->colspan = $this->deepest_level + 1;
645 $headercell->text = get_string('name');
646 return $headercell;
dc482cfa 647 }
648
649 public function get_category_cell($category, $levelclass, $params) {
54a007e8 650 global $OUTPUT;
dc482cfa 651 if (empty($params['name']) || empty($params['level'])) {
652 throw new Exception('Array key (name or level) missing from 3rd param of grade_edit_tree_column_name::get_category_cell($category, $levelclass, $params)');
653 }
2e3ec2af 654 $moveaction = isset($params['moveaction']) ? $params['moveaction'] : '';
d75a2367 655 $categorycell = parent::get_category_cell($category, $levelclass, $params);
54a007e8 656 $categorycell->colspan = ($this->deepest_level +1) - $params['level'];
2e3ec2af 657 $categorycell->text = $OUTPUT->heading($moveaction . $params['name'], 4);
54a007e8 658 return $categorycell;
dc482cfa 659 }
660
661 public function get_item_cell($item, $params) {
662 global $CFG;
663
664 if (empty($params['element']) || empty($params['name']) || empty($params['level'])) {
665 throw new Exception('Array key (name, level or element) missing from 2nd param of grade_edit_tree_column_name::get_item_cell($item, $params)');
666 }
667
668 $name = $params['name'];
2e3ec2af 669 $moveaction = isset($params['moveaction']) ? $params['moveaction'] : '';
dc482cfa 670
d75a2367 671 $itemcell = parent::get_item_cell($item, $params);
54a007e8 672 $itemcell->colspan = ($this->deepest_level + 1) - $params['level'];
2e3ec2af 673 $itemcell->text = $moveaction . $name;
54a007e8 674 return $itemcell;
dc482cfa 675 }
dc482cfa 676}
677
b79bf7a2
MG
678/**
679 * Class grade_edit_tree_column_weight
680 *
681 * @package core_grades
682 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
683 */
653a8648 684class grade_edit_tree_column_weight extends grade_edit_tree_column {
dc482cfa 685
686 public function get_header_cell() {
c3b834b4 687 global $OUTPUT;
54a007e8 688 $headercell = clone($this->headercell);
bfe969e8 689 $headercell->text = get_string('weights', 'grades').$OUTPUT->help_icon('aggregationcoefweight', 'grades');
54a007e8 690 return $headercell;
dc482cfa 691 }
692
693 public function get_category_cell($category, $levelclass, $params) {
694
695 $item = $category->get_grade_item();
d75a2367 696 $categorycell = parent::get_category_cell($category, $levelclass, $params);
bfc8c1d4 697 $categorycell->text = grade_edit_tree::get_weight_input($item);
54a007e8 698 return $categorycell;
dc482cfa 699 }
700
701 public function get_item_cell($item, $params) {
8b5c4545 702 global $CFG;
dc482cfa 703 if (empty($params['element'])) {
704 throw new Exception('Array key (element) missing from 2nd param of grade_edit_tree_column_weightorextracredit::get_item_cell($item, $params)');
705 }
d75a2367 706 $itemcell = parent::get_item_cell($item, $params);
54a007e8 707 $itemcell->text = '&nbsp;';
6aeebc25 708 $object = $params['element']['object'];
dc482cfa 709
6aeebc25
FM
710 if (!in_array($object->itemtype, array('courseitem', 'categoryitem', 'category'))
711 && !in_array($object->gradetype, array(GRADE_TYPE_NONE, GRADE_TYPE_TEXT))
8b5c4545
FM
712 && (!$object->is_outcome_item() || $object->load_parent_category()->aggregateoutcomes)
713 && ($object->gradetype != GRADE_TYPE_SCALE || !empty($CFG->grade_includescalesinaggregation))) {
bfc8c1d4 714 $itemcell->text = grade_edit_tree::get_weight_input($item);
dc482cfa 715 }
716
54a007e8 717 return $itemcell;
dc482cfa 718 }
dc482cfa 719}
720
b79bf7a2
MG
721/**
722 * Class grade_edit_tree_column_range
723 *
724 * @package core_grades
725 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
726 */
dc482cfa 727class grade_edit_tree_column_range extends grade_edit_tree_column {
728
729 public function get_header_cell() {
54a007e8 730 $headercell = clone($this->headercell);
731 $headercell->text = get_string('maxgrade', 'grades');
732 return $headercell;
dc482cfa 733 }
734
735 public function get_category_cell($category, $levelclass, $params) {
d75a2367 736 $categorycell = parent::get_category_cell($category, $levelclass, $params);
54a007e8 737 $categorycell->text = ' - ';
738 return $categorycell;
dc482cfa 739 }
740
741 public function get_item_cell($item, $params) {
1c1f64a2 742 global $DB, $OUTPUT;
46409b20 743
b5e00814 744 // If the parent aggregation is Natural, we should show the number, even for scales, as that value is used...
e3a65c9e 745 // ...in the computation. For text grades, the grademax is not used, so we can still show the no value string.
3f874a9d 746 $parentcat = $item->get_parent_category();
e3a65c9e
EM
747 if ($item->gradetype == GRADE_TYPE_TEXT) {
748 $grademax = ' - ';
bfc8c1d4 749 } else if ($item->gradetype == GRADE_TYPE_SCALE) {
3998cc5c 750 $scale = $DB->get_record('scale', array('id' => $item->scaleid));
e2a84477
AD
751 $scale_items = null;
752 if (empty($scale)) { //if the item is using a scale that's been removed
753 $scale_items = array();
754 } else {
755 $scale_items = explode(',', $scale->scale);
756 }
3f874a9d 757 if ($parentcat->aggregation == GRADE_AGGREGATE_SUM) {
290ddb73
MG
758 $grademax = end($scale_items) . ' (' .
759 format_float($item->grademax, $item->get_decimals()) . ')';
760 } else {
761 $grademax = end($scale_items) . ' (' . count($scale_items) . ')';
762 }
dc482cfa 763 } else {
bfc8c1d4 764 $grademax = format_float($item->grademax, $item->get_decimals());
dc482cfa 765 }
766
3f874a9d
EM
767 $isextracredit = false;
768 if ($item->aggregationcoef > 0) {
769 // For category grade items, we need the grandparent category.
770 // The parent is just category the grade item represents.
771 if ($item->is_category_item()) {
772 $grandparentcat = $parentcat->get_parent_category();
773 if ($grandparentcat->is_extracredit_used()) {
774 $isextracredit = true;
775 }
776 } else if ($parentcat->is_extracredit_used()) {
777 $isextracredit = true;
778 }
779 }
780 if ($isextracredit) {
3712ee3e
ZD
781 $grademax .= ' ' . html_writer::tag('abbr', get_string('aggregationcoefextrasumabbr', 'grades'),
782 array('title' => get_string('aggregationcoefextrasum', 'grades')));
783 }
784
d75a2367 785 $itemcell = parent::get_item_cell($item, $params);
54a007e8 786 $itemcell->text = $grademax;
787 return $itemcell;
dc482cfa 788 }
dc482cfa 789}
790
b79bf7a2
MG
791/**
792 * Class grade_edit_tree_column_actions
793 *
794 * @package core_grades
795 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
796 */
dc482cfa 797class grade_edit_tree_column_actions extends grade_edit_tree_column {
798
799 public function __construct($params) {
71297a3f 800 parent::__construct();
dc482cfa 801 }
802
803 public function get_header_cell() {
54a007e8 804 $headercell = clone($this->headercell);
54a007e8 805 $headercell->text = get_string('actions');
806 return $headercell;
dc482cfa 807 }
808
809 public function get_category_cell($category, $levelclass, $params) {
810
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 }
814
d75a2367 815 $categorycell = parent::get_category_cell($category, $levelclass, $params);
54a007e8 816 $categorycell->text = $params['actions'];
817 return $categorycell;
dc482cfa 818 }
819
820 public function get_item_cell($item, $params) {
821 if (empty($params['actions'])) {
822 throw new Exception('Array key (actions) missing from 2nd param of grade_edit_tree_column_actions::get_item_cell($item, $params)');
823 }
d75a2367 824 $itemcell = parent::get_item_cell($item, $params);
54a007e8 825 $itemcell->text = $params['actions'];
826 return $itemcell;
dc482cfa 827 }
dc482cfa 828}
829
b79bf7a2
MG
830/**
831 * Class grade_edit_tree_column_select
832 *
833 * @package core_grades
834 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
835 */
dc482cfa 836class grade_edit_tree_column_select extends grade_edit_tree_column {
837
838 public function get_header_cell() {
54a007e8 839 $headercell = clone($this->headercell);
54a007e8 840 $headercell->text = get_string('select');
841 return $headercell;
dc482cfa 842 }
843
844 public function get_category_cell($category, $levelclass, $params) {
dc482cfa 845 if (empty($params['eid'])) {
846 throw new Exception('Array key (eid) missing from 3rd param of grade_edit_tree_column_select::get_category_cell($category, $levelclass, $params)');
847 }
b5233f9b
AN
848 $selectall = html_writer::link('#', get_string('all'), [
849 'data-action' => 'grade_edittree-index-bulkselect',
850 'data-checked' => true,
851 ]);
852 $selectnone = html_writer::link('#', get_string('none'), [
853 'data-action' => 'grade_edittree-index-bulkselect',
854 'data-checked' => false,
855 ]);
dc482cfa 856
d75a2367 857 $categorycell = parent::get_category_cell($category, $levelclass, $params);
b5233f9b 858 $categorycell->text = $selectall . ' / ' . $selectnone;
54a007e8 859 return $categorycell;
dc482cfa 860 }
861
862 public function get_item_cell($item, $params) {
54a007e8 863 if (empty($params['itemtype']) || empty($params['eid'])) {
2149326b 864 print_error('missingitemtypeoreid', 'core_grades');
54a007e8 865 }
d75a2367 866 $itemcell = parent::get_item_cell($item, $params);
54a007e8 867
868 if ($params['itemtype'] != 'course' && $params['itemtype'] != 'category') {
6723bc66 869 $itemcell->text = '<label class="accesshide" for="select_'.$params['eid'].'">'.
7400be1b 870 get_string('select', 'grades', $item->itemname).'</label>
ef93efbf 871 <input class="itemselect ignoredirty" type="checkbox" name="select_'.$params['eid'].'" id="select_'.$params['eid'].
b5233f9b 872 '"/>';
dc482cfa 873 }
6723bc66 874 return $itemcell;
dc482cfa 875 }
dc482cfa 876}
6c3ef410 877