Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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  /**
  18   * This file contains the forms to create and edit an instance of this module
  19   *
  20   * @package   mod_assign
  21   * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
  26  
  27  
  28  require_once($CFG->libdir.'/formslib.php');
  29  require_once($CFG->dirroot . '/mod/assign/locallib.php');
  30  require_once('HTML/QuickForm/input.php');
  31  
  32  /**
  33   * Assignment grade form
  34   *
  35   * @package   mod_assign
  36   * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
  37   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class mod_assign_grade_form extends moodleform {
  40      /** @var assignment $assignment */
  41      private $assignment;
  42  
  43      /**
  44       * Define the form - called by parent constructor.
  45       */
  46      public function definition() {
  47          $mform = $this->_form;
  48  
  49          list($assignment, $data, $params) = $this->_customdata;
  50          // Visible elements.
  51          $this->assignment = $assignment;
  52          $assignment->add_grade_form_elements($mform, $data, $params);
  53  
  54          if ($data) {
  55              $this->set_data($data);
  56          }
  57      }
  58  
  59      /**
  60       * This is required so when using "Save and next", each form is not defaulted to the previous form.
  61       * Giving each form a unique identitifer is enough to prevent this
  62       * (include the rownum in the form name).
  63       *
  64       * @return string - The unique identifier for this form.
  65       */
  66      protected function get_form_identifier() {
  67          $params = $this->_customdata[2];
  68          return get_class($this) . '_' . $params['userid'];
  69      }
  70  
  71      /**
  72       * Perform minimal validation on the grade form
  73       * @param array $data
  74       * @param array $files
  75       */
  76      public function validation($data, $files) {
  77          global $DB;
  78          $errors = parent::validation($data, $files);
  79          $instance = $this->assignment->get_instance();
  80  
  81          // Advanced grading.
  82          if (!array_key_exists('grade', $data)) {
  83              return $errors;
  84          }
  85  
  86          if ($instance->grade > 0) {
  87              if (unformat_float($data['grade'], true) === false && (!empty($data['grade']))) {
  88                  $errors['grade'] = get_string('invalidfloatforgrade', 'assign', $data['grade']);
  89              } else if (unformat_float($data['grade']) > $instance->grade) {
  90                  $errors['grade'] = get_string('gradeabovemaximum', 'assign', $instance->grade);
  91              } else if (unformat_float($data['grade']) < 0) {
  92                  $errors['grade'] = get_string('gradebelowzero', 'assign');
  93              }
  94          } else {
  95              // This is a scale.
  96              if ($scale = $DB->get_record('scale', array('id'=>-($instance->grade)))) {
  97                  $scaleoptions = make_menu_from_list($scale->scale);
  98                  if ((int)$data['grade'] !== -1 && !array_key_exists((int)$data['grade'], $scaleoptions)) {
  99                      $errors['grade'] = get_string('invalidgradeforscale', 'assign');
 100                  }
 101              }
 102          }
 103          return $errors;
 104      }
 105  }