3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 if (!defined('MOODLE_INTERNAL')) {
19 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
22 require_once $CFG->libdir.'/formslib.php';
24 class edit_scale_form extends moodleform {
25 function definition() {
27 $mform =& $this->_form;
30 $mform->addElement('header', 'general', get_string('scale'));
32 $mform->addElement('text', 'name', get_string('name'), 'size="40"');
33 $mform->addRule('name', get_string('required'), 'required', null, 'client');
34 $mform->setType('name', PARAM_TEXT);
36 $mform->addElement('advcheckbox', 'standard', get_string('scalestandard'));
37 $mform->addHelpButton('standard', 'scalestandard');
39 $mform->addElement('static', 'used', get_string('used'));
41 $mform->addElement('textarea', 'scale', get_string('scale'), array('cols'=>50, 'rows'=>2));
42 $mform->addHelpButton('scale', 'scale');
43 $mform->addRule('scale', get_string('required'), 'required', null, 'client');
44 $mform->setType('scale', PARAM_TEXT);
46 $mform->addElement('editor', 'description_editor', get_string('description'), null, $this->_customdata['editoroptions']);
49 $mform->addElement('hidden', 'id', 0);
50 $mform->setType('id', PARAM_INT);
52 $mform->addElement('hidden', 'courseid', 0);
53 $mform->setType('courseid', PARAM_INT);
55 /// add return tracking info
56 $gpr = $this->_customdata['gpr'];
57 $gpr->add_mform_elements($mform);
59 //-------------------------------------------------------------------------------
61 $this->add_action_buttons();
65 /// tweak the form - depending on existing data
66 function definition_after_data() {
69 $mform =& $this->_form;
71 $courseid = $mform->getElementValue('courseid');
73 if ($id = $mform->getElementValue('id')) {
74 $scale = grade_scale::fetch(array('id'=>$id));
75 $used = $scale->is_used();
78 $mform->hardFreeze('scale');
81 if (empty($courseid)) {
82 $mform->hardFreeze('standard');
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)
86 $mform->hardFreeze('standard');
88 } else if ($used and !empty($scale->courseid)) {
89 $mform->hardFreeze('standard');
92 $usedstr = $scale->is_used() ? get_string('yes') : get_string('no');
93 $used_el =& $mform->getElement('used');
94 $used_el->setValue($usedstr);
97 $mform->removeElement('used');
98 if (empty($courseid) or !has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM))) {
99 $mform->hardFreeze('standard');
104 /// perform extra validation before submission
105 function validation($data, $files) {
106 global $CFG, $COURSE, $DB;
108 $errors = parent::validation($data, $files);
110 // we can not allow 2 scales with the same exact scale as this creates
111 // problems for backup/restore
113 $old = grade_scale::fetch(array('id'=>$data['id']));
115 if (array_key_exists('standard', $data)) {
116 if (empty($data['standard'])) {
117 $courseid = $COURSE->id;
123 $courseid = $old->courseid;
126 if (array_key_exists('scale', $data)) {
127 $count = $DB->count_records('scale', array('courseid'=>$courseid, 'scale'=>$data['scale']));
129 if (empty($old->id) or $old->courseid != $courseid) {
131 $errors['scale'] = get_string('duplicatescale', 'grades');
134 } else if ($old->scale != $data['scale']) {
136 $errors['scale'] = get_string('duplicatescale', 'grades');
140 $options = explode(',', $data['scale']);
141 if (count($options) < 2) {
142 $errors['scale'] = get_string('badlyformattedscale', 'grades');