2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Edit form for grade scales
20 * @package core_grades
21 * @copyright 2007 Petr Skoda
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 if (!defined('MOODLE_INTERNAL')) {
26 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
29 require_once $CFG->libdir
.'/formslib.php';
31 class edit_scale_form
extends moodleform
{
32 function definition() {
34 $mform =& $this->_form
;
37 $mform->addElement('header', 'general', get_string('scale'));
39 $mform->addElement('text', 'name', get_string('name'), 'size="40"');
40 $mform->addRule('name', get_string('required'), 'required', null, 'client');
41 $mform->setType('name', PARAM_TEXT
);
43 $mform->addElement('advcheckbox', 'standard', get_string('scalestandard'));
44 $mform->addHelpButton('standard', 'scalestandard');
46 $mform->addElement('static', 'used', get_string('used'));
48 $mform->addElement('textarea', 'scale', get_string('scale'), array('cols'=>50, 'rows'=>2));
49 $mform->addHelpButton('scale', 'scale');
50 $mform->addRule('scale', get_string('required'), 'required', null, 'client');
51 $mform->setType('scale', PARAM_TEXT
);
53 $mform->addElement('editor', 'description_editor', get_string('description'), null, $this->_customdata
['editoroptions']);
56 $mform->addElement('hidden', 'id', 0);
57 $mform->setType('id', PARAM_INT
);
59 $mform->addElement('hidden', 'courseid', 0);
60 $mform->setType('courseid', PARAM_INT
);
62 /// add return tracking info
63 $gpr = $this->_customdata
['gpr'];
64 $gpr->add_mform_elements($mform);
66 //-------------------------------------------------------------------------------
68 $this->add_action_buttons();
72 /// tweak the form - depending on existing data
73 function definition_after_data() {
76 $mform =& $this->_form
;
78 $courseid = $mform->getElementValue('courseid');
80 if ($id = $mform->getElementValue('id')) {
81 $scale = grade_scale
::fetch(array('id'=>$id));
82 $used = $scale->is_used();
85 $mform->hardFreeze('scale');
88 if (empty($courseid)) {
89 $mform->hardFreeze('standard');
91 } else if (!has_capability('moodle/course:managescales', context_system
::instance())) {
92 //if they dont have managescales at system level the shouldnt be allowed to make scales standard (or not standard)
93 $mform->hardFreeze('standard');
95 } else if ($used and !empty($scale->courseid
)) {
96 $mform->hardFreeze('standard');
99 $usedstr = $scale->is_used() ?
get_string('yes') : get_string('no');
100 $used_el =& $mform->getElement('used');
101 $used_el->setValue($usedstr);
104 $mform->removeElement('used');
105 if (empty($courseid) or !has_capability('moodle/course:managescales', context_system
::instance())) {
106 $mform->hardFreeze('standard');
111 /// perform extra validation before submission
112 function validation($data, $files) {
113 global $CFG, $COURSE, $DB;
115 $errors = parent
::validation($data, $files);
117 // we can not allow 2 scales with the same exact scale as this creates
118 // problems for backup/restore
120 $old = grade_scale
::fetch(array('id'=>$data['id']));
122 if (array_key_exists('standard', $data)) {
123 if (empty($data['standard'])) {
124 $courseid = $COURSE->id
;
130 $courseid = $old->courseid
;
133 if (array_key_exists('scale', $data)) {
134 $scalearray = explode(',', $data['scale']);
135 $scalearray = array_map('trim', $scalearray);
136 $scaleoptioncount = count($scalearray);
138 if (count($scalearray) < 1) {
139 $errors['scale'] = get_string('badlyformattedscale', 'grades');
141 $thescale = implode(',',$scalearray);
143 //this check strips out whitespace from the scale we're validating but not from those already in the DB
144 $count = $DB->count_records_select('scale', "courseid=:courseid AND ".$DB->sql_compare_text('scale', core_text
::strlen($thescale)).'=:scale',
145 array('courseid'=>$courseid, 'scale'=>$thescale));
148 //if this is a new scale but we found a duplice in the DB
149 //or we found a duplicate in another course report the error
150 if (empty($old->id
) or $old->courseid
!= $courseid) {
151 $errors['scale'] = get_string('duplicatescale', 'grades');
152 } else if ($old->scale
!== $thescale and $old->scale
!== $data['scale']) {
153 //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
154 $errors['scale'] = get_string('duplicatescale', 'grades');