|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00018 if (!defined('MOODLE_INTERNAL')) { 00019 die('Direct access to this script is forbidden.'); 00020 } 00021 00022 require_once $CFG->libdir.'/formslib.php'; 00023 00024 class edit_scale_form extends moodleform { 00025 function definition() { 00026 global $CFG; 00027 $mform =& $this->_form; 00028 00029 // visible elements 00030 $mform->addElement('header', 'general', get_string('scale')); 00031 00032 $mform->addElement('text', 'name', get_string('name'), 'size="40"'); 00033 $mform->addRule('name', get_string('required'), 'required', null, 'client'); 00034 $mform->setType('name', PARAM_TEXT); 00035 00036 $mform->addElement('advcheckbox', 'standard', get_string('scalestandard')); 00037 $mform->addHelpButton('standard', 'scalestandard'); 00038 00039 $mform->addElement('static', 'used', get_string('used')); 00040 00041 $mform->addElement('textarea', 'scale', get_string('scale'), array('cols'=>50, 'rows'=>2)); 00042 $mform->addHelpButton('scale', 'scale'); 00043 $mform->addRule('scale', get_string('required'), 'required', null, 'client'); 00044 $mform->setType('scale', PARAM_TEXT); 00045 00046 $mform->addElement('editor', 'description_editor', get_string('description'), null, $this->_customdata['editoroptions']); 00047 00048 // hidden params 00049 $mform->addElement('hidden', 'id', 0); 00050 $mform->setType('id', PARAM_INT); 00051 00052 $mform->addElement('hidden', 'courseid', 0); 00053 $mform->setType('courseid', PARAM_INT); 00054 00056 $gpr = $this->_customdata['gpr']; 00057 $gpr->add_mform_elements($mform); 00058 00059 //------------------------------------------------------------------------------- 00060 // buttons 00061 $this->add_action_buttons(); 00062 } 00063 00064 00066 function definition_after_data() { 00067 global $CFG; 00068 00069 $mform =& $this->_form; 00070 00071 $courseid = $mform->getElementValue('courseid'); 00072 00073 if ($id = $mform->getElementValue('id')) { 00074 $scale = grade_scale::fetch(array('id'=>$id)); 00075 $used = $scale->is_used(); 00076 00077 if ($used) { 00078 $mform->hardFreeze('scale'); 00079 } 00080 00081 if (empty($courseid)) { 00082 $mform->hardFreeze('standard'); 00083 00084 } else if (!has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM))) { 00085 //if they dont have managescales at system level the shouldnt be allowed to make scales standard (or not standard) 00086 $mform->hardFreeze('standard'); 00087 00088 } else if ($used and !empty($scale->courseid)) { 00089 $mform->hardFreeze('standard'); 00090 } 00091 00092 $usedstr = $scale->is_used() ? get_string('yes') : get_string('no'); 00093 $used_el =& $mform->getElement('used'); 00094 $used_el->setValue($usedstr); 00095 00096 } else { 00097 $mform->removeElement('used'); 00098 if (empty($courseid) or !has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM))) { 00099 $mform->hardFreeze('standard'); 00100 } 00101 } 00102 } 00103 00105 function validation($data, $files) { 00106 global $CFG, $COURSE, $DB; 00107 00108 $errors = parent::validation($data, $files); 00109 00110 // we can not allow 2 scales with the same exact scale as this creates 00111 // problems for backup/restore 00112 00113 $old = grade_scale::fetch(array('id'=>$data['id'])); 00114 00115 if (array_key_exists('standard', $data)) { 00116 if (empty($data['standard'])) { 00117 $courseid = $COURSE->id; 00118 } else { 00119 $courseid = 0; 00120 } 00121 00122 } else { 00123 $courseid = $old->courseid; 00124 } 00125 00126 if (array_key_exists('scale', $data)) { 00127 $scalearray = explode(',', $data['scale']); 00128 $scalearray = array_map('trim', $scalearray); 00129 $scaleoptioncount = count($scalearray); 00130 00131 if (count($scalearray) < 2) { 00132 $errors['scale'] = get_string('badlyformattedscale', 'grades'); 00133 } else { 00134 $thescale = implode(',',$scalearray); 00135 00136 $textlib = textlib_get_instance(); 00137 //this check strips out whitespace from the scale we're validating but not from those already in the DB 00138 $count = $DB->count_records_select('scale', "courseid=:courseid AND ".$DB->sql_compare_text('scale', $textlib->strlen($thescale)).'=:scale', 00139 array('courseid'=>$courseid, 'scale'=>$thescale)); 00140 00141 if ($count) { 00142 //if this is a new scale but we found a duplice in the DB 00143 //or we found a duplicate in another course report the error 00144 if (empty($old->id) or $old->courseid != $courseid) { 00145 $errors['scale'] = get_string('duplicatescale', 'grades'); 00146 } else if ($old->scale !== $thescale and $old->scale !== $data['scale']) { 00147 //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 00148 $errors['scale'] = get_string('duplicatescale', 'grades'); 00149 } 00150 } 00151 } 00152 } 00153 00154 return $errors; 00155 } 00156 } 00157 00158