From f4a61ac99a53a756bbdf050a100ef96f6126429d Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Tue, 27 Nov 2018 09:22:06 +0800 Subject: [PATCH 1/3] MDL-64214 core_calendar: fix event types form element handling --- .../local/event/forms/managesubscriptions.php | 4 +++ calendar/lib.php | 8 ------ calendar/managesubscriptions.php | 28 ++++++++++++++++++- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/calendar/classes/local/event/forms/managesubscriptions.php b/calendar/classes/local/event/forms/managesubscriptions.php index 102761f7116..61ef73f087c 100644 --- a/calendar/classes/local/event/forms/managesubscriptions.php +++ b/calendar/classes/local/event/forms/managesubscriptions.php @@ -40,6 +40,7 @@ class managesubscriptions extends \moodleform { * Defines the form used to add calendar subscriptions. */ public function definition() { + global $PAGE; $mform = $this->_form; $eventtypes = calendar_get_allowed_event_types(); if (in_array(true, $eventtypes, true) === false) { @@ -86,6 +87,9 @@ class managesubscriptions extends \moodleform { // Eventtype: 0 = user, 1 = global, anything else = course ID. $mform->addElement('submit', 'add', get_string('add')); + + // Add the javascript required to enhance this mform. + $PAGE->requires->js_call_amd('core_calendar/event_form', 'init', [$mform->getAttribute('id')]); } /** diff --git a/calendar/lib.php b/calendar/lib.php index a91e4f2eca8..2e78d15b6b5 100644 --- a/calendar/lib.php +++ b/calendar/lib.php @@ -2783,14 +2783,6 @@ function calendar_add_subscription($sub) { unset($sub->groupcourseid); } - // Pull the group id back out of the value. The form saves the value - // as "-" to allow the javascript to work correctly. - if (!empty($sub->groupid)) { - list($courseid, $groupid) = explode('-', $sub->groupid); - $sub->courseid = $courseid; - $sub->groupid = $groupid; - } - // Default course id if none is set. if (empty($sub->courseid)) { if ($sub->eventtype === 'site') { diff --git a/calendar/managesubscriptions.php b/calendar/managesubscriptions.php index 87289a6c9be..9b5c301a2dd 100644 --- a/calendar/managesubscriptions.php +++ b/calendar/managesubscriptions.php @@ -71,7 +71,33 @@ $importresults = ''; $formdata = $form->get_data(); if (!empty($formdata)) { require_sesskey(); // Must have sesskey for all actions. - $subscriptionid = calendar_add_subscription($formdata); + // The course field on group event type is named groupcourseid. + // So it's easy to assume it is going to add a subscription as group events. + if (isset($formdata->groupcourseid)) { + $courseid = $formdata->groupcourseid; + // We need to fetch the groups again for group event type. + // The groupid gets filtered out as part of the $form->get_data call because there form is originally + // constructed without any options for the groups. + // Consequently with no options it means there can’t be a valid value so it end up ignoring it. + require_once($CFG->libdir . '/grouplib.php'); + $groupcoursedata = groups_get_course_data($courseid); + $groups = []; + if (!empty($groupcoursedata->groups)) { + foreach ($groupcoursedata->groups as $groupid => $groupdata) { + $groups[$groupid] = $groupdata->name; + } + } + $simulatedformdata = [ + 'groups' => $groups, + 'courseid' => $courseid, + ]; + // As commented above, we need to reconstruct the list of valid group options and recreate the form with + // those options for the form validation to recognise that the groupid value in the POST data is valid. + $form = new \core_calendar\local\event\forms\managesubscriptions(null, $simulatedformdata); + } else { + $subscriptionid = calendar_add_subscription($formdata); + } + if ($formdata->importfrom == CALENDAR_IMPORT_FROM_FILE) { // Blank the URL if it's a file import. $formdata->url = ''; From c6dd258bfd89ac1c23db1b97649d994cf4a74c0f Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Thu, 29 Nov 2018 10:31:05 +0800 Subject: [PATCH 2/3] MDL-64214 calendar: fix failing unit tests --- calendar/tests/events_test.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/calendar/tests/events_test.php b/calendar/tests/events_test.php index 5750d507f55..76f8374effc 100644 --- a/calendar/tests/events_test.php +++ b/calendar/tests/events_test.php @@ -533,7 +533,8 @@ class core_calendar_events_testcase extends advanced_testcase { $subscription = new stdClass(); $subscription->eventtype = 'group'; $subscription->name = 'test'; - $subscription->groupid = "{$courseid}-{$groupid}"; + $subscription->courseid = $courseid; + $subscription->groupid = $groupid; // Trigger and capture the event. $sink = $this->redirectEvents(); @@ -668,7 +669,9 @@ class core_calendar_events_testcase extends advanced_testcase { $subscription = new stdClass(); $subscription->eventtype = 'group'; $subscription->name = 'test'; - $subscription->groupid = "{$courseid}-{$groupid}"; + $subscription->courseid = $courseid; + $subscription->groupid = $groupid; + $subscription->id = calendar_add_subscription($subscription); // Now edit it. $subscription->name = 'awesome'; @@ -797,7 +800,8 @@ class core_calendar_events_testcase extends advanced_testcase { $subscription = new stdClass(); $subscription->eventtype = 'group'; $subscription->name = 'test'; - $subscription->groupid = "{$courseid}-{$groupid}"; + $subscription->groupid = $groupid; + $subscription->courseid = $courseid; $subscription->id = calendar_add_subscription($subscription); // Trigger and capture the event. From 673340157ab84e4d31c0528b739f30ee1ec2bb9e Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Thu, 29 Nov 2018 19:49:51 +0800 Subject: [PATCH 3/3] MDL-64214 calendar: Populate the groups select box on page load --- calendar/managesubscriptions.php | 46 +++++++++++++------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/calendar/managesubscriptions.php b/calendar/managesubscriptions.php index 9b5c301a2dd..93457043525 100644 --- a/calendar/managesubscriptions.php +++ b/calendar/managesubscriptions.php @@ -33,6 +33,7 @@ $categoryid = optional_param('category', null, PARAM_INT); // Used for processing subscription actions. $subscriptionid = optional_param('id', 0, PARAM_INT); $pollinterval = optional_param('pollinterval', 0, PARAM_INT); +$groupcourseid = optional_param('groupcourseid', 0, PARAM_INT); $action = optional_param('action', '', PARAM_INT); $url = new moodle_url('/calendar/managesubscriptions.php'); @@ -61,7 +62,22 @@ if (!calendar_user_can_add_event($course)) { print_error('errorcannotimport', 'calendar'); } -$form = new \core_calendar\local\event\forms\managesubscriptions(null, ['courseid' => $course->id]); +// Populate the 'group' select box based on the given 'groupcourseid', if necessary. +$groups = []; +if (!empty($groupcourseid)) { + require_once($CFG->libdir . '/grouplib.php'); + $groupcoursedata = groups_get_course_data($groupcourseid); + if (!empty($groupcoursedata->groups)) { + foreach ($groupcoursedata->groups as $groupid => $groupdata) { + $groups[$groupid] = $groupdata->name; + } + } +} +$customdata = [ + 'courseid' => $course->id, + 'groups' => $groups, +]; +$form = new \core_calendar\local\event\forms\managesubscriptions(null, $customdata); $form->set_data(array( 'course' => $course->id )); @@ -71,33 +87,7 @@ $importresults = ''; $formdata = $form->get_data(); if (!empty($formdata)) { require_sesskey(); // Must have sesskey for all actions. - // The course field on group event type is named groupcourseid. - // So it's easy to assume it is going to add a subscription as group events. - if (isset($formdata->groupcourseid)) { - $courseid = $formdata->groupcourseid; - // We need to fetch the groups again for group event type. - // The groupid gets filtered out as part of the $form->get_data call because there form is originally - // constructed without any options for the groups. - // Consequently with no options it means there can’t be a valid value so it end up ignoring it. - require_once($CFG->libdir . '/grouplib.php'); - $groupcoursedata = groups_get_course_data($courseid); - $groups = []; - if (!empty($groupcoursedata->groups)) { - foreach ($groupcoursedata->groups as $groupid => $groupdata) { - $groups[$groupid] = $groupdata->name; - } - } - $simulatedformdata = [ - 'groups' => $groups, - 'courseid' => $courseid, - ]; - // As commented above, we need to reconstruct the list of valid group options and recreate the form with - // those options for the form validation to recognise that the groupid value in the POST data is valid. - $form = new \core_calendar\local\event\forms\managesubscriptions(null, $simulatedformdata); - } else { - $subscriptionid = calendar_add_subscription($formdata); - } - + $subscriptionid = calendar_add_subscription($formdata); if ($formdata->importfrom == CALENDAR_IMPORT_FROM_FILE) { // Blank the URL if it's a file import. $formdata->url = '';