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.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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 moodleform to edit the grade options for an individual grade category
  19   *
  20   * @package   core_grades
  21   * @copyright 2007 Petr Skoda
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  if (!defined('MOODLE_INTERNAL')) {
  26      die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
  27  }
  28  
  29  require_once $CFG->libdir.'/formslib.php';
  30  
  31  class edit_category_form extends moodleform {
  32      private $aggregation_options = array();
  33  
  34      function definition() {
  35          global $CFG, $COURSE, $DB, $OUTPUT;
  36          $mform =& $this->_form;
  37  
  38          $category = $this->_customdata['current'];
  39  
  40          $this->aggregation_options = grade_helper::get_aggregation_strings();
  41  
  42          // visible elements
  43          $mform->addElement('header', 'headercategory', get_string('gradecategory', 'grades'));
  44          $mform->addElement('text', 'fullname', get_string('categoryname', 'grades'));
  45          $mform->setType('fullname', PARAM_TEXT);
  46          $mform->addRule('fullname', null, 'required', null, 'client');
  47  
  48          $mform->addElement('select', 'aggregation', get_string('aggregation', 'grades'), $this->aggregation_options);
  49          $mform->addHelpButton('aggregation', 'aggregation', 'grades');
  50  
  51          $mform->addElement('checkbox', 'aggregateonlygraded', get_string('aggregateonlygraded', 'grades'));
  52          $mform->addHelpButton('aggregateonlygraded', 'aggregateonlygraded', 'grades');
  53  
  54          if (empty($CFG->enableoutcomes)) {
  55              $mform->addElement('hidden', 'aggregateoutcomes');
  56              $mform->setType('aggregateoutcomes', PARAM_INT);
  57          } else {
  58              $mform->addElement('checkbox', 'aggregateoutcomes', get_string('aggregateoutcomes', 'grades'));
  59              $mform->addHelpButton('aggregateoutcomes', 'aggregateoutcomes', 'grades');
  60          }
  61  
  62          $mform->addElement('text', 'keephigh', get_string('keephigh', 'grades'), 'size="3"');
  63          $mform->setType('keephigh', PARAM_INT);
  64          $mform->addHelpButton('keephigh', 'keephigh', 'grades');
  65  
  66          $mform->addElement('text', 'droplow', get_string('droplow', 'grades'), 'size="3"');
  67          $mform->setType('droplow', PARAM_INT);
  68          $mform->addHelpButton('droplow', 'droplow', 'grades');
  69          $mform->disabledIf('droplow', 'keephigh', 'noteq', 0);
  70  
  71          $mform->disabledIf('keephigh', 'droplow', 'noteq', 0);
  72          $mform->disabledIf('droplow', 'keephigh', 'noteq', 0);
  73  
  74          // Grade item settings
  75          // Displayed as Category total to avoid confusion between grade items requiring marking and category totals
  76          $mform->addElement('header', 'general', get_string('categorytotal', 'grades'));
  77          $mform->setExpanded('general');
  78  
  79          $mform->addElement('text', 'grade_item_itemname', get_string('categorytotalname', 'grades'));
  80          $mform->setType('grade_item_itemname', PARAM_TEXT);
  81  
  82          $mform->addElement('text', 'grade_item_iteminfo', get_string('iteminfo', 'grades'));
  83          $mform->addHelpButton('grade_item_iteminfo', 'iteminfo', 'grades');
  84          $mform->setType('grade_item_iteminfo', PARAM_TEXT);
  85  
  86          $mform->addElement('text', 'grade_item_idnumber', get_string('idnumbermod'));
  87          $mform->addHelpButton('grade_item_idnumber', 'idnumbermod');
  88          $mform->setType('grade_item_idnumber', PARAM_RAW);
  89  
  90          if (!empty($category->id)) {
  91              $gradecategory = grade_category::fetch(array('id' => $category->id));
  92              $gradeitem = $gradecategory->load_grade_item();
  93  
  94              // If grades exist set a message so the user knows why they can not alter the grade type or scale.
  95              // We could never change the grade type for external items, so only need to show this for manual grade items.
  96              if ($gradeitem->has_overridden_grades()) {
  97                  // Set a message so the user knows why the can not alter the grade type or scale.
  98                  if ($gradeitem->gradetype == GRADE_TYPE_SCALE) {
  99                      $gradesexistmsg = get_string('modgradecategorycantchangegradetyporscalemsg', 'grades');
 100                  } else {
 101                      $gradesexistmsg = get_string('modgradecategorycantchangegradetypemsg', 'grades');
 102                  }
 103                  $notification = new \core\output\notification($gradesexistmsg, \core\output\notification::NOTIFY_INFO);
 104                  $notification->set_show_closebutton(false);
 105                  $mform->addElement('static', 'gradesexistmsg', '', $OUTPUT->render($notification));
 106              }
 107          }
 108  
 109          $options = array(GRADE_TYPE_NONE=>get_string('typenone', 'grades'),
 110                           GRADE_TYPE_VALUE=>get_string('typevalue', 'grades'),
 111                           GRADE_TYPE_SCALE=>get_string('typescale', 'grades'),
 112                           GRADE_TYPE_TEXT=>get_string('typetext', 'grades'));
 113  
 114          $mform->addElement('select', 'grade_item_gradetype', get_string('gradetype', 'grades'), $options);
 115          $mform->addHelpButton('grade_item_gradetype', 'gradetype', 'grades');
 116          $mform->setDefault('grade_item_gradetype', GRADE_TYPE_VALUE);
 117          $mform->disabledIf('grade_item_gradetype', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
 118  
 119          //$mform->addElement('text', 'calculation', get_string('calculation', 'grades'));
 120          //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_TEXT);
 121          //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_NONE);
 122  
 123          $options = array(0=>get_string('usenoscale', 'grades'));
 124          if ($scales = grade_scale::fetch_all_local($COURSE->id)) {
 125              foreach ($scales as $scale) {
 126                  $options[$scale->id] = $scale->get_name();
 127              }
 128          }
 129          if ($scales = grade_scale::fetch_all_global()) {
 130              foreach ($scales as $scale) {
 131                  $options[$scale->id] = $scale->get_name();
 132              }
 133          }
 134          // ugly BC hack - it was possible to use custom scale from other courses :-(
 135          if (!empty($category->grade_item_scaleid) and !isset($options[$category->grade_item_scaleid])) {
 136              if ($scale = grade_scale::fetch(array('id'=>$category->grade_item_scaleid))) {
 137                  $options[$scale->id] = $scale->get_name().' '.get_string('incorrectcustomscale', 'grades');
 138              }
 139          }
 140          $mform->addElement('select', 'grade_item_scaleid', get_string('scale'), $options);
 141          $mform->addHelpButton('grade_item_scaleid', 'typescale', 'grades');
 142          $mform->disabledIf('grade_item_scaleid', 'grade_item_gradetype', 'noteq', GRADE_TYPE_SCALE);
 143          $mform->disabledIf('grade_item_scaleid', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
 144  
 145          $choices = array();
 146          $choices[''] = get_string('choose');
 147          $choices['no'] = get_string('no');
 148          $choices['yes'] = get_string('yes');
 149          $mform->addElement('select', 'grade_item_rescalegrades', get_string('modgradecategoryrescalegrades', 'grades'), $choices);
 150          $mform->addHelpButton('grade_item_rescalegrades', 'modgradecategoryrescalegrades', 'grades');
 151          $mform->disabledIf('grade_item_rescalegrades', 'grade_item_gradetype', 'noteq', GRADE_TYPE_VALUE);
 152  
 153          $mform->addElement('float', 'grade_item_grademax', get_string('grademax', 'grades'));
 154          $mform->addHelpButton('grade_item_grademax', 'grademax', 'grades');
 155          $mform->disabledIf('grade_item_grademax', 'grade_item_gradetype', 'noteq', GRADE_TYPE_VALUE);
 156          $mform->disabledIf('grade_item_grademax', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
 157  
 158          if ((bool) get_config('moodle', 'grade_report_showmin')) {
 159              $mform->addElement('float', 'grade_item_grademin', get_string('grademin', 'grades'));
 160              $mform->addHelpButton('grade_item_grademin', 'grademin', 'grades');
 161              $mform->disabledIf('grade_item_grademin', 'grade_item_gradetype', 'noteq', GRADE_TYPE_VALUE);
 162              $mform->disabledIf('grade_item_grademin', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
 163          }
 164  
 165          $mform->addElement('float', 'grade_item_gradepass', get_string('gradepass', 'grades'));
 166          $mform->addHelpButton('grade_item_gradepass', 'gradepass', 'grades');
 167          $mform->disabledIf('grade_item_gradepass', 'grade_item_gradetype', 'eq', GRADE_TYPE_NONE);
 168          $mform->disabledIf('grade_item_gradepass', 'grade_item_gradetype', 'eq', GRADE_TYPE_TEXT);
 169  
 170          /// grade display prefs
 171          $default_gradedisplaytype = grade_get_setting($COURSE->id, 'displaytype', $CFG->grade_displaytype);
 172          $options = array(GRADE_DISPLAY_TYPE_DEFAULT            => get_string('default', 'grades'),
 173                           GRADE_DISPLAY_TYPE_REAL               => get_string('real', 'grades'),
 174                           GRADE_DISPLAY_TYPE_PERCENTAGE         => get_string('percentage', 'grades'),
 175                           GRADE_DISPLAY_TYPE_LETTER             => get_string('letter', 'grades'),
 176                           GRADE_DISPLAY_TYPE_REAL_PERCENTAGE    => get_string('realpercentage', 'grades'),
 177                           GRADE_DISPLAY_TYPE_REAL_LETTER        => get_string('realletter', 'grades'),
 178                           GRADE_DISPLAY_TYPE_LETTER_REAL        => get_string('letterreal', 'grades'),
 179                           GRADE_DISPLAY_TYPE_LETTER_PERCENTAGE  => get_string('letterpercentage', 'grades'),
 180                           GRADE_DISPLAY_TYPE_PERCENTAGE_LETTER  => get_string('percentageletter', 'grades'),
 181                           GRADE_DISPLAY_TYPE_PERCENTAGE_REAL    => get_string('percentagereal', 'grades')
 182                           );
 183  
 184          asort($options);
 185  
 186          foreach ($options as $key=>$option) {
 187              if ($key == $default_gradedisplaytype) {
 188                  $options[GRADE_DISPLAY_TYPE_DEFAULT] = get_string('defaultprev', 'grades', $option);
 189                  break;
 190              }
 191          }
 192          $mform->addElement('select', 'grade_item_display', get_string('gradedisplaytype', 'grades'), $options);
 193          $mform->addHelpButton('grade_item_display', 'gradedisplaytype', 'grades');
 194          $mform->disabledIf('grade_item_display', 'grade_item_gradetype', 'in',
 195              array(GRADE_TYPE_TEXT, GRADE_TYPE_NONE));
 196  
 197          $default_gradedecimals = grade_get_setting($COURSE->id, 'decimalpoints', $CFG->grade_decimalpoints);
 198          $options = array(-1=>get_string('defaultprev', 'grades', $default_gradedecimals), 0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5);
 199          $mform->addElement('select', 'grade_item_decimals', get_string('decimalpoints', 'grades'), $options);
 200          $mform->addHelpButton('grade_item_decimals', 'decimalpoints', 'grades');
 201          $mform->setDefault('grade_item_decimals', -1);
 202          $mform->disabledIf('grade_item_decimals', 'grade_item_display', 'eq', GRADE_DISPLAY_TYPE_LETTER);
 203          $mform->disabledIf('grade_item_decimals', 'grade_item_gradetype', 'in',
 204              array(GRADE_TYPE_TEXT, GRADE_TYPE_NONE));
 205  
 206          if ($default_gradedisplaytype == GRADE_DISPLAY_TYPE_LETTER) {
 207              $mform->disabledIf('grade_item_decimals', 'grade_item_display', "eq", GRADE_DISPLAY_TYPE_DEFAULT);
 208          }
 209  
 210          /// hiding
 211          // advcheckbox is not compatible with disabledIf!
 212          $mform->addElement('checkbox', 'grade_item_hidden', get_string('hidden', 'grades'));
 213          $mform->addHelpButton('grade_item_hidden', 'hidden', 'grades');
 214          $mform->addElement('date_time_selector', 'grade_item_hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional'=>true));
 215          $mform->disabledIf('grade_item_hidden', 'grade_item_hiddenuntil[enabled]', 'checked');
 216  
 217          /// locking
 218          $mform->addElement('checkbox', 'grade_item_locked', get_string('locked', 'grades'));
 219          $mform->addHelpButton('grade_item_locked', 'locked', 'grades');
 220  
 221          $mform->addElement('date_time_selector', 'grade_item_locktime', get_string('locktime', 'grades'), array('optional'=>true));
 222          $mform->disabledIf('grade_item_locktime', 'grade_item_gradetype', 'eq', GRADE_TYPE_NONE);
 223  
 224  /// parent category related settings
 225          $mform->addElement('header', 'headerparent', get_string('parentcategory', 'grades'));
 226          $mform->setExpanded('headerparent');
 227  
 228          $mform->addElement('advcheckbox', 'grade_item_weightoverride', get_string('adjustedweight', 'grades'));
 229          $mform->addHelpButton('grade_item_weightoverride', 'weightoverride', 'grades');
 230  
 231          $mform->addElement('float', 'grade_item_aggregationcoef2', get_string('weight', 'grades'));
 232          $mform->addHelpButton('grade_item_aggregationcoef2', 'weight', 'grades');
 233          $mform->disabledIf('grade_item_aggregationcoef2', 'grade_item_weightoverride');
 234  
 235          $options = array();
 236          $default = -1;
 237          $categories = grade_category::fetch_all(array('courseid'=>$COURSE->id));
 238  
 239          foreach ($categories as $cat) {
 240              $cat->apply_forced_settings();
 241              $options[$cat->id] = $cat->get_name();
 242              if ($cat->is_course_category()) {
 243                  $default = $cat->id;
 244              }
 245          }
 246  
 247          if (count($categories) > 1) {
 248              $mform->addElement('select', 'parentcategory', get_string('parentcategory', 'grades'), $options);
 249              $mform->setDefault('parentcategory', $default);
 250              $mform->addElement('static', 'currentparentaggregation', get_string('currentparentaggregation', 'grades'));
 251          }
 252  
 253          // hidden params
 254          $mform->addElement('hidden', 'id', 0);
 255          $mform->setType('id', PARAM_INT);
 256  
 257          $mform->addElement('hidden', 'courseid', 0);
 258          $mform->setType('courseid', PARAM_INT);
 259  
 260  /// add return tracking info
 261          $gpr = $this->_customdata['gpr'];
 262          $gpr->add_mform_elements($mform);
 263  
 264  //-------------------------------------------------------------------------------
 265          // buttons
 266          $this->add_action_buttons();
 267  //-------------------------------------------------------------------------------
 268          $this->set_data($category);
 269      }
 270  
 271  
 272      function definition_after_data() {
 273          global $CFG, $COURSE;
 274  
 275          $mform =& $this->_form;
 276  
 277          $somecat = new grade_category();
 278  
 279          foreach ($somecat->forceable as $property) {
 280              if ((int)$CFG->{"grade_{$property}_flag"} & 1) {
 281                  if ($mform->elementExists($property)) {
 282                      if (empty($CFG->grade_hideforcedsettings)) {
 283                          $mform->hardFreeze($property);
 284                      } else {
 285                          if ($mform->elementExists($property)) {
 286                              $mform->removeElement($property);
 287                          }
 288                      }
 289                  }
 290              }
 291          }
 292  
 293          if ($CFG->grade_droplow > 0) {
 294              if ($mform->elementExists('keephigh')) {
 295                  $mform->removeElement('keephigh');
 296              }
 297          } else if ($CFG->grade_keephigh > 0) {
 298              if ($mform->elementExists('droplow')) {
 299                  $mform->removeElement('droplow');
 300              }
 301          }
 302  
 303          if ($id = $mform->getElementValue('id')) {
 304              $grade_category = grade_category::fetch(array('id'=>$id));
 305              $grade_item = $grade_category->load_grade_item();
 306  
 307              // remove agg coef if not used
 308              if ($grade_category->is_course_category()) {
 309                  if ($mform->elementExists('parentcategory')) {
 310                      $mform->removeElement('parentcategory');
 311                  }
 312                  if ($mform->elementExists('currentparentaggregation')) {
 313                      $mform->removeElement('currentparentaggregation');
 314                  }
 315  
 316              } else {
 317                  // if we wanted to change parent of existing category - we would have to verify there are no circular references in parents!!!
 318                  if ($mform->elementExists('parentcategory')) {
 319                      $mform->hardFreeze('parentcategory');
 320                  }
 321                  $parent_cat = $grade_category->get_parent_category();
 322                  $mform->setDefault('currentparentaggregation', $this->aggregation_options[$parent_cat->aggregation]);
 323  
 324              }
 325  
 326              // Prevent the user from using drop lowest/keep highest when the aggregation method cannot handle it.
 327              if (!$grade_category->can_apply_limit_rules()) {
 328                  if ($mform->elementExists('keephigh')) {
 329                      $mform->setConstant('keephigh', 0);
 330                      $mform->hardFreeze('keephigh');
 331                  }
 332                  if ($mform->elementExists('droplow')) {
 333                      $mform->setConstant('droplow', 0);
 334                      $mform->hardFreeze('droplow');
 335                  }
 336              }
 337  
 338              if ($grade_item->is_calculated()) {
 339                  // following elements are ignored when calculation formula used
 340                  if ($mform->elementExists('aggregation')) {
 341                      $mform->removeElement('aggregation');
 342                  }
 343                  if ($mform->elementExists('keephigh')) {
 344                      $mform->removeElement('keephigh');
 345                  }
 346                  if ($mform->elementExists('droplow')) {
 347                      $mform->removeElement('droplow');
 348                  }
 349                  if ($mform->elementExists('aggregateonlygraded')) {
 350                      $mform->removeElement('aggregateonlygraded');
 351                  }
 352                  if ($mform->elementExists('aggregateoutcomes')) {
 353                      $mform->removeElement('aggregateoutcomes');
 354                  }
 355              }
 356  
 357              // If it is a course category, remove the "required" rule from the "fullname" element
 358              if ($grade_category->is_course_category()) {
 359                  unset($mform->_rules['fullname']);
 360                  $key = array_search('fullname', $mform->_required);
 361                  unset($mform->_required[$key]);
 362              }
 363  
 364              // If it is a course category and its fullname is ?, show an empty field
 365              if ($grade_category->is_course_category() && $mform->getElementValue('fullname') == '?') {
 366                  $mform->setDefault('fullname', '');
 367              }
 368              // remove unwanted aggregation options
 369              if ($mform->elementExists('aggregation')) {
 370                  $allaggoptions = array_keys($this->aggregation_options);
 371                  $agg_el =& $mform->getElement('aggregation');
 372                  $visible = explode(',', $CFG->grade_aggregations_visible);
 373                  if (!is_null($grade_category->aggregation)) {
 374                      // current type is always visible
 375                      $visible[] = $grade_category->aggregation;
 376                  }
 377                  foreach ($allaggoptions as $type) {
 378                      if (!in_array($type, $visible)) {
 379                          $agg_el->removeOption($type);
 380                      }
 381                  }
 382              }
 383  
 384          } else {
 385              // adding new category
 386              if ($mform->elementExists('currentparentaggregation')) {
 387                  $mform->removeElement('currentparentaggregation');
 388              }
 389              // remove unwanted aggregation options
 390              if ($mform->elementExists('aggregation')) {
 391                  $allaggoptions = array_keys($this->aggregation_options);
 392                  $agg_el =& $mform->getElement('aggregation');
 393                  $visible = explode(',', $CFG->grade_aggregations_visible);
 394                  foreach ($allaggoptions as $type) {
 395                      if (!in_array($type, $visible)) {
 396                          $agg_el->removeOption($type);
 397                      }
 398                  }
 399              }
 400  
 401              $mform->removeElement('grade_item_rescalegrades');
 402          }
 403  
 404  
 405          // no parent header for course category
 406          if (!$mform->elementExists('parentcategory')) {
 407              $mform->removeElement('headerparent');
 408          }
 409  
 410  /// GRADE ITEM
 411          if ($id = $mform->getElementValue('id')) {
 412              $grade_category = grade_category::fetch(array('id'=>$id));
 413              $grade_item = $grade_category->load_grade_item();
 414  
 415              // Load appropriate "hidden"/"hidden until" defaults.
 416              if ($grade_item->is_hiddenuntil()) {
 417                  $mform->setDefault('grade_item_hiddenuntil', $grade_item->get_hidden());
 418              } else {
 419                  $mform->setDefault('grade_item_hidden', $grade_item->get_hidden());
 420              }
 421  
 422              if ($grade_item->is_outcome_item()) {
 423                  // we have to prevent incompatible modifications of outcomes if outcomes disabled
 424                  $mform->removeElement('grade_item_grademax');
 425                  if ($mform->elementExists('grade_item_grademin')) {
 426                      $mform->removeElement('grade_item_grademin');
 427                  }
 428                  $mform->removeElement('grade_item_gradetype');
 429                  $mform->removeElement('grade_item_display');
 430                  $mform->removeElement('grade_item_decimals');
 431                  $mform->hardFreeze('grade_item_scaleid');
 432              // Only show the option to rescale grades on a category if its corresponding grade_item has overridden grade_grades.
 433              } else if ($grade_item->has_overridden_grades()) {
 434                  // Can't change the grade type or the scale if there are grades.
 435                  $mform->hardFreeze('grade_item_gradetype, grade_item_scaleid');
 436  
 437                  // If we are using scles then remove the unnecessary rescale and grade fields.
 438                  if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
 439                      $mform->removeElement('grade_item_rescalegrades');
 440                      $mform->removeElement('grade_item_grademax');
 441                      if ($mform->elementExists('grade_item_grademin')) {
 442                          $mform->removeElement('grade_item_grademin');
 443                      }
 444                  } else { // Not using scale, so remove it.
 445                      $mform->removeElement('grade_item_scaleid');
 446                      $mform->disabledIf('grade_item_grademax', 'grade_item_rescalegrades', 'eq', '');
 447                      $mform->disabledIf('grade_item_grademin', 'grade_item_rescalegrades', 'eq', '');
 448                  }
 449              } else { // Remove the rescale element if there are no grades.
 450                  $mform->removeElement('grade_item_rescalegrades');
 451              }
 452  
 453              //remove the aggregation coef element if not needed
 454              if ($grade_item->is_course_item()) {
 455                  if ($mform->elementExists('grade_item_aggregationcoef')) {
 456                      $mform->removeElement('grade_item_aggregationcoef');
 457                  }
 458  
 459                  if ($mform->elementExists('grade_item_weightoverride')) {
 460                      $mform->removeElement('grade_item_weightoverride');
 461                  }
 462                  if ($mform->elementExists('grade_item_aggregationcoef2')) {
 463                      $mform->removeElement('grade_item_aggregationcoef2');
 464                  }
 465              } else {
 466                  if ($grade_item->is_category_item()) {
 467                      $category = $grade_item->get_item_category();
 468                      $parent_category = $category->get_parent_category();
 469                  } else {
 470                      $parent_category = $grade_item->get_parent_category();
 471                  }
 472  
 473                  $parent_category->apply_forced_settings();
 474  
 475                  if (!$parent_category->is_aggregationcoef_used()) {
 476                      if ($mform->elementExists('grade_item_aggregationcoef')) {
 477                          $mform->removeElement('grade_item_aggregationcoef');
 478                      }
 479                  } else {
 480  
 481                      $coefstring = $grade_item->get_coefstring();
 482  
 483                      if ($coefstring == 'aggregationcoefextrasum' || $coefstring == 'aggregationcoefextraweightsum') {
 484                          // advcheckbox is not compatible with disabledIf!
 485                          $coefstring = 'aggregationcoefextrasum';
 486                          $element =& $mform->createElement('checkbox', 'grade_item_aggregationcoef', get_string($coefstring, 'grades'));
 487                      } else {
 488                          $element =& $mform->createElement('text', 'grade_item_aggregationcoef', get_string($coefstring, 'grades'));
 489                      }
 490                      $mform->insertElementBefore($element, 'parentcategory');
 491                      $mform->addHelpButton('grade_item_aggregationcoef', $coefstring, 'grades');
 492                  }
 493  
 494                  // Remove fields used by natural weighting if the parent category is not using natural weighting.
 495                  // Or if the item is a scale and scales are not used in aggregation.
 496                  if ($parent_category->aggregation != GRADE_AGGREGATE_SUM
 497                          || (empty($CFG->grade_includescalesinaggregation) && $grade_item->gradetype == GRADE_TYPE_SCALE)) {
 498                      if ($mform->elementExists('grade_item_weightoverride')) {
 499                          $mform->removeElement('grade_item_weightoverride');
 500                      }
 501                      if ($mform->elementExists('grade_item_aggregationcoef2')) {
 502                          $mform->removeElement('grade_item_aggregationcoef2');
 503                      }
 504                  }
 505              }
 506          }
 507      }
 508  
 509      function validation($data, $files) {
 510          global $COURSE;
 511          $gradeitem = false;
 512          if ($data['id']) {
 513              $gradecategory = grade_category::fetch(array('id' => $data['id']));
 514              $gradeitem = $gradecategory->load_grade_item();
 515          }
 516  
 517          $errors = parent::validation($data, $files);
 518  
 519          if (array_key_exists('grade_item_gradetype', $data) and $data['grade_item_gradetype'] == GRADE_TYPE_SCALE) {
 520              if (empty($data['grade_item_scaleid'])) {
 521                  $errors['grade_item_scaleid'] = get_string('missingscale', 'grades');
 522              }
 523          }
 524  
 525          // We need to make all the validations related with grademax and grademin
 526          // with them being correct floats, keeping the originals unmodified for
 527          // later validations / showing the form back...
 528          // TODO: Note that once MDL-73994 is fixed we'll have to re-visit this and
 529          // adapt the code below to the new values arriving here, without forgetting
 530          // the special case of empties and nulls.
 531          $grademax = isset($data['grade_item_grademax']) ? unformat_float($data['grade_item_grademax']) : null;
 532          $grademin = isset($data['grade_item_grademin']) ? unformat_float($data['grade_item_grademin']) : null;
 533  
 534          if (!is_null($grademin) and !is_null($grademax)) {
 535              if (($grademax != 0 OR $grademin != 0) AND
 536                      ($grademax == $grademin OR $grademax < $grademin)) {
 537                  $errors['grade_item_grademin'] = get_string('incorrectminmax', 'grades');
 538                  $errors['grade_item_grademax'] = get_string('incorrectminmax', 'grades');
 539              }
 540          }
 541  
 542          if ($data['id'] && $gradeitem->has_overridden_grades()) {
 543              if ($gradeitem->gradetype == GRADE_TYPE_VALUE) {
 544                  if (grade_floats_different($grademin, $gradeitem->grademin) ||
 545                      grade_floats_different($grademax, $gradeitem->grademax)) {
 546                      if (empty($data['grade_item_rescalegrades'])) {
 547                          $errors['grade_item_rescalegrades'] = get_string('mustchooserescaleyesorno', 'grades');
 548                      }
 549                  }
 550              }
 551          }
 552          return $errors;
 553      }
 554  }