gradebook MDL-24682 corrected a capability check that was incorrectly allowing some...
[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)) {
32648682 127 $count = $DB->count_records('scale', array('courseid'=>$courseid, 'scale'=>$data['scale']));
8fafbc0e 128
129 if (empty($old->id) or $old->courseid != $courseid) {
130 if ($count) {
131 $errors['scale'] = get_string('duplicatescale', 'grades');
132 }
133
134 } else if ($old->scale != $data['scale']) {
135 if ($count) {
136 $errors['scale'] = get_string('duplicatescale', 'grades');
137 }
138 }
139
dc482cfa 140 $options = explode(',', $data['scale']);
141 if (count($options) < 2) {
142 $errors['scale'] = get_string('badlyformattedscale', 'grades');
8fafbc0e 143 }
78ad5f3f 144 }
145
a78890d5 146 return $errors;
78ad5f3f 147 }
78ad5f3f 148}
149
6c3ef410 150