Merge branch 'MDL-73163-311' of https://github.com/lameze/moodle into MOODLE_311_STABLE
[moodle.git] / mod / assign / gradeform.php
blobd2980840c2552b5b5f141751979e61fa08f5c4cd
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.
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/>.
17 /**
18 * This file contains the forms to create and edit an instance of this module
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
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');
32 /**
33 * Assignment grade form
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
39 class mod_assign_grade_form extends moodleform {
40 /** @var assignment $assignment */
41 private $assignment;
43 /**
44 * Define the form - called by parent constructor.
46 public function definition() {
47 $mform = $this->_form;
49 list($assignment, $data, $params) = $this->_customdata;
50 // Visible elements.
51 $this->assignment = $assignment;
52 $assignment->add_grade_form_elements($mform, $data, $params);
54 if ($data) {
55 $this->set_data($data);
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).
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'];
71 /**
72 * Perform minimal validation on the grade form
73 * @param array $data
74 * @param array $files
76 public function validation($data, $files) {
77 global $DB;
78 $errors = parent::validation($data, $files);
79 $instance = $this->assignment->get_instance();
81 // Advanced grading.
82 if (!array_key_exists('grade', $data)) {
83 return $errors;
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');
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');
103 return $errors;