Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/calendar/event_form.php
Go to the documentation of this file.
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 
00029 if (!defined('MOODLE_INTERNAL')) {
00030     die('Direct access to this script is forbidden.');    
00031 }
00032 
00033 require_once($CFG->dirroot.'/lib/formslib.php');
00034 
00041 class event_form extends moodleform {
00045     function definition () {
00046         global $CFG, $USER, $OUTPUT;
00047         $mform = $this->_form;
00048         $newevent = (empty($this->_customdata->event) || empty($this->_customdata->event->id));
00049         $repeatedevents = (!empty($this->_customdata->event->eventrepeats) && $this->_customdata->event->eventrepeats>0);
00050         $hasduration = (!empty($this->_customdata->hasduration) && $this->_customdata->hasduration);
00051 
00052         if ($newevent) {
00053             $eventtypes = $this->_customdata->eventtypes;
00054             $options = array();
00055             if (!empty($eventtypes->user)) {
00056                 $options['user'] = get_string('user');
00057             }
00058             if (!empty($eventtypes->groups) && is_array($eventtypes->groups)) {
00059                 $options['group'] = get_string('group');
00060             }
00061             if (!empty($eventtypes->courses)) {
00062                 $options['course'] = get_string('course');
00063             }
00064             if (!empty($eventtypes->site)) {
00065                 $options['site'] = get_string('site');
00066             }
00067 
00068             $mform->addElement('select', 'eventtype', get_string('eventkind', 'calendar'), $options);
00069             $mform->addRule('eventtype', get_string('required'), 'required');
00070 
00071             if (!empty($eventtypes->groups) && is_array($eventtypes->groups)) {
00072                 $groupoptions = array();
00073                 foreach ($eventtypes->groups as $group) {
00074                     $groupoptions[$group->id] = $group->name;
00075                 }
00076                 $mform->addElement('select', 'groupid', get_string('typegroup', 'calendar'), $groupoptions);
00077                 $mform->disabledIf('groupid', 'eventtype', 'noteq', 'group');
00078             }
00079         }
00080 
00081         // Add some hidden fields
00082         $mform->addElement('hidden', 'id');
00083         $mform->setType('id', PARAM_INT);
00084         $mform->setDefault('id', 0);
00085 
00086         $mform->addElement('hidden', 'courseid');
00087         $mform->setType('courseid', PARAM_INT);
00088 
00089         $mform->addElement('hidden', 'userid');
00090         $mform->setType('userid', PARAM_INT);
00091         $mform->setDefault('userid', $USER->id);
00092 
00093         $mform->addElement('hidden', 'modulename');
00094         $mform->setType('modulename', PARAM_INT);
00095         $mform->setDefault('modulename', '');
00096 
00097         $mform->addElement('hidden', 'instance');
00098         $mform->setType('instance', PARAM_INT);
00099         $mform->setDefault('instance', 0);
00100 
00101         $mform->addElement('hidden', 'action');
00102         $mform->setType('action', PARAM_INT);
00103 
00104         // Normal fields
00105         $mform->addElement('text', 'name', get_string('eventname','calendar'), 'size="50"');
00106         $mform->addRule('name', get_string('required'), 'required');
00107         $mform->setType('name', PARAM_TEXT);
00108 
00109         $mform->addElement('editor', 'description', get_string('eventdescription','calendar'), null, $this->_customdata->event->editoroptions);
00110         $mform->setType('description', PARAM_RAW);
00111 
00112         $mform->addElement('date_time_selector', 'timestart', get_string('date'));
00113         $mform->addRule('timestart', get_string('required'), 'required');
00114 
00115         $mform->addElement('radio', 'duration', get_string('eventduration', 'calendar'), get_string('durationnone', 'calendar'), 0);
00116 
00117         $mform->addElement('radio', 'duration', null, get_string('durationuntil', 'calendar'), 1);
00118         $mform->addElement('date_time_selector', 'timedurationuntil', '&nbsp;');
00119         $mform->disabledIf('timedurationuntil','duration','noteq', 1);
00120 
00121         $mform->addElement('radio', 'duration', null, get_string('durationminutes', 'calendar'), 2);
00122         $mform->addElement('text', 'timedurationminutes', null);
00123         $mform->setType('timedurationminutes', PARAM_INT);
00124         $mform->disabledIf('timedurationminutes','duration','noteq', 2);
00125 
00126         $mform->setDefault('duration', ($hasduration)?1:0);
00127 
00128         if ($newevent) {
00129 
00130             $mform->addElement('checkbox', 'repeat', get_string('repeatevent', 'calendar'), null, 'repeat');
00131             $mform->addElement('text', 'repeats', get_string('repeatweeksl', 'calendar'), 'maxlength="10" size="10"');
00132             $mform->setType('repeats', PARAM_INT);
00133             $mform->setDefault('repeats', 1);
00134             $mform->disabledIf('repeats','repeat','notchecked');
00135 
00136         } else if ($repeatedevents > 0) {
00137 
00138             $mform->addElement('hidden', 'repeatid');
00139             $mform->setType('repeatid', PARAM_INT);
00140 
00141             $mform->addElement('header', 'repeatedevents', get_string('repeatedevents', 'calendar'));
00142             $mform->addElement('checkbox', 'repeateditall', null, get_string('repeateditall', 'calendar', $repeatedevents), 'repeat');
00143             $mform->setDefault('repeateditall', 'checked');
00144 
00145         }
00146 
00147         $this->add_action_buttons(false, get_string('savechanges'));
00148     }
00149 
00157     function validation($data, $files) {
00158         global $DB, $CFG;
00159 
00160         $errors = parent::validation($data, $files);
00161 
00162         if ($data['courseid'] > 0) {
00163             if ($course = $DB->get_record('course', array('id'=>$data['courseid']))) {
00164                 if ($data['timestart'] < $course->startdate) {
00165                     $errors['timestart'] = get_string('errorbeforecoursestart', 'calendar');
00166                 }
00167             } else {
00168                 $errors['courseid'] = get_string('invalidcourse', 'error');
00169             }
00170 
00171         }
00172 
00173         if ($data['duration'] == 1 && $data['timestart'] > $data['timedurationuntil']) {
00174             $errors['timedurationuntil'] = get_string('invalidtimedurationuntil', 'calendar');
00175         } else if ($data['duration'] == 2 && (trim($data['timedurationminutes']) == '' || $data['timedurationminutes'] < 1)) {
00176             $errors['timedurationminutes'] = get_string('invalidtimedurationminutes', 'calendar');
00177         }
00178 
00179         return $errors;
00180     }
00181 
00182 }
 All Data Structures Namespaces Files Functions Variables Enumerations