<?php 
    include_once $CFG->libdir.'/formslib.php';
    //$course = get_record('course', 'id', ($form->courseid) ? $form->courseid : $site->id);
 class calendar_event_form extends moodleform {

        function definition() {
            global $USER, $CFG;
            $mform    =& $this->_form;
            $form = $this->_customdata['form'];
            $mform->addElement('header','general', get_string("general"));
            $mform->addElement('text','name', get_string('eventname', 'calendar'),'maxlength="254" size="50"');
            $mform->addElement('htmleditor','description', get_string('eventdescription','calendar'), array('rows'=>'15', 'cols'=>'35'));
            $mform->addElement('date_time_selector', 'startdate', get_string('eventdate','calendar'));
            $radio = array();
            $radio[] = &MoodleQuickForm::createElement('radio', 'duration', null, get_string("durationnone",'calendar'), 0);
            $radio[] = &MoodleQuickForm::createElement('radio', 'duration', null, get_string("durationuntil",'calendar'), 1);
            $radio[] = &MoodleQuickForm::createElement('date_time_selector','timeduration');
            $radio[] = &MoodleQuickForm::createElement('radio', 'duration', null, get_string("durationminutes",'calendar'), 2);
            $radio[] = &MoodleQuickForm::createElement('text','minutes',get_string("durationminutes",'calendar'),'maxlength="254" size="20"');
            $mform->addGroup($radio, 'duration', get_string("eventduration",'calendar'),array("<br />"," ","<br />"," "), '<br />', false);
            $repeatradio = array();
            $repeatradio[] = &MoodleQuickForm::createElement('radio', 'repeat', null, get_string("repeatnone",'calendar'), 0);
            $repeatradio[] = &MoodleQuickForm::createElement('radio', 'repeat', null, get_string("repeatweeksl",'calendar'), 1);
            $repeatradio[] = &MoodleQuickForm::createElement('text','repeats');
            $mform->addGroup($repeatradio, 'repeat', get_string("eventrepeat",'calendar'), array("<br />"," "), false);
            $mform->addElement('submit', 'submit', get_string("savechanges"));
            $mform->setDefault('courseid',$form->courseid);
            $mform->addElement('hidden', 'courseid',$form->courseid);
            $mform->addElement('hidden', 'groupid',$form->groupid);
            $mform->addElement('hidden', 'userid',$form->userid);
            $mform->addElement('hidden', 'modulename',$form->modulename);
            $mform->addElement('hidden', 'eventtype',$form->eventtype);
            $mform->addElement('hidden', 'instance',$form->instance);
            //$mform->addElement('hidden', 'format',$form->format);
            $mform->addElement('hidden', 'action',"new");
            $mform->addElement('hidden', 'type',"defined");
            $mform->addRule('name', get_string("missingfullname"), 'required', null, 'client');
           // $mform->addRule('description', get_string("missingfullname"), 'required', null, 'client');
           // $mform->addFormRule('form_check');
         }
 
    function validation($fields){
        global $course;
       
        $errors = array();
        $fields['name'] = trim($fields['name']);
        $fields['description'] = trim($fields['description']);
        if(empty($fields['name'])) {
            $errors['name'] = get_string('errornoeventname', 'calendar');
        }
        if(empty($fields['description'])) {
            $errors['description'] = get_string('errornodescription', 'calendar');
        }
        if($fields['duration'] == 2 and !($fields['duration']['minutes'] > 0 and $fields['duration']['minutes'] < 1000)) {
            $errors['minutes'] = get_string('errorinvalidminutes', 'calendar');
        }
        if (!empty($fields['repeat']) and !($fields['repeats'] > 1 and $fields['repeats'] < 100)) {
            $errors['repeats'] = get_string('errorinvalidrepeats', 'calendar');
        }
        if(!empty($fields['courseid'])) {
            // Timestamps must be >= course startdate
            $course = get_record('course', 'id', $fields['courseid']);
            if($course === false) {
                error('Event belongs to invalid course');
            }
            else if($fields['timestart']< $course->startdate) {
                $errors['timestart'] = get_string('errorbeforecoursestart', 'calendar');
            }
        }
        if (0==count($errors)){
            return true;
        }else {
            return $errors;
        }
    }
    }
?>
