Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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  namespace core_grades\form;
  18  
  19  use context;
  20  use context_course;
  21  use core_form\dynamic_form;
  22  use grade_category;
  23  use grade_edit_tree;
  24  use grade_helper;
  25  use grade_item;
  26  use grade_plugin_return;
  27  use grade_scale;
  28  use moodle_url;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  require_once($CFG->dirroot . '/grade/lib.php');
  33  require_once($CFG->dirroot . '/grade/edit/tree/lib.php');
  34  
  35  /**
  36   * Prints the add category gradebook form
  37   *
  38   * @copyright 2023 Ilya Tregubov <ilya@moodle.com>
  39   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  40   * @package core_grades
  41   */
  42  class add_category extends dynamic_form {
  43  
  44      /** Grade plugin return tracking object.
  45       * @var object $gpr
  46       */
  47      public $gpr;
  48  
  49      /** Available aggregations.
  50       * @var array|null $aggregation_options
  51       */
  52      private ?array $aggregation_options;
  53  
  54      /**
  55       * Helper function to grab the current grade category based on information within the form.
  56       *
  57       * @return array
  58       * @throws \moodle_exception
  59       */
  60      private function get_gradecategory(): array {
  61          $courseid = $this->optional_param('courseid', null, PARAM_INT);
  62          $id = $this->optional_param('category', null, PARAM_INT);
  63  
  64          if ($gradecategory = grade_category::fetch(['id' => $id, 'courseid' => $courseid])) {
  65              $gradecategory->apply_forced_settings();
  66              $category = $gradecategory->get_record_data();
  67              // Set parent.
  68              $category->parentcategory = $gradecategory->parent;
  69              $gradeitem = $gradecategory->load_grade_item();
  70              // Normalize coef values if needed.
  71              $parentcategory = $gradecategory->get_parent_category();
  72  
  73              foreach ($gradeitem->get_record_data() as $key => $value) {
  74                  $category->{"grade_item_$key"} = $value;
  75              }
  76  
  77              $decimalpoints = $gradeitem->get_decimals();
  78  
  79              $category->grade_item_grademax   = format_float($category->grade_item_grademax, $decimalpoints);
  80              $category->grade_item_grademin   = format_float($category->grade_item_grademin, $decimalpoints);
  81              $category->grade_item_gradepass  = format_float($category->grade_item_gradepass, $decimalpoints);
  82              $category->grade_item_multfactor = format_float($category->grade_item_multfactor, 4);
  83              $category->grade_item_plusfactor = format_float($category->grade_item_plusfactor, 4);
  84              $category->grade_item_aggregationcoef2 = format_float($category->grade_item_aggregationcoef2 * 100.0, 4);
  85  
  86              if (isset($parentcategory)) {
  87                  if ($parentcategory->aggregation == GRADE_AGGREGATE_SUM ||
  88                      $parentcategory->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2) {
  89                      $category->grade_item_aggregationcoef = $category->grade_item_aggregationcoef == 0 ? 0 : 1;
  90                  } else {
  91                      $category->grade_item_aggregationcoef = format_float($category->grade_item_aggregationcoef, 4);
  92                  }
  93              }
  94              // Check if the gradebook is frozen. This allows grades not be altered at all until a user verifies that they
  95              // wish to update the grades.
  96              $gradebookcalculationsfreeze = get_config('core', 'gradebook_calculations_freeze_' . $courseid);
  97              // Stick with the original code if the grade book is frozen.
  98              if ($gradebookcalculationsfreeze && (int)$gradebookcalculationsfreeze <= 20150627) {
  99                  if ($category->aggregation == GRADE_AGGREGATE_SUM) {
 100                      // Input fields for grademin and grademax are disabled for the "Natural" category,
 101                      // this means they will be ignored if user does not change aggregation method.
 102                      // But if user does change aggregation method the default values should be used.
 103                      $category->grademax = 100;
 104                      $category->grade_item_grademax = 100;
 105                      $category->grademin = 0;
 106                      $category->grade_item_grademin = 0;
 107                  }
 108              } else {
 109                  if ($category->aggregation == GRADE_AGGREGATE_SUM && !$gradeitem->is_calculated()) {
 110                      // Input fields for grademin and grademax are disabled for the "Natural" category,
 111                      // this means they will be ignored if user does not change aggregation method.
 112                      // But if user does change aggregation method the default values should be used.
 113                      // This does not apply to calculated category totals.
 114                      $category->grademax = 100;
 115                      $category->grade_item_grademax = 100;
 116                      $category->grademin = 0;
 117                      $category->grade_item_grademin = 0;
 118                  }
 119              }
 120          } else {
 121              $gradecategory = new grade_category(['courseid' => $courseid], false);
 122              $gradecategory->apply_default_settings();
 123              $gradecategory->apply_forced_settings();
 124  
 125              $category = $gradecategory->get_record_data();
 126  
 127              $gradeitem = new grade_item(['courseid' => $courseid, 'itemtype' => 'manual'], false);
 128              foreach ($gradeitem->get_record_data() as $key => $value) {
 129                  $category->{"grade_item_$key"} = $value;
 130              }
 131          }
 132  
 133          return [
 134              'gradecategory' => $gradecategory,
 135              'categoryitem' => $category,
 136              'gradeitem' => $gradeitem
 137          ];
 138      }
 139  
 140      /**
 141       * Form definition
 142       *
 143       * @return void
 144       * @throws \coding_exception
 145       * @throws \dml_exception
 146       * @throws \moodle_exception
 147       */
 148      protected function definition(): void {
 149          global $CFG, $OUTPUT, $COURSE;
 150          $courseid = $this->optional_param('courseid', null, PARAM_INT);
 151          $id = $this->optional_param('category', 0, PARAM_INT);
 152          $gprplugin = $this->optional_param('gpr_plugin', '', PARAM_TEXT);
 153  
 154          if ($gprplugin && ($gprplugin !== 'tree')) {
 155              $this->gpr = new grade_plugin_return(['type' => 'report', 'plugin' => $gprplugin, 'courseid' => $courseid]);
 156          } else {
 157              $this->gpr = new grade_plugin_return(['type' => 'edit', 'plugin' => 'tree', 'courseid' => $courseid]);
 158          }
 159  
 160          $mform = $this->_form;
 161  
 162          $this->aggregation_options = grade_helper::get_aggregation_strings();
 163  
 164          $local = $this->get_gradecategory();
 165          $category = $local['categoryitem'];
 166  
 167          // Hidden elements.
 168          $mform->addElement('hidden', 'id', 0);
 169          $mform->setType('id', PARAM_INT);
 170  
 171          $mform->addElement('hidden', 'courseid', $courseid);
 172          $mform->setType('courseid', PARAM_INT);
 173  
 174          $mform->addElement('hidden', 'category', $id);
 175          $mform->setType('category', PARAM_INT);
 176  
 177          // Visible elements.
 178          $mform->addElement('text', 'fullname', get_string('categoryname', 'grades'));
 179          $mform->setType('fullname', PARAM_TEXT);
 180          $mform->addRule('fullname', null, 'required', null, 'client');
 181  
 182          $mform->addElement('select', 'aggregation', get_string('aggregation', 'grades'), $this->aggregation_options);
 183          $mform->addHelpButton('aggregation', 'aggregation', 'grades');
 184  
 185          $mform->addElement('checkbox', 'aggregateonlygraded', get_string('aggregateonlygraded', 'grades'));
 186          $mform->addHelpButton('aggregateonlygraded', 'aggregateonlygraded', 'grades');
 187  
 188          if (empty($CFG->enableoutcomes)) {
 189              $mform->addElement('hidden', 'aggregateoutcomes');
 190              $mform->setType('aggregateoutcomes', PARAM_INT);
 191          } else {
 192              $mform->addElement('checkbox', 'aggregateoutcomes', get_string('aggregateoutcomes', 'grades'));
 193              $mform->addHelpButton('aggregateoutcomes', 'aggregateoutcomes', 'grades');
 194          }
 195  
 196          $mform->addElement('text', 'keephigh', get_string('keephigh', 'grades'), 'size="3"');
 197          $mform->setType('keephigh', PARAM_INT);
 198          $mform->addHelpButton('keephigh', 'keephigh', 'grades');
 199  
 200          $mform->addElement('text', 'droplow', get_string('droplow', 'grades'), 'size="3"');
 201          $mform->setType('droplow', PARAM_INT);
 202          $mform->addHelpButton('droplow', 'droplow', 'grades');
 203          $mform->hideIf('droplow', 'keephigh', 'noteq', 0);
 204  
 205          $mform->hideIf('keephigh', 'droplow', 'noteq', 0);
 206          $mform->hideIf('droplow', 'keephigh', 'noteq', 0);
 207  
 208          if (!empty($category->id)) {
 209              $gradeitem = $local['gradeitem'];
 210              // If grades exist set a message so the user knows why they can not alter the grade type or scale.
 211              // We could never change the grade type for external items, so only need to show this for manual grade items.
 212              if ($gradeitem->has_overridden_grades()) {
 213                  // Set a message so the user knows why the can not alter the grade type or scale.
 214                  if ($gradeitem->gradetype == GRADE_TYPE_SCALE) {
 215                      $gradesexistmsg = get_string('modgradecategorycantchangegradetyporscalemsg', 'grades');
 216                  } else {
 217                      $gradesexistmsg = get_string('modgradecategorycantchangegradetypemsg', 'grades');
 218                  }
 219                  $notification = new \core\output\notification($gradesexistmsg, \core\output\notification::NOTIFY_INFO);
 220                  $notification->set_show_closebutton(false);
 221                  $mform->addElement('static', 'gradesexistmsg', '', $OUTPUT->render($notification));
 222              }
 223          }
 224  
 225          $options = [
 226              GRADE_TYPE_NONE => get_string('typenone', 'grades'),
 227              GRADE_TYPE_VALUE => get_string('typevalue', 'grades'),
 228              GRADE_TYPE_SCALE => get_string('typescale', 'grades'),
 229              GRADE_TYPE_TEXT => get_string('typetext', 'grades')
 230          ];
 231  
 232          $mform->addElement('select', 'grade_item_gradetype', get_string('gradetype', 'grades'), $options);
 233          $mform->addHelpButton('grade_item_gradetype', 'gradetype', 'grades');
 234          $mform->setDefault('grade_item_gradetype', GRADE_TYPE_VALUE);
 235          $mform->hideIf('grade_item_gradetype', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
 236  
 237          $options = [0 => get_string('usenoscale', 'grades')];
 238          if ($scales = grade_scale::fetch_all_local($COURSE->id)) {
 239              foreach ($scales as $scale) {
 240                  $options[$scale->id] = $scale->get_name();
 241              }
 242          }
 243          if ($scales = grade_scale::fetch_all_global()) {
 244              foreach ($scales as $scale) {
 245                  $options[$scale->id] = $scale->get_name();
 246              }
 247          }
 248          // Ugly BC hack - it was possible to use custom scale from other courses.
 249          if (!empty($category->grade_item_scaleid) && !isset($options[$category->grade_item_scaleid])) {
 250              if ($scale = grade_scale::fetch(['id' => $category->grade_item_scaleid])) {
 251                  $options[$scale->id] = $scale->get_name().' '.get_string('incorrectcustomscale', 'grades');
 252              }
 253          }
 254          $mform->addElement('select', 'grade_item_scaleid', get_string('scale'), $options);
 255          $mform->addHelpButton('grade_item_scaleid', 'typescale', 'grades');
 256          $mform->hideIf('grade_item_scaleid', 'grade_item_gradetype', 'noteq', GRADE_TYPE_SCALE);
 257          $mform->hideIf('grade_item_scaleid', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
 258  
 259          $choices = [];
 260          $choices[''] = get_string('choose');
 261          $choices['no'] = get_string('no');
 262          $choices['yes'] = get_string('yes');
 263          $mform->addElement('select', 'grade_item_rescalegrades', get_string('modgradecategoryrescalegrades', 'grades'), $choices);
 264          $mform->addHelpButton('grade_item_rescalegrades', 'modgradecategoryrescalegrades', 'grades');
 265          $mform->hideIf('grade_item_rescalegrades', 'grade_item_gradetype', 'noteq', GRADE_TYPE_VALUE);
 266  
 267          $mform->addElement('float', 'grade_item_grademax', get_string('grademax', 'grades'));
 268          $mform->addHelpButton('grade_item_grademax', 'grademax', 'grades');
 269          $mform->hideIf('grade_item_grademax', 'grade_item_gradetype', 'noteq', GRADE_TYPE_VALUE);
 270          $mform->hideIf('grade_item_grademax', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
 271  
 272          if ((bool) get_config('moodle', 'grade_report_showmin')) {
 273              $mform->addElement('float', 'grade_item_grademin', get_string('grademin', 'grades'));
 274              $mform->addHelpButton('grade_item_grademin', 'grademin', 'grades');
 275              $mform->hideIf('grade_item_grademin', 'grade_item_gradetype', 'noteq', GRADE_TYPE_VALUE);
 276              $mform->hideIf('grade_item_grademin', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
 277          }
 278  
 279          // Hiding.
 280          // advcheckbox is not compatible with disabledIf!
 281          $mform->addElement('checkbox', 'grade_item_hidden', get_string('hidden', 'grades'));
 282          $mform->addHelpButton('grade_item_hidden', 'hidden', 'grades');
 283  
 284          // Locking.
 285          $mform->addElement('checkbox', 'grade_item_locked', get_string('locked', 'grades'));
 286          $mform->addHelpButton('grade_item_locked', 'locked', 'grades');
 287  
 288          $mform->addElement('advcheckbox', 'grade_item_weightoverride', get_string('adjustedweight', 'grades'));
 289          $mform->addHelpButton('grade_item_weightoverride', 'weightoverride', 'grades');
 290  
 291          $mform->addElement('float', 'grade_item_aggregationcoef2', get_string('weight', 'grades'));
 292          $mform->addHelpButton('grade_item_aggregationcoef2', 'weight', 'grades');
 293          $mform->hideIf('grade_item_aggregationcoef2', 'grade_item_weightoverride');
 294  
 295          $options = [];
 296          $default = -1;
 297          $categories = grade_category::fetch_all(['courseid' => $courseid]);
 298  
 299          foreach ($categories as $cat) {
 300              $cat->apply_forced_settings();
 301              $options[$cat->id] = $cat->get_name();
 302              if ($cat->is_course_category()) {
 303                  $default = $cat->id;
 304              }
 305          }
 306  
 307          if (count($categories) > 1) {
 308              $mform->addElement('select', 'parentcategory', get_string('parentcategory', 'grades'), $options);
 309              $mform->setDefault('parentcategory', $default);
 310          }
 311  
 312          $params = ['courseid' => $courseid];
 313          if ($id > 0) {
 314              $params['id'] = $id;
 315          }
 316          $url = new moodle_url('/grade/edit/tree/category.php', $params);
 317          $url = $this->gpr->add_url_params($url);
 318          $url = '<a class="showadvancedform" href="' . $url . '">' . get_string('showmore', 'form') .'</a>';
 319          $mform->addElement('static', 'advancedform', $url);
 320  
 321          // Add return tracking info.
 322          $this->gpr->add_mform_elements($mform);
 323  
 324          $this->set_data($category);
 325      }
 326  
 327      /**
 328       * This method implements changes to the form that need to be made once the form data is set.
 329       */
 330      public function definition_after_data(): void {
 331          global $CFG;
 332  
 333          $mform =& $this->_form;
 334  
 335          $categoryobject = new grade_category();
 336  
 337          foreach ($categoryobject->forceable as $property) {
 338              if ((int)$CFG->{"grade_{$property}_flag"} & 1) {
 339                  if ($mform->elementExists($property)) {
 340                      if (empty($CFG->grade_hideforcedsettings)) {
 341                          $mform->hardFreeze($property);
 342                      } else {
 343                          if ($mform->elementExists($property)) {
 344                              $mform->removeElement($property);
 345                          }
 346                      }
 347                  }
 348              }
 349          }
 350  
 351          if ($CFG->grade_droplow > 0) {
 352              if ($mform->elementExists('keephigh')) {
 353                  $mform->removeElement('keephigh');
 354              }
 355          } else if ($CFG->grade_keephigh > 0) {
 356              if ($mform->elementExists('droplow')) {
 357                  $mform->removeElement('droplow');
 358              }
 359          }
 360  
 361          if ($id = $mform->getElementValue('id')) {
 362              $gradecategory = grade_category::fetch(['id' => $id]);
 363              $gradeitem = $gradecategory->load_grade_item();
 364  
 365              // Remove agg coef if not used.
 366              if ($gradecategory->is_course_category()) {
 367                  if ($mform->elementExists('parentcategory')) {
 368                      $mform->removeElement('parentcategory');
 369                  }
 370              } else {
 371                  // If we wanted to change parent of existing category
 372                  // we would have to verify there are no circular references in parents!!!
 373                  if ($mform->elementExists('parentcategory')) {
 374                      $mform->hardFreeze('parentcategory');
 375                  }
 376              }
 377  
 378              // Prevent the user from using drop lowest/keep highest when the aggregation method cannot handle it.
 379              if (!$gradecategory->can_apply_limit_rules()) {
 380                  if ($mform->elementExists('keephigh')) {
 381                      $mform->setConstant('keephigh', 0);
 382                      $mform->hardFreeze('keephigh');
 383                  }
 384                  if ($mform->elementExists('droplow')) {
 385                      $mform->setConstant('droplow', 0);
 386                      $mform->hardFreeze('droplow');
 387                  }
 388              }
 389  
 390              if ($gradeitem->is_calculated()) {
 391                  $gradesexistmsg = get_string('calculationwarning', 'grades');
 392                  $gradesexisthtml = '<div class=\'alert alert-warning\'>' . $gradesexistmsg . '</div>';
 393                  $mform->addElement('static', 'gradesexistmsg', '', $gradesexisthtml);
 394  
 395                  // Following elements are ignored when calculation formula used.
 396                  if ($mform->elementExists('aggregation')) {
 397                      $mform->removeElement('aggregation');
 398                  }
 399                  if ($mform->elementExists('keephigh')) {
 400                      $mform->removeElement('keephigh');
 401                  }
 402                  if ($mform->elementExists('droplow')) {
 403                      $mform->removeElement('droplow');
 404                  }
 405                  if ($mform->elementExists('aggregateonlygraded')) {
 406                      $mform->removeElement('aggregateonlygraded');
 407                  }
 408                  if ($mform->elementExists('aggregateoutcomes')) {
 409                      $mform->removeElement('aggregateoutcomes');
 410                  }
 411              }
 412  
 413              // If it is a course category, remove the "required" rule from the "fullname" element.
 414              if ($gradecategory->is_course_category()) {
 415                  unset($mform->_rules['fullname']);
 416                  $key = array_search('fullname', $mform->_required);
 417                  unset($mform->_required[$key]);
 418              }
 419  
 420              // If it is a course category and its fullname is ?, show an empty field.
 421              if ($gradecategory->is_course_category() && $mform->getElementValue('fullname') == '?') {
 422                  $mform->setDefault('fullname', '');
 423              }
 424              // Remove unwanted aggregation options.
 425              if ($mform->elementExists('aggregation')) {
 426                  $allaggoptions = array_keys($this->aggregation_options);
 427                  $aggel =& $mform->getElement('aggregation');
 428                  $visible = explode(',', $CFG->grade_aggregations_visible);
 429                  if (!is_null($gradecategory->aggregation)) {
 430                      // Current type is always visible.
 431                      $visible[] = $gradecategory->aggregation;
 432                  }
 433                  foreach ($allaggoptions as $type) {
 434                      if (!in_array($type, $visible)) {
 435                          $aggel->removeOption($type);
 436                      }
 437                  }
 438              }
 439  
 440          } else {
 441              // Adding new category
 442              // Remove unwanted aggregation options.
 443              if ($mform->elementExists('aggregation')) {
 444                  $allaggoptions = array_keys($this->aggregation_options);
 445                  $aggel =& $mform->getElement('aggregation');
 446                  $visible = explode(',', $CFG->grade_aggregations_visible);
 447                  foreach ($allaggoptions as $type) {
 448                      if (!in_array($type, $visible)) {
 449                          $aggel->removeOption($type);
 450                      }
 451                  }
 452              }
 453  
 454              $mform->removeElement('grade_item_rescalegrades');
 455          }
 456  
 457          // Grade item.
 458          if ($id = $mform->getElementValue('id')) {
 459              $gradecategory = grade_category::fetch(['id' => $id]);
 460              $gradeitem = $gradecategory->load_grade_item();
 461  
 462              // Load appropriate "hidden"/"hidden until" defaults.
 463              if (!$gradeitem->is_hiddenuntil()) {
 464                  $mform->setDefault('grade_item_hidden', $gradeitem->get_hidden());
 465              }
 466  
 467              if ($gradeitem->has_overridden_grades()) {
 468                  // Can't change the grade type or the scale if there are grades.
 469                  $mform->hardFreeze('grade_item_gradetype, grade_item_scaleid');
 470  
 471                  // If we are using scales then remove the unnecessary rescale and grade fields.
 472                  if ($gradeitem->gradetype == GRADE_TYPE_SCALE) {
 473                      $mform->removeElement('grade_item_rescalegrades');
 474                      $mform->removeElement('grade_item_grademax');
 475                      if ($mform->elementExists('grade_item_grademin')) {
 476                          $mform->removeElement('grade_item_grademin');
 477                      }
 478                  } else {
 479                      // Not using scale, so remove it.
 480                      $mform->removeElement('grade_item_scaleid');
 481                      $mform->hideIf('grade_item_grademax', 'grade_item_rescalegrades', 'eq', '');
 482                      $mform->hideIf('grade_item_grademin', 'grade_item_rescalegrades', 'eq', '');
 483                  }
 484              } else { // Remove the rescale element if there are no grades.
 485                  $mform->removeElement('grade_item_rescalegrades');
 486              }
 487  
 488              // Remove the aggregation coef element if not needed.
 489              if ($gradeitem->is_course_item()) {
 490                  if ($mform->elementExists('grade_item_aggregationcoef')) {
 491                      $mform->removeElement('grade_item_aggregationcoef');
 492                  }
 493  
 494                  if ($mform->elementExists('grade_item_weightoverride')) {
 495                      $mform->removeElement('grade_item_weightoverride');
 496                  }
 497                  if ($mform->elementExists('grade_item_aggregationcoef2')) {
 498                      $mform->removeElement('grade_item_aggregationcoef2');
 499                  }
 500              } else {
 501                  if ($gradeitem->is_category_item()) {
 502                      $category = $gradeitem->get_item_category();
 503                      $parentcategory = $category->get_parent_category();
 504                  } else {
 505                      $parentcategory = $gradeitem->get_parent_category();
 506                  }
 507  
 508                  $parentcategory->apply_forced_settings();
 509  
 510                  if (!$parentcategory->is_aggregationcoef_used()) {
 511                      if ($mform->elementExists('grade_item_aggregationcoef')) {
 512                          $mform->removeElement('grade_item_aggregationcoef');
 513                      }
 514                  } else {
 515                      $coefstring = $gradeitem->get_coefstring();
 516  
 517                      if ($coefstring == 'aggregationcoefextrasum' || $coefstring == 'aggregationcoefextraweightsum') {
 518                          // Advcheckbox is not compatible with disabledIf!
 519                          $coefstring = 'aggregationcoefextrasum';
 520                          $element =& $mform->createElement('checkbox', 'grade_item_aggregationcoef',
 521                              get_string($coefstring, 'grades'));
 522                      } else {
 523                          $element =& $mform->createElement('text', 'grade_item_aggregationcoef',
 524                              get_string($coefstring, 'grades'));
 525                          $mform->setType('grade_item_aggregationcoef', PARAM_FLOAT);
 526                      }
 527                      $mform->insertElementBefore($element, 'parentcategory');
 528                      $mform->addHelpButton('grade_item_aggregationcoef', $coefstring, 'grades');
 529                  }
 530  
 531                  // Remove fields used by natural weighting if the parent category is not using natural weighting.
 532                  // Or if the item is a scale and scales are not used in aggregation.
 533                  if ($parentcategory->aggregation != GRADE_AGGREGATE_SUM
 534                      || (empty($CFG->grade_includescalesinaggregation) && $gradeitem->gradetype == GRADE_TYPE_SCALE)) {
 535                      if ($mform->elementExists('grade_item_weightoverride')) {
 536                          $mform->removeElement('grade_item_weightoverride');
 537                      }
 538                      if ($mform->elementExists('grade_item_aggregationcoef2')) {
 539                          $mform->removeElement('grade_item_aggregationcoef2');
 540                      }
 541                  }
 542  
 543              }
 544          }
 545      }
 546  
 547      /**
 548       * Return form context
 549       *
 550       * @return context
 551       */
 552      protected function get_context_for_dynamic_submission(): context {
 553          $courseid = $this->optional_param('courseid', null, PARAM_INT);
 554          return context_course::instance($courseid);
 555      }
 556  
 557      /**
 558       * Check if current user has access to this form, otherwise throw exception
 559       *
 560       * @return void
 561       * @throws \required_capability_exception
 562       */
 563      protected function check_access_for_dynamic_submission(): void {
 564          $courseid = $this->optional_param('courseid', null, PARAM_INT);
 565          require_capability('moodle/grade:manage', context_course::instance($courseid));
 566      }
 567  
 568      /**
 569       * Load in existing data as form defaults
 570       *
 571       * @return void
 572       */
 573      public function set_data_for_dynamic_submission(): void {
 574          $this->set_data((object)[
 575              'courseid' => $this->optional_param('courseid', null, PARAM_INT),
 576              'category' => $this->optional_param('category', null, PARAM_INT)
 577          ]);
 578      }
 579  
 580      /**
 581       * Returns url to set in $PAGE->set_url() when form is being rendered or submitted via AJAX
 582       *
 583       * @return moodle_url
 584       * @throws \moodle_exception
 585       */
 586      protected function get_page_url_for_dynamic_submission(): moodle_url {
 587          $params = [
 588              'id' => $this->optional_param('courseid', null, PARAM_INT),
 589              'category' => $this->optional_param('category', null, PARAM_INT),
 590          ];
 591          return new moodle_url('/grade/edit/tree/index.php', $params);
 592      }
 593  
 594      /**
 595       * Process the form submission, used if form was submitted via AJAX
 596       *
 597       * @return array
 598       * @throws \moodle_exception
 599       */
 600      public function process_dynamic_submission(): array {
 601          $data = $this->get_data();
 602  
 603          $url = $this->gpr->get_return_url('index.php?id=' . $data->courseid);
 604          $local = $this->get_gradecategory();
 605          $gradecategory = $local['gradecategory'];
 606  
 607          grade_edit_tree::update_gradecategory($gradecategory, $data);
 608  
 609          return [
 610              'result' => true,
 611              'url' => $url,
 612              'errors' => [],
 613          ];
 614      }
 615  
 616      /**
 617       * Form validation.
 618       *
 619       * @param array $data array of ("fieldname"=>value) of submitted data
 620       * @param array $files array of uploaded files "element_name"=>tmp_file_path
 621       * @return array of "element_name"=>"error_description" if there are errors,
 622       *         or an empty array if everything is OK (true allowed for backwards compatibility too).
 623       */
 624      public function validation($data, $files): array {
 625          $gradeitem = false;
 626          if ($data['id']) {
 627              $gradecategory = grade_category::fetch(['id' => $data['id']]);
 628              $gradeitem = $gradecategory->load_grade_item();
 629          }
 630  
 631          $errors = parent::validation($data, $files);
 632  
 633          if (array_key_exists('grade_item_gradetype', $data) && $data['grade_item_gradetype'] == GRADE_TYPE_SCALE) {
 634              if (empty($data['grade_item_scaleid'])) {
 635                  $errors['grade_item_scaleid'] = get_string('missingscale', 'grades');
 636              }
 637          }
 638  
 639          // We need to make all the validations related with grademax and grademin
 640          // with them being correct floats, keeping the originals unmodified for
 641          // later validations / showing the form back...
 642          // TODO: Note that once MDL-73994 is fixed we'll have to re-visit this and
 643          // adapt the code below to the new values arriving here, without forgetting
 644          // the special case of empties and nulls.
 645          $grademax = isset($data['grade_item_grademax']) ? unformat_float($data['grade_item_grademax']) : null;
 646          $grademin = isset($data['grade_item_grademin']) ? unformat_float($data['grade_item_grademin']) : null;
 647  
 648          if (!is_null($grademin) && !is_null($grademax)) {
 649              if (($grademax != 0 || $grademin != 0) && ($grademax == $grademin || $grademax < $grademin)) {
 650                  $errors['grade_item_grademin'] = get_string('incorrectminmax', 'grades');
 651                  $errors['grade_item_grademax'] = get_string('incorrectminmax', 'grades');
 652              }
 653          }
 654  
 655          if ($data['id'] && $gradeitem->has_overridden_grades()) {
 656              if ($gradeitem->gradetype == GRADE_TYPE_VALUE) {
 657                  if (grade_floats_different($grademin, $gradeitem->grademin) ||
 658                      grade_floats_different($grademax, $gradeitem->grademax)) {
 659                      if (empty($data['grade_item_rescalegrades'])) {
 660                          $errors['grade_item_rescalegrades'] = get_string('mustchooserescaleyesorno', 'grades');
 661                      }
 662                  }
 663              }
 664          }
 665          return $errors;
 666      }
 667  }