|
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_calculation_form extends moodleform { 00025 public $available; 00026 public $noidnumbers; 00027 00028 function definition() { 00029 global $COURSE; 00030 00031 $mform =& $this->_form; 00032 00033 $itemid = $this->_customdata['itemid']; 00034 00035 $this->available = grade_item::fetch_all(array('courseid'=>$COURSE->id)); 00036 $this->noidnumbers = array(); 00037 00038 // All items that have no idnumbers are added to a separate section of the form (hidden by default), 00039 // enabling the user to assign idnumbers to these grade_items. 00040 foreach ($this->available as $item) { 00041 if (empty($item->idnumber)) { 00042 $this->noidnumbers[$item->id] = $item; 00043 unset($this->available[$item->id]); 00044 } 00045 if ($item->id == $itemid) { // Do not include the current grade_item in the available section 00046 unset($this->available[$item->id]); 00047 } 00048 } 00049 00051 $mform->addElement('header', 'general', get_string('gradeitem', 'grades')); 00052 $mform->addElement('static', 'itemname', get_string('itemname', 'grades')); 00053 $mform->addElement('textarea', 'calculation', get_string('calculation', 'grades'), 'cols="60" rows="5"'); 00054 $mform->addHelpButton('calculation', 'calculation', 'grades'); 00055 00057 $mform->addElement('hidden', 'id', 0); 00058 $mform->setType('id', PARAM_INT); 00059 00060 $mform->addElement('hidden', 'courseid', 0); 00061 $mform->setType('courseid', PARAM_INT); 00062 00063 $mform->addElement('hidden', 'section', 0); 00064 $mform->setType('section', PARAM_ALPHA); 00065 $mform->setDefault('section', 'calculation'); 00066 00068 $gpr = $this->_customdata['gpr']; 00069 $gpr->add_mform_elements($mform); 00070 00071 $this->add_action_buttons(); 00072 } 00073 00074 function definition_after_data() { 00075 global $CFG, $COURSE; 00076 00077 $mform =& $this->_form; 00078 } 00079 00081 function validation($data, $files) { 00082 $errors = parent::validation($data, $files); 00083 00084 $mform =& $this->_form; 00085 00086 // check the calculation formula 00087 if ($data['calculation'] != '') { 00088 $grade_item = grade_item::fetch(array('id'=>$data['id'], 'courseid'=>$data['courseid'])); 00089 $calculation = calc_formula::unlocalize(stripslashes($data['calculation'])); 00090 $result = $grade_item->validate_formula($calculation); 00091 if ($result !== true) { 00092 $errors['calculation'] = $result; 00093 } 00094 } 00095 00096 return $errors; 00097 } 00098 00099 } 00100