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