calendar MDL-20601 Conversion of calendar to mforms and new html editor + file API
[moodle.git] / calendar / event_form.php
CommitLineData
76d9df3f
SH
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * The mform for creating and editing a calendar event
20 *
21 * @copyright 2009 Sam Hemelryk
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package calendar
24 */
25
26 /**
27 * Always include formslib
28 */
29require_once($CFG->dirroot.'/lib/formslib.php');
30
31/**
32 * The mform class for creating and editing a calendar
33 *
34 * @copyright 2009 Sam Hemelryk
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class event_form extends moodleform {
38 /**
39 * The form definition
40 */
41 function definition () {
42 global $CFG, $USER, $OUTPUT;
43 $mform = $this->_form;
44 $newevent = (empty($this->_customdata->event) || empty($this->_customdata->event->id));
45 $repeatedevents = (!empty($this->_customdata->event->eventrepeats) && $this->_customdata->event->eventrepeats>0);
46 $hasduration = (!empty($this->_customdata->hasduration) && $this->_customdata->hasduration);
47
48 if ($newevent) {
49 $eventtypes = $this->_customdata->eventtypes;
50 $options = array();
51 if (!empty($eventtypes->user)) {
52 $options['user'] = get_string('user');
53 }
54 if (!empty($eventtypes->groups) && is_array($eventtypes->groups)) {
55 $options['group'] = get_string('group');
56 }
57 if (!empty($eventtypes->courses)) {
58 $options['course'] = get_string('course');
59 }
60 if (!empty($eventtypes->site)) {
61 $options['site'] = get_string('site');
62 }
63
64 $mform->addElement('select', 'eventtype', get_string('eventkind', 'calendar'), $options);
65 $mform->addRule('eventtype', get_string('required'), 'required');
66
67 if (!empty($eventtypes->groups) && is_array($eventtypes->groups)) {
68 $groupoptions = array();
69 foreach ($eventtypes->groups as $group) {
70 $groupoptions[$group->id] = $group->name;
71 }
72 $mform->addElement('select', 'groupid', get_string('typegroup', 'calendar'), $groupoptions);
73 $mform->disabledIf('groupid', 'eventtype', 'noteq', 'group');
74 }
75 }
76
77 // Add some hidden fields
78 $mform->addElement('hidden', 'id');
79 $mform->setType('id', PARAM_INT);
80 $mform->setDefault('id', 0);
81
82 $mform->addElement('hidden', 'courseid');
83 $mform->setType('courseid', PARAM_INT);
84
85 $mform->addElement('hidden', 'userid');
86 $mform->setType('userid', PARAM_INT);
87 $mform->setDefault('userid', $USER->id);
88
89 $mform->addElement('hidden', 'modulename');
90 $mform->setType('modulename', PARAM_INT);
91 $mform->setDefault('modulename', '');
92
93 $mform->addElement('hidden', 'instance');
94 $mform->setType('instance', PARAM_INT);
95 $mform->setDefault('instance', 0);
96
97 $mform->addElement('hidden', 'action');
98 $mform->setType('action', PARAM_INT);
99
100 // Normal fields
101 $mform->addElement('text', 'name', get_string('eventname','calendar'), 'size="50"');
102 $mform->addRule('name', get_string('required'), 'required');
103 $mform->setType('name', PARAM_TEXT);
104
105 $mform->addElement('editor', 'description', get_string('eventdescription','calendar'), null, $this->_customdata->event->editoroptions);
106 $mform->setType('description', PARAM_RAW);
107
108 $mform->addElement('date_time_selector', 'timestart', get_string('date'));
109 $mform->addRule('timestart', get_string('required'), 'required');
110
111 $mform->addElement('radio', 'duration', get_string('eventduration', 'calendar'), get_string('durationnone', 'calendar'), 0);
112
113 $mform->addElement('radio', 'duration', null, get_string('durationuntil', 'calendar'), 1);
114 $mform->addElement('date_time_selector', 'timedurationuntil', null);
115 $mform->disabledIf('timedurationuntil','duration','noteq', 1);
116
117 $mform->addElement('radio', 'duration', null, get_string('durationminutes', 'calendar'), 2);
118 $mform->addElement('text', 'timedurationminutes', null);
119 $mform->setType('timedurationminutes', PARAM_INT);
120 $mform->disabledIf('timedurationminutes','duration','noteq', 2);
121
122 $mform->setDefault('duration', ($hasduration)?1:0);
123
124 if ($newevent) {
125
126 $mform->addElement('checkbox', 'repeat', get_string('repeatevent', 'calendar'), null, 'repeat');
127 $mform->addElement('text', 'repeats', get_string('repeatweeksl', 'calendar'), 'maxlength="10" size="10"');
128 $mform->setType('repeats', PARAM_INT);
129 $mform->setDefault('repeats', 1);
130 $mform->disabledIf('repeats','repeat','notchecked');
131
132 } else if ($repeatedevents > 0) {
133
134 $mform->addElement('hidden', 'repeatid');
135 $mform->setType('repeatid', PARAM_INT);
136
137 $mform->addElement('header', 'repeatedevents', get_string('repeatedevents', 'calendar'));
138 $mform->addElement('checkbox', 'repeateditall', null, get_string('repeateditall', 'calendar', $repeatedevents), 'repeat');
139 $mform->setDefault('repeateditall', 'checked');
140
141 }
142
143 $this->add_action_buttons(false, get_string('savechanges'));
144 }
145
146 /**
147 * A bit of custom validation for this form
148 *
149 * @param array $data An assoc array of field=>value
150 * @param array $files An array of files
151 * @return array
152 */
153 function validation($data, $files) {
154 global $DB, $CFG;
155
156 $errors = parent::validation($data, $files);
157
158 if ($data['courseid'] > 0) {
159 if ($course = $DB->get_record('course', array('id'=>$data['courseid']))) {
160 if ($data['timestart'] < $course->startdate) {
161 $errors['timestart'] = get_string('errorbeforecoursestart', 'calendar');
162 }
163 } else {
164 $errors['courseid'] = get_string('invalidcourse', 'error');
165 }
166
167 }
168
169 if ($data['duration'] == 1 && $data['timestart'] > $data['timedurationuntil']) {
170 $errors['timedurationuntil'] = get_string('invalidtimedurationuntil', 'calendar');
171 } else if ($data['duration'] == 2 && (trim($data['timedurationminutes']) == '' || $data['timedurationminutes'] < 1)) {
172 $errors['timedurationminutes'] = get_string('invalidtimedurationminutes', 'calendar');
173 }
174
175 return $errors;
176 }
177
178}