Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  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  
  25  class grade_edit_tree {
  26      public $columns = array();
  27  
  28      /**
  29       * @var grade_tree $gtree   @see grade/lib.php
  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  
  45      public $uses_weight = false;
  46  
  47      public $table;
  48  
  49      public $categories = array();
  50  
  51      /**
  52       * Show calculator icons next to manual grade items
  53       * @var bool $show_calculations
  54       */
  55      private $show_calculations;
  56  
  57      /**
  58       * Constructor
  59       */
  60      public function __construct($gtree, $moving, $gpr) {
  61          global $USER, $OUTPUT, $COURSE;
  62  
  63          $systemdefault = get_config('moodle', 'grade_report_showcalculations');
  64          $this->show_calculations = get_user_preferences('grade_report_showcalculations', $systemdefault);
  65  
  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  
  71          $this->columns = array(grade_edit_tree_column::factory('name', array('deepest_level' => $this->deepest_level)));
  72  
  73          if ($this->uses_weight) {
  74              $this->columns[] = grade_edit_tree_column::factory('weight', array('adv' => 'weight'));
  75          }
  76  
  77          $this->columns[] = grade_edit_tree_column::factory('range'); // This is not a setting... How do we deal with it?
  78          $this->columns[] = grade_edit_tree_column::factory('actions');
  79  
  80          if ($this->deepest_level > 1) {
  81              $this->columns[] = grade_edit_tree_column::factory('select');
  82          }
  83  
  84          $this->table = new html_table();
  85          $this->table->id = "grade_edit_tree_table";
  86          $this->table->attributes['class'] = 'generaltable simple setup-grades';
  87          if ($this->moving) {
  88              $this->table->attributes['class'] .= ' moving';
  89          }
  90  
  91          foreach ($this->columns as $column) {
  92              if (!($this->moving && $column->hide_when_moving)) {
  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);
  99      }
 100  
 101      /**
 102       * Recursive function for building the table holding the grade categories and items,
 103       * with CSS indentation and styles.
 104       *
 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)
 108       *
 109       * @return string HTML
 110       */
 111      public function build_html_tree($element, $totals, $parents, $level, &$row_count) {
 112          global $CFG, $COURSE, $PAGE, $OUTPUT;
 113  
 114          $object = $element['object'];
 115          $eid    = $element['eid'];
 116          $object->name = $this->gtree->get_element_header($element, true, true, true, true, true);
 117          $object->stripped_name = $this->gtree->get_element_header($element, false, false, false);
 118          $is_category_item = false;
 119          if ($element['type'] == 'categoryitem' || $element['type'] == 'courseitem') {
 120              $is_category_item = true;
 121          }
 122  
 123          $rowclasses = array();
 124          foreach ($parents as $parent_eid) {
 125              $rowclasses[] = $parent_eid;
 126          }
 127  
 128          $moveaction = '';
 129          $actionsmenu = new action_menu();
 130          $actionsmenu->set_menu_trigger(get_string('edit'));
 131          $actionsmenu->set_owner_selector('grade-item-' . $eid);
 132          $actionsmenu->set_boundary('window');
 133  
 134          if (!$is_category_item && ($icon = $this->gtree->get_edit_icon($element, $this->gpr, true))) {
 135              $actionsmenu->add($icon);
 136          }
 137          // MDL-49281 if grade_item already has calculation, it should be editable even if global setting is off.
 138          $type = $element['type'];
 139          $iscalculated = ($type == 'item' or $type == 'courseitem' or $type == 'categoryitem') && $object->is_calculated();
 140          $icon = $this->gtree->get_calculation_icon($element, $this->gpr, true);
 141          if ($iscalculated || ($this->show_calculations && $icon)) {
 142              $actionsmenu->add($icon);
 143          }
 144  
 145          if ($element['type'] == 'item' or ($element['type'] == 'category' and $element['depth'] > 1)) {
 146              if ($this->element_deletable($element)) {
 147                  $aurl = new moodle_url('index.php', array('id' => $COURSE->id, 'action' => 'delete', 'eid' => $eid, 'sesskey' => sesskey()));
 148                  $icon = new action_menu_link_secondary($aurl, new pix_icon('t/delete', get_string('delete')), get_string('delete'));
 149                  $actionsmenu->add($icon);
 150              }
 151  
 152              if ($this->element_duplicatable($element)) {
 153                  $duplicateparams = array();
 154                  $duplicateparams['id'] = $COURSE->id;
 155                  $duplicateparams['action'] = 'duplicate';
 156                  $duplicateparams['eid'] = $eid;
 157                  $duplicateparams['sesskey'] = sesskey();
 158                  $aurl = new moodle_url('index.php', $duplicateparams);
 159                  $duplicateicon = new pix_icon('t/copy', get_string('duplicate'));
 160                  $icon = new action_menu_link_secondary($aurl, $duplicateicon, get_string('duplicate'));
 161                  $actionsmenu->add($icon);
 162              }
 163  
 164              $aurl = new moodle_url('index.php', array('id' => $COURSE->id, 'action' => 'moveselect', 'eid' => $eid, 'sesskey' => sesskey()));
 165              $moveaction .= $OUTPUT->action_icon($aurl, new pix_icon('t/move', get_string('move')));
 166          }
 167  
 168          if ($icon = $this->gtree->get_hiding_icon($element, $this->gpr, true)) {
 169              $actionsmenu->add($icon);
 170          }
 171  
 172          if ($icon = $this->gtree->get_reset_icon($element, $this->gpr, true)) {
 173              $actionsmenu->add($icon);
 174          }
 175  
 176          $actions = $OUTPUT->render($actionsmenu);
 177  
 178          $returnrows = array();
 179          $root = false;
 180  
 181          $id = required_param('id', PARAM_INT);
 182  
 183          /// prepare move target if needed
 184          $last = '';
 185  
 186          /// print the list items now
 187          if ($this->moving == $eid) {
 188              // do not diplay children
 189              $cell = new html_table_cell();
 190              $cell->colspan = 12;
 191              $cell->attributes['class'] = $element['type'] . ' moving column-name level' .
 192                  ($level + 1) . ' level' . ($level % 2 ? 'even' : 'odd');
 193              $cell->text = $object->name.' ('.get_string('move').')';
 194              return array(new html_table_row(array($cell)));
 195          }
 196  
 197          if ($element['type'] == 'category') {
 198              $level++;
 199              $this->categories[$object->id] = $object->stripped_name;
 200              $category = grade_category::fetch(array('id' => $object->id));
 201              $item = $category->get_grade_item();
 202  
 203              // Add aggregation coef input if not a course item and if parent category has correct aggregation type
 204              $dimmed = ($item->is_hidden()) ? 'dimmed_text' : '';
 205  
 206              // Before we print the category's row, we must find out how many rows will appear below it (for the filler cell's rowspan)
 207              $aggregation_position = grade_get_setting($COURSE->id, 'aggregationposition', $CFG->grade_aggregationposition);
 208              $category_total_data = null; // Used if aggregationposition is set to "last", so we can print it last
 209  
 210              $html_children = array();
 211  
 212              $row_count = 0;
 213  
 214              foreach($element['children'] as $child_el) {
 215                  $moveto = null;
 216  
 217                  if (empty($child_el['object']->itemtype)) {
 218                      $child_el['object']->itemtype = false;
 219                  }
 220  
 221                  if (($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category') && !$totals) {
 222                      continue;
 223                  }
 224  
 225                  $child_eid = $child_el['eid'];
 226                  $first = '';
 227  
 228                  if ($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category') {
 229                      $first = array('first' => 1);
 230                      $child_eid = $eid;
 231                  }
 232  
 233                  if ($this->moving && $this->moving != $child_eid) {
 234  
 235                      $strmove     = get_string('move');
 236                      $actions = $moveaction = ''; // no action icons when moving
 237  
 238                      $aurl = new moodle_url('index.php', array('id' => $COURSE->id, 'action' => 'move', 'eid' => $this->moving, 'moveafter' => $child_eid, 'sesskey' => sesskey()));
 239                      if ($first) {
 240                          $aurl->params($first);
 241                      }
 242  
 243                      $cell = new html_table_cell();
 244                      $cell->colspan = 12;
 245                      $cell->attributes['class'] = 'movehere level' . ($level + 1) . ' level' . ($level % 2 ? 'even' : 'odd');
 246  
 247                      $cell->text = html_writer::link($aurl, '', array('title' => get_string('movehere'), 'class' => 'movehere'));
 248  
 249                      $moveto = new html_table_row(array($cell));
 250                  }
 251  
 252                  $newparents = $parents;
 253                  $newparents[] = $eid;
 254  
 255                  $row_count++;
 256                  $child_row_count = 0;
 257  
 258                  // If moving, do not print course and category totals, but still print the moveto target box
 259                  if ($this->moving && ($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category')) {
 260                      $html_children[] = $moveto;
 261                  } elseif ($child_el['object']->itemtype == 'course' || $child_el['object']->itemtype == 'category') {
 262                      // We don't build the item yet because we first need to know the deepest level of categories (for category/name colspans)
 263                      $category_total_item = $this->build_html_tree($child_el, $totals, $newparents, $level, $child_row_count);
 264                      if (!$aggregation_position) {
 265                          $html_children = array_merge($html_children, $category_total_item);
 266                      }
 267                  } else {
 268                      $html_children = array_merge($html_children, $this->build_html_tree($child_el, $totals, $newparents, $level, $child_row_count));
 269                      if (!empty($moveto)) {
 270                          $html_children[] = $moveto;
 271                      }
 272  
 273                      if ($this->moving) {
 274                          $row_count++;
 275                      }
 276                  }
 277  
 278                  $row_count += $child_row_count;
 279  
 280                  // If the child is a category, increment row_count by one more (for the extra coloured row)
 281                  if ($child_el['type'] == 'category') {
 282                      $row_count++;
 283                  }
 284              }
 285  
 286              // Print category total at the end if aggregation position is "last" (1)
 287              if (!empty($category_total_item) && $aggregation_position) {
 288                  $html_children = array_merge($html_children, $category_total_item);
 289              }
 290  
 291              // Determine if we are at the root
 292              if (isset($element['object']->grade_item) && $element['object']->grade_item->is_course_item()) {
 293                  $root = true;
 294              }
 295  
 296              $levelclass = "level$level level" . ($level % 2 ? 'odd' : 'even');
 297  
 298              $courseclass = '';
 299              if ($level == 1) {
 300                  $courseclass = 'coursecategory';
 301              }
 302  
 303              $row = new html_table_row();
 304              $row->id = 'grade-item-' . $eid;
 305              $row->attributes['class'] = $courseclass . ' category ' . $dimmed;
 306              $row->attributes['data-category'] = $eid;
 307              $row->attributes['data-itemid'] = $category->get_grade_item()->id;
 308              foreach ($rowclasses as $class) {
 309                  $row->attributes['class'] .= ' ' . $class;
 310              }
 311  
 312              $headercell = new html_table_cell();
 313              $headercell->header = true;
 314              $headercell->scope = 'row';
 315              $headercell->attributes['class'] = 'cell column-rowspan rowspan ' . $levelclass;
 316              $headercell->attributes['aria-hidden'] = 'true';
 317              $headercell->rowspan = $row_count + 1;
 318              $row->cells[] = $headercell;
 319  
 320              foreach ($this->columns as $column) {
 321                  if (!($this->moving && $column->hide_when_moving)) {
 322                      $row->cells[] = $column->get_category_cell($category, $levelclass, [
 323                          'id' => $id,
 324                          'name' => $object->name,
 325                          'level' => $level,
 326                          'actions' => $actions,
 327                          'moveaction' => $moveaction,
 328                          'eid' => $eid,
 329                      ]);
 330                  }
 331              }
 332  
 333              $returnrows[] = $row;
 334  
 335              $returnrows = array_merge($returnrows, $html_children);
 336  
 337              // Print a coloured row to show the end of the category across the table
 338              $endcell = new html_table_cell();
 339              $endcell->colspan = (19 - $level);
 340              $endcell->attributes['class'] = 'emptyrow colspan ' . $levelclass;
 341              $endcell->attributes['aria-hidden'] = 'true';
 342  
 343              $returnrows[] = new html_table_row(array($endcell));
 344  
 345          } else { // Dealing with a grade item
 346  
 347              $item = grade_item::fetch(array('id' => $object->id));
 348              $element['type'] = 'item';
 349              $element['object'] = $item;
 350  
 351              $categoryitemclass = '';
 352              if ($item->itemtype == 'category') {
 353                  $categoryitemclass = 'categoryitem';
 354              }
 355              if ($item->itemtype == 'course') {
 356                  $categoryitemclass = 'courseitem';
 357              }
 358  
 359              $dimmed = ($item->is_hidden()) ? "dimmed_text" : "";
 360              $gradeitemrow = new html_table_row();
 361              $gradeitemrow->id = 'grade-item-' . $eid;
 362              $gradeitemrow->attributes['class'] = $categoryitemclass . ' item ' . $dimmed;
 363              $gradeitemrow->attributes['data-itemid'] = $object->id;
 364              foreach ($rowclasses as $class) {
 365                  $gradeitemrow->attributes['class'] .= ' ' . $class;
 366              }
 367  
 368              foreach ($this->columns as $column) {
 369                  if (!($this->moving && $column->hide_when_moving)) {
 370                      $gradeitemrow->cells[] = $column->get_item_cell($item, array('id' => $id, 'name' => $object->name,
 371                          'level' => $level, 'actions' => $actions, 'element' => $element, 'eid' => $eid,
 372                          'moveaction' => $moveaction, 'itemtype' => $object->itemtype));
 373                  }
 374              }
 375  
 376              $returnrows[] = $gradeitemrow;
 377          }
 378  
 379          return $returnrows;
 380  
 381      }
 382  
 383      /**
 384       * Given a grade_item object, returns a labelled input if an aggregation coefficient (weight or extra credit) applies to it.
 385       * @param grade_item $item
 386       * @return string HTML
 387       */
 388      static function get_weight_input($item) {
 389          global $OUTPUT;
 390  
 391          if (!is_object($item) || get_class($item) !== 'grade_item') {
 392              throw new Exception('grade_edit_tree::get_weight_input($item) was given a variable that is not of the required type (grade_item object)');
 393              return false;
 394          }
 395  
 396          if ($item->is_course_item()) {
 397              return '';
 398          }
 399  
 400          $parent_category = $item->get_parent_category();
 401          $parent_category->apply_forced_settings();
 402          $aggcoef = $item->get_coefstring();
 403  
 404          $itemname = $item->itemname;
 405          if ($item->is_category_item()) {
 406              // Remember, the parent category of a category item is the category itself.
 407              $itemname = $parent_category->get_name();
 408          }
 409          $str = '';
 410  
 411          if ($aggcoef == 'aggregationcoefweight' || $aggcoef == 'aggregationcoef' || $aggcoef == 'aggregationcoefextraweight') {
 412  
 413              return $OUTPUT->render_from_template('core_grades/weight_field', [
 414                  'id' => $item->id,
 415                  'itemname' => $itemname,
 416                  'value' => self::format_number($item->aggregationcoef)
 417              ]);
 418  
 419          } else if ($aggcoef == 'aggregationcoefextraweightsum') {
 420  
 421              $tpldata = [
 422                  'id' => $item->id,
 423                  'itemname' => $itemname,
 424                  'value' => self::format_number($item->aggregationcoef2 * 100.0),
 425                  'checked' => $item->weightoverride,
 426                  'disabled' => !$item->weightoverride
 427              ];
 428              $str .= $OUTPUT->render_from_template('core_grades/weight_override_field', $tpldata);
 429  
 430          }
 431  
 432          return $str;
 433      }
 434  
 435      // Trims trailing zeros.
 436      // Used on the 'Gradebook setup' page for grade items settings like aggregation co-efficient.
 437      // Grader report has its own decimal place settings so they are handled elsewhere.
 438      static function format_number($number) {
 439          $formatted = rtrim(format_float($number, 4),'0');
 440          if (substr($formatted, -1)==get_string('decsep', 'langconfig')) { //if last char is the decimal point
 441              $formatted .= '0';
 442          }
 443          return $formatted;
 444      }
 445  
 446      /**
 447       * Given an element of the grade tree, returns whether it is deletable or not (only manual grade items are deletable)
 448       *
 449       * @param array $element
 450       * @return bool
 451       */
 452      function element_deletable($element) {
 453          global $COURSE;
 454  
 455          if ($element['type'] != 'item') {
 456              return true;
 457          }
 458  
 459          $grade_item = $element['object'];
 460  
 461          if ($grade_item->itemtype != 'mod' or $grade_item->is_outcome_item() or $grade_item->gradetype == GRADE_TYPE_NONE) {
 462              return true;
 463          }
 464  
 465          $modinfo = get_fast_modinfo($COURSE);
 466          if (!isset($modinfo->instances[$grade_item->itemmodule][$grade_item->iteminstance])) {
 467              // module does not exist
 468              return true;
 469          }
 470  
 471          return false;
 472      }
 473  
 474      /**
 475       * Given an element of the grade tree, returns whether it is duplicatable or not (only manual grade items are duplicatable)
 476       *
 477       * @param array $element
 478       * @return bool
 479       */
 480      public function element_duplicatable($element) {
 481          if ($element['type'] != 'item') {
 482              return false;
 483          }
 484  
 485          $gradeitem = $element['object'];
 486          if ($gradeitem->itemtype != 'mod') {
 487              return true;
 488          }
 489          return false;
 490      }
 491  
 492      /**
 493       * Given the grade tree and an array of element ids (e.g. c15, i42), and expecting the 'moveafter' URL param,
 494       * moves the selected items to the requested location. Then redirects the user to the given $returnurl
 495       *
 496       * @param object $gtree The grade tree (a recursive representation of the grade categories and grade items)
 497       * @param array $eids
 498       * @param string $returnurl
 499       */
 500      function move_elements($eids, $returnurl) {
 501          $moveafter = required_param('moveafter', PARAM_INT);
 502  
 503          if (!is_array($eids)) {
 504              $eids = array($eids);
 505          }
 506  
 507          if(!$after_el = $this->gtree->locate_element("cg$moveafter")) {
 508              throw new \moodle_exception('invalidelementid', '', $returnurl);
 509          }
 510  
 511          $after = $after_el['object'];
 512          $parent = $after;
 513          $sortorder = $after->get_sortorder();
 514  
 515          foreach ($eids as $eid) {
 516              if (!$element = $this->gtree->locate_element($eid)) {
 517                  throw new \moodle_exception('invalidelementid', '', $returnurl);
 518              }
 519              $object = $element['object'];
 520  
 521              $object->set_parent($parent->id);
 522              $object->move_after_sortorder($sortorder);
 523              $sortorder++;
 524          }
 525  
 526          redirect($returnurl, '', 0);
 527      }
 528  
 529      /**
 530       * Recurses through the entire grade tree to find and return the maximum depth of the tree.
 531       * This should be run only once from the root element (course category), and is used for the
 532       * indentation of the Name column's cells (colspan)
 533       *
 534       * @param array $element An array of values representing a grade tree's element (all grade items in this case)
 535       * @param int $level The level of the current recursion
 536       * @param int $deepest_level A value passed to each subsequent level of recursion and incremented if $level > $deepest_level
 537       * @return int Deepest level
 538       */
 539      function get_deepest_level($element, $level=0, $deepest_level=1) {
 540          $object = $element['object'];
 541  
 542          $level++;
 543          $coefstring = $element['object']->get_coefstring();
 544          if ($element['type'] == 'category') {
 545              if ($coefstring == 'aggregationcoefweight' || $coefstring == 'aggregationcoefextraweightsum' ||
 546                      $coefstring == 'aggregationcoefextraweight') {
 547                  $this->uses_weight = true;
 548              }
 549  
 550              foreach($element['children'] as $child_el) {
 551                  if ($level > $deepest_level) {
 552                      $deepest_level = $level;
 553                  }
 554                  $deepest_level = $this->get_deepest_level($child_el, $level, $deepest_level);
 555              }
 556  
 557              $category = grade_category::fetch(array('id' => $object->id));
 558              $item = $category->get_grade_item();
 559              if ($item->gradetype == GRADE_TYPE_NONE) {
 560                  // Add 1 more level for grade category that has no total.
 561                  $deepest_level++;
 562              }
 563          }
 564  
 565          return $deepest_level;
 566      }
 567  
 568      /**
 569       * Updates the provided gradecategory item with the provided data.
 570       *
 571       * @param grade_category $gradecategory The category to update.
 572       * @param stdClass $data the data to update the category with.
 573       * @return void
 574       */
 575      public static function update_gradecategory(grade_category $gradecategory, stdClass $data) {
 576          // If no fullname is entered for a course category, put ? in the DB.
 577          if (!isset($data->fullname) || $data->fullname == '') {
 578              $data->fullname = '?';
 579          }
 580  
 581          if (!isset($data->aggregateonlygraded)) {
 582              $data->aggregateonlygraded = 0;
 583          }
 584          if (!isset($data->aggregateoutcomes)) {
 585              $data->aggregateoutcomes = 0;
 586          }
 587          grade_category::set_properties($gradecategory, $data);
 588  
 589          // CATEGORY.
 590          if (empty($gradecategory->id)) {
 591              $gradecategory->insert();
 592  
 593          } else {
 594              $gradecategory->update();
 595          }
 596  
 597          // GRADE ITEM.
 598          // Grade item data saved with prefix "grade_item_".
 599          $itemdata = new stdClass();
 600          foreach ($data as $k => $v) {
 601              if (preg_match('/grade_item_(.*)/', $k, $matches)) {
 602                  $itemdata->{$matches[1]} = $v;
 603              }
 604          }
 605  
 606          if (!isset($itemdata->aggregationcoef)) {
 607              $itemdata->aggregationcoef = 0;
 608          }
 609  
 610          if (!isset($itemdata->gradepass) || $itemdata->gradepass == '') {
 611              $itemdata->gradepass = 0;
 612          }
 613  
 614          if (!isset($itemdata->grademax) || $itemdata->grademax == '') {
 615              $itemdata->grademax = 0;
 616          }
 617  
 618          if (!isset($itemdata->grademin) || $itemdata->grademin == '') {
 619              $itemdata->grademin = 0;
 620          }
 621  
 622          $hidden      = empty($itemdata->hidden) ? 0 : $itemdata->hidden;
 623          $hiddenuntil = empty($itemdata->hiddenuntil) ? 0 : $itemdata->hiddenuntil;
 624          unset($itemdata->hidden);
 625          unset($itemdata->hiddenuntil);
 626  
 627          $locked   = empty($itemdata->locked) ? 0 : $itemdata->locked;
 628          $locktime = empty($itemdata->locktime) ? 0 : $itemdata->locktime;
 629          unset($itemdata->locked);
 630          unset($itemdata->locktime);
 631  
 632          $convert = array('grademax', 'grademin', 'gradepass', 'multfactor', 'plusfactor', 'aggregationcoef', 'aggregationcoef2');
 633          foreach ($convert as $param) {
 634              if (property_exists($itemdata, $param)) {
 635                  $itemdata->$param = unformat_float($itemdata->$param);
 636              }
 637          }
 638          if (isset($itemdata->aggregationcoef2)) {
 639              $itemdata->aggregationcoef2 = $itemdata->aggregationcoef2 / 100.0;
 640          }
 641  
 642          // When creating a new category, a number of grade item fields are filled out automatically, and are required.
 643          // If the user leaves these fields empty during creation of a category, we let the default values take effect.
 644          // Otherwise, we let the user-entered grade item values take effect.
 645          $gradeitem = $gradecategory->load_grade_item();
 646          $gradeitemcopy = fullclone($gradeitem);
 647          grade_item::set_properties($gradeitem, $itemdata);
 648  
 649          if (empty($gradeitem->id)) {
 650              $gradeitem->id = $gradeitemcopy->id;
 651          }
 652          if (empty($gradeitem->grademax) && $gradeitem->grademax != '0') {
 653              $gradeitem->grademax = $gradeitemcopy->grademax;
 654          }
 655          if (empty($gradeitem->grademin) && $gradeitem->grademin != '0') {
 656              $gradeitem->grademin = $gradeitemcopy->grademin;
 657          }
 658          if (empty($gradeitem->gradepass) && $gradeitem->gradepass != '0') {
 659              $gradeitem->gradepass = $gradeitemcopy->gradepass;
 660          }
 661          if (empty($gradeitem->aggregationcoef) && $gradeitem->aggregationcoef != '0') {
 662              $gradeitem->aggregationcoef = $gradeitemcopy->aggregationcoef;
 663          }
 664  
 665          // Handle null decimals value - must be done before update!
 666          if (!property_exists($itemdata, 'decimals') or $itemdata->decimals < 0) {
 667              $gradeitem->decimals = null;
 668          }
 669  
 670          // Change weightoverride flag. Check if the value is set, because it is not when the checkbox is not ticked.
 671          $itemdata->weightoverride = isset($itemdata->weightoverride) ? $itemdata->weightoverride : 0;
 672          if ($gradeitem->weightoverride != $itemdata->weightoverride && $gradecategory->aggregation == GRADE_AGGREGATE_SUM) {
 673              // If we are using natural weight and the weight has been un-overriden, force parent category to recalculate weights.
 674              $gradecategory->force_regrading();
 675          }
 676          $gradeitem->weightoverride = $itemdata->weightoverride;
 677  
 678          $gradeitem->outcomeid = null;
 679  
 680          // This means we want to rescale overridden grades as well.
 681          if (!empty($data->grade_item_rescalegrades) && $data->grade_item_rescalegrades == 'yes') {
 682              $gradeitem->markasoverriddenwhengraded = false;
 683              $gradeitem->rescale_grades_keep_percentage($gradeitemcopy->grademin, $gradeitemcopy->grademax,
 684                  $gradeitem->grademin, $gradeitem->grademax, 'gradebook');
 685          }
 686  
 687          // Only update the category's 'hidden' status if it has changed. Leaving a category as 'unhidden' (checkbox left
 688          // unmarked) and submitting the form without this conditional check will result in displaying any grade items that
 689          // are in the category, including those that were previously 'hidden'.
 690          if (($gradecategory->get_hidden() != $hiddenuntil) || ($gradecategory->get_hidden() != $hidden)) {
 691              if ($hiddenuntil) {
 692                  $gradecategory->set_hidden($hiddenuntil, true);
 693              } else {
 694                  $gradecategory->set_hidden($hidden, true);
 695              }
 696          }
 697  
 698          $gradeitem->set_locktime($locktime); // Locktime first - it might be removed when unlocking.
 699          $gradeitem->set_locked($locked, false, true);
 700  
 701          $gradeitem->update(); // We don't need to insert it, it's already created when the category is created.
 702  
 703          // Set parent if needed.
 704          if (isset($data->parentcategory)) {
 705              $gradecategory->set_parent($data->parentcategory, 'gradebook');
 706          }
 707      }
 708  }
 709  
 710  /**
 711   * Class grade_edit_tree_column
 712   *
 713   * @package   core_grades
 714   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 715   */
 716  abstract class grade_edit_tree_column {
 717      public $forced;
 718      public $hidden;
 719      public $forced_hidden;
 720      public $advanced_hidden;
 721      public $hide_when_moving = true;
 722      /**
 723       * html_table_cell object used as a template for header cells in all categories.
 724       * It must be cloned before being used.
 725       * @var html_table_cell $headercell
 726       */
 727      public $headercell;
 728      /**
 729       * html_table_cell object used as a template for category cells in all categories.
 730       * It must be cloned before being used.
 731       * @var html_table_cell $categorycell
 732       */
 733      public $categorycell;
 734      /**
 735       * html_table_cell object used as a template for item cells in all categories.
 736       * It must be cloned before being used.
 737       * @var html_table_cell $itemcell
 738       */
 739      public $itemcell;
 740  
 741      public static function factory($name, $params=array()) {
 742          $class_name = "grade_edit_tree_column_$name";
 743          if (class_exists($class_name)) {
 744              return new $class_name($params);
 745          }
 746      }
 747  
 748      public abstract function get_header_cell();
 749  
 750      public function get_category_cell($category, $levelclass, $params) {
 751          $cell = clone($this->categorycell);
 752          $cell->attributes['class'] .= ' ' . $levelclass;
 753          return $cell;
 754      }
 755  
 756      public function get_item_cell($item, $params) {
 757          $cell = clone($this->itemcell);
 758          if (isset($params['level'])) {
 759              $level = $params['level'] + (($item->itemtype == 'category' || $item->itemtype == 'course') ? 0 : 1);
 760              $cell->attributes['class'] .= ' level' . $level;
 761              $cell->attributes['class'] .= ' level' . ($level % 2 ? 'odd' : 'even');
 762          }
 763          return $cell;
 764      }
 765  
 766      public function __construct() {
 767          $this->headercell = new html_table_cell();
 768          $this->headercell->header = true;
 769          $this->headercell->attributes['class'] = 'header';
 770  
 771          $this->categorycell = new html_table_cell();
 772          $this->categorycell->attributes['class']  = 'cell';
 773  
 774          $this->itemcell = new html_table_cell();
 775          $this->itemcell->attributes['class'] = 'cell';
 776  
 777          if (preg_match('/^grade_edit_tree_column_(\w*)$/', get_class($this), $matches)) {
 778              $this->headercell->attributes['class'] .= ' column-' . $matches[1];
 779              $this->categorycell->attributes['class'] .= ' column-' . $matches[1];
 780              $this->itemcell->attributes['class'] .= ' column-' . $matches[1];
 781          }
 782      }
 783  }
 784  
 785  /**
 786   * Class grade_edit_tree_column_name
 787   *
 788   * @package   core_grades
 789   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 790   */
 791  class grade_edit_tree_column_name extends grade_edit_tree_column {
 792      public $forced = false;
 793      public $hidden = false;
 794      public $forced_hidden = false;
 795      public $advanced_hidden = false;
 796      public $deepest_level = 1;
 797      public $hide_when_moving = false;
 798  
 799      public function __construct($params) {
 800          if (empty($params['deepest_level'])) {
 801              throw new Exception('Tried to instantiate a grade_edit_tree_column_name object without the "deepest_level" param!');
 802          }
 803  
 804          $this->deepest_level = $params['deepest_level'];
 805          parent::__construct();
 806      }
 807  
 808      public function get_header_cell() {
 809          $headercell = clone($this->headercell);
 810          $headercell->colspan = $this->deepest_level + 1;
 811          $headercell->text = get_string('name');
 812          return $headercell;
 813      }
 814  
 815      public function get_category_cell($category, $levelclass, $params) {
 816          global $OUTPUT;
 817          if (empty($params['name']) || empty($params['level'])) {
 818              throw new Exception('Array key (name or level) missing from 3rd param of grade_edit_tree_column_name::get_category_cell($category, $levelclass, $params)');
 819          }
 820          $moveaction = isset($params['moveaction']) ? $params['moveaction'] : '';
 821          $categorycell = parent::get_category_cell($category, $levelclass, $params);
 822          $categorycell->colspan = ($this->deepest_level +1) - $params['level'];
 823          $categorycell->text = $OUTPUT->heading($moveaction . $params['name'], 4);
 824          return $categorycell;
 825      }
 826  
 827      public function get_item_cell($item, $params) {
 828          global $CFG;
 829  
 830          if (empty($params['element']) || empty($params['name']) || empty($params['level'])) {
 831              throw new Exception('Array key (name, level or element) missing from 2nd param of grade_edit_tree_column_name::get_item_cell($item, $params)');
 832          }
 833  
 834          $name = $params['name'];
 835          $moveaction = isset($params['moveaction']) ? $params['moveaction'] : '';
 836  
 837          $itemcell = parent::get_item_cell($item, $params);
 838          $itemcell->colspan = ($this->deepest_level + 1) - $params['level'];
 839          $itemcell->text = $moveaction . $name;
 840          return $itemcell;
 841      }
 842  }
 843  
 844  /**
 845   * Class grade_edit_tree_column_weight
 846   *
 847   * @package   core_grades
 848   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 849   */
 850  class grade_edit_tree_column_weight extends grade_edit_tree_column {
 851  
 852      public function get_header_cell() {
 853          global $OUTPUT;
 854          $headercell = clone($this->headercell);
 855          $headercell->text = get_string('weights', 'grades').$OUTPUT->help_icon('aggregationcoefweight', 'grades');
 856          return $headercell;
 857      }
 858  
 859      public function get_category_cell($category, $levelclass, $params) {
 860  
 861          $item = $category->get_grade_item();
 862          $categorycell = parent::get_category_cell($category, $levelclass, $params);
 863          $categorycell->text = grade_edit_tree::get_weight_input($item);
 864          return $categorycell;
 865      }
 866  
 867      public function get_item_cell($item, $params) {
 868          global $CFG;
 869          if (empty($params['element'])) {
 870              throw new Exception('Array key (element) missing from 2nd param of grade_edit_tree_column_weightorextracredit::get_item_cell($item, $params)');
 871          }
 872          $itemcell = parent::get_item_cell($item, $params);
 873          $itemcell->text = '&nbsp;';
 874          $object = $params['element']['object'];
 875  
 876          if (!in_array($object->itemtype, array('courseitem', 'categoryitem', 'category'))
 877                  && !in_array($object->gradetype, array(GRADE_TYPE_NONE, GRADE_TYPE_TEXT))
 878                  && (!$object->is_outcome_item() || $object->load_parent_category()->aggregateoutcomes)
 879                  && ($object->gradetype != GRADE_TYPE_SCALE || !empty($CFG->grade_includescalesinaggregation))) {
 880              $itemcell->text = grade_edit_tree::get_weight_input($item);
 881          }
 882  
 883          return $itemcell;
 884      }
 885  }
 886  
 887  /**
 888   * Class grade_edit_tree_column_range
 889   *
 890   * @package   core_grades
 891   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 892   */
 893  class grade_edit_tree_column_range extends grade_edit_tree_column {
 894  
 895      public function get_header_cell() {
 896          $headercell = clone($this->headercell);
 897          $headercell->text = get_string('maxgrade', 'grades');
 898          return $headercell;
 899      }
 900  
 901      public function get_category_cell($category, $levelclass, $params) {
 902          $categorycell = parent::get_category_cell($category, $levelclass, $params);
 903          $categorycell->text = ' - ';
 904          return $categorycell;
 905      }
 906  
 907      public function get_item_cell($item, $params) {
 908          global $DB, $OUTPUT;
 909  
 910          // If the parent aggregation is Natural, we should show the number, even for scales, as that value is used...
 911          // ...in the computation. For text grades, the grademax is not used, so we can still show the no value string.
 912          $parentcat = $item->get_parent_category();
 913          if ($item->gradetype == GRADE_TYPE_TEXT) {
 914              $grademax = ' - ';
 915          } else if ($item->gradetype == GRADE_TYPE_SCALE) {
 916              $scale = $DB->get_record('scale', array('id' => $item->scaleid));
 917              $scale_items = null;
 918              if (empty($scale)) { //if the item is using a scale that's been removed
 919                  $scale_items = array();
 920              } else {
 921                  $scale_items = explode(',', $scale->scale);
 922              }
 923              if ($parentcat->aggregation == GRADE_AGGREGATE_SUM) {
 924                  $grademax = end($scale_items) . ' (' .
 925                          format_float($item->grademax, $item->get_decimals()) . ')';
 926              } else {
 927                  $grademax = end($scale_items) . ' (' . count($scale_items) . ')';
 928              }
 929          } else {
 930              $grademax = format_float($item->grademax, $item->get_decimals());
 931          }
 932  
 933          $isextracredit = false;
 934          if ($item->aggregationcoef > 0) {
 935              // For category grade items, we need the grandparent category.
 936              // The parent is just category the grade item represents.
 937              if ($item->is_category_item()) {
 938                  $grandparentcat = $parentcat->get_parent_category();
 939                  if ($grandparentcat->is_extracredit_used()) {
 940                      $isextracredit = true;
 941                  }
 942              } else if ($parentcat->is_extracredit_used()) {
 943                  $isextracredit = true;
 944              }
 945          }
 946          if ($isextracredit) {
 947              $grademax .= ' ' . html_writer::tag('abbr', get_string('aggregationcoefextrasumabbr', 'grades'),
 948                  array('title' => get_string('aggregationcoefextrasum', 'grades')));
 949          }
 950  
 951          $itemcell = parent::get_item_cell($item, $params);
 952          $itemcell->text = $grademax;
 953          return $itemcell;
 954      }
 955  }
 956  
 957  /**
 958   * Class grade_edit_tree_column_actions
 959   *
 960   * @package   core_grades
 961   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 962   */
 963  class grade_edit_tree_column_actions extends grade_edit_tree_column {
 964  
 965      public function __construct($params) {
 966          parent::__construct();
 967      }
 968  
 969      public function get_header_cell() {
 970          $headercell = clone($this->headercell);
 971          $headercell->text = get_string('actions');
 972          return $headercell;
 973      }
 974  
 975      public function get_category_cell($category, $levelclass, $params) {
 976  
 977          if (empty($params['actions'])) {
 978              throw new Exception('Array key (actions) missing from 3rd param of grade_edit_tree_column_actions::get_category_actions($category, $levelclass, $params)');
 979          }
 980  
 981          $categorycell = parent::get_category_cell($category, $levelclass, $params);
 982          $categorycell->text = $params['actions'];
 983          return $categorycell;
 984      }
 985  
 986      public function get_item_cell($item, $params) {
 987          if (empty($params['actions'])) {
 988              throw new Exception('Array key (actions) missing from 2nd param of grade_edit_tree_column_actions::get_item_cell($item, $params)');
 989          }
 990          $itemcell = parent::get_item_cell($item, $params);
 991          $itemcell->text = $params['actions'];
 992          return $itemcell;
 993      }
 994  }
 995  
 996  /**
 997   * Class grade_edit_tree_column_select
 998   *
 999   * @package   core_grades
1000   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1001   */
1002  class grade_edit_tree_column_select extends grade_edit_tree_column {
1003  
1004      public function get_header_cell() {
1005          $headercell = clone($this->headercell);
1006          $headercell->text = get_string('select');
1007          return $headercell;
1008      }
1009  
1010      public function get_category_cell($category, $levelclass, $params) {
1011          global $OUTPUT;
1012  
1013          if (empty($params['eid'])) {
1014              throw new Exception('Array key (eid) missing from 3rd param of grade_edit_tree_column_select::get_category_cell($category, $levelclass, $params)');
1015          }
1016  
1017          // Get toggle group for this master checkbox.
1018          $togglegroup = $this->get_checkbox_togglegroup($category);
1019          // Set label for this master checkbox.
1020          $masterlabel = get_string('all');
1021          // Use category name if available.
1022          if ($category->fullname !== '?') {
1023              $masterlabel = format_string($category->fullname, true, ['escape' => false]);
1024              // Limit the displayed category name to prevent the Select column from getting too wide.
1025              if (core_text::strlen($masterlabel) > 20) {
1026                  $masterlabel = get_string('textellipsis', 'core', core_text::substr($masterlabel, 0, 12));
1027              }
1028          }
1029          // Build the master checkbox.
1030          $mastercheckbox = new \core\output\checkbox_toggleall($togglegroup, true, [
1031              'id' => 'select_category_' . $category->id,
1032              'name' => $togglegroup,
1033              'value' => 1,
1034              'classes' => 'itemselect ignoredirty',
1035              'label' => $masterlabel,
1036              // Consistent label to prevent the select column from resizing.
1037              'selectall' => $masterlabel,
1038              'deselectall' => $masterlabel,
1039              'labelclasses' => 'm-0',
1040          ]);
1041  
1042          $categorycell = parent::get_category_cell($category, $levelclass, $params);
1043          $categorycell->text = $OUTPUT->render($mastercheckbox);
1044          return $categorycell;
1045      }
1046  
1047      public function get_item_cell($item, $params) {
1048          if (empty($params['itemtype']) || empty($params['eid'])) {
1049              throw new \moodle_exception('missingitemtypeoreid', 'core_grades');
1050          }
1051          $itemcell = parent::get_item_cell($item, $params);
1052  
1053          if ($params['itemtype'] != 'course' && $params['itemtype'] != 'category') {
1054              global $OUTPUT;
1055  
1056              // Fetch the grade item's category.
1057              $category = grade_category::fetch(['id' => $item->categoryid]);
1058              $togglegroup = $this->get_checkbox_togglegroup($category);
1059  
1060              $checkboxid = 'select_' . $params['eid'];
1061              $checkbox = new \core\output\checkbox_toggleall($togglegroup, false, [
1062                  'id' => $checkboxid,
1063                  'name' => $checkboxid,
1064                  'label' => get_string('select', 'grades', $item->itemname),
1065                  'labelclasses' => 'accesshide',
1066                  'classes' => 'itemselect ignoredirty',
1067              ]);
1068              $itemcell->text = $OUTPUT->render($checkbox);
1069          }
1070          return $itemcell;
1071      }
1072  
1073      /**
1074       * Generates a toggle group name for a bulk-action checkbox based on the given grade category.
1075       *
1076       * @param grade_category $category The grade category.
1077       * @return string
1078       */
1079      protected function get_checkbox_togglegroup(grade_category $category): string {
1080          $levels = [];
1081          $categories = explode('/', $category->path);
1082          foreach ($categories as $categoryid) {
1083              $level = 'category' . $categoryid;
1084              if (!in_array($level, $levels)) {
1085                  $levels[] = 'category' . $categoryid;
1086              }
1087          }
1088          $togglegroup = implode(' ', $levels);
1089  
1090          return $togglegroup;
1091      }
1092  }