2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * This file contains the forms to create and edit an instance of this module
21 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
28 /** Include formslib.php */
29 require_once ($CFG->libdir.'/formslib.php');
30 /** Include locallib.php */
31 require_once($CFG->dirroot . '/mod/assign/locallib.php');
32 /** Required for advanced grading */
33 require_once('HTML/QuickForm/input.php');
36 * Assignment grade form
39 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class mod_assign_grade_form extends moodleform {
43 /** @var assignment $assignment */
47 * Define the form - called by parent constructor
49 function definition() {
50 $mform = $this->_form;
52 list($assignment, $data, $params) = $this->_customdata;
54 $this->assignment = $assignment;
55 $assignment->add_grade_form_elements($mform, $data, $params);
58 $this->set_data($data);
63 * This is required so when using "Save and next", each form is not defaulted to the previous form.
64 * Giving each form a unique identitifer is enough to prevent this (include the rownum in the form name).
66 * @return string - The unique identifier for this form.
68 protected function get_form_identifier() {
69 $params = $this->_customdata[2];
70 return get_class($this) . '_' . $params['rownum'];
74 * Perform minimal validation on the grade form
78 function validation($data, $files) {
80 $errors = parent::validation($data, $files);
82 if (!array_key_exists('grade', $data)) {
86 if ($this->assignment->get_instance()->grade > 0) {
87 if (unformat_float($data['grade']) === null && (!empty($data['grade']))) {
88 $errors['grade'] = get_string('invalidfloatforgrade', 'assign', $data['grade']);
89 } else if (unformat_float($data['grade']) > $this->assignment->get_instance()->grade) {
90 $errors['grade'] = get_string('gradeabovemaximum', 'assign', $this->assignment->get_instance()->grade);
91 } else if (unformat_float($data['grade']) < 0) {
92 $errors['grade'] = get_string('gradebelowzero', 'assign');
96 if ($scale = $DB->get_record('scale', array('id'=>-($this->assignment->get_instance()->grade)))) {
97 $scaleoptions = make_menu_from_list($scale->scale);
98 if (!array_key_exists((int)$data['grade'], $scaleoptions)) {
99 $errors['grade'] = get_string('invalidgradeforscale', 'assign');