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