3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation; either version 2 of the License, or //
15 // (at your option) any later version. //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details: //
22 // http://www.gnu.org/copyleft/gpl.html //
24 ///////////////////////////////////////////////////////////////////////////
26 require_once $CFG->libdir
.'/formslib.php';
28 class edit_scale_form
extends moodleform
{
29 function definition() {
31 $mform =& $this->_form
;
34 $mform->addElement('header', 'general', get_string('scale'));
36 $mform->addElement('text', 'name', get_string('name'), 'size="40"');
37 $mform->addRule('name', get_string('required'), 'required', null, 'client');
38 $mform->setType('name', PARAM_TEXT
);
40 $mform->addElement('advcheckbox', 'standard', get_string('scalestandard'));
41 $mform->setHelpButton('standard', array('scalestandard', get_string('scalestandard'), 'grade'));
43 $mform->addElement('static', 'used', get_string('used'));
45 $mform->addElement('textarea', 'scale', get_string('scale'), array('cols'=>50, 'rows'=>2));
46 $mform->setHelpButton('scale', array('scales', get_string('scale')));
47 $mform->addRule('scale', get_string('required'), 'required', null, 'client');
48 $mform->setType('scale', PARAM_TEXT
);
50 $mform->addElement('htmleditor', 'description', get_string('description'), array('cols'=>80, 'rows'=>20));
54 $mform->addElement('hidden', 'id', 0);
55 $mform->setType('id', PARAM_INT
);
57 $mform->addElement('hidden', 'courseid', 0);
58 $mform->setType('courseid', PARAM_INT
);
60 /// add return tracking info
61 $gpr = $this->_customdata
['gpr'];
62 $gpr->add_mform_elements($mform);
64 //-------------------------------------------------------------------------------
66 $this->add_action_buttons();
70 /// tweak the form - depending on existing data
71 function definition_after_data() {
74 $mform =& $this->_form
;
76 $courseid = $mform->getElementValue('courseid');
78 if ($id = $mform->getElementValue('id')) {
79 $scale = grade_scale
::fetch(array('id'=>$id));
80 $used = $scale->is_used();
83 $mform->hardFreeze('scale');
86 if (empty($courseid)) {
87 $mform->hardFreeze('standard');
89 } else if (empty($scale->courseid
) and !has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM
))) {
90 $mform->hardFreeze('standard');
92 } else if ($used and !empty($scale->courseid
)) {
93 $mform->hardFreeze('standard');
96 $usedstr = $scale->is_used() ?
get_string('yes') : get_string('no');
97 $used_el =& $mform->getElement('used');
98 $used_el->setValue($usedstr);
101 $mform->removeElement('used');
102 if (empty($courseid) or !has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM
))) {
103 $mform->hardFreeze('standard');
108 /// perform extra validation before submission
109 function validation($data, $files) {
110 global $CFG, $COURSE;
112 $errors = parent
::validation($data, $files);
114 // we can not allow 2 scales with the same exact scale as this creates
115 // problems for backup/restore
117 $old = grade_scale
::fetch(array('id'=>$data['id']));
119 if (array_key_exists('standard', $data)) {
120 if (empty($data['standard'])) {
121 $courseid = $COURSE->id
;
127 $courseid = $old->courseid
;
130 if (array_key_exists('scale', $data)) {
131 $count = count_records('scale', 'courseid', $courseid, 'scale', $data['scale']);
133 if (empty($old->id
) or $old->courseid
!= $courseid) {
135 $errors['scale'] = get_string('duplicatescale', 'grades');
138 } else if ($old->scale
!= $data['scale']) {
140 $errors['scale'] = get_string('duplicatescale', 'grades');
144 $options = explode(',', $data['scale']);
145 if (count($options) < 2) {
146 $errors['scale'] = get_string('error');