MDL-59382 calendar: add modal to create and update events

This commit is contained in:
Ryan Wyllie 2017-07-24 08:01:14 +00:00
parent 6103fd2efe
commit aa0912258d
23 changed files with 2392 additions and 157 deletions

View file

@ -0,0 +1,283 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The mform for creating a calendar event. Based on the
* old event form.
*
* @copyright 2017 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package calendar
*/
namespace core_calendar\local\event\forms;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/lib/formslib.php');
/**
* The mform class for creating a calendar event.
*
* @copyright 2017 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class create extends \moodleform {
/**
* The form definition
*/
public function definition () {
global $PAGE;
$mform = $this->_form;
$haserror = !empty($this->_customdata['haserror']);
$eventtypes = calendar_get_all_allowed_types();
$mform->setDisableShortforms();
$mform->disable_form_change_checker();
// Empty string so that the element doesn't get rendered.
$mform->addElement('header', 'general', '');
$this->add_default_hidden_elements($mform);
// Event name field.
$mform->addElement('text', 'name', get_string('eventname','calendar'), 'size="50"');
$mform->addRule('name', get_string('required'), 'required', null, 'client');
$mform->setType('name', PARAM_TEXT);
// Event time start field.
$mform->addElement('date_time_selector', 'timestart', get_string('date'));
// Add the select elements for the available event types.
$this->add_event_type_elements($mform, $eventtypes);
// ********* START OF ADVANCED ELEMENTS *********.
// Advanced elements are not visible to the user by default. They are
// displayed through the user of a show more / less button.
$mform->addElement('editor', 'description', get_string('eventdescription','calendar'), ['rows' => 3]);
$mform->setType('description', PARAM_RAW);
$mform->setAdvanced('description');
// Add the variety of elements allowed for selecting event duration.
$this->add_event_duration_elements($mform);
// Add the form elements for repeating events.
$this->add_event_repeat_elements($mform);
// Add the javascript required to enhance this mform. Including the show/hide of advanced elements
// and the display of the correct select elements for chosen event types.
$PAGE->requires->js_call_amd('core_calendar/event_form', 'init', [$mform->getAttribute('id'), $haserror]);
}
/**
* A bit of custom validation for this form
*
* @param array $data An assoc array of field=>value
* @param array $files An array of files
* @return array
*/
public function validation($data, $files) {
global $DB, $CFG;
$errors = parent::validation($data, $files);
$coursekey = isset($data['groupcourseid']) ? 'groupcourseid' : 'courseid';
if (isset($data[$coursekey]) && $data[$coursekey] > 0) {
if ($course = $DB->get_record('course', ['id' => $data[$coursekey]])) {
if ($data['timestart'] < $course->startdate) {
$errors['timestart'] = get_string('errorbeforecoursestart', 'calendar');
}
} else {
$errors[$coursekey] = get_string('invalidcourse', 'error');
}
}
if ($data['duration'] == 1 && $data['timestart'] > $data['timedurationuntil']) {
$errors['durationgroup'] = get_string('invalidtimedurationuntil', 'calendar');
} else if ($data['duration'] == 2 && (trim($data['timedurationminutes']) == '' || $data['timedurationminutes'] < 1)) {
$errors['durationgroup'] = get_string('invalidtimedurationminutes', 'calendar');
}
return $errors;
}
/**
* Add the list of hidden elements that should appear in this form each
* time. These elements will never be visible to the user.
*
* @method add_default_hidden_elements
* @param MoodleQuickForm $mform
*/
protected function add_default_hidden_elements($mform) {
global $USER;
// Add some hidden fields
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->setDefault('id', 0);
$mform->addElement('hidden', 'userid');
$mform->setType('userid', PARAM_INT);
$mform->setDefault('userid', $USER->id);
$mform->addElement('hidden', 'modulename');
$mform->setType('modulename', PARAM_INT);
$mform->setDefault('modulename', '');
$mform->addElement('hidden', 'instance');
$mform->setType('instance', PARAM_INT);
$mform->setDefault('instance', 0);
$mform->addElement('hidden', 'visible');
$mform->setType('visible', PARAM_INT);
$mform->setDefault('visible', 1);
}
/**
* Add the appropriate elements for the available event types.
*
* If the only event type available is 'user' then we add a hidden
* element because there is nothing for the user to choose.
*
* If more than one type is available then we add the elements as
* follows:
* - Always add the event type selector
* - Elements per type:
* - course: add an additional select element with each
* course as an option.
* - group: add a select element for the course (different
* from the above course select) and a select
* element for the group.
*
* @method add_event_type_elements
* @param MoodleQuickForm $mform
* @param array $eventtypes The available event types for the user
*/
protected function add_event_type_elements($mform, $eventtypes) {
$options = [];
if (isset($eventtypes['user'])) {
$options['user'] = get_string('user');
}
if (isset($eventtypes['group'])) {
$options['group'] = get_string('group');
}
if (isset($eventtypes['course'])) {
$options['course'] = get_string('course');
}
if (isset($eventtypes['site'])) {
$options['site'] = get_string('site');
}
// If we only have one event type and it's 'user' event then don't bother
// rendering the select boxes because there is no choice for the user to
// make.
if (count(array_keys($eventtypes)) == 1 && isset($eventtypes['user'])) {
$mform->addElement('hidden', 'eventtype');
$mform->setType('eventtype', PARAM_TEXT);
$mform->setDefault('eventtype', 'user');
// Render a static element to tell the user what type of event will
// be created.
$mform->addElement('static', 'staticeventtype', get_string('eventkind', 'calendar'), $options['user']);
return;
} else {
$mform->addElement('select', 'eventtype', get_string('eventkind', 'calendar'), $options);
}
if (isset($eventtypes['course'])) {
$courseoptions = [];
foreach ($eventtypes['course'] as $course) {
$courseoptions[$course->id] = format_string($course->fullname, true,
['context' => \context_course::instance($course->id)]);
}
$mform->addElement('select', 'courseid', get_string('course'), $courseoptions);
$mform->disabledIf('courseid', 'eventtype', 'noteq', 'course');
}
if (isset($eventtypes['group'])) {
$courseoptions = [];
foreach ($eventtypes['groupcourses'] as $course) {
$courseoptions[$course->id] = format_string($course->fullname, true,
['context' => \context_course::instance($course->id)]);
}
$mform->addElement('select', 'groupcourseid', get_string('course'), $courseoptions);
$mform->disabledIf('groupcourseid', 'eventtype', 'noteq', 'group');
$groupoptions = [];
foreach ($eventtypes['group'] as $group) {
// We are formatting it this way in order to provide the javascript both
// the course and group ids so that it can enhance the form for the user.
$index = "{$group->courseid}-{$group->id}";
$groupoptions[$index] = format_string($group->name, true,
['context' => \context_course::instance($group->courseid)]);
}
$mform->addElement('select', 'groupid', get_string('group'), $groupoptions);
$mform->disabledIf('groupid', 'eventtype', 'noteq', 'group');
}
}
/**
* Add the various elements to express the duration options available
* for an event.
*
* @method add_event_duration_elements
* @param MoodleQuickForm $mform
*/
protected function add_event_duration_elements($mform) {
$group = [];
$group[] = $mform->createElement('radio', 'duration', null, get_string('durationnone', 'calendar'), 0);
$group[] = $mform->createElement('radio', 'duration', null, get_string('durationuntil', 'calendar'), 1);
$group[] = $mform->createElement('date_time_selector', 'timedurationuntil', '');
$group[] = $mform->createElement('radio', 'duration', null, get_string('durationminutes', 'calendar'), 2);
$group[] = $mform->createElement('text', 'timedurationminutes', get_string('durationminutes', 'calendar'));
$mform->addGroup($group, 'durationgroup', get_string('eventduration', 'calendar'), '<br />', false);
$mform->setAdvanced('durationgroup');
$mform->disabledIf('timedurationuntil', 'duration', 'noteq', 1);
$mform->disabledIf('timedurationuntil[day]', 'duration', 'noteq', 1);
$mform->disabledIf('timedurationuntil[month]', 'duration', 'noteq', 1);
$mform->disabledIf('timedurationuntil[year]', 'duration', 'noteq', 1);
$mform->disabledIf('timedurationuntil[hour]', 'duration', 'noteq', 1);
$mform->disabledIf('timedurationuntil[minute]', 'duration', 'noteq', 1);
$mform->setType('timedurationminutes', PARAM_INT);
$mform->disabledIf('timedurationminutes','duration','noteq', 2);
$mform->setDefault('duration', 0);
}
/**
* Add the repeat elements for the form when creating a new event.
*
* @method add_event_repeat_elements
* @param MoodleQuickForm $mform
*/
protected function add_event_repeat_elements($mform) {
$mform->addElement('checkbox', 'repeat', get_string('repeatevent', 'calendar'), null);
$mform->addElement('text', 'repeats', get_string('repeatweeksl', 'calendar'), 'maxlength="10" size="10"');
$mform->setType('repeats', PARAM_INT);
$mform->setDefault('repeats', 1);
$mform->disabledIf('repeats','repeat','notchecked');
$mform->setAdvanced('repeat');
$mform->setAdvanced('repeats');
}
}

View file

@ -0,0 +1,60 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The mform for updating a calendar event. Based on the
* old event form.
*
* @copyright 2017 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package calendar
*/
namespace core_calendar\local\event\forms;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/lib/formslib.php');
/**
* The mform class for updating a calendar event.
*
* @copyright 2017 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class update extends create {
/**
* Add the repeat elements for the form when editing an existing event.
*
* @method add_event_repeat_elements
* @param MoodleQuickForm $mform
* @param stdClass $event The event properties
*/
protected function add_event_repeat_elements($mform) {
$event = $this->_customdata['event'];
$mform->addElement('hidden', 'repeatid');
$mform->setType('repeatid', PARAM_INT);
$group = [];
$group[] = $mform->createElement('radio', 'repeateditall', null, get_string('repeateditall', 'calendar', $event->eventrepeats), 1);
$group[] = $mform->createElement('radio', 'repeateditall', null, get_string('repeateditthis', 'calendar'), 0);
$mform->addGroup($group, 'repeatgroup', get_string('repeatedevents', 'calendar'), '<br />', false);
$mform->setDefault('repeateditall', 1);
$mform->setAdvanced('repeatgroup');
}
}

View file

@ -0,0 +1,121 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Event create form and update form mapper.
*
* @package core_calendar
* @copyright 2017 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_calendar\local\event\mappers;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/calendar/lib.php');
/**
* Event create form and update form mapper class.
*
* This class will perform the necessary data transformations to take
* a legacy event and build the appropriate data structure for both the
* create and update event forms.
*
* It will also do the reverse transformation
* and take the returned form data and provide a data structure that can
* be used to set legacy event properties.
*
* @copyright 2017 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class create_update_form_mapper implements create_update_form_mapper_interface {
/**
* Generate the appropriate data for the form from a legacy event.
*
* @method from_legacy_event_to_data
* @param calendar_event $legacyevent
* @return stdClass
*/
public function from_legacy_event_to_data(\calendar_event $legacyevent) {
$legacyevent->count_repeats();
$data = $legacyevent->properties(true);
$data->timedurationuntil = $legacyevent->timestart + $legacyevent->timeduration;
$data->duration = (empty($legacyevent->timeduration)) ? 0 : 1;
if ($legacyevent->eventtype == 'group') {
// Set up the correct value for the to display on the form.
$data->groupid = "{$legacyevent->courseid}-{$legacyevent->groupid}";
$data->groupcourseid = $legacyevent->courseid;
}
return $data;
}
/**
* Generate the appropriate calendar_event properties from the form data.
*
* @method from_data_to_event_properties
* @param stdClass $data
* @return stdClass
*/
public function from_data_to_event_properties(\stdClass $data) {
$properties = clone($data);
// Undo the form definition work around to allow us to have two different
// course selectors present depending on which event type the user selects.
if (isset($data->groupcourseid)) {
$properties->courseid = $data->groupcourseid;
unset($properties->groupcourseid);
}
// Pull the group id back out of the value. The form saves the value
// as "<courseid>-<groupid>" to allow the javascript to work correctly.
if (isset($data->groupid)) {
list($courseid, $groupid) = explode('-', $data->groupid);
$properties->groupid = $groupid;
}
// Default course id if none is set.
if (!isset($data->courseid)) {
$properties->courseid = 0;
}
// Decode the form fields back into valid event property.
$properties->timeduration = $this->get_time_duration_from_form_data($data);
return $properties;
}
/**
* A helper function to calculate the time duration for an event based on
* the event_form data.
*
* @method get_time_duration_from_form_data
* @param \stdClass $data event_form data
* @return int
*/
private function get_time_duration_from_form_data(\stdClass $data) {
if ($data->duration == 1) {
return $data->timedurationuntil- $data->timestart;
} else if ($data->duration == 2) {
return $data->timedurationminutes * MINSECS;
} else {
return 0;
}
}
}

View file

@ -0,0 +1,55 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Create update form mapper interface.
*
* @package core_calendar
* @copyright 2017 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_calendar\local\event\mappers;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/calendar/lib.php');
/**
* Interface for a create_update_form_mapper class
*
* @copyright 2017 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
interface create_update_form_mapper_interface {
/**
* Generate the appropriate data for the form from a legacy event.
*
* @method from_legacy_event_to_data
* @param calendar_event $legacyevent
* @return stdClass
*/
public function from_legacy_event_to_data(\calendar_event $legacyevent);
/**
* Generate the appropriate calendar_event properties from the form data.
*
* @method from_data_to_event_properties
* @param stdClass $data
* @return stdClass
*/
public function from_data_to_event_properties(\stdClass $data);
}