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