Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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_alignment(action_menu::TL, action_menu::BL);
 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['title'] = $object->stripped_name;
 316              $headercell->attributes['class'] = 'cell column-rowspan rowspan ' . $levelclass;
 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  
 342              $returnrows[] = new html_table_row(array($endcell));
 343  
 344          } else { // Dealing with a grade item
 345  
 346              $item = grade_item::fetch(array('id' => $object->id));
 347              $element['type'] = 'item';
 348              $element['object'] = $item;
 349  
 350              $categoryitemclass = '';
 351              if ($item->itemtype == 'category') {
 352                  $categoryitemclass = 'categoryitem';
 353              }
 354              if ($item->itemtype == 'course') {
 355                  $categoryitemclass = 'courseitem';
 356              }
 357  
 358              $dimmed = ($item->is_hidden()) ? "dimmed_text" : "";
 359              $gradeitemrow = new html_table_row();
 360              $gradeitemrow->id = 'grade-item-' . $eid;
 361              $gradeitemrow->attributes['class'] = $categoryitemclass . ' item ' . $dimmed;
 362              $gradeitemrow->attributes['data-itemid'] = $object->id;
 363              foreach ($rowclasses as $class) {
 364                  $gradeitemrow->attributes['class'] .= ' ' . $class;
 365              }
 366  
 367              foreach ($this->columns as $column) {
 368                  if (!($this->moving && $column->hide_when_moving)) {
 369                      $gradeitemrow->cells[] = $column->get_item_cell($item, array('id' => $id, 'name' => $object->name,
 370                          'level' => $level, 'actions' => $actions, 'element' => $element, 'eid' => $eid,
 371                          'moveaction' => $moveaction, 'itemtype' => $object->itemtype));
 372                  }
 373              }
 374  
 375              $returnrows[] = $gradeitemrow;
 376          }
 377  
 378          return $returnrows;
 379  
 380      }
 381  
 382      /**
 383       * Given a grade_item object, returns a labelled input if an aggregation coefficient (weight or extra credit) applies to it.
 384       * @param grade_item $item
 385       * @return string HTML
 386       */
 387      static function get_weight_input($item) {
 388          global $OUTPUT;
 389  
 390          if (!is_object($item) || get_class($item) !== 'grade_item') {
 391              throw new Exception('grade_edit_tree::get_weight_input($item) was given a variable that is not of the required type (grade_item object)');
 392              return false;
 393          }
 394  
 395          if ($item->is_course_item()) {
 396              return '';
 397          }
 398  
 399          $parent_category = $item->get_parent_category();
 400          $parent_category->apply_forced_settings();
 401          $aggcoef = $item->get_coefstring();
 402  
 403          $itemname = $item->itemname;
 404          if ($item->is_category_item()) {
 405              // Remember, the parent category of a category item is the category itself.
 406              $itemname = $parent_category->get_name();
 407          }
 408          $str = '';
 409  
 410          if ($aggcoef == 'aggregationcoefweight' || $aggcoef == 'aggregationcoef' || $aggcoef == 'aggregationcoefextraweight') {
 411  
 412              return $OUTPUT->render_from_template('core_grades/weight_field', [
 413                  'id' => $item->id,
 414                  'itemname' => $itemname,
 415                  'value' => self::format_number($item->aggregationcoef)
 416              ]);
 417  
 418          } else if ($aggcoef == 'aggregationcoefextraweightsum') {
 419  
 420              $tpldata = [
 421                  'id' => $item->id,
 422                  'itemname' => $itemname,
 423                  'value' => self::format_number($item->aggregationcoef2 * 100.0),
 424                  'checked' => $item->weightoverride,
 425                  'disabled' => !$item->weightoverride
 426              ];
 427              $str .= $OUTPUT->render_from_template('core_grades/weight_override_field', $tpldata);
 428  
 429          }
 430  
 431          return $str;
 432      }
 433  
 434      // Trims trailing zeros.
 435      // Used on the 'Gradebook setup' page for grade items settings like aggregation co-efficient.
 436      // Grader report has its own decimal place settings so they are handled elsewhere.
 437      static function format_number($number) {
 438          $formatted = rtrim(format_float($number, 4),'0');
 439          if (substr($formatted, -1)==get_string('decsep', 'langconfig')) { //if last char is the decimal point
 440              $formatted .= '0';
 441          }
 442          return $formatted;
 443      }
 444  
 445      /**
 446       * Given an element of the grade tree, returns whether it is deletable or not (only manual grade items are deletable)
 447       *
 448       * @param array $element
 449       * @return bool
 450       */
 451      function element_deletable($element) {
 452          global $COURSE;
 453  
 454          if ($element['type'] != 'item') {
 455              return true;
 456          }
 457  
 458          $grade_item = $element['object'];
 459  
 460          if ($grade_item->itemtype != 'mod' or $grade_item->is_outcome_item() or $grade_item->gradetype == GRADE_TYPE_NONE) {
 461              return true;
 462          }
 463  
 464          $modinfo = get_fast_modinfo($COURSE);
 465          if (!isset($modinfo->instances[$grade_item->itemmodule][$grade_item->iteminstance])) {
 466              // module does not exist
 467              return true;
 468          }
 469  
 470          return false;
 471      }
 472  
 473      /**
 474       * Given an element of the grade tree, returns whether it is duplicatable or not (only manual grade items are duplicatable)
 475       *
 476       * @param array $element
 477       * @return bool
 478       */
 479      public function element_duplicatable($element) {
 480          if ($element['type'] != 'item') {
 481              return false;
 482          }
 483  
 484          $gradeitem = $element['object'];
 485          if ($gradeitem->itemtype != 'mod') {
 486              return true;
 487          }
 488          return false;
 489      }
 490  
 491      /**
 492       * Given the grade tree and an array of element ids (e.g. c15, i42), and expecting the 'moveafter' URL param,
 493       * moves the selected items to the requested location. Then redirects the user to the given $returnurl
 494       *
 495       * @param object $gtree The grade tree (a recursive representation of the grade categories and grade items)
 496       * @param array $eids
 497       * @param string $returnurl
 498       */
 499      function move_elements($eids, $returnurl) {
 500          $moveafter = required_param('moveafter', PARAM_INT);
 501  
 502          if (!is_array($eids)) {
 503              $eids = array($eids);
 504          }
 505  
 506          if(!$after_el = $this->gtree->locate_element("cg$moveafter")) {
 507              print_error('invalidelementid', '', $returnurl);
 508          }
 509  
 510          $after = $after_el['object'];
 511          $parent = $after;
 512          $sortorder = $after->get_sortorder();
 513  
 514          foreach ($eids as $eid) {
 515              if (!$element = $this->gtree->locate_element($eid)) {
 516                  print_error('invalidelementid', '', $returnurl);
 517              }
 518              $object = $element['object'];
 519  
 520              $object->set_parent($parent->id);
 521              $object->move_after_sortorder($sortorder);
 522              $sortorder++;
 523          }
 524  
 525          redirect($returnurl, '', 0);
 526      }
 527  
 528      /**
 529       * Recurses through the entire grade tree to find and return the maximum depth of the tree.
 530       * This should be run only once from the root element (course category), and is used for the
 531       * indentation of the Name column's cells (colspan)
 532       *
 533       * @param array $element An array of values representing a grade tree's element (all grade items in this case)
 534       * @param int $level The level of the current recursion
 535       * @param int $deepest_level A value passed to each subsequent level of recursion and incremented if $level > $deepest_level
 536       * @return int Deepest level
 537       */
 538      function get_deepest_level($element, $level=0, $deepest_level=1) {
 539          $object = $element['object'];
 540  
 541          $level++;
 542          $coefstring = $element['object']->get_coefstring();
 543          if ($element['type'] == 'category') {
 544              if ($coefstring == 'aggregationcoefweight' || $coefstring == 'aggregationcoefextraweightsum' ||
 545                      $coefstring == 'aggregationcoefextraweight') {
 546                  $this->uses_weight = true;
 547              }
 548  
 549              foreach($element['children'] as $child_el) {
 550                  if ($level > $deepest_level) {
 551                      $deepest_level = $level;
 552                  }
 553                  $deepest_level = $this->get_deepest_level($child_el, $level, $deepest_level);
 554              }
 555  
 556              $category = grade_category::fetch(array('id' => $object->id));
 557              $item = $category->get_grade_item();
 558              if ($item->gradetype == GRADE_TYPE_NONE) {
 559                  // Add 1 more level for grade category that has no total.
 560                  $deepest_level++;
 561              }
 562          }
 563  
 564          return $deepest_level;
 565      }
 566  
 567      /**
 568       * Updates the provided gradecategory item with the provided data.
 569       *
 570       * @param grade_category $gradecategory The category to update.
 571       * @param stdClass $data the data to update the category with.
 572       * @return void
 573       */
 574      public static function update_gradecategory(grade_category $gradecategory, stdClass $data) {
 575          // If no fullname is entered for a course category, put ? in the DB.
 576          if (!isset($data->fullname) || $data->fullname == '') {
 577              $data->fullname = '?';
 578          }
 579  
 580          if (!isset($data->aggregateonlygraded)) {
 581              $data->aggregateonlygraded = 0;
 582          }
 583          if (!isset($data->aggregateoutcomes)) {
 584              $data->aggregateoutcomes = 0;
 585          }
 586          grade_category::set_properties($gradecategory, $data);
 587  
 588          // CATEGORY.
 589          if (empty($gradecategory->id)) {
 590              $gradecategory->insert();
 591  
 592          } else {
 593              $gradecategory->update();
 594          }
 595  
 596          // GRADE ITEM.
 597          // Grade item data saved with prefix "grade_item_".
 598          $itemdata = new stdClass();
 599          foreach ($data as $k => $v) {
 600              if (preg_match('/grade_item_(.*)/', $k, $matches)) {
 601                  $itemdata->{$matches[1]} = $v;
 602              }
 603          }
 604  
 605          if (!isset($itemdata->aggregationcoef)) {
 606              $itemdata->aggregationcoef = 0;
 607          }
 608  
 609          if (!isset($itemdata->gradepass) || $itemdata->gradepass == '') {
 610              $itemdata->gradepass = 0;
 611          }
 612  
 613          if (!isset($itemdata->grademax) || $itemdata->grademax == '') {
 614              $itemdata->grademax = 0;
 615          }
 616  
 617          if (!isset($itemdata->grademin) || $itemdata->grademin == '') {
 618              $itemdata->grademin = 0;
 619          }
 620  
 621          $hidden      = empty($itemdata->hidden) ? 0 : $itemdata->hidden;
 622          $hiddenuntil = empty($itemdata->hiddenuntil) ? 0 : $itemdata->hiddenuntil;
 623          unset($itemdata->hidden);
 624          unset($itemdata->hiddenuntil);
 625  
 626          $locked   = empty($itemdata->locked) ? 0 : $itemdata->locked;
 627          $locktime = empty($itemdata->locktime) ? 0 : $itemdata->locktime;
 628          unset($itemdata->locked);
 629          unset($itemdata->locktime);
 630  
 631          $convert = array('grademax', 'grademin', 'gradepass', 'multfactor', 'plusfactor', 'aggregationcoef', 'aggregationcoef2');
 632          foreach ($convert as $param) {
 633              if (property_exists($itemdata, $param)) {
 634                  $itemdata->$param = unformat_float($itemdata->$param);
 635              }
 636          }
 637          if (isset($itemdata->aggregationcoef2)) {
 638              $itemdata->aggregationcoef2 = $itemdata->aggregationcoef2 / 100.0;
 639          }
 640  
 641          // When creating a new category, a number of grade item fields are filled out automatically, and are required.
 642          // If the user leaves these fields empty during creation of a category, we let the default values take effect.
 643          // Otherwise, we let the user-entered grade item values take effect.
 644          $gradeitem = $gradecategory->load_grade_item();
 645          $gradeitemcopy = fullclone($gradeitem);
 646          grade_item::set_properties($gradeitem, $itemdata);
 647  
 648          if (empty($gradeitem->id)) {
 649              $gradeitem->id = $gradeitemcopy->id;
 650          }
 651          if (empty($gradeitem->grademax) && $gradeitem->grademax != '0') {
 652              $gradeitem->grademax = $gradeitemcopy->grademax;
 653          }
 654          if (empty($gradeitem->grademin) && $gradeitem->grademin != '0') {
 655              $gradeitem->grademin = $gradeitemcopy->grademin;
 656          }
 657          if (empty($gradeitem->gradepass) && $gradeitem->gradepass != '0') {
 658              $gradeitem->gradepass = $gradeitemcopy->gradepass;
 659          }
 660          if (empty($gradeitem->aggregationcoef) && $gradeitem->aggregationcoef != '0') {
 661              $gradeitem->aggregationcoef = $gradeitemcopy->aggregationcoef;
 662          }
 663  
 664          // Handle null decimals value - must be done before update!
 665          if (!property_exists($itemdata, 'decimals') or $itemdata->decimals < 0) {
 666              $gradeitem->decimals = null;
 667          }
 668  
 669          // Change weightoverride flag. Check if the value is set, because it is not when the checkbox is not ticked.
 670          $itemdata->weightoverride = isset($itemdata->weightoverride) ? $itemdata->weightoverride : 0;
 671          if ($gradeitem->weightoverride != $itemdata->weightoverride && $gradecategory->aggregation == GRADE_AGGREGATE_SUM) {
 672              // If we are using natural weight and the weight has been un-overriden, force parent category to recalculate weights.
 673              $gradecategory->force_regrading();
 674          }
 675          $gradeitem->weightoverride = $itemdata->weightoverride;
 676  
 677          $gradeitem->outcomeid = null;
 678  
 679          // This means we want to rescale overridden grades as well.
 680          if (!empty($data->grade_item_rescalegrades) && $data->grade_item_rescalegrades == 'yes') {
 681              $gradeitem->markasoverriddenwhengraded = false;
 682              $gradeitem->rescale_grades_keep_percentage($gradeitemcopy->grademin, $gradeitemcopy->grademax,
 683                  $gradeitem->grademin, $gradeitem->grademax, 'gradebook');
 684          }
 685  
 686          // Only update the category's 'hidden' status if it has changed. Leaving a category as 'unhidden' (checkbox left
 687          // unmarked) and submitting the form without this conditional check will result in displaying any grade items that
 688          // are in the category, including those that were previously 'hidden'.
 689          if (($gradecategory->get_hidden() != $hiddenuntil) || ($gradecategory->get_hidden() != $hidden)) {
 690              if ($hiddenuntil) {
 691                  $gradecategory->set_hidden($hiddenuntil, true);
 692              } else {
 693                  $gradecategory->set_hidden($hidden, true);
 694              }
 695          }
 696  
 697          $gradeitem->set_locktime($locktime); // Locktime first - it might be removed when unlocking.
 698          $gradeitem->set_locked($locked, false, true);
 699  
 700          $gradeitem->update(); // We don't need to insert it, it's already created when the category is created.
 701  
 702          // Set parent if needed.
 703          if (isset($data->parentcategory)) {
 704              $gradecategory->set_parent($data->parentcategory, 'gradebook');
 705          }
 706      }
 707  }
 708  
 709  /**
 710   * Class grade_edit_tree_column
 711   *
 712   * @package   core_grades
 713   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 714   */
 715  abstract class grade_edit_tree_column {
 716      public $forced;
 717      public $hidden;
 718      public $forced_hidden;
 719      public $advanced_hidden;
 720      public $hide_when_moving = true;
 721      /**
 722       * html_table_cell object used as a template for header cells in all categories.
 723       * It must be cloned before being used.
 724       * @var html_table_cell $headercell
 725       */
 726      public $headercell;
 727      /**
 728       * html_table_cell object used as a template for category cells in all categories.
 729       * It must be cloned before being used.
 730       * @var html_table_cell $categorycell
 731       */
 732      public $categorycell;
 733      /**
 734       * html_table_cell object used as a template for item cells in all categories.
 735       * It must be cloned before being used.
 736       * @var html_table_cell $itemcell
 737       */
 738      public $itemcell;
 739  
 740      public static function factory($name, $params=array()) {
 741          $class_name = "grade_edit_tree_column_$name";
 742          if (class_exists($class_name)) {
 743              return new $class_name($params);
 744          }
 745      }
 746  
 747      public abstract function get_header_cell();
 748  
 749      public function get_category_cell($category, $levelclass, $params) {
 750          $cell = clone($this->categorycell);
 751          $cell->attributes['class'] .= ' ' . $levelclass;
 752          $cell->attributes['text'] = '';
 753          return $cell;
 754      }
 755  
 756      public function get_item_cell($item, $params) {
 757          $cell = clone($this->itemcell);
 758          $cell->attributes['text'] = '';
 759          if (isset($params['level'])) {
 760              $level = $params['level'] + (($item->itemtype == 'category' || $item->itemtype == 'course') ? 0 : 1);
 761              $cell->attributes['class'] .= ' level' . $level;
 762              $cell->attributes['class'] .= ' level' . ($level % 2 ? 'odd' : 'even');
 763          }
 764          return $cell;
 765      }
 766  
 767      public function __construct() {
 768          $this->headercell = new html_table_cell();
 769          $this->headercell->header = true;
 770          $this->headercell->attributes['class'] = 'header';
 771  
 772          $this->categorycell = new html_table_cell();
 773          $this->categorycell->attributes['class']  = 'cell';
 774  
 775          $this->itemcell = new html_table_cell();
 776          $this->itemcell->attributes['class'] = 'cell';
 777  
 778          if (preg_match('/^grade_edit_tree_column_(\w*)$/', get_class($this), $matches)) {
 779              $this->headercell->attributes['class'] .= ' column-' . $matches[1];
 780              $this->categorycell->attributes['class'] .= ' column-' . $matches[1];
 781              $this->itemcell->attributes['class'] .= ' column-' . $matches[1];
 782          }
 783      }
 784  }
 785  
 786  /**
 787   * Class grade_edit_tree_column_name
 788   *
 789   * @package   core_grades
 790   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 791   */
 792  class grade_edit_tree_column_name extends grade_edit_tree_column {
 793      public $forced = false;
 794      public $hidden = false;
 795      public $forced_hidden = false;
 796      public $advanced_hidden = false;
 797      public $deepest_level = 1;
 798      public $hide_when_moving = false;
 799  
 800      public function __construct($params) {
 801          if (empty($params['deepest_level'])) {
 802              throw new Exception('Tried to instantiate a grade_edit_tree_column_name object without the "deepest_level" param!');
 803          }
 804  
 805          $this->deepest_level = $params['deepest_level'];
 806          parent::__construct();
 807      }
 808  
 809      public function get_header_cell() {
 810          $headercell = clone($this->headercell);
 811          $headercell->colspan = $this->deepest_level + 1;
 812          $headercell->text = get_string('name');
 813          return $headercell;
 814      }
 815  
 816      public function get_category_cell($category, $levelclass, $params) {
 817          global $OUTPUT;
 818          if (empty($params['name']) || empty($params['level'])) {
 819              throw new Exception('Array key (name or level) missing from 3rd param of grade_edit_tree_column_name::get_category_cell($category, $levelclass, $params)');
 820          }
 821          $moveaction = isset($params['moveaction']) ? $params['moveaction'] : '';
 822          $categorycell = parent::get_category_cell($category, $levelclass, $params);
 823          $categorycell->colspan = ($this->deepest_level +1) - $params['level'];
 824          $categorycell->text = $OUTPUT->heading($moveaction . $params['name'], 4);
 825          return $categorycell;
 826      }
 827  
 828      public function get_item_cell($item, $params) {
 829          global $CFG;
 830  
 831          if (empty($params['element']) || empty($params['name']) || empty($params['level'])) {
 832              throw new Exception('Array key (name, level or element) missing from 2nd param of grade_edit_tree_column_name::get_item_cell($item, $params)');
 833          }
 834  
 835          $name = $params['name'];
 836          $moveaction = isset($params['moveaction']) ? $params['moveaction'] : '';
 837  
 838          $itemcell = parent::get_item_cell($item, $params);
 839          $itemcell->colspan = ($this->deepest_level + 1) - $params['level'];
 840          $itemcell->text = $moveaction . $name;
 841          return $itemcell;
 842      }
 843  }
 844  
 845  /**
 846   * Class grade_edit_tree_column_weight
 847   *
 848   * @package   core_grades
 849   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 850   */
 851  class grade_edit_tree_column_weight extends grade_edit_tree_column {
 852  
 853      public function get_header_cell() {
 854          global $OUTPUT;
 855          $headercell = clone($this->headercell);
 856          $headercell->text = get_string('weights', 'grades').$OUTPUT->help_icon('aggregationcoefweight', 'grades');
 857          return $headercell;
 858      }
 859  
 860      public function get_category_cell($category, $levelclass, $params) {
 861  
 862          $item = $category->get_grade_item();
 863          $categorycell = parent::get_category_cell($category, $levelclass, $params);
 864          $categorycell->text = grade_edit_tree::get_weight_input($item);
 865          return $categorycell;
 866      }
 867  
 868      public function get_item_cell($item, $params) {
 869          global $CFG;
 870          if (empty($params['element'])) {
 871              throw new Exception('Array key (element) missing from 2nd param of grade_edit_tree_column_weightorextracredit::get_item_cell($item, $params)');
 872          }
 873          $itemcell = parent::get_item_cell($item, $params);
 874          $itemcell->text = '&nbsp;';
 875          $object = $params['element']['object'];
 876  
 877          if (!in_array($object->itemtype, array('courseitem', 'categoryitem', 'category'))
 878                  && !in_array($object->gradetype, array(GRADE_TYPE_NONE, GRADE_TYPE_TEXT))
 879                  && (!$object->is_outcome_item() || $object->load_parent_category()->aggregateoutcomes)
 880                  && ($object->gradetype != GRADE_TYPE_SCALE || !empty($CFG->grade_includescalesinaggregation))) {
 881              $itemcell->text = grade_edit_tree::get_weight_input($item);
 882          }
 883  
 884          return $itemcell;
 885      }
 886  }
 887  
 888  /**
 889   * Class grade_edit_tree_column_range
 890   *
 891   * @package   core_grades
 892   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 893   */
 894  class grade_edit_tree_column_range extends grade_edit_tree_column {
 895  
 896      public function get_header_cell() {
 897          $headercell = clone($this->headercell);
 898          $headercell->text = get_string('maxgrade', 'grades');
 899          return $headercell;
 900      }
 901  
 902      public function get_category_cell($category, $levelclass, $params) {
 903          $categorycell = parent::get_category_cell($category, $levelclass, $params);
 904          $categorycell->text = ' - ';
 905          return $categorycell;
 906      }
 907  
 908      public function get_item_cell($item, $params) {
 909          global $DB, $OUTPUT;
 910  
 911          // If the parent aggregation is Natural, we should show the number, even for scales, as that value is used...
 912          // ...in the computation. For text grades, the grademax is not used, so we can still show the no value string.
 913          $parentcat = $item->get_parent_category();
 914          if ($item->gradetype == GRADE_TYPE_TEXT) {
 915              $grademax = ' - ';
 916          } else if ($item->gradetype == GRADE_TYPE_SCALE) {
 917              $scale = $DB->get_record('scale', array('id' => $item->scaleid));
 918              $scale_items = null;
 919              if (empty($scale)) { //if the item is using a scale that's been removed
 920                  $scale_items = array();
 921              } else {
 922                  $scale_items = explode(',', $scale->scale);
 923              }
 924              if ($parentcat->aggregation == GRADE_AGGREGATE_SUM) {
 925                  $grademax = end($scale_items) . ' (' .
 926                          format_float($item->grademax, $item->get_decimals()) . ')';
 927              } else {
 928                  $grademax = end($scale_items) . ' (' . count($scale_items) . ')';
 929              }
 930          } else {
 931              $grademax = format_float($item->grademax, $item->get_decimals());
 932          }
 933  
 934          $isextracredit = false;
 935          if ($item->aggregationcoef > 0) {
 936              // For category grade items, we need the grandparent category.
 937              // The parent is just category the grade item represents.
 938              if ($item->is_category_item()) {
 939                  $grandparentcat = $parentcat->get_parent_category();
 940                  if ($grandparentcat->is_extracredit_used()) {
 941                      $isextracredit = true;
 942                  }
 943              } else if ($parentcat->is_extracredit_used()) {
 944                  $isextracredit = true;
 945              }
 946          }
 947          if ($isextracredit) {
 948              $grademax .= ' ' . html_writer::tag('abbr', get_string('aggregationcoefextrasumabbr', 'grades'),
 949                  array('title' => get_string('aggregationcoefextrasum', 'grades')));
 950          }
 951  
 952          $itemcell = parent::get_item_cell($item, $params);
 953          $itemcell->text = $grademax;
 954          return $itemcell;
 955      }
 956  }
 957  
 958  /**
 959   * Class grade_edit_tree_column_actions
 960   *
 961   * @package   core_grades
 962   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 963   */
 964  class grade_edit_tree_column_actions extends grade_edit_tree_column {
 965  
 966      public function __construct($params) {
 967          parent::__construct();
 968      }
 969  
 970      public function get_header_cell() {
 971          $headercell = clone($this->headercell);
 972          $headercell->text = get_string('actions');
 973          return $headercell;
 974      }
 975  
 976      public function get_category_cell($category, $levelclass, $params) {
 977  
 978          if (empty($params['actions'])) {
 979              throw new Exception('Array key (actions) missing from 3rd param of grade_edit_tree_column_actions::get_category_actions($category, $levelclass, $params)');
 980          }
 981  
 982          $categorycell = parent::get_category_cell($category, $levelclass, $params);
 983          $categorycell->text = $params['actions'];
 984          return $categorycell;
 985      }
 986  
 987      public function get_item_cell($item, $params) {
 988          if (empty($params['actions'])) {
 989              throw new Exception('Array key (actions) missing from 2nd param of grade_edit_tree_column_actions::get_item_cell($item, $params)');
 990          }
 991          $itemcell = parent::get_item_cell($item, $params);
 992          $itemcell->text = $params['actions'];
 993          return $itemcell;
 994      }
 995  }
 996  
 997  /**
 998   * Class grade_edit_tree_column_select
 999   *
1000   * @package   core_grades
1001   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1002   */
1003  class grade_edit_tree_column_select extends grade_edit_tree_column {
1004  
1005      public function get_header_cell() {
1006          $headercell = clone($this->headercell);
1007          $headercell->text = get_string('select');
1008          return $headercell;
1009      }
1010  
1011      public function get_category_cell($category, $levelclass, $params) {
1012          global $OUTPUT;
1013  
1014          if (empty($params['eid'])) {
1015              throw new Exception('Array key (eid) missing from 3rd param of grade_edit_tree_column_select::get_category_cell($category, $levelclass, $params)');
1016          }
1017  
1018          // Get toggle group for this master checkbox.
1019          $togglegroup = $this->get_checkbox_togglegroup($category);
1020          // Set label for this master checkbox.
1021          $masterlabel = get_string('all');
1022          // Use category name if available.
1023          if ($category->fullname !== '?') {
1024              $masterlabel = format_string($category->fullname, true, ['escape' => false]);
1025              // Limit the displayed category name to prevent the Select column from getting too wide.
1026              if (core_text::strlen($masterlabel) > 20) {
1027                  $masterlabel = get_string('textellipsis', 'core', core_text::substr($masterlabel, 0, 12));
1028              }
1029          }
1030          // Build the master checkbox.
1031          $mastercheckbox = new \core\output\checkbox_toggleall($togglegroup, true, [
1032              'id' => $togglegroup,
1033              'name' => $togglegroup,
1034              'value' => 1,
1035              'classes' => 'itemselect ignoredirty',
1036              'label' => $masterlabel,
1037              // Consistent label to prevent the select column from resizing.
1038              'selectall' => $masterlabel,
1039              'deselectall' => $masterlabel,
1040              'labelclasses' => 'm-0',
1041          ]);
1042  
1043          $categorycell = parent::get_category_cell($category, $levelclass, $params);
1044          $categorycell->text = $OUTPUT->render($mastercheckbox);
1045          return $categorycell;
1046      }
1047  
1048      public function get_item_cell($item, $params) {
1049          if (empty($params['itemtype']) || empty($params['eid'])) {
1050              print_error('missingitemtypeoreid', 'core_grades');
1051          }
1052          $itemcell = parent::get_item_cell($item, $params);
1053  
1054          if ($params['itemtype'] != 'course' && $params['itemtype'] != 'category') {
1055              global $OUTPUT;
1056  
1057              // Fetch the grade item's category.
1058              $category = grade_category::fetch(['id' => $item->categoryid]);
1059              $togglegroup = $this->get_checkbox_togglegroup($category);
1060  
1061              $checkboxid = 'select_' . $params['eid'];
1062              $checkbox = new \core\output\checkbox_toggleall($togglegroup, false, [
1063                  'id' => $checkboxid,
1064                  'name' => $checkboxid,
1065                  'label' => get_string('select', 'grades', $item->itemname),
1066                  'labelclasses' => 'accesshide',
1067                  'classes' => 'itemselect ignoredirty',
1068              ]);
1069              $itemcell->text = $OUTPUT->render($checkbox);
1070          }
1071          return $itemcell;
1072      }
1073  
1074      /**
1075       * Generates a toggle group name for a bulk-action checkbox based on the given grade category.
1076       *
1077       * @param grade_category $category The grade category.
1078       * @return string
1079       */
1080      protected function get_checkbox_togglegroup(grade_category $category): string {
1081          $levels = [];
1082          $categories = explode('/', $category->path);
1083          foreach ($categories as $categoryid) {
1084              $level = 'category' . $categoryid;
1085              if (!in_array($level, $levels)) {
1086                  $levels[] = 'category' . $categoryid;
1087              }
1088          }
1089          $togglegroup = implode(' ', $levels);
1090  
1091          return $togglegroup;
1092      }
1093  }