MDL-47541 core_grades: hints about aggregation settings on setup page
[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) {
6b608f8f 112 global $CFG, $COURSE, $USER, $OUTPUT;
71297a3f 113
dc482cfa 114 $object = $element['object'];
115 $eid = $element['eid'];
2be0d5c3 116 $object->name = $this->gtree->get_element_header($element, 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
129 $actions = '';
2e3ec2af 130 $moveaction = '';
dc482cfa 131
132 if (!$is_category_item) {
133 $actions .= $this->gtree->get_edit_icon($element, $this->gpr);
134 }
135
0e999796
AD
136 if ($this->show_calculations) {
137 $actions .= $this->gtree->get_calculation_icon($element, $this->gpr);
138 }
dc482cfa 139
140 if ($element['type'] == 'item' or ($element['type'] == 'category' and $element['depth'] > 1)) {
141 if ($this->element_deletable($element)) {
8ae8bf8a 142 $aurl = new moodle_url('index.php', array('id' => $COURSE->id, 'action' => 'delete', 'eid' => $eid, 'sesskey' => sesskey()));
c63923bd 143 $actions .= $OUTPUT->action_icon($aurl, new pix_icon('t/delete', get_string('delete')));
dc482cfa 144 }
1c1f64a2 145
8ae8bf8a 146 $aurl = new moodle_url('index.php', array('id' => $COURSE->id, 'action' => 'moveselect', 'eid' => $eid, 'sesskey' => sesskey()));
2e3ec2af 147 $moveaction .= $OUTPUT->action_icon($aurl, new pix_icon('t/move', get_string('move')));
dc482cfa 148 }
149
150 $actions .= $this->gtree->get_hiding_icon($element, $this->gpr);
2e3ec2af 151
ae93f353 152 $actions .= $this->gtree->get_reset_icon($element, $this->gpr);
dc482cfa 153
54a007e8 154 $returnrows = array();
dc482cfa 155 $root = false;
156
dc482cfa 157 $id = required_param('id', PARAM_INT);
158
159 /// prepare move target if needed
160 $last = '';
161
162 /// print the list items now
163 if ($this->moving == $eid) {
dc482cfa 164 // do not diplay children
54a007e8 165 $cell = new html_table_cell();
166 $cell->colspan = 12;
d75a2367
MG
167 $cell->attributes['class'] = $element['type'] . ' moving column-name level' .
168 ($level + 1) . ' level' . ($level % 2 ? 'even' : 'odd');
54a007e8 169 $cell->text = $object->name.' ('.get_string('move').')';
8cea545e 170 return array(new html_table_row(array($cell)));
dc482cfa 171 }
172
173 if ($element['type'] == 'category') {
174 $level++;
54a007e8 175 $this->categories[$object->id] = $object->stripped_name;
dc482cfa 176 $category = grade_category::fetch(array('id' => $object->id));
177 $item = $category->get_grade_item();
178
179 // Add aggregation coef input if not a course item and if parent category has correct aggregation type
54a007e8 180 $dimmed = ($item->is_hidden()) ? 'dimmed' : '';
dc482cfa 181
182 // Before we print the category's row, we must find out how many rows will appear below it (for the filler cell's rowspan)
183 $aggregation_position = grade_get_setting($COURSE->id, 'aggregationposition', $CFG->grade_aggregationposition);
184 $category_total_data = null; // Used if aggregationposition is set to "last", so we can print it last
185
54a007e8 186 $html_children = array();
dc482cfa 187
188 $row_count = 0;
189
190 foreach($element['children'] as $child_el) {
54a007e8 191 $moveto = null;
dc482cfa 192
193 if (empty($child_el['object']->itemtype)) {
194 $child_el['object']->itemtype = false;
195 }
196
197 if (($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category') && !$totals) {
198 continue;
199 }
200
54a007e8 201 $child_eid = $child_el['eid'];
dc482cfa 202 $first = '';
203
204 if ($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category') {
1c1f64a2 205 $first = array('first' => 1);
dc482cfa 206 $child_eid = $eid;
207 }
208
209 if ($this->moving && $this->moving != $child_eid) {
210
211 $strmove = get_string('move');
212 $strmovehere = get_string('movehere');
2e3ec2af 213 $actions = $moveaction = ''; // no action icons when moving
dc482cfa 214
8ae8bf8a 215 $aurl = new moodle_url('index.php', array('id' => $COURSE->id, 'action' => 'move', 'eid' => $this->moving, 'moveafter' => $child_eid, 'sesskey' => sesskey()));
1c1f64a2 216 if ($first) {
8ae8bf8a 217 $aurl->params($first);
1c1f64a2 218 }
71297a3f 219
54a007e8 220 $cell = new html_table_cell();
221 $cell->colspan = 12;
d75a2367 222 $cell->attributes['class'] = 'movehere level' . ($level + 1) . ' level' . ($level % 2 ? 'even' : 'odd');
6ef4878b 223
84d32de3
AD
224 $icon = new pix_icon('movehere', $strmovehere, null, array('class'=>'movetarget'));
225 $cell->text = $OUTPUT->action_icon($aurl, $icon);
1c1f64a2 226
8cea545e 227 $moveto = new html_table_row(array($cell));
dc482cfa 228 }
229
230 $newparents = $parents;
231 $newparents[] = $eid;
232
233 $row_count++;
234 $child_row_count = 0;
235
236 // If moving, do not print course and category totals, but still print the moveto target box
237 if ($this->moving && ($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category')) {
54a007e8 238 $html_children[] = $moveto;
dc482cfa 239 } elseif ($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category') {
240 // We don't build the item yet because we first need to know the deepest level of categories (for category/name colspans)
54a007e8 241 $category_total_item = $this->build_html_tree($child_el, $totals, $newparents, $level, $child_row_count);
dc482cfa 242 if (!$aggregation_position) {
54a007e8 243 $html_children = array_merge($html_children, $category_total_item);
dc482cfa 244 }
245 } else {
54a007e8 246 $html_children = array_merge($html_children, $this->build_html_tree($child_el, $totals, $newparents, $level, $child_row_count));
247 if (!empty($moveto)) {
248 $html_children[] = $moveto;
249 }
dc482cfa 250
251 if ($this->moving) {
252 $row_count++;
253 }
254 }
255
256 $row_count += $child_row_count;
257
258 // If the child is a category, increment row_count by one more (for the extra coloured row)
259 if ($child_el['type'] == 'category') {
260 $row_count++;
261 }
262 }
263
264 // Print category total at the end if aggregation position is "last" (1)
265 if (!empty($category_total_item) && $aggregation_position) {
54a007e8 266 $html_children = array_merge($html_children, $category_total_item);
dc482cfa 267 }
268
54a007e8 269 // Determine if we are at the root
dc482cfa 270 if (isset($element['object']->grade_item) && $element['object']->grade_item->is_course_item()) {
dc482cfa 271 $root = true;
272 }
273
d75a2367 274 $levelclass = "level$level level" . ($level % 2 ? 'odd' : 'even');
2be6ca78 275
b189277f 276 $courseclass = '';
277 if ($level == 1) {
278 $courseclass = 'coursecategory';
279 }
71297a3f 280
54a007e8 281 $row = new html_table_row();
16be8974 282 $row->attributes['class'] = $courseclass . ' category ' . $dimmed;
54a007e8 283 foreach ($rowclasses as $class) {
16be8974 284 $row->attributes['class'] .= ' ' . $class;
54a007e8 285 }
dc482cfa 286
54a007e8 287 $headercell = new html_table_cell();
288 $headercell->header = true;
289 $headercell->scope = 'row';
16be8974 290 $headercell->attributes['title'] = $object->stripped_name;
d75a2367 291 $headercell->attributes['class'] = 'cell column-rowspan rowspan ' . $levelclass;
bd26d932 292 $headercell->rowspan = $row_count + 1;
54a007e8 293 $row->cells[] = $headercell;
dc482cfa 294
295 foreach ($this->columns as $column) {
b79bf7a2 296 if (!($this->moving && $column->hide_when_moving)) {
2e3ec2af
MG
297 $row->cells[] = $column->get_category_cell($category, $levelclass, array('id' => $id,
298 'name' => $object->name, 'level' => $level, 'actions' => $actions,
299 'moveaction' => $moveaction, 'eid' => $eid));
dc482cfa 300 }
301 }
71297a3f 302
54a007e8 303 $returnrows[] = $row;
dc482cfa 304
54a007e8 305 $returnrows = array_merge($returnrows, $html_children);
dc482cfa 306
6ef4878b 307 // Print a coloured row to show the end of the category across the table
54a007e8 308 $endcell = new html_table_cell();
309 $endcell->colspan = (19 - $level);
d75a2367 310 $endcell->attributes['class'] = 'emptyrow colspan ' . $levelclass;
54a007e8 311
0e35ba6f 312 $returnrows[] = new html_table_row(array($endcell));
dc482cfa 313
314 } else { // Dealing with a grade item
315
316 $item = grade_item::fetch(array('id' => $object->id));
317 $element['type'] = 'item';
318 $element['object'] = $item;
2be6ca78 319
b189277f 320 $categoryitemclass = '';
321 if ($item->itemtype == 'category') {
322 $categoryitemclass = 'categoryitem';
323 }
d75a2367
MG
324 if ($item->itemtype == 'course') {
325 $categoryitemclass = 'courseitem';
326 }
dc482cfa 327
54a007e8 328 $dimmed = ($item->is_hidden()) ? "dimmed_text" : "";
329 $gradeitemrow = new html_table_row();
16be8974 330 $gradeitemrow->attributes['class'] = $categoryitemclass . ' item ' . $dimmed;
54a007e8 331 foreach ($rowclasses as $class) {
16be8974 332 $gradeitemrow->attributes['class'] .= ' ' . $class;
54a007e8 333 }
dc482cfa 334
335 foreach ($this->columns as $column) {
b79bf7a2 336 if (!($this->moving && $column->hide_when_moving)) {
2e3ec2af
MG
337 $gradeitemrow->cells[] = $column->get_item_cell($item, array('id' => $id, 'name' => $object->name,
338 'level' => $level, 'actions' => $actions, 'element' => $element, 'eid' => $eid,
339 'moveaction' => $moveaction, 'itemtype' => $object->itemtype));
dc482cfa 340 }
341 }
342
54a007e8 343 $returnrows[] = $gradeitemrow;
dc482cfa 344 }
345
54a007e8 346 return $returnrows;
dc482cfa 347
348 }
349
350 /**
351 * Given a grade_item object, returns a labelled input if an aggregation coefficient (weight or extra credit) applies to it.
352 * @param grade_item $item
353 * @return string HTML
354 */
bfc8c1d4 355 static function get_weight_input($item) {
1c1f64a2 356 global $OUTPUT;
357
dc482cfa 358 if (!is_object($item) || get_class($item) !== 'grade_item') {
359 throw new Exception('grade_edit_tree::get_weight_input($item) was given a variable that is not of the required type (grade_item object)');
360 return false;
361 }
362
363 if ($item->is_course_item()) {
364 return '';
365 }
366
367 $parent_category = $item->get_parent_category();
dc482cfa 368 $parent_category->apply_forced_settings();
653a8648 369 $aggcoef = $item->get_coefstring();
dc482cfa 370
b0c91d72
FM
371 $itemname = $item->itemname;
372 if ($item->is_category_item()) {
373 // Remember, the parent category of a category item is the category itself.
374 $itemname = $parent_category->get_name();
375 }
bfc8c1d4
MG
376 $str = '';
377
378 if ($aggcoef == 'aggregationcoefweight' || $aggcoef == 'aggregationcoef' || $aggcoef == 'aggregationcoefextraweight') {
b6a1e366 379 return '<label class="accesshide" for="weight_'.$item->id.'">'.
b0c91d72 380 get_string('extracreditvalue', 'grades', $itemname).'</label>'.
351c8e08 381 '<input type="text" size="6" id="weight_'.$item->id.'" name="weight_'.$item->id.'"
3cf33f51 382 value="'.grade_edit_tree::format_number($item->aggregationcoef).'" />';
bfc8c1d4 383 } else if ($aggcoef == 'aggregationcoefextraweightsum') {
6c37563e
FM
384
385 $checkboxname = 'weightoverride_' . $item->id;
b0c91d72 386 $checkboxlbl = html_writer::tag('label', get_string('overrideweightofa', 'grades', $itemname),
6c37563e
FM
387 array('for' => $checkboxname, 'class' => 'accesshide'));
388 $checkbox = html_writer::empty_tag('input', array('name' => $checkboxname,
389 'type' => 'hidden', 'value' => 0));
390 $checkbox .= html_writer::empty_tag('input', array('name' => $checkboxname,
391 'type' => 'checkbox', 'value' => 1, 'id' => $checkboxname, 'class' => 'weightoverride',
392 'checked' => ($item->weightoverride ? 'checked' : null)));
200e5173 393
b6a1e366 394 $name = 'weight_' . $item->id;
200e5173
JC
395 $hiddenlabel = html_writer::tag(
396 'label',
b0c91d72 397 get_string('weightofa', 'grades', $itemname),
200e5173
JC
398 array(
399 'class' => 'accesshide',
400 'for' => $name
401 )
402 );
403
404 $input = html_writer::empty_tag(
405 'input',
406 array(
407 'type' => 'text',
408 'size' => 6,
409 'id' => $name,
410 'name' => $name,
6c37563e
FM
411 'value' => grade_edit_tree::format_number($item->aggregationcoef2 * 100.0),
412 'disabled' => ($item->weightoverride ? null : 'disabled')
200e5173
JC
413 )
414 );
415
6c37563e 416 $str .= $checkboxlbl . $checkbox . $hiddenlabel . $input;
bfc8c1d4
MG
417
418 if ($item->aggregationcoef > 0) {
419 $str .= ' ' . html_writer::tag('abbr', get_string('aggregationcoefextrasumabbr', 'grades'),
420 array('title' => get_string('aggregationcoefextrasum', 'grades')));
421 }
dc482cfa 422 }
bfc8c1d4
MG
423
424 return $str;
cd3a391c 425 }
dc482cfa 426
6ef4878b 427 //Trims trailing zeros
4592df46 428 //Used on the 'categories and items' page for grade items settings like aggregation co-efficient
e229506d 429 //Grader report has its own decimal place settings so they are handled elsewhere
e2bb3c92 430 static function format_number($number) {
4592df46 431 $formatted = rtrim(format_float($number, 4),'0');
dacd5daa 432 if (substr($formatted, -1)==get_string('decsep', 'langconfig')) { //if last char is the decimal point
4592df46
AD
433 $formatted .= '0';
434 }
435 return $formatted;
3cf33f51
AD
436 }
437
dc482cfa 438 /**
439 * Given an element of the grade tree, returns whether it is deletable or not (only manual grade items are deletable)
440 *
441 * @param array $element
442 * @return bool
443 */
444 function element_deletable($element) {
445 global $COURSE;
446
447 if ($element['type'] != 'item') {
448 return true;
449 }
450
451 $grade_item = $element['object'];
452
453 if ($grade_item->itemtype != 'mod' or $grade_item->is_outcome_item() or $grade_item->gradetype == GRADE_TYPE_NONE) {
454 return true;
455 }
456
457 $modinfo = get_fast_modinfo($COURSE);
458 if (!isset($modinfo->instances[$grade_item->itemmodule][$grade_item->iteminstance])) {
459 // module does not exist
460 return true;
461 }
462
463 return false;
464 }
465
466 /**
467 * Given the grade tree and an array of element ids (e.g. c15, i42), and expecting the 'moveafter' URL param,
468 * moves the selected items to the requested location. Then redirects the user to the given $returnurl
469 *
470 * @param object $gtree The grade tree (a recursive representation of the grade categories and grade items)
471 * @param array $eids
472 * @param string $returnurl
473 */
474 function move_elements($eids, $returnurl) {
475 $moveafter = required_param('moveafter', PARAM_INT);
476
477 if (!is_array($eids)) {
478 $eids = array($eids);
479 }
480
ddb9c95f 481 if(!$after_el = $this->gtree->locate_element("cg$moveafter")) {
dc482cfa 482 print_error('invalidelementid', '', $returnurl);
483 }
484
485 $after = $after_el['object'];
486 $parent = $after;
487 $sortorder = $after->get_sortorder();
488
489 foreach ($eids as $eid) {
490 if (!$element = $this->gtree->locate_element($eid)) {
491 print_error('invalidelementid', '', $returnurl);
492 }
493 $object = $element['object'];
494
495 $object->set_parent($parent->id);
496 $object->move_after_sortorder($sortorder);
4d2f7923 497 $sortorder++;
dc482cfa 498 }
499
500 redirect($returnurl, '', 0);
501 }
502
503 /**
504 * Recurses through the entire grade tree to find and return the maximum depth of the tree.
505 * This should be run only once from the root element (course category), and is used for the
506 * indentation of the Name column's cells (colspan)
507 *
508 * @param array $element An array of values representing a grade tree's element (all grade items in this case)
509 * @param int $level The level of the current recursion
510 * @param int $deepest_level A value passed to each subsequent level of recursion and incremented if $level > $deepest_level
511 * @return int Deepest level
512 */
513 function get_deepest_level($element, $level=0, $deepest_level=1) {
514 $object = $element['object'];
515
516 $level++;
653a8648 517 $coefstring = $element['object']->get_coefstring();
dc482cfa 518 if ($element['type'] == 'category') {
bfc8c1d4
MG
519 if ($coefstring == 'aggregationcoefweight' || $coefstring == 'aggregationcoefextraweightsum' ||
520 $coefstring == 'aggregationcoefextraweight') {
521 $this->uses_weight = true;
677bc073 522 }
653a8648 523
dc482cfa 524 foreach($element['children'] as $child_el) {
525 if ($level > $deepest_level) {
526 $deepest_level = $level;
527 }
528 $deepest_level = $this->get_deepest_level($child_el, $level, $deepest_level);
529 }
530 }
531
532 return $deepest_level;
533 }
534}
535
b79bf7a2
MG
536/**
537 * Class grade_edit_tree_column
538 *
539 * @package core_grades
540 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
541 */
dc482cfa 542abstract class grade_edit_tree_column {
543 public $forced;
544 public $hidden;
545 public $forced_hidden;
546 public $advanced_hidden;
547 public $hide_when_moving = true;
54a007e8 548 /**
549 * html_table_cell object used as a template for header cells in all categories.
550 * It must be cloned before being used.
551 * @var html_table_cell $headercell
552 */
553 public $headercell;
554 /**
555 * html_table_cell object used as a template for category cells in all categories.
556 * It must be cloned before being used.
557 * @var html_table_cell $categorycell
558 */
559 public $categorycell;
560 /**
561 * html_table_cell object used as a template for item cells in all categories.
562 * It must be cloned before being used.
563 * @var html_table_cell $itemcell
564 */
565 public $itemcell;
dc482cfa 566
567 public static function factory($name, $params=array()) {
568 $class_name = "grade_edit_tree_column_$name";
569 if (class_exists($class_name)) {
570 return new $class_name($params);
571 }
572 }
573
574 public abstract function get_header_cell();
575
d75a2367
MG
576 public function get_category_cell($category, $levelclass, $params) {
577 $cell = clone($this->categorycell);
578 $cell->attributes['class'] .= ' ' . $levelclass;
579 $cell->attributes['text'] = '';
580 return $cell;
581 }
dc482cfa 582
d75a2367
MG
583 public function get_item_cell($item, $params) {
584 $cell = clone($this->itemcell);
585 $cell->attributes['text'] = '';
586 if (isset($params['level'])) {
587 $level = $params['level'] + (($item->itemtype == 'category' || $item->itemtype == 'course') ? 0 : 1);
588 $cell->attributes['class'] .= ' level' . $level;
589 $cell->attributes['class'] .= ' level' . ($level % 2 ? 'odd' : 'even');
590 }
591 return $cell;
592 }
dc482cfa 593
54a007e8 594 public function __construct() {
595 $this->headercell = new html_table_cell();
596 $this->headercell->header = true;
16be8974 597 $this->headercell->attributes['class'] = 'header';
54a007e8 598
599 $this->categorycell = new html_table_cell();
16be8974 600 $this->categorycell->attributes['class'] = 'cell';
54a007e8 601
602 $this->itemcell = new html_table_cell();
16be8974 603 $this->itemcell->attributes['class'] = 'cell';
d75a2367
MG
604
605 if (preg_match('/^grade_edit_tree_column_(\w*)$/', get_class($this), $matches)) {
606 $this->headercell->attributes['class'] .= ' column-' . $matches[1];
607 $this->categorycell->attributes['class'] .= ' column-' . $matches[1];
608 $this->itemcell->attributes['class'] .= ' column-' . $matches[1];
609 }
54a007e8 610 }
dc482cfa 611}
612
b79bf7a2
MG
613/**
614 * Class grade_edit_tree_column_name
615 *
616 * @package core_grades
617 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
618 */
dc482cfa 619class grade_edit_tree_column_name extends grade_edit_tree_column {
620 public $forced = false;
621 public $hidden = false;
622 public $forced_hidden = false;
623 public $advanced_hidden = false;
624 public $deepest_level = 1;
625 public $hide_when_moving = false;
626
627 public function __construct($params) {
628 if (empty($params['deepest_level'])) {
629 throw new Exception('Tried to instantiate a grade_edit_tree_column_name object without the "deepest_level" param!');
630 }
631
632 $this->deepest_level = $params['deepest_level'];
54a007e8 633 parent::__construct();
dc482cfa 634 }
635
636 public function get_header_cell() {
54a007e8 637 $headercell = clone($this->headercell);
54a007e8 638 $headercell->colspan = $this->deepest_level + 1;
639 $headercell->text = get_string('name');
640 return $headercell;
dc482cfa 641 }
642
643 public function get_category_cell($category, $levelclass, $params) {
54a007e8 644 global $OUTPUT;
dc482cfa 645 if (empty($params['name']) || empty($params['level'])) {
646 throw new Exception('Array key (name or level) missing from 3rd param of grade_edit_tree_column_name::get_category_cell($category, $levelclass, $params)');
647 }
2e3ec2af 648 $moveaction = isset($params['moveaction']) ? $params['moveaction'] : '';
d75a2367 649 $categorycell = parent::get_category_cell($category, $levelclass, $params);
54a007e8 650 $categorycell->colspan = ($this->deepest_level +1) - $params['level'];
2e3ec2af 651 $categorycell->text = $OUTPUT->heading($moveaction . $params['name'], 4);
54a007e8 652 return $categorycell;
dc482cfa 653 }
654
655 public function get_item_cell($item, $params) {
656 global $CFG;
657
658 if (empty($params['element']) || empty($params['name']) || empty($params['level'])) {
659 throw new Exception('Array key (name, level or element) missing from 2nd param of grade_edit_tree_column_name::get_item_cell($item, $params)');
660 }
661
662 $name = $params['name'];
2e3ec2af 663 $moveaction = isset($params['moveaction']) ? $params['moveaction'] : '';
dc482cfa 664
d75a2367 665 $itemcell = parent::get_item_cell($item, $params);
54a007e8 666 $itemcell->colspan = ($this->deepest_level + 1) - $params['level'];
2e3ec2af 667 $itemcell->text = $moveaction . $name;
54a007e8 668 return $itemcell;
dc482cfa 669 }
dc482cfa 670}
671
b79bf7a2
MG
672/**
673 * Class grade_edit_tree_column_weight
674 *
675 * @package core_grades
676 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
677 */
653a8648 678class grade_edit_tree_column_weight extends grade_edit_tree_column {
dc482cfa 679
680 public function get_header_cell() {
c3b834b4 681 global $OUTPUT;
54a007e8 682 $headercell = clone($this->headercell);
bfe969e8 683 $headercell->text = get_string('weights', 'grades').$OUTPUT->help_icon('aggregationcoefweight', 'grades');
54a007e8 684 return $headercell;
dc482cfa 685 }
686
687 public function get_category_cell($category, $levelclass, $params) {
688
689 $item = $category->get_grade_item();
d75a2367 690 $categorycell = parent::get_category_cell($category, $levelclass, $params);
bfc8c1d4 691 $categorycell->text = grade_edit_tree::get_weight_input($item);
54a007e8 692 return $categorycell;
dc482cfa 693 }
694
695 public function get_item_cell($item, $params) {
696 if (empty($params['element'])) {
697 throw new Exception('Array key (element) missing from 2nd param of grade_edit_tree_column_weightorextracredit::get_item_cell($item, $params)');
698 }
d75a2367 699 $itemcell = parent::get_item_cell($item, $params);
54a007e8 700 $itemcell->text = '&nbsp;';
dc482cfa 701
b8436ccb 702 if (!in_array($params['element']['object']->itemtype, array('courseitem', 'categoryitem', 'category'))) {
bfc8c1d4 703 $itemcell->text = grade_edit_tree::get_weight_input($item);
dc482cfa 704 }
705
54a007e8 706 return $itemcell;
dc482cfa 707 }
dc482cfa 708}
709
b79bf7a2
MG
710/**
711 * Class grade_edit_tree_column_range
712 *
713 * @package core_grades
714 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
715 */
dc482cfa 716class grade_edit_tree_column_range extends grade_edit_tree_column {
717
718 public function get_header_cell() {
54a007e8 719 $headercell = clone($this->headercell);
720 $headercell->text = get_string('maxgrade', 'grades');
721 return $headercell;
dc482cfa 722 }
723
724 public function get_category_cell($category, $levelclass, $params) {
d75a2367 725 $categorycell = parent::get_category_cell($category, $levelclass, $params);
54a007e8 726 $categorycell->text = ' - ';
727 return $categorycell;
dc482cfa 728 }
729
730 public function get_item_cell($item, $params) {
1c1f64a2 731 global $DB, $OUTPUT;
46409b20 732
b5e00814 733 // If the parent aggregation is Natural, we should show the number, even for scales, as that value is used...
e3a65c9e 734 // ...in the computation. For text grades, the grademax is not used, so we can still show the no value string.
46409b20 735 $parent_cat = $item->get_parent_category();
e3a65c9e
EM
736 if ($item->gradetype == GRADE_TYPE_TEXT) {
737 $grademax = ' - ';
bfc8c1d4 738 } else if ($item->gradetype == GRADE_TYPE_SCALE) {
3998cc5c 739 $scale = $DB->get_record('scale', array('id' => $item->scaleid));
e2a84477
AD
740 $scale_items = null;
741 if (empty($scale)) { //if the item is using a scale that's been removed
742 $scale_items = array();
743 } else {
744 $scale_items = explode(',', $scale->scale);
745 }
290ddb73
MG
746 if ($parent_cat->aggregation == GRADE_AGGREGATE_SUM) {
747 $grademax = end($scale_items) . ' (' .
748 format_float($item->grademax, $item->get_decimals()) . ')';
749 } else {
750 $grademax = end($scale_items) . ' (' . count($scale_items) . ')';
751 }
dc482cfa 752 } else {
bfc8c1d4 753 $grademax = format_float($item->grademax, $item->get_decimals());
dc482cfa 754 }
755
d75a2367 756 $itemcell = parent::get_item_cell($item, $params);
54a007e8 757 $itemcell->text = $grademax;
758 return $itemcell;
dc482cfa 759 }
dc482cfa 760}
761
b79bf7a2
MG
762/**
763 * Class grade_edit_tree_column_actions
764 *
765 * @package core_grades
766 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
767 */
dc482cfa 768class grade_edit_tree_column_actions extends grade_edit_tree_column {
769
770 public function __construct($params) {
71297a3f 771 parent::__construct();
dc482cfa 772 }
773
774 public function get_header_cell() {
54a007e8 775 $headercell = clone($this->headercell);
54a007e8 776 $headercell->text = get_string('actions');
777 return $headercell;
dc482cfa 778 }
779
780 public function get_category_cell($category, $levelclass, $params) {
781
782 if (empty($params['actions'])) {
783 throw new Exception('Array key (actions) missing from 3rd param of grade_edit_tree_column_actions::get_category_actions($category, $levelclass, $params)');
784 }
785
d75a2367 786 $categorycell = parent::get_category_cell($category, $levelclass, $params);
54a007e8 787 $categorycell->text = $params['actions'];
788 return $categorycell;
dc482cfa 789 }
790
791 public function get_item_cell($item, $params) {
792 if (empty($params['actions'])) {
793 throw new Exception('Array key (actions) missing from 2nd param of grade_edit_tree_column_actions::get_item_cell($item, $params)');
794 }
d75a2367 795 $itemcell = parent::get_item_cell($item, $params);
54a007e8 796 $itemcell->text = $params['actions'];
797 return $itemcell;
dc482cfa 798 }
dc482cfa 799}
800
b79bf7a2
MG
801/**
802 * Class grade_edit_tree_column_select
803 *
804 * @package core_grades
805 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
806 */
dc482cfa 807class grade_edit_tree_column_select extends grade_edit_tree_column {
808
809 public function get_header_cell() {
54a007e8 810 $headercell = clone($this->headercell);
54a007e8 811 $headercell->text = get_string('select');
812 return $headercell;
dc482cfa 813 }
814
815 public function get_category_cell($category, $levelclass, $params) {
54a007e8 816 global $OUTPUT;
dc482cfa 817 if (empty($params['eid'])) {
818 throw new Exception('Array key (eid) missing from 3rd param of grade_edit_tree_column_select::get_category_cell($category, $levelclass, $params)');
819 }
4ee753eb
PS
820 $selectall = new action_link(new moodle_url('#'), get_string('all'), new component_action('click', 'togglecheckboxes', array('eid' => $params['eid'], 'check' => true)));
821 $selectnone = new action_link(new moodle_url('#'), get_string('none'), new component_action('click', 'togglecheckboxes', array('eid' => $params['eid'], 'check' => false)));
dc482cfa 822
d75a2367 823 $categorycell = parent::get_category_cell($category, $levelclass, $params);
4ee753eb 824 $categorycell->text = $OUTPUT->render($selectall) . '<br />' . $OUTPUT->render($selectnone);
54a007e8 825 return $categorycell;
dc482cfa 826 }
827
828 public function get_item_cell($item, $params) {
54a007e8 829 if (empty($params['itemtype']) || empty($params['eid'])) {
2f0e96e4 830 error('Array key (itemtype or eid) missing from 2nd param of grade_edit_tree_column_select::get_item_cell($item, $params)');
54a007e8 831 }
d75a2367 832 $itemcell = parent::get_item_cell($item, $params);
54a007e8 833
834 if ($params['itemtype'] != 'course' && $params['itemtype'] != 'category') {
6723bc66 835 $itemcell->text = '<label class="accesshide" for="select_'.$params['eid'].'">'.
7400be1b 836 get_string('select', 'grades', $item->itemname).'</label>
ef93efbf 837 <input class="itemselect ignoredirty" type="checkbox" name="select_'.$params['eid'].'" id="select_'.$params['eid'].
7400be1b 838 '" onchange="toggleCategorySelector();"/>'; // TODO: convert to YUI handler
dc482cfa 839 }
6723bc66 840 return $itemcell;
dc482cfa 841 }
dc482cfa 842}
6c3ef410 843