Commit | Line | Data |
---|---|---|
aa091225 | 1 | <?php |
aa091225 RW |
2 | // This file is part of Moodle - http://moodle.org/ |
3 | // | |
4 | // Moodle is free software: you can redistribute it and/or modify | |
5 | // it under the terms of the GNU General Public License as published by | |
6 | // the Free Software Foundation, either version 3 of the License, or | |
7 | // (at your option) any later version. | |
8 | // | |
9 | // Moodle is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | // | |
14 | // You should have received a copy of the GNU General Public License | |
15 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
17 | /** | |
b2af3705 | 18 | * The mform for creating a calendar event. Based on the old event form. |
aa091225 | 19 | * |
b2af3705 | 20 | * @package core_calendar |
aa091225 RW |
21 | * @copyright 2017 Ryan Wyllie <ryan@moodle.com> |
22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
aa091225 RW |
23 | */ |
24 | namespace core_calendar\local\event\forms; | |
25 | ||
26 | defined('MOODLE_INTERNAL') || die(); | |
27 | ||
28 | require_once($CFG->dirroot.'/lib/formslib.php'); | |
29 | ||
30 | /** | |
31 | * The mform class for creating a calendar event. | |
32 | * | |
33 | * @copyright 2017 Ryan Wyllie <ryan@moodle.com> | |
34 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
35 | */ | |
36 | class create extends \moodleform { | |
37 | /** | |
38 | * The form definition | |
39 | */ | |
40 | public function definition () { | |
41 | global $PAGE; | |
42 | ||
43 | $mform = $this->_form; | |
f6e8cc83 | 44 | $starttime = isset($this->_customdata['starttime']) ? $this->_customdata['starttime'] : 0; |
aa091225 RW |
45 | $eventtypes = calendar_get_all_allowed_types(); |
46 | ||
6bb88899 RW |
47 | if (empty($eventtypes)) { |
48 | print_error('nopermissiontoupdatecalendar'); | |
49 | } | |
50 | ||
aa091225 RW |
51 | $mform->setDisableShortforms(); |
52 | $mform->disable_form_change_checker(); | |
53 | ||
54 | // Empty string so that the element doesn't get rendered. | |
55 | $mform->addElement('header', 'general', ''); | |
56 | ||
57 | $this->add_default_hidden_elements($mform); | |
58 | ||
59 | // Event name field. | |
705eea84 | 60 | $mform->addElement('text', 'name', get_string('eventname', 'calendar'), 'size="50"'); |
aa091225 RW |
61 | $mform->addRule('name', get_string('required'), 'required', null, 'client'); |
62 | $mform->setType('name', PARAM_TEXT); | |
63 | ||
64 | // Event time start field. | |
f6e8cc83 | 65 | $mform->addElement('date_time_selector', 'timestart', get_string('date'), ['defaulttime' => $starttime]); |
aa091225 RW |
66 | |
67 | // Add the select elements for the available event types. | |
68 | $this->add_event_type_elements($mform, $eventtypes); | |
69 | ||
705eea84 SL |
70 | // Start of advanced elements. |
71 | // Advanced elements are not visible to the user by default. | |
72 | // They are displayed through the user of a show more / less button. | |
73 | $mform->addElement('editor', 'description', get_string('eventdescription', 'calendar'), ['rows' => 3]); | |
aa091225 RW |
74 | $mform->setType('description', PARAM_RAW); |
75 | $mform->setAdvanced('description'); | |
76 | ||
77 | // Add the variety of elements allowed for selecting event duration. | |
78 | $this->add_event_duration_elements($mform); | |
79 | ||
80 | // Add the form elements for repeating events. | |
81 | $this->add_event_repeat_elements($mform); | |
82 | ||
705eea84 | 83 | // Add the javascript required to enhance this mform. |
02e091b1 | 84 | $PAGE->requires->js_call_amd('core_calendar/event_form', 'init', [$mform->getAttribute('id')]); |
aa091225 RW |
85 | } |
86 | ||
87 | /** | |
88 | * A bit of custom validation for this form | |
89 | * | |
90 | * @param array $data An assoc array of field=>value | |
91 | * @param array $files An array of files | |
92 | * @return array | |
93 | */ | |
94 | public function validation($data, $files) { | |
95 | global $DB, $CFG; | |
96 | ||
97 | $errors = parent::validation($data, $files); | |
98 | $coursekey = isset($data['groupcourseid']) ? 'groupcourseid' : 'courseid'; | |
6bb88899 RW |
99 | $eventtypes = calendar_get_all_allowed_types(); |
100 | $eventtype = isset($data['eventtype']) ? $data['eventtype'] : null; | |
101 | ||
102 | if (empty($eventtype) || !isset($eventtypes[$eventtype])) { | |
103 | $errors['eventtype'] = get_string('invalideventtype', 'calendar'); | |
104 | } | |
aa091225 RW |
105 | |
106 | if (isset($data[$coursekey]) && $data[$coursekey] > 0) { | |
107 | if ($course = $DB->get_record('course', ['id' => $data[$coursekey]])) { | |
108 | if ($data['timestart'] < $course->startdate) { | |
109 | $errors['timestart'] = get_string('errorbeforecoursestart', 'calendar'); | |
110 | } | |
111 | } else { | |
112 | $errors[$coursekey] = get_string('invalidcourse', 'error'); | |
113 | } | |
114 | } | |
115 | ||
116 | if ($data['duration'] == 1 && $data['timestart'] > $data['timedurationuntil']) { | |
117 | $errors['durationgroup'] = get_string('invalidtimedurationuntil', 'calendar'); | |
118 | } else if ($data['duration'] == 2 && (trim($data['timedurationminutes']) == '' || $data['timedurationminutes'] < 1)) { | |
119 | $errors['durationgroup'] = get_string('invalidtimedurationminutes', 'calendar'); | |
120 | } | |
121 | ||
122 | return $errors; | |
123 | } | |
124 | ||
125 | /** | |
126 | * Add the list of hidden elements that should appear in this form each | |
127 | * time. These elements will never be visible to the user. | |
128 | * | |
aa091225 RW |
129 | * @param MoodleQuickForm $mform |
130 | */ | |
131 | protected function add_default_hidden_elements($mform) { | |
132 | global $USER; | |
133 | ||
705eea84 | 134 | // Add some hidden fields. |
aa091225 RW |
135 | $mform->addElement('hidden', 'id'); |
136 | $mform->setType('id', PARAM_INT); | |
137 | $mform->setDefault('id', 0); | |
138 | ||
139 | $mform->addElement('hidden', 'userid'); | |
140 | $mform->setType('userid', PARAM_INT); | |
141 | $mform->setDefault('userid', $USER->id); | |
142 | ||
143 | $mform->addElement('hidden', 'modulename'); | |
144 | $mform->setType('modulename', PARAM_INT); | |
145 | $mform->setDefault('modulename', ''); | |
146 | ||
147 | $mform->addElement('hidden', 'instance'); | |
148 | $mform->setType('instance', PARAM_INT); | |
149 | $mform->setDefault('instance', 0); | |
150 | ||
151 | $mform->addElement('hidden', 'visible'); | |
152 | $mform->setType('visible', PARAM_INT); | |
153 | $mform->setDefault('visible', 1); | |
154 | } | |
155 | ||
156 | /** | |
157 | * Add the appropriate elements for the available event types. | |
158 | * | |
159 | * If the only event type available is 'user' then we add a hidden | |
160 | * element because there is nothing for the user to choose. | |
161 | * | |
162 | * If more than one type is available then we add the elements as | |
163 | * follows: | |
164 | * - Always add the event type selector | |
165 | * - Elements per type: | |
166 | * - course: add an additional select element with each | |
167 | * course as an option. | |
168 | * - group: add a select element for the course (different | |
169 | * from the above course select) and a select | |
170 | * element for the group. | |
171 | * | |
aa091225 RW |
172 | * @param MoodleQuickForm $mform |
173 | * @param array $eventtypes The available event types for the user | |
174 | */ | |
175 | protected function add_event_type_elements($mform, $eventtypes) { | |
176 | $options = []; | |
177 | ||
178 | if (isset($eventtypes['user'])) { | |
179 | $options['user'] = get_string('user'); | |
180 | } | |
181 | if (isset($eventtypes['group'])) { | |
182 | $options['group'] = get_string('group'); | |
183 | } | |
184 | if (isset($eventtypes['course'])) { | |
185 | $options['course'] = get_string('course'); | |
186 | } | |
187 | if (isset($eventtypes['site'])) { | |
188 | $options['site'] = get_string('site'); | |
189 | } | |
190 | ||
191 | // If we only have one event type and it's 'user' event then don't bother | |
192 | // rendering the select boxes because there is no choice for the user to | |
193 | // make. | |
194 | if (count(array_keys($eventtypes)) == 1 && isset($eventtypes['user'])) { | |
195 | $mform->addElement('hidden', 'eventtype'); | |
196 | $mform->setType('eventtype', PARAM_TEXT); | |
197 | $mform->setDefault('eventtype', 'user'); | |
198 | ||
199 | // Render a static element to tell the user what type of event will | |
200 | // be created. | |
201 | $mform->addElement('static', 'staticeventtype', get_string('eventkind', 'calendar'), $options['user']); | |
202 | return; | |
203 | } else { | |
204 | $mform->addElement('select', 'eventtype', get_string('eventkind', 'calendar'), $options); | |
205 | } | |
206 | ||
207 | if (isset($eventtypes['course'])) { | |
208 | $courseoptions = []; | |
209 | foreach ($eventtypes['course'] as $course) { | |
210 | $courseoptions[$course->id] = format_string($course->fullname, true, | |
211 | ['context' => \context_course::instance($course->id)]); | |
212 | } | |
213 | ||
214 | $mform->addElement('select', 'courseid', get_string('course'), $courseoptions); | |
4d7f28d4 | 215 | $mform->hideIf('courseid', 'eventtype', 'noteq', 'course'); |
aa091225 RW |
216 | } |
217 | ||
218 | if (isset($eventtypes['group'])) { | |
219 | $courseoptions = []; | |
220 | foreach ($eventtypes['groupcourses'] as $course) { | |
221 | $courseoptions[$course->id] = format_string($course->fullname, true, | |
222 | ['context' => \context_course::instance($course->id)]); | |
223 | } | |
224 | ||
225 | $mform->addElement('select', 'groupcourseid', get_string('course'), $courseoptions); | |
4d7f28d4 | 226 | $mform->hideIf('groupcourseid', 'eventtype', 'noteq', 'group'); |
aa091225 RW |
227 | |
228 | $groupoptions = []; | |
229 | foreach ($eventtypes['group'] as $group) { | |
230 | // We are formatting it this way in order to provide the javascript both | |
231 | // the course and group ids so that it can enhance the form for the user. | |
232 | $index = "{$group->courseid}-{$group->id}"; | |
233 | $groupoptions[$index] = format_string($group->name, true, | |
234 | ['context' => \context_course::instance($group->courseid)]); | |
235 | } | |
236 | ||
237 | $mform->addElement('select', 'groupid', get_string('group'), $groupoptions); | |
4d7f28d4 | 238 | $mform->hideIf('groupid', 'eventtype', 'noteq', 'group'); |
aa091225 RW |
239 | } |
240 | } | |
241 | ||
242 | /** | |
243 | * Add the various elements to express the duration options available | |
244 | * for an event. | |
245 | * | |
aa091225 RW |
246 | * @param MoodleQuickForm $mform |
247 | */ | |
248 | protected function add_event_duration_elements($mform) { | |
249 | $group = []; | |
250 | $group[] = $mform->createElement('radio', 'duration', null, get_string('durationnone', 'calendar'), 0); | |
251 | $group[] = $mform->createElement('radio', 'duration', null, get_string('durationuntil', 'calendar'), 1); | |
252 | $group[] = $mform->createElement('date_time_selector', 'timedurationuntil', ''); | |
253 | $group[] = $mform->createElement('radio', 'duration', null, get_string('durationminutes', 'calendar'), 2); | |
254 | $group[] = $mform->createElement('text', 'timedurationminutes', get_string('durationminutes', 'calendar')); | |
255 | ||
256 | $mform->addGroup($group, 'durationgroup', get_string('eventduration', 'calendar'), '<br />', false); | |
257 | $mform->setAdvanced('durationgroup'); | |
258 | ||
259 | $mform->disabledIf('timedurationuntil', 'duration', 'noteq', 1); | |
260 | $mform->disabledIf('timedurationuntil[day]', 'duration', 'noteq', 1); | |
261 | $mform->disabledIf('timedurationuntil[month]', 'duration', 'noteq', 1); | |
262 | $mform->disabledIf('timedurationuntil[year]', 'duration', 'noteq', 1); | |
263 | $mform->disabledIf('timedurationuntil[hour]', 'duration', 'noteq', 1); | |
264 | $mform->disabledIf('timedurationuntil[minute]', 'duration', 'noteq', 1); | |
265 | ||
266 | $mform->setType('timedurationminutes', PARAM_INT); | |
705eea84 | 267 | $mform->disabledIf('timedurationminutes', 'duration', 'noteq', 2); |
aa091225 RW |
268 | |
269 | $mform->setDefault('duration', 0); | |
270 | } | |
271 | ||
272 | /** | |
273 | * Add the repeat elements for the form when creating a new event. | |
274 | * | |
aa091225 RW |
275 | * @param MoodleQuickForm $mform |
276 | */ | |
277 | protected function add_event_repeat_elements($mform) { | |
278 | $mform->addElement('checkbox', 'repeat', get_string('repeatevent', 'calendar'), null); | |
279 | $mform->addElement('text', 'repeats', get_string('repeatweeksl', 'calendar'), 'maxlength="10" size="10"'); | |
280 | $mform->setType('repeats', PARAM_INT); | |
281 | $mform->setDefault('repeats', 1); | |
705eea84 | 282 | $mform->disabledIf('repeats', 'repeat', 'notchecked'); |
aa091225 RW |
283 | $mform->setAdvanced('repeat'); |
284 | $mform->setAdvanced('repeats'); | |
285 | } | |
286 | } |