Merge branch 'MDL-35726' of git://github.com/netspotau/moodle-mod_assign
[moodle.git] / mod / assign / gradeform.php
CommitLineData
bbd0e548
DW
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
25defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
26
27
28/** Include formslib.php */
29require_once ($CFG->libdir.'/formslib.php');
30/** Include locallib.php */
31require_once($CFG->dirroot . '/mod/assign/locallib.php');
32/** Required for advanced grading */
33require_once('HTML/QuickForm/input.php');
34
35/**
36 * Assignment grade form
37 *
38 * @package mod_assign
39 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class mod_assign_grade_form extends moodleform {
43 /** @var assignment $assignment */
44 private $assignment;
45
46 /**
47 * Define the form - called by parent constructor
48 */
49 function definition() {
50 $mform = $this->_form;
51
52 list($assignment, $data, $params) = $this->_customdata;
53 // visible elements
54 $this->assignment = $assignment;
55 $assignment->add_grade_form_elements($mform, $data, $params);
56
57 if ($data) {
58 $this->set_data($data);
59 }
60 }
61
26af8102
DW
62 /**
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).
65 *
66 * @return string - The unique identifier for this form.
67 */
68 protected function get_form_identifier() {
69 $params = $this->_customdata[2];
70 return get_class($this) . '_' . $params['rownum'];
71 }
72
bbd0e548
DW
73 /**
74 * Perform minimal validation on the grade form
75 * @param array $data
76 * @param array $files
77 */
78 function validation($data, $files) {
79 global $DB;
80 $errors = parent::validation($data, $files);
81 // advanced grading
82 if (!array_key_exists('grade', $data)) {
83 return $errors;
84 }
85
86 if ($this->assignment->get_instance()->grade > 0) {
4913af93 87 if (unformat_float($data['grade']) === null && (!empty($data['grade']))) {
bbd0e548 88 $errors['grade'] = get_string('invalidfloatforgrade', 'assign', $data['grade']);
4913af93 89 } else if (unformat_float($data['grade']) > $this->assignment->get_instance()->grade) {
bbd0e548 90 $errors['grade'] = get_string('gradeabovemaximum', 'assign', $this->assignment->get_instance()->grade);
4913af93 91 } else if (unformat_float($data['grade']) < 0) {
bbd0e548
DW
92 $errors['grade'] = get_string('gradebelowzero', 'assign');
93 }
94 } else {
95 // this is a scale
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');
100 }
101 }
102 }
103 return $errors;
104 }
105
106}