gradebook MDL-25358 improved validation of scales
[moodle.git] / grade / edit / scale / edit_form.php
CommitLineData
e060e33d 1<?php
78ad5f3f 2
e060e33d 3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
8ad36f4c 17
bfebaf64
MD
18if (!defined('MOODLE_INTERNAL')) {
19 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
20}
21
78ad5f3f 22require_once $CFG->libdir.'/formslib.php';
23
24class edit_scale_form extends moodleform {
25 function definition() {
26 global $CFG;
27 $mform =& $this->_form;
28
29 // visible elements
30 $mform->addElement('header', 'general', get_string('scale'));
31
678a79ca 32 $mform->addElement('text', 'name', get_string('name'), 'size="40"');
78ad5f3f 33 $mform->addRule('name', get_string('required'), 'required', null, 'client');
34 $mform->setType('name', PARAM_TEXT);
35
173a9d21 36 $mform->addElement('advcheckbox', 'standard', get_string('scalestandard'));
bd77df31 37 $mform->addHelpButton('standard', 'scalestandard');
78ad5f3f 38
85c9ebb9 39 $mform->addElement('static', 'used', get_string('used'));
78ad5f3f 40
41 $mform->addElement('textarea', 'scale', get_string('scale'), array('cols'=>50, 'rows'=>2));
fffc7ddf 42 $mform->addHelpButton('scale', 'scale');
78ad5f3f 43 $mform->addRule('scale', get_string('required'), 'required', null, 'client');
44 $mform->setType('scale', PARAM_TEXT);
45
8bdc9cac 46 $mform->addElement('editor', 'description_editor', get_string('description'), null, $this->_customdata['editoroptions']);
78ad5f3f 47
48 // hidden params
49 $mform->addElement('hidden', 'id', 0);
50 $mform->setType('id', PARAM_INT);
51
52 $mform->addElement('hidden', 'courseid', 0);
53 $mform->setType('courseid', PARAM_INT);
54
55/// add return tracking info
56 $gpr = $this->_customdata['gpr'];
57 $gpr->add_mform_elements($mform);
58
59//-------------------------------------------------------------------------------
60 // buttons
61 $this->add_action_buttons();
62 }
63
64
65/// tweak the form - depending on existing data
66 function definition_after_data() {
67 global $CFG;
68
69 $mform =& $this->_form;
70
173a9d21 71 $courseid = $mform->getElementValue('courseid');
72
78ad5f3f 73 if ($id = $mform->getElementValue('id')) {
74 $scale = grade_scale::fetch(array('id'=>$id));
85c9ebb9 75 $used = $scale->is_used();
173a9d21 76
85c9ebb9 77 if ($used) {
78ad5f3f 78 $mform->hardFreeze('scale');
79 }
173a9d21 80
81 if (empty($courseid)) {
82 $mform->hardFreeze('standard');
83
d13c358a
AD
84 } else if (!has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM))) {
85 //if they dont have managescales at system level the shouldnt be allowed to make scales standard (or not standard)
173a9d21 86 $mform->hardFreeze('standard');
87
85c9ebb9 88 } else if ($used and !empty($scale->courseid)) {
173a9d21 89 $mform->hardFreeze('standard');
90 }
91
85c9ebb9 92 $usedstr = $scale->is_used() ? get_string('yes') : get_string('no');
93 $used_el =& $mform->getElement('used');
94 $used_el->setValue($usedstr);
78ad5f3f 95
96 } else {
85c9ebb9 97 $mform->removeElement('used');
173a9d21 98 if (empty($courseid) or !has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM))) {
99 $mform->hardFreeze('standard');
78ad5f3f 100 }
101 }
102 }
103
104/// perform extra validation before submission
a78890d5 105 function validation($data, $files) {
32648682 106 global $CFG, $COURSE, $DB;
11a14999 107
a78890d5 108 $errors = parent::validation($data, $files);
11a14999 109
ed3cdf07 110 // we can not allow 2 scales with the same exact scale as this creates
111 // problems for backup/restore
8fafbc0e 112
113 $old = grade_scale::fetch(array('id'=>$data['id']));
114
115 if (array_key_exists('standard', $data)) {
116 if (empty($data['standard'])) {
117 $courseid = $COURSE->id;
118 } else {
119 $courseid = 0;
120 }
121
122 } else {
123 $courseid = $old->courseid;
11a14999 124 }
78ad5f3f 125
8fafbc0e 126 if (array_key_exists('scale', $data)) {
d2e66a60
AD
127 $scalearray = explode(',', $data['scale']);
128 $scalearray = array_map('trim', $scalearray);
129 $scaleoptioncount = count($scalearray);
8fafbc0e 130
d2e66a60
AD
131 if (count($scalearray) < 2) {
132 $errors['scale'] = get_string('badlyformattedscale', 'grades');
133 } else {
134 $thescale = implode(',',$scalearray);
8fafbc0e 135
d2e66a60
AD
136 $textlib = textlib_get_instance();
137 //this check strips out whitespace from the scale we're validating but not from those already in the DB
138 $count = $DB->count_records_select('scale', "courseid=:courseid AND ".$DB->sql_compare_text('scale', $textlib->strlen($thescale)).'=:scale',
139 array('courseid'=>$courseid, 'scale'=>$thescale));
8fafbc0e 140
d2e66a60
AD
141 if ($count) {
142 //if this is a new scale but we found a duplice in the DB
143 //or we found a duplicate in another course report the error
144 if (empty($old->id) or $old->courseid != $courseid) {
145 $errors['scale'] = get_string('duplicatescale', 'grades');
146 } else if ($old->scale !== $thescale and $old->scale !== $data['scale']) {
147 //if the old scale from DB is different but we found a duplicate then we're trying to modify a scale to be a duplicate
148 $errors['scale'] = get_string('duplicatescale', 'grades');
149 }
150 }
8fafbc0e 151 }
78ad5f3f 152 }
153
a78890d5 154 return $errors;
78ad5f3f 155 }
78ad5f3f 156}
157
6c3ef410 158