2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * The mform for creating a calendar event. Based on the
21 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace core_calendar\local\event\forms;
26 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->dirroot.'/lib/formslib.php');
31 * The mform class for creating a calendar event.
33 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class create extends \moodleform {
40 public function definition () {
43 $mform = $this->_form;
44 $haserror = !empty($this->_customdata['haserror']);
45 $eventtypes = calendar_get_all_allowed_types();
47 $mform->setDisableShortforms();
48 $mform->disable_form_change_checker();
50 // Empty string so that the element doesn't get rendered.
51 $mform->addElement('header', 'general', '');
53 $this->add_default_hidden_elements($mform);
56 $mform->addElement('text', 'name', get_string('eventname', 'calendar'), 'size="50"');
57 $mform->addRule('name', get_string('required'), 'required', null, 'client');
58 $mform->setType('name', PARAM_TEXT);
60 // Event time start field.
61 $mform->addElement('date_time_selector', 'timestart', get_string('date'));
63 // Add the select elements for the available event types.
64 $this->add_event_type_elements($mform, $eventtypes);
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]);
70 $mform->setType('description', PARAM_RAW);
71 $mform->setAdvanced('description');
73 // Add the variety of elements allowed for selecting event duration.
74 $this->add_event_duration_elements($mform);
76 // Add the form elements for repeating events.
77 $this->add_event_repeat_elements($mform);
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.
81 $PAGE->requires->js_call_amd('core_calendar/event_form', 'init', [$mform->getAttribute('id'), $haserror]);
85 * A bit of custom validation for this form
87 * @param array $data An assoc array of field=>value
88 * @param array $files An array of files
91 public function validation($data, $files) {
94 $errors = parent::validation($data, $files);
95 $coursekey = isset($data['groupcourseid']) ? 'groupcourseid' : 'courseid';
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');
103 $errors[$coursekey] = get_string('invalidcourse', 'error');
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');
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.
120 * @method add_default_hidden_elements
121 * @param MoodleQuickForm $mform
123 protected function add_default_hidden_elements($mform) {
126 // Add some hidden fields.
127 $mform->addElement('hidden', 'id');
128 $mform->setType('id', PARAM_INT);
129 $mform->setDefault('id', 0);
131 $mform->addElement('hidden', 'userid');
132 $mform->setType('userid', PARAM_INT);
133 $mform->setDefault('userid', $USER->id);
135 $mform->addElement('hidden', 'modulename');
136 $mform->setType('modulename', PARAM_INT);
137 $mform->setDefault('modulename', '');
139 $mform->addElement('hidden', 'instance');
140 $mform->setType('instance', PARAM_INT);
141 $mform->setDefault('instance', 0);
143 $mform->addElement('hidden', 'visible');
144 $mform->setType('visible', PARAM_INT);
145 $mform->setDefault('visible', 1);
149 * Add the appropriate elements for the available event types.
151 * If the only event type available is 'user' then we add a hidden
152 * element because there is nothing for the user to choose.
154 * If more than one type is available then we add the elements as
156 * - Always add the event type selector
157 * - Elements per type:
158 * - course: add an additional select element with each
159 * course as an option.
160 * - group: add a select element for the course (different
161 * from the above course select) and a select
162 * element for the group.
164 * @method add_event_type_elements
165 * @param MoodleQuickForm $mform
166 * @param array $eventtypes The available event types for the user
168 protected function add_event_type_elements($mform, $eventtypes) {
171 if (isset($eventtypes['user'])) {
172 $options['user'] = get_string('user');
174 if (isset($eventtypes['group'])) {
175 $options['group'] = get_string('group');
177 if (isset($eventtypes['course'])) {
178 $options['course'] = get_string('course');
180 if (isset($eventtypes['site'])) {
181 $options['site'] = get_string('site');
184 // If we only have one event type and it's 'user' event then don't bother
185 // rendering the select boxes because there is no choice for the user to
187 if (count(array_keys($eventtypes)) == 1 && isset($eventtypes['user'])) {
188 $mform->addElement('hidden', 'eventtype');
189 $mform->setType('eventtype', PARAM_TEXT);
190 $mform->setDefault('eventtype', 'user');
192 // Render a static element to tell the user what type of event will
194 $mform->addElement('static', 'staticeventtype', get_string('eventkind', 'calendar'), $options['user']);
197 $mform->addElement('select', 'eventtype', get_string('eventkind', 'calendar'), $options);
200 if (isset($eventtypes['course'])) {
202 foreach ($eventtypes['course'] as $course) {
203 $courseoptions[$course->id] = format_string($course->fullname, true,
204 ['context' => \context_course::instance($course->id)]);
207 $mform->addElement('select', 'courseid', get_string('course'), $courseoptions);
208 $mform->disabledIf('courseid', 'eventtype', 'noteq', 'course');
211 if (isset($eventtypes['group'])) {
213 foreach ($eventtypes['groupcourses'] as $course) {
214 $courseoptions[$course->id] = format_string($course->fullname, true,
215 ['context' => \context_course::instance($course->id)]);
218 $mform->addElement('select', 'groupcourseid', get_string('course'), $courseoptions);
219 $mform->disabledIf('groupcourseid', 'eventtype', 'noteq', 'group');
222 foreach ($eventtypes['group'] as $group) {
223 // We are formatting it this way in order to provide the javascript both
224 // the course and group ids so that it can enhance the form for the user.
225 $index = "{$group->courseid}-{$group->id}";
226 $groupoptions[$index] = format_string($group->name, true,
227 ['context' => \context_course::instance($group->courseid)]);
230 $mform->addElement('select', 'groupid', get_string('group'), $groupoptions);
231 $mform->disabledIf('groupid', 'eventtype', 'noteq', 'group');
236 * Add the various elements to express the duration options available
239 * @method add_event_duration_elements
240 * @param MoodleQuickForm $mform
242 protected function add_event_duration_elements($mform) {
244 $group[] = $mform->createElement('radio', 'duration', null, get_string('durationnone', 'calendar'), 0);
245 $group[] = $mform->createElement('radio', 'duration', null, get_string('durationuntil', 'calendar'), 1);
246 $group[] = $mform->createElement('date_time_selector', 'timedurationuntil', '');
247 $group[] = $mform->createElement('radio', 'duration', null, get_string('durationminutes', 'calendar'), 2);
248 $group[] = $mform->createElement('text', 'timedurationminutes', get_string('durationminutes', 'calendar'));
250 $mform->addGroup($group, 'durationgroup', get_string('eventduration', 'calendar'), '<br />', false);
251 $mform->setAdvanced('durationgroup');
253 $mform->disabledIf('timedurationuntil', 'duration', 'noteq', 1);
254 $mform->disabledIf('timedurationuntil[day]', 'duration', 'noteq', 1);
255 $mform->disabledIf('timedurationuntil[month]', 'duration', 'noteq', 1);
256 $mform->disabledIf('timedurationuntil[year]', 'duration', 'noteq', 1);
257 $mform->disabledIf('timedurationuntil[hour]', 'duration', 'noteq', 1);
258 $mform->disabledIf('timedurationuntil[minute]', 'duration', 'noteq', 1);
260 $mform->setType('timedurationminutes', PARAM_INT);
261 $mform->disabledIf('timedurationminutes', 'duration', 'noteq', 2);
263 $mform->setDefault('duration', 0);
267 * Add the repeat elements for the form when creating a new event.
269 * @method add_event_repeat_elements
270 * @param MoodleQuickForm $mform
272 protected function add_event_repeat_elements($mform) {
273 $mform->addElement('checkbox', 'repeat', get_string('repeatevent', 'calendar'), null);
274 $mform->addElement('text', 'repeats', get_string('repeatweeksl', 'calendar'), 'maxlength="10" size="10"');
275 $mform->setType('repeats', PARAM_INT);
276 $mform->setDefault('repeats', 1);
277 $mform->disabledIf('repeats', 'repeat', 'notchecked');
278 $mform->setAdvanced('repeat');
279 $mform->setAdvanced('repeats');