Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

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

Differences Between: [Versions 310 and 401] [Versions 39 and 401] [Versions 401 and 403]

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