MDL-47062 Grades: Add an upgrade step to warn about changes in upgrade.
[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
bfc8c1d4
MG
47 /** @var bool indicates if tree has categories with aggregation method other than Natural. */
48 protected $uses_non_natural = false;
49
54a007e8 50 public $table;
71297a3f 51
54a007e8 52 public $categories = array();
0e999796
AD
53
54 /**
55 * Show calculator icons next to manual grade items
56 * @var bool $show_calculations
57 */
58 private $show_calculations;
59
dc482cfa 60 /**
61 * Constructor
62 */
63 public function __construct($gtree, $moving=false, $gpr) {
54a007e8 64 global $USER, $OUTPUT, $COURSE;
65
0e999796
AD
66 $systemdefault = get_config('moodle', 'grade_report_showcalculations');
67 $this->show_calculations = get_user_preferences('grade_report_showcalculations', $systemdefault);
68
dc482cfa 69 $this->gtree = $gtree;
70 $this->moving = $moving;
71 $this->gpr = $gpr;
72 $this->deepest_level = $this->get_deepest_level($this->gtree->top_element);
73
bfc8c1d4
MG
74 $this->columns = array(grade_edit_tree_column::factory('name', array('deepest_level' => $this->deepest_level)));
75
76 if ($this->uses_non_natural) {
77 $this->columns[] = grade_edit_tree_column::factory('aggregation', array('flag' => true));
78 }
653a8648 79
80 if ($this->uses_weight) {
b6a1e366 81 $this->columns[] = grade_edit_tree_column::factory('weight', array('adv' => 'weight'));
653a8648 82 }
653a8648 83
84 $this->columns[] = grade_edit_tree_column::factory('range'); // This is not a setting... How do we deal with it?
653a8648 85 $this->columns[] = grade_edit_tree_column::factory('actions');
bfc8c1d4
MG
86
87 if ($this->deepest_level > 1) {
88 $this->columns[] = grade_edit_tree_column::factory('select');
89 }
54a007e8 90
54a007e8 91 $this->table = new html_table();
92 $this->table->id = "grade_edit_tree_table";
912d1990 93 $this->table->attributes['class'] = 'generaltable simple';
54a007e8 94
95 foreach ($this->columns as $column) {
b79bf7a2 96 if (!($this->moving && $column->hide_when_moving)) {
54a007e8 97 $this->table->head[] = $column->get_header_cell();
98 }
99 }
100
101 $rowcount = 0;
102 $this->table->data = $this->build_html_tree($this->gtree->top_element, true, array(), 0, $rowcount);
dc482cfa 103 }
104
105 /**
106 * Recursive function for building the table holding the grade categories and items,
107 * with CSS indentation and styles.
108 *
1c1f64a2 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)
dc482cfa 112 *
113 * @return string HTML
114 */
54a007e8 115 public function build_html_tree($element, $totals, $parents, $level, &$row_count) {
6b608f8f 116 global $CFG, $COURSE, $USER, $OUTPUT;
71297a3f 117
dc482cfa 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);
122
123 $is_category_item = false;
124 if ($element['type'] == 'categoryitem' || $element['type'] == 'courseitem') {
125 $is_category_item = true;
126 }
127
54a007e8 128 $rowclasses = array();
dc482cfa 129 foreach ($parents as $parent_eid) {
54a007e8 130 $rowclasses[] = $parent_eid;
dc482cfa 131 }
132
133 $actions = '';
2e3ec2af 134 $moveaction = '';
dc482cfa 135
136 if (!$is_category_item) {
137 $actions .= $this->gtree->get_edit_icon($element, $this->gpr);
138 }
139
0e999796
AD
140 if ($this->show_calculations) {
141 $actions .= $this->gtree->get_calculation_icon($element, $this->gpr);
142 }
dc482cfa 143
144 if ($element['type'] == 'item' or ($element['type'] == 'category' and $element['depth'] > 1)) {
145 if ($this->element_deletable($element)) {
8ae8bf8a 146 $aurl = new moodle_url('index.php', array('id' => $COURSE->id, 'action' => 'delete', 'eid' => $eid, 'sesskey' => sesskey()));
c63923bd 147 $actions .= $OUTPUT->action_icon($aurl, new pix_icon('t/delete', get_string('delete')));
dc482cfa 148 }
1c1f64a2 149
8ae8bf8a 150 $aurl = new moodle_url('index.php', array('id' => $COURSE->id, 'action' => 'moveselect', 'eid' => $eid, 'sesskey' => sesskey()));
2e3ec2af 151 $moveaction .= $OUTPUT->action_icon($aurl, new pix_icon('t/move', get_string('move')));
dc482cfa 152 }
153
154 $actions .= $this->gtree->get_hiding_icon($element, $this->gpr);
2e3ec2af 155
ae93f353 156 $actions .= $this->gtree->get_reset_icon($element, $this->gpr);
dc482cfa 157
54a007e8 158 $returnrows = array();
dc482cfa 159 $root = false;
160
dc482cfa 161 $id = required_param('id', PARAM_INT);
162
163 /// prepare move target if needed
164 $last = '';
165
166 /// print the list items now
167 if ($this->moving == $eid) {
dc482cfa 168 // do not diplay children
54a007e8 169 $cell = new html_table_cell();
170 $cell->colspan = 12;
16be8974 171 $cell->attributes['class'] = $element['type'] . ' moving';
54a007e8 172 $cell->text = $object->name.' ('.get_string('move').')';
8cea545e 173 return array(new html_table_row(array($cell)));
dc482cfa 174 }
175
176 if ($element['type'] == 'category') {
177 $level++;
54a007e8 178 $this->categories[$object->id] = $object->stripped_name;
dc482cfa 179 $category = grade_category::fetch(array('id' => $object->id));
180 $item = $category->get_grade_item();
181
182 // Add aggregation coef input if not a course item and if parent category has correct aggregation type
54a007e8 183 $dimmed = ($item->is_hidden()) ? 'dimmed' : '';
dc482cfa 184
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
188
54a007e8 189 $html_children = array();
dc482cfa 190
191 $row_count = 0;
192
193 foreach($element['children'] as $child_el) {
54a007e8 194 $moveto = null;
dc482cfa 195
196 if (empty($child_el['object']->itemtype)) {
197 $child_el['object']->itemtype = false;
198 }
199
200 if (($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category') && !$totals) {
201 continue;
202 }
203
54a007e8 204 $child_eid = $child_el['eid'];
dc482cfa 205 $first = '';
206
207 if ($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category') {
1c1f64a2 208 $first = array('first' => 1);
dc482cfa 209 $child_eid = $eid;
210 }
211
212 if ($this->moving && $this->moving != $child_eid) {
213
214 $strmove = get_string('move');
215 $strmovehere = get_string('movehere');
2e3ec2af 216 $actions = $moveaction = ''; // no action icons when moving
dc482cfa 217
8ae8bf8a 218 $aurl = new moodle_url('index.php', array('id' => $COURSE->id, 'action' => 'move', 'eid' => $this->moving, 'moveafter' => $child_eid, 'sesskey' => sesskey()));
1c1f64a2 219 if ($first) {
8ae8bf8a 220 $aurl->params($first);
1c1f64a2 221 }
71297a3f 222
54a007e8 223 $cell = new html_table_cell();
224 $cell->colspan = 12;
6ef4878b 225
84d32de3
AD
226 $icon = new pix_icon('movehere', $strmovehere, null, array('class'=>'movetarget'));
227 $cell->text = $OUTPUT->action_icon($aurl, $icon);
1c1f64a2 228
8cea545e 229 $moveto = new html_table_row(array($cell));
dc482cfa 230 }
231
232 $newparents = $parents;
233 $newparents[] = $eid;
234
235 $row_count++;
236 $child_row_count = 0;
237
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')) {
54a007e8 240 $html_children[] = $moveto;
dc482cfa 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)
54a007e8 243 $category_total_item = $this->build_html_tree($child_el, $totals, $newparents, $level, $child_row_count);
dc482cfa 244 if (!$aggregation_position) {
54a007e8 245 $html_children = array_merge($html_children, $category_total_item);
dc482cfa 246 }
247 } else {
54a007e8 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 }
dc482cfa 252
253 if ($this->moving) {
254 $row_count++;
255 }
256 }
257
258 $row_count += $child_row_count;
259
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 }
265
266 // Print category total at the end if aggregation position is "last" (1)
267 if (!empty($category_total_item) && $aggregation_position) {
54a007e8 268 $html_children = array_merge($html_children, $category_total_item);
dc482cfa 269 }
270
54a007e8 271 // Determine if we are at the root
dc482cfa 272 if (isset($element['object']->grade_item) && $element['object']->grade_item->is_course_item()) {
dc482cfa 273 $root = true;
274 }
275
54a007e8 276 $levelclass = "level$level";
2be6ca78 277
b189277f 278 $courseclass = '';
279 if ($level == 1) {
280 $courseclass = 'coursecategory';
281 }
71297a3f 282
54a007e8 283 $row = new html_table_row();
16be8974 284 $row->attributes['class'] = $courseclass . ' category ' . $dimmed;
54a007e8 285 foreach ($rowclasses as $class) {
16be8974 286 $row->attributes['class'] .= ' ' . $class;
54a007e8 287 }
dc482cfa 288
54a007e8 289 $headercell = new html_table_cell();
290 $headercell->header = true;
291 $headercell->scope = 'row';
16be8974
DM
292 $headercell->attributes['title'] = $object->stripped_name;
293 $headercell->attributes['class'] = 'cell rowspan ' . $levelclass;
bd26d932 294 $headercell->rowspan = $row_count + 1;
54a007e8 295 $row->cells[] = $headercell;
dc482cfa 296
297 foreach ($this->columns as $column) {
b79bf7a2 298 if (!($this->moving && $column->hide_when_moving)) {
2e3ec2af
MG
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));
dc482cfa 302 }
303 }
71297a3f 304
54a007e8 305 $returnrows[] = $row;
dc482cfa 306
54a007e8 307 $returnrows = array_merge($returnrows, $html_children);
dc482cfa 308
6ef4878b 309 // Print a coloured row to show the end of the category across the table
54a007e8 310 $endcell = new html_table_cell();
311 $endcell->colspan = (19 - $level);
16be8974 312 $endcell->attributes['class'] = 'colspan ' . $levelclass;
54a007e8 313
0e35ba6f 314 $returnrows[] = new html_table_row(array($endcell));
dc482cfa 315
316 } else { // Dealing with a grade item
317
318 $item = grade_item::fetch(array('id' => $object->id));
319 $element['type'] = 'item';
320 $element['object'] = $item;
2be6ca78 321
b189277f 322 $categoryitemclass = '';
323 if ($item->itemtype == 'category') {
324 $categoryitemclass = 'categoryitem';
325 }
dc482cfa 326
54a007e8 327 $dimmed = ($item->is_hidden()) ? "dimmed_text" : "";
328 $gradeitemrow = new html_table_row();
16be8974 329 $gradeitemrow->attributes['class'] = $categoryitemclass . ' item ' . $dimmed;
54a007e8 330 foreach ($rowclasses as $class) {
16be8974 331 $gradeitemrow->attributes['class'] .= ' ' . $class;
54a007e8 332 }
dc482cfa 333
334 foreach ($this->columns as $column) {
b79bf7a2 335 if (!($this->moving && $column->hide_when_moving)) {
2e3ec2af
MG
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));
dc482cfa 339 }
340 }
341
54a007e8 342 $returnrows[] = $gradeitemrow;
dc482cfa 343 }
344
54a007e8 345 return $returnrows;
dc482cfa 346
347 }
348
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 */
bfc8c1d4 354 static function get_weight_input($item) {
1c1f64a2 355 global $OUTPUT;
356
dc482cfa 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 }
361
362 if ($item->is_course_item()) {
363 return '';
364 }
365
366 $parent_category = $item->get_parent_category();
dc482cfa 367 $parent_category->apply_forced_settings();
653a8648 368 $aggcoef = $item->get_coefstring();
dc482cfa 369
b0c91d72
FM
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 }
bfc8c1d4
MG
375 $str = '';
376
377 if ($aggcoef == 'aggregationcoefweight' || $aggcoef == 'aggregationcoef' || $aggcoef == 'aggregationcoefextraweight') {
b6a1e366 378 return '<label class="accesshide" for="weight_'.$item->id.'">'.
b0c91d72 379 get_string('extracreditvalue', 'grades', $itemname).'</label>'.
351c8e08 380 '<input type="text" size="6" id="weight_'.$item->id.'" name="weight_'.$item->id.'"
3cf33f51 381 value="'.grade_edit_tree::format_number($item->aggregationcoef).'" />';
bfc8c1d4 382 } else if ($aggcoef == 'aggregationcoefextraweightsum') {
6c37563e
FM
383
384 $checkboxname = 'weightoverride_' . $item->id;
b0c91d72 385 $checkboxlbl = html_writer::tag('label', get_string('overrideweightofa', 'grades', $itemname),
6c37563e
FM
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)));
200e5173 392
b6a1e366 393 $name = 'weight_' . $item->id;
200e5173
JC
394 $hiddenlabel = html_writer::tag(
395 'label',
b0c91d72 396 get_string('weightofa', 'grades', $itemname),
200e5173
JC
397 array(
398 'class' => 'accesshide',
399 'for' => $name
400 )
401 );
402
403 $input = html_writer::empty_tag(
404 'input',
405 array(
406 'type' => 'text',
407 'size' => 6,
408 'id' => $name,
409 'name' => $name,
6c37563e
FM
410 'value' => grade_edit_tree::format_number($item->aggregationcoef2 * 100.0),
411 'disabled' => ($item->weightoverride ? null : 'disabled')
200e5173
JC
412 )
413 );
414
6c37563e 415 $str .= $checkboxlbl . $checkbox . $hiddenlabel . $input;
bfc8c1d4
MG
416
417 if ($item->aggregationcoef > 0) {
418 $str .= ' ' . html_writer::tag('abbr', get_string('aggregationcoefextrasumabbr', 'grades'),
419 array('title' => get_string('aggregationcoefextrasum', 'grades')));
420 }
dc482cfa 421 }
bfc8c1d4
MG
422
423 return $str;
cd3a391c 424 }
dc482cfa 425
6ef4878b 426 //Trims trailing zeros
4592df46 427 //Used on the 'categories and items' page for grade items settings like aggregation co-efficient
e229506d 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 ($element['object']->aggregation != GRADE_AGGREGATE_SUM) {
519 $this->uses_non_natural = true;
653a8648 520 }
bfc8c1d4
MG
521
522 if ($coefstring == 'aggregationcoefweight' || $coefstring == 'aggregationcoefextraweightsum' ||
523 $coefstring == 'aggregationcoefextraweight') {
524 $this->uses_weight = true;
677bc073 525 }
653a8648 526
dc482cfa 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 }
534
535 return $deepest_level;
536 }
537}
538
b79bf7a2
MG
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 */
dc482cfa 545abstract 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;
54a007e8 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;
dc482cfa 569
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 }
576
577 public abstract function get_header_cell();
578
579 public abstract function get_category_cell($category, $levelclass, $params);
580
581 public abstract function get_item_cell($item, $params);
582
54a007e8 583 public function __construct() {
584 $this->headercell = new html_table_cell();
585 $this->headercell->header = true;
586 $this->headercell->style = 'whitespace: normal;';
16be8974 587 $this->headercell->attributes['class'] = 'header';
54a007e8 588
589 $this->categorycell = new html_table_cell();
16be8974 590 $this->categorycell->attributes['class'] = 'cell';
54a007e8 591
592 $this->itemcell = new html_table_cell();
16be8974 593 $this->itemcell->attributes['class'] = 'cell';
54a007e8 594 }
dc482cfa 595}
596
b79bf7a2
MG
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 */
dc482cfa 603class 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;
610
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 }
615
616 $this->deepest_level = $params['deepest_level'];
54a007e8 617 parent::__construct();
dc482cfa 618 }
619
620 public function get_header_cell() {
54a007e8 621 $headercell = clone($this->headercell);
16be8974 622 $headercell->attributes['class'] .= ' name';
54a007e8 623 $headercell->colspan = $this->deepest_level + 1;
624 $headercell->text = get_string('name');
625 return $headercell;
dc482cfa 626 }
627
628 public function get_category_cell($category, $levelclass, $params) {
54a007e8 629 global $OUTPUT;
dc482cfa 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 }
2e3ec2af 633 $moveaction = isset($params['moveaction']) ? $params['moveaction'] : '';
54a007e8 634 $categorycell = clone($this->categorycell);
16be8974 635 $categorycell->attributes['class'] .= ' name ' . $levelclass;
54a007e8 636 $categorycell->colspan = ($this->deepest_level +1) - $params['level'];
2e3ec2af 637 $categorycell->text = $OUTPUT->heading($moveaction . $params['name'], 4);
54a007e8 638 return $categorycell;
dc482cfa 639 }
640
641 public function get_item_cell($item, $params) {
642 global $CFG;
643
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 }
647
648 $name = $params['name'];
2e3ec2af 649 $moveaction = isset($params['moveaction']) ? $params['moveaction'] : '';
dc482cfa 650
54a007e8 651 $itemcell = clone($this->itemcell);
16be8974 652 $itemcell->attributes['class'] .= ' name';
54a007e8 653 $itemcell->colspan = ($this->deepest_level + 1) - $params['level'];
2e3ec2af 654 $itemcell->text = $moveaction . $name;
54a007e8 655 return $itemcell;
dc482cfa 656 }
dc482cfa 657}
658
b79bf7a2
MG
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 */
665class grade_edit_tree_column_aggregation extends grade_edit_tree_column {
dc482cfa 666
667 public function __construct($params) {
668 parent::__construct('aggregation');
669 }
670
671 public function get_header_cell() {
c3b834b4 672 global $OUTPUT;
54a007e8 673 $headercell = clone($this->headercell);
f89b71eb 674 $headercell->text = get_string('aggregation', 'grades').$OUTPUT->help_icon('aggregation', 'grades');
54a007e8 675 return $headercell;
dc482cfa 676 }
677
678 public function get_category_cell($category, $levelclass, $params) {
0a95bebf 679 global $CFG, $OUTPUT;
dc482cfa 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 }
683
bfe969e8 684 $options = grade_helper::get_aggregation_strings();
bfc8c1d4 685 $aggregation = $options[$category->aggregation];
dc482cfa 686
54a007e8 687 $categorycell = clone($this->categorycell);
16be8974 688 $categorycell->attributes['class'] .= ' ' . $levelclass;
54a007e8 689 $categorycell->text = $aggregation;
690 return $categorycell;
dc482cfa 691
692 }
693
694 public function get_item_cell($item, $params) {
54a007e8 695 $itemcell = clone($this->itemcell);
696 $itemcell->text = ' - ';
697 return $itemcell;
dc482cfa 698 }
699}
700
b79bf7a2
MG
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 */
653a8648 707class grade_edit_tree_column_weight extends grade_edit_tree_column {
dc482cfa 708
709 public function get_header_cell() {
c3b834b4 710 global $OUTPUT;
54a007e8 711 $headercell = clone($this->headercell);
bfe969e8 712 $headercell->text = get_string('weights', 'grades').$OUTPUT->help_icon('aggregationcoefweight', 'grades');
54a007e8 713 return $headercell;
dc482cfa 714 }
715
716 public function get_category_cell($category, $levelclass, $params) {
717
718 $item = $category->get_grade_item();
54a007e8 719 $categorycell = clone($this->categorycell);
16be8974 720 $categorycell->attributes['class'] .= ' ' . $levelclass;
bfc8c1d4 721 $categorycell->text = grade_edit_tree::get_weight_input($item);
54a007e8 722 return $categorycell;
dc482cfa 723 }
724
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 }
54a007e8 729 $itemcell = clone($this->itemcell);
730 $itemcell->text = '&nbsp;';
dc482cfa 731
b8436ccb 732 if (!in_array($params['element']['object']->itemtype, array('courseitem', 'categoryitem', 'category'))) {
bfc8c1d4 733 $itemcell->text = grade_edit_tree::get_weight_input($item);
dc482cfa 734 }
735
54a007e8 736 return $itemcell;
dc482cfa 737 }
dc482cfa 738}
739
b79bf7a2
MG
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 */
dc482cfa 746class grade_edit_tree_column_range extends grade_edit_tree_column {
747
748 public function get_header_cell() {
54a007e8 749 $headercell = clone($this->headercell);
750 $headercell->text = get_string('maxgrade', 'grades');
751 return $headercell;
dc482cfa 752 }
753
754 public function get_category_cell($category, $levelclass, $params) {
54a007e8 755 $categorycell = clone($this->categorycell);
16be8974 756 $categorycell->attributes['class'] .= ' range ' . $levelclass;
54a007e8 757 $categorycell->text = ' - ';
758 return $categorycell;
dc482cfa 759 }
760
761 public function get_item_cell($item, $params) {
1c1f64a2 762 global $DB, $OUTPUT;
46409b20 763
b5e00814 764 // If the parent aggregation is Natural, we should show the number, even for scales, as that value is used...
e3a65c9e 765 // ...in the computation. For text grades, the grademax is not used, so we can still show the no value string.
46409b20 766 $parent_cat = $item->get_parent_category();
e3a65c9e
EM
767 if ($item->gradetype == GRADE_TYPE_TEXT) {
768 $grademax = ' - ';
bfc8c1d4 769 } else if ($item->gradetype == GRADE_TYPE_SCALE) {
3998cc5c 770 $scale = $DB->get_record('scale', array('id' => $item->scaleid));
e2a84477
AD
771 $scale_items = null;
772 if (empty($scale)) { //if the item is using a scale that's been removed
773 $scale_items = array();
774 } else {
775 $scale_items = explode(',', $scale->scale);
776 }
290ddb73
MG
777 if ($parent_cat->aggregation == GRADE_AGGREGATE_SUM) {
778 $grademax = end($scale_items) . ' (' .
779 format_float($item->grademax, $item->get_decimals()) . ')';
780 } else {
781 $grademax = end($scale_items) . ' (' . count($scale_items) . ')';
782 }
dc482cfa 783 } else {
bfc8c1d4 784 $grademax = format_float($item->grademax, $item->get_decimals());
dc482cfa 785 }
786
54a007e8 787 $itemcell = clone($this->itemcell);
788 $itemcell->text = $grademax;
789 return $itemcell;
dc482cfa 790 }
dc482cfa 791}
792
b79bf7a2
MG
793/**
794 * Class grade_edit_tree_column_actions
795 *
796 * @package core_grades
797 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
798 */
dc482cfa 799class grade_edit_tree_column_actions extends grade_edit_tree_column {
800
801 public function __construct($params) {
71297a3f 802 parent::__construct();
dc482cfa 803 }
804
805 public function get_header_cell() {
54a007e8 806 $headercell = clone($this->headercell);
16be8974 807 $headercell->attributes['class'] .= ' actions';
54a007e8 808 $headercell->text = get_string('actions');
809 return $headercell;
dc482cfa 810 }
811
812 public function get_category_cell($category, $levelclass, $params) {
813
814 if (empty($params['actions'])) {
815 throw new Exception('Array key (actions) missing from 3rd param of grade_edit_tree_column_actions::get_category_actions($category, $levelclass, $params)');
816 }
817
54a007e8 818 $categorycell = clone($this->categorycell);
16be8974 819 $categorycell->attributes['class'] .= ' ' . $levelclass;
54a007e8 820 $categorycell->text = $params['actions'];
821 return $categorycell;
dc482cfa 822 }
823
824 public function get_item_cell($item, $params) {
825 if (empty($params['actions'])) {
826 throw new Exception('Array key (actions) missing from 2nd param of grade_edit_tree_column_actions::get_item_cell($item, $params)');
827 }
54a007e8 828 $itemcell = clone($this->itemcell);
16be8974 829 $itemcell->attributes['class'] .= ' actions';
54a007e8 830 $itemcell->text = $params['actions'];
831 return $itemcell;
dc482cfa 832 }
dc482cfa 833}
834
b79bf7a2
MG
835/**
836 * Class grade_edit_tree_column_select
837 *
838 * @package core_grades
839 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
840 */
dc482cfa 841class grade_edit_tree_column_select extends grade_edit_tree_column {
842
843 public function get_header_cell() {
54a007e8 844 $headercell = clone($this->headercell);
16be8974 845 $headercell->attributes['class'] .= ' selection';
54a007e8 846 $headercell->text = get_string('select');
847 return $headercell;
dc482cfa 848 }
849
850 public function get_category_cell($category, $levelclass, $params) {
54a007e8 851 global $OUTPUT;
dc482cfa 852 if (empty($params['eid'])) {
853 throw new Exception('Array key (eid) missing from 3rd param of grade_edit_tree_column_select::get_category_cell($category, $levelclass, $params)');
854 }
4ee753eb
PS
855 $selectall = new action_link(new moodle_url('#'), get_string('all'), new component_action('click', 'togglecheckboxes', array('eid' => $params['eid'], 'check' => true)));
856 $selectnone = new action_link(new moodle_url('#'), get_string('none'), new component_action('click', 'togglecheckboxes', array('eid' => $params['eid'], 'check' => false)));
dc482cfa 857
54a007e8 858 $categorycell = clone($this->categorycell);
6723bc66 859 $categorycell->attributes['class'] .= ' selection ' . $levelclass;
4ee753eb 860 $categorycell->text = $OUTPUT->render($selectall) . '<br />' . $OUTPUT->render($selectnone);
54a007e8 861 return $categorycell;
dc482cfa 862 }
863
864 public function get_item_cell($item, $params) {
54a007e8 865 if (empty($params['itemtype']) || empty($params['eid'])) {
2f0e96e4 866 error('Array key (itemtype or eid) missing from 2nd param of grade_edit_tree_column_select::get_item_cell($item, $params)');
54a007e8 867 }
6723bc66
MG
868 $itemcell = clone($this->itemcell);
869 $itemcell->attributes['class'] .= ' selection';
870 $itemcell->text = '';
54a007e8 871
872 if ($params['itemtype'] != 'course' && $params['itemtype'] != 'category') {
6723bc66 873 $itemcell->text = '<label class="accesshide" for="select_'.$params['eid'].'">'.
7400be1b 874 get_string('select', 'grades', $item->itemname).'</label>
ef93efbf 875 <input class="itemselect ignoredirty" type="checkbox" name="select_'.$params['eid'].'" id="select_'.$params['eid'].
7400be1b 876 '" onchange="toggleCategorySelector();"/>'; // TODO: convert to YUI handler
dc482cfa 877 }
5174f3c5 878 //html_writer::table() will wrap the item cell contents in a <TD> so don't do it here
6723bc66 879 return $itemcell;
dc482cfa 880 }
dc482cfa 881}
6c3ef410 882