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 require_once($CFG->libdir
.'/formslib.php');
29 require_once($CFG->dirroot
. '/mod/assign/locallib.php');
30 require_once('HTML/QuickForm/input.php');
33 * Assignment grade form
36 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class mod_assign_grade_form
extends moodleform
{
40 /** @var assignment $assignment */
44 * Define the form - called by parent constructor.
46 public function definition() {
47 $mform = $this->_form
;
49 list($assignment, $data, $params) = $this->_customdata
;
51 $this->assignment
= $assignment;
52 $assignment->add_grade_form_elements($mform, $data, $params);
55 $this->set_data($data);
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).
64 * @return string - The unique identifier for this form.
66 protected function get_form_identifier() {
67 $params = $this->_customdata
[2];
68 return get_class($this) . '_' . $params['userid'];
72 * Perform minimal validation on the grade form
76 public function validation($data, $files) {
78 $errors = parent
::validation($data, $files);
79 $instance = $this->assignment
->get_instance();
82 if (!array_key_exists('grade', $data)) {
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');
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');