MDL-59921 calendar: replace selector for groups events
[moodle.git] / calendar / classes / local / event / forms / create.php
CommitLineData
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 */
24namespace core_calendar\local\event\forms;
25
26defined('MOODLE_INTERNAL') || die();
27
28require_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 */
36class create extends \moodleform {
ea5f7707
RW
37
38 /**
39 * Build the editor options using the given context.
40 *
41 * @param \context $context A Moodle context
42 * @return array
43 */
44 public static function build_editor_options(\context $context) {
45 global $CFG;
46
47 return [
48 'context' => $context,
49 'maxfiles' => EDITOR_UNLIMITED_FILES,
50 'maxbytes' => $CFG->maxbytes,
51 'noclean' => true
52 ];
53 }
54
aa091225
RW
55 /**
56 * The form definition
57 */
05f184bb 58 public function definition() {
aa091225
RW
59 global $PAGE;
60
61 $mform = $this->_form;
f6e8cc83 62 $starttime = isset($this->_customdata['starttime']) ? $this->_customdata['starttime'] : 0;
ea5f7707 63 $editoroptions = !(empty($this->_customdata['editoroptions'])) ? $this->_customdata['editoroptions'] : null;
aa091225
RW
64 $eventtypes = calendar_get_all_allowed_types();
65
6bb88899
RW
66 if (empty($eventtypes)) {
67 print_error('nopermissiontoupdatecalendar');
68 }
69
aa091225
RW
70 $mform->setDisableShortforms();
71 $mform->disable_form_change_checker();
72
73 // Empty string so that the element doesn't get rendered.
74 $mform->addElement('header', 'general', '');
75
76 $this->add_default_hidden_elements($mform);
77
78 // Event name field.
705eea84 79 $mform->addElement('text', 'name', get_string('eventname', 'calendar'), 'size="50"');
aa091225
RW
80 $mform->addRule('name', get_string('required'), 'required', null, 'client');
81 $mform->setType('name', PARAM_TEXT);
82
83 // Event time start field.
f6e8cc83 84 $mform->addElement('date_time_selector', 'timestart', get_string('date'), ['defaulttime' => $starttime]);
aa091225
RW
85
86 // Add the select elements for the available event types.
87 $this->add_event_type_elements($mform, $eventtypes);
88
705eea84
SL
89 // Start of advanced elements.
90 // Advanced elements are not visible to the user by default.
91 // They are displayed through the user of a show more / less button.
ea5f7707 92 $mform->addElement('editor', 'description', get_string('eventdescription', 'calendar'), ['rows' => 3], $editoroptions);
aa091225
RW
93 $mform->setType('description', PARAM_RAW);
94 $mform->setAdvanced('description');
95
96 // Add the variety of elements allowed for selecting event duration.
97 $this->add_event_duration_elements($mform);
98
99 // Add the form elements for repeating events.
100 $this->add_event_repeat_elements($mform);
101
705eea84 102 // Add the javascript required to enhance this mform.
02e091b1 103 $PAGE->requires->js_call_amd('core_calendar/event_form', 'init', [$mform->getAttribute('id')]);
aa091225
RW
104 }
105
106 /**
107 * A bit of custom validation for this form
108 *
109 * @param array $data An assoc array of field=>value
110 * @param array $files An array of files
111 * @return array
112 */
113 public function validation($data, $files) {
114 global $DB, $CFG;
115
116 $errors = parent::validation($data, $files);
117 $coursekey = isset($data['groupcourseid']) ? 'groupcourseid' : 'courseid';
6bb88899
RW
118 $eventtypes = calendar_get_all_allowed_types();
119 $eventtype = isset($data['eventtype']) ? $data['eventtype'] : null;
120
121 if (empty($eventtype) || !isset($eventtypes[$eventtype])) {
122 $errors['eventtype'] = get_string('invalideventtype', 'calendar');
123 }
aa091225
RW
124
125 if (isset($data[$coursekey]) && $data[$coursekey] > 0) {
126 if ($course = $DB->get_record('course', ['id' => $data[$coursekey]])) {
127 if ($data['timestart'] < $course->startdate) {
128 $errors['timestart'] = get_string('errorbeforecoursestart', 'calendar');
129 }
130 } else {
131 $errors[$coursekey] = get_string('invalidcourse', 'error');
132 }
133 }
134
4b9c662b
SL
135 if ($eventtype == 'course' && empty($data['courseid'])) {
136 $errors['courseid'] = get_string('selectacourse');
137 }
138
86244e91
SL
139 if ($eventtype == 'group' && empty($data['groupcourseid'])) {
140 $errors['groupcourseid'] = get_string('selectacourse');
141 }
142
aa091225
RW
143 if ($data['duration'] == 1 && $data['timestart'] > $data['timedurationuntil']) {
144 $errors['durationgroup'] = get_string('invalidtimedurationuntil', 'calendar');
145 } else if ($data['duration'] == 2 && (trim($data['timedurationminutes']) == '' || $data['timedurationminutes'] < 1)) {
146 $errors['durationgroup'] = get_string('invalidtimedurationminutes', 'calendar');
147 }
148
149 return $errors;
150 }
151
152 /**
153 * Add the list of hidden elements that should appear in this form each
154 * time. These elements will never be visible to the user.
155 *
aa091225
RW
156 * @param MoodleQuickForm $mform
157 */
158 protected function add_default_hidden_elements($mform) {
159 global $USER;
160
705eea84 161 // Add some hidden fields.
aa091225
RW
162 $mform->addElement('hidden', 'id');
163 $mform->setType('id', PARAM_INT);
164 $mform->setDefault('id', 0);
165
166 $mform->addElement('hidden', 'userid');
167 $mform->setType('userid', PARAM_INT);
168 $mform->setDefault('userid', $USER->id);
169
170 $mform->addElement('hidden', 'modulename');
171 $mform->setType('modulename', PARAM_INT);
172 $mform->setDefault('modulename', '');
173
174 $mform->addElement('hidden', 'instance');
175 $mform->setType('instance', PARAM_INT);
176 $mform->setDefault('instance', 0);
177
178 $mform->addElement('hidden', 'visible');
179 $mform->setType('visible', PARAM_INT);
180 $mform->setDefault('visible', 1);
181 }
182
183 /**
184 * Add the appropriate elements for the available event types.
185 *
186 * If the only event type available is 'user' then we add a hidden
187 * element because there is nothing for the user to choose.
188 *
189 * If more than one type is available then we add the elements as
190 * follows:
191 * - Always add the event type selector
192 * - Elements per type:
193 * - course: add an additional select element with each
194 * course as an option.
195 * - group: add a select element for the course (different
196 * from the above course select) and a select
197 * element for the group.
198 *
aa091225
RW
199 * @param MoodleQuickForm $mform
200 * @param array $eventtypes The available event types for the user
201 */
202 protected function add_event_type_elements($mform, $eventtypes) {
203 $options = [];
204
205 if (isset($eventtypes['user'])) {
206 $options['user'] = get_string('user');
207 }
208 if (isset($eventtypes['group'])) {
209 $options['group'] = get_string('group');
210 }
211 if (isset($eventtypes['course'])) {
212 $options['course'] = get_string('course');
213 }
05f184bb
AN
214 if (isset($eventtypes['category'])) {
215 $options['category'] = get_string('category');
216 }
aa091225
RW
217 if (isset($eventtypes['site'])) {
218 $options['site'] = get_string('site');
219 }
220
221 // If we only have one event type and it's 'user' event then don't bother
222 // rendering the select boxes because there is no choice for the user to
223 // make.
224 if (count(array_keys($eventtypes)) == 1 && isset($eventtypes['user'])) {
225 $mform->addElement('hidden', 'eventtype');
226 $mform->setType('eventtype', PARAM_TEXT);
227 $mform->setDefault('eventtype', 'user');
228
229 // Render a static element to tell the user what type of event will
230 // be created.
231 $mform->addElement('static', 'staticeventtype', get_string('eventkind', 'calendar'), $options['user']);
232 return;
233 } else {
234 $mform->addElement('select', 'eventtype', get_string('eventkind', 'calendar'), $options);
235 }
236
05f184bb
AN
237 if (isset($eventtypes['category'])) {
238 $categoryoptions = [];
239 foreach ($eventtypes['category'] as $id => $category) {
240 $categoryoptions[$id] = $category;
241 }
242
243 $mform->addElement('select', 'categoryid', get_string('category'), $categoryoptions);
244 $mform->hideIf('categoryid', 'eventtype', 'noteq', 'category');
245 }
246
aa091225 247 if (isset($eventtypes['course'])) {
4b9c662b 248 $mform->addElement('course', 'courseid', get_string('course'), ['limittoenrolled' => true]);
4d7f28d4 249 $mform->hideIf('courseid', 'eventtype', 'noteq', 'course');
aa091225
RW
250 }
251
252 if (isset($eventtypes['group'])) {
86244e91
SL
253 // Exclude courses without group.
254 $excludedcourses = array_diff(array_keys($eventtypes['course']), array_keys($eventtypes['groupcourses']));
aa091225 255
86244e91
SL
256 $options = ['limittoenrolled' => true, 'exclude' => $excludedcourses];
257 $mform->addElement('course', 'groupcourseid', get_string('course'), $options);
4d7f28d4 258 $mform->hideIf('groupcourseid', 'eventtype', 'noteq', 'group');
aa091225
RW
259
260 $groupoptions = [];
261 foreach ($eventtypes['group'] as $group) {
262 // We are formatting it this way in order to provide the javascript both
263 // the course and group ids so that it can enhance the form for the user.
264 $index = "{$group->courseid}-{$group->id}";
265 $groupoptions[$index] = format_string($group->name, true,
266 ['context' => \context_course::instance($group->courseid)]);
267 }
268
269 $mform->addElement('select', 'groupid', get_string('group'), $groupoptions);
4d7f28d4 270 $mform->hideIf('groupid', 'eventtype', 'noteq', 'group');
86244e91 271 $mform->disabledIf('groupid', 'groupcourseid', 'eq', '');
aa091225
RW
272 }
273 }
274
275 /**
276 * Add the various elements to express the duration options available
277 * for an event.
278 *
aa091225
RW
279 * @param MoodleQuickForm $mform
280 */
281 protected function add_event_duration_elements($mform) {
282 $group = [];
283 $group[] = $mform->createElement('radio', 'duration', null, get_string('durationnone', 'calendar'), 0);
284 $group[] = $mform->createElement('radio', 'duration', null, get_string('durationuntil', 'calendar'), 1);
285 $group[] = $mform->createElement('date_time_selector', 'timedurationuntil', '');
286 $group[] = $mform->createElement('radio', 'duration', null, get_string('durationminutes', 'calendar'), 2);
287 $group[] = $mform->createElement('text', 'timedurationminutes', get_string('durationminutes', 'calendar'));
288
289 $mform->addGroup($group, 'durationgroup', get_string('eventduration', 'calendar'), '<br />', false);
290 $mform->setAdvanced('durationgroup');
291
292 $mform->disabledIf('timedurationuntil', 'duration', 'noteq', 1);
293 $mform->disabledIf('timedurationuntil[day]', 'duration', 'noteq', 1);
294 $mform->disabledIf('timedurationuntil[month]', 'duration', 'noteq', 1);
295 $mform->disabledIf('timedurationuntil[year]', 'duration', 'noteq', 1);
296 $mform->disabledIf('timedurationuntil[hour]', 'duration', 'noteq', 1);
297 $mform->disabledIf('timedurationuntil[minute]', 'duration', 'noteq', 1);
298
299 $mform->setType('timedurationminutes', PARAM_INT);
705eea84 300 $mform->disabledIf('timedurationminutes', 'duration', 'noteq', 2);
aa091225
RW
301
302 $mform->setDefault('duration', 0);
303 }
304
305 /**
306 * Add the repeat elements for the form when creating a new event.
307 *
aa091225
RW
308 * @param MoodleQuickForm $mform
309 */
310 protected function add_event_repeat_elements($mform) {
311 $mform->addElement('checkbox', 'repeat', get_string('repeatevent', 'calendar'), null);
312 $mform->addElement('text', 'repeats', get_string('repeatweeksl', 'calendar'), 'maxlength="10" size="10"');
313 $mform->setType('repeats', PARAM_INT);
314 $mform->setDefault('repeats', 1);
705eea84 315 $mform->disabledIf('repeats', 'repeat', 'notchecked');
aa091225
RW
316 $mform->setAdvanced('repeat');
317 $mform->setAdvanced('repeats');
318 }
319}