MDL-37624 calendar: Added location support
[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
a5327e44
DW
26use context_system;
27
aa091225
RW
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 {
ea5f7707 39
0df5c743
AN
40 use eventtype;
41
ea5f7707
RW
42 /**
43 * Build the editor options using the given context.
44 *
45 * @param \context $context A Moodle context
46 * @return array
47 */
48 public static function build_editor_options(\context $context) {
49 global $CFG;
50
51 return [
52 'context' => $context,
53 'maxfiles' => EDITOR_UNLIMITED_FILES,
54 'maxbytes' => $CFG->maxbytes,
3c2b703e
SL
55 'noclean' => true,
56 'autosave' => false
ea5f7707
RW
57 ];
58 }
59
aa091225
RW
60 /**
61 * The form definition
62 */
05f184bb 63 public function definition() {
aa091225
RW
64 global $PAGE;
65
66 $mform = $this->_form;
f6e8cc83 67 $starttime = isset($this->_customdata['starttime']) ? $this->_customdata['starttime'] : 0;
ea5f7707 68 $editoroptions = !(empty($this->_customdata['editoroptions'])) ? $this->_customdata['editoroptions'] : null;
aa091225
RW
69 $eventtypes = calendar_get_all_allowed_types();
70
6bb88899
RW
71 if (empty($eventtypes)) {
72 print_error('nopermissiontoupdatecalendar');
73 }
74
aa091225
RW
75 $mform->setDisableShortforms();
76 $mform->disable_form_change_checker();
77
78 // Empty string so that the element doesn't get rendered.
79 $mform->addElement('header', 'general', '');
80
81 $this->add_default_hidden_elements($mform);
82
83 // Event name field.
705eea84 84 $mform->addElement('text', 'name', get_string('eventname', 'calendar'), 'size="50"');
aa091225
RW
85 $mform->addRule('name', get_string('required'), 'required', null, 'client');
86 $mform->setType('name', PARAM_TEXT);
87
88 // Event time start field.
f6e8cc83 89 $mform->addElement('date_time_selector', 'timestart', get_string('date'), ['defaulttime' => $starttime]);
aa091225
RW
90
91 // Add the select elements for the available event types.
92 $this->add_event_type_elements($mform, $eventtypes);
93
705eea84
SL
94 // Start of advanced elements.
95 // Advanced elements are not visible to the user by default.
96 // They are displayed through the user of a show more / less button.
ea5f7707 97 $mform->addElement('editor', 'description', get_string('eventdescription', 'calendar'), ['rows' => 3], $editoroptions);
aa091225
RW
98 $mform->setType('description', PARAM_RAW);
99 $mform->setAdvanced('description');
100
ef4e04ee
MH
101 $mform->addElement('text', 'location', get_string('location', 'moodle'), 'size="50"');
102 $mform->setType('location', PARAM_RAW_TRIMMED);
103 $mform->setAdvanced('location');
104
aa091225
RW
105 // Add the variety of elements allowed for selecting event duration.
106 $this->add_event_duration_elements($mform);
107
108 // Add the form elements for repeating events.
109 $this->add_event_repeat_elements($mform);
110
705eea84 111 // Add the javascript required to enhance this mform.
02e091b1 112 $PAGE->requires->js_call_amd('core_calendar/event_form', 'init', [$mform->getAttribute('id')]);
aa091225
RW
113 }
114
115 /**
116 * A bit of custom validation for this form
117 *
118 * @param array $data An assoc array of field=>value
119 * @param array $files An array of files
120 * @return array
121 */
122 public function validation($data, $files) {
123 global $DB, $CFG;
124
125 $errors = parent::validation($data, $files);
126 $coursekey = isset($data['groupcourseid']) ? 'groupcourseid' : 'courseid';
6bb88899
RW
127 $eventtypes = calendar_get_all_allowed_types();
128 $eventtype = isset($data['eventtype']) ? $data['eventtype'] : null;
129
130 if (empty($eventtype) || !isset($eventtypes[$eventtype])) {
131 $errors['eventtype'] = get_string('invalideventtype', 'calendar');
132 }
aa091225
RW
133
134 if (isset($data[$coursekey]) && $data[$coursekey] > 0) {
135 if ($course = $DB->get_record('course', ['id' => $data[$coursekey]])) {
136 if ($data['timestart'] < $course->startdate) {
137 $errors['timestart'] = get_string('errorbeforecoursestart', 'calendar');
138 }
139 } else {
140 $errors[$coursekey] = get_string('invalidcourse', 'error');
141 }
142 }
143
4b9c662b
SL
144 if ($eventtype == 'course' && empty($data['courseid'])) {
145 $errors['courseid'] = get_string('selectacourse');
146 }
147
86244e91
SL
148 if ($eventtype == 'group' && empty($data['groupcourseid'])) {
149 $errors['groupcourseid'] = get_string('selectacourse');
150 }
151
aa091225
RW
152 if ($data['duration'] == 1 && $data['timestart'] > $data['timedurationuntil']) {
153 $errors['durationgroup'] = get_string('invalidtimedurationuntil', 'calendar');
154 } else if ($data['duration'] == 2 && (trim($data['timedurationminutes']) == '' || $data['timedurationminutes'] < 1)) {
155 $errors['durationgroup'] = get_string('invalidtimedurationminutes', 'calendar');
156 }
157
158 return $errors;
159 }
160
161 /**
162 * Add the list of hidden elements that should appear in this form each
163 * time. These elements will never be visible to the user.
164 *
aa091225
RW
165 * @param MoodleQuickForm $mform
166 */
167 protected function add_default_hidden_elements($mform) {
168 global $USER;
169
705eea84 170 // Add some hidden fields.
aa091225
RW
171 $mform->addElement('hidden', 'id');
172 $mform->setType('id', PARAM_INT);
173 $mform->setDefault('id', 0);
174
175 $mform->addElement('hidden', 'userid');
176 $mform->setType('userid', PARAM_INT);
177 $mform->setDefault('userid', $USER->id);
178
179 $mform->addElement('hidden', 'modulename');
180 $mform->setType('modulename', PARAM_INT);
181 $mform->setDefault('modulename', '');
182
183 $mform->addElement('hidden', 'instance');
184 $mform->setType('instance', PARAM_INT);
185 $mform->setDefault('instance', 0);
186
187 $mform->addElement('hidden', 'visible');
188 $mform->setType('visible', PARAM_INT);
189 $mform->setDefault('visible', 1);
190 }
191
aa091225
RW
192 /**
193 * Add the various elements to express the duration options available
194 * for an event.
195 *
aa091225
RW
196 * @param MoodleQuickForm $mform
197 */
198 protected function add_event_duration_elements($mform) {
199 $group = [];
200 $group[] = $mform->createElement('radio', 'duration', null, get_string('durationnone', 'calendar'), 0);
201 $group[] = $mform->createElement('radio', 'duration', null, get_string('durationuntil', 'calendar'), 1);
202 $group[] = $mform->createElement('date_time_selector', 'timedurationuntil', '');
203 $group[] = $mform->createElement('radio', 'duration', null, get_string('durationminutes', 'calendar'), 2);
204 $group[] = $mform->createElement('text', 'timedurationminutes', get_string('durationminutes', 'calendar'));
205
206 $mform->addGroup($group, 'durationgroup', get_string('eventduration', 'calendar'), '<br />', false);
207 $mform->setAdvanced('durationgroup');
208
209 $mform->disabledIf('timedurationuntil', 'duration', 'noteq', 1);
210 $mform->disabledIf('timedurationuntil[day]', 'duration', 'noteq', 1);
211 $mform->disabledIf('timedurationuntil[month]', 'duration', 'noteq', 1);
212 $mform->disabledIf('timedurationuntil[year]', 'duration', 'noteq', 1);
213 $mform->disabledIf('timedurationuntil[hour]', 'duration', 'noteq', 1);
214 $mform->disabledIf('timedurationuntil[minute]', 'duration', 'noteq', 1);
215
216 $mform->setType('timedurationminutes', PARAM_INT);
705eea84 217 $mform->disabledIf('timedurationminutes', 'duration', 'noteq', 2);
aa091225
RW
218
219 $mform->setDefault('duration', 0);
220 }
221
222 /**
223 * Add the repeat elements for the form when creating a new event.
224 *
aa091225
RW
225 * @param MoodleQuickForm $mform
226 */
227 protected function add_event_repeat_elements($mform) {
228 $mform->addElement('checkbox', 'repeat', get_string('repeatevent', 'calendar'), null);
229 $mform->addElement('text', 'repeats', get_string('repeatweeksl', 'calendar'), 'maxlength="10" size="10"');
230 $mform->setType('repeats', PARAM_INT);
231 $mform->setDefault('repeats', 1);
705eea84 232 $mform->disabledIf('repeats', 'repeat', 'notchecked');
aa091225
RW
233 $mform->setAdvanced('repeat');
234 $mform->setAdvanced('repeats');
235 }
236}