From 8afe9f8a1484b55e444a3704e3f56adb30315044 Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Tue, 12 Sep 2017 10:47:01 +0800 Subject: [PATCH] MDL-59750 core_calendar: better handling of event subscription --- calendar/amd/build/calendar.min.js | Bin 3648 -> 3308 bytes calendar/amd/src/calendar.js | 37 +------- calendar/classes/external/day_exporter.php | 2 + calendar/classes/external/event_exporter.php | 23 ----- .../classes/external/event_exporter_base.php | 8 ++ .../external/event_subscription_exporter.php | 85 ++++++++++++++++++ calendar/renderer.php | 5 +- .../templates/event_subscription.mustache | 48 ++++++++++ .../templates/event_summary_body.mustache | 4 +- lang/en/calendar.php | 3 +- 10 files changed, 150 insertions(+), 65 deletions(-) create mode 100644 calendar/classes/external/event_subscription_exporter.php create mode 100644 calendar/templates/event_subscription.mustache diff --git a/calendar/amd/build/calendar.min.js b/calendar/amd/build/calendar.min.js index 19997029788198d35d4c8b26bb4b01184e7045f5..8e4150affbb0506d9710897d19d2ee705df287de 100644 GIT binary patch delta 197 zcmX>g^G0%m8?#2XUV3VYYguYuiBqL#ibkTQUP(r3o<>?}UUErhex62>W_4zohGNp> zPs~w%iJI1BiA4&@wn=)aAT8EKsU@XFc?$U&$q<2(%7RpM6B0G6p~{dH*(T~FZ_Z+M zVq`4byq2wvY4RF2?#XtX^B9Xa-{o`xicVJIp2b))c`x^A#)8S6JloldfKIV4p6tf^ Ok+THItgWrpv<3iwh(MuPjy)72=pqafyl zs!$~Xr(J^klsTBI5Cc>6DAV1T5#a|+w?Q(~z-G+*`&tV(dYrmQ)r_KJXlh$CY(s7B z;)mfUqr$B)839u+IoFd%WvJQ?ABn7@ur&d*NG5c7!2{rSn%!9|G-)cij($Fc_* zzM|zp-b' + langStr + ''; - } - return langStr; - }); - }; - /** * Render the event summary modal. * @@ -109,31 +94,11 @@ define([ throw new Error('Error encountered while trying to fetch calendar event with ID: ' + eventId); } var eventData = getEventResponse.event; - var eventTypePromise = getEventType(eventData.eventtype); - // If the calendar event has event source, get the source's language string/link. - if (eventData.displayeventsource) { - eventData.subscription = JSON.parse(eventData.subscription); - var eventSourceParams = { - url: eventData.subscription.url, - name: eventData.subscription.name - }; - var eventSourcePromise = getEventSource(eventSourceParams); - - // Return event data with event type and event source info. - return $.when(eventTypePromise, eventSourcePromise).then(function(eventType, eventSource) { - eventData.eventtype = eventType; - eventData.source = eventSource; - return eventData; - }); - } - - // Return event data with event type info. - return eventTypePromise.then(function(eventType) { + return getEventType(eventData.eventtype).then(function(eventType) { eventData.eventtype = eventType; return eventData; }); - }).then(function(eventData) { // Build the modal parameters from the event data. var modalParams = { diff --git a/calendar/classes/external/day_exporter.php b/calendar/classes/external/day_exporter.php index 0f5b92b2bac..2e56f45a415 100644 --- a/calendar/classes/external/day_exporter.php +++ b/calendar/classes/external/day_exporter.php @@ -26,6 +26,8 @@ namespace core_calendar\external; defined('MOODLE_INTERNAL') || die(); +require_once($CFG->dirroot . '/calendar/lib.php'); + use core\external\exporter; use renderer_base; use moodle_url; diff --git a/calendar/classes/external/event_exporter.php b/calendar/classes/external/event_exporter.php index 917f07ebbf2..35ce4f9ff1a 100644 --- a/calendar/classes/external/event_exporter.php +++ b/calendar/classes/external/event_exporter.php @@ -51,13 +51,6 @@ class event_exporter extends event_exporter_base { $values = parent::define_other_properties(); - $values['displayeventsource'] = ['type' => PARAM_BOOL]; - $values['subscription'] = [ - 'type' => PARAM_RAW, - 'optional' => true, - 'default' => null, - 'null' => NULL_ALLOWED - ]; $values['isactionevent'] = ['type' => PARAM_BOOL]; $values['iscourseevent'] = ['type' => PARAM_BOOL]; $values['candelete'] = ['type' => PARAM_BOOL]; @@ -130,22 +123,6 @@ class event_exporter extends event_exporter_base { $values['course'] = $coursesummaryexporter->export($output); } - // Handle event subscription. - $values['subscription'] = null; - $values['displayeventsource'] = false; - if ($event->get_subscription()) { - $subscription = calendar_get_subscription($event->get_subscription()->get('id')); - if (!empty($subscription) && $CFG->calendar_showicalsource) { - $values['displayeventsource'] = true; - $subscriptiondata = new \stdClass(); - if (!empty($subscription->url)) { - $subscriptiondata->url = $subscription->url; - } - $subscriptiondata->name = $subscription->name; - $values['subscription'] = json_encode($subscriptiondata); - } - } - if ($group = $event->get_group()) { $values['groupname'] = format_string($group->get('name'), true, ['context' => \context_course::instance($event->get_course()->get('id'))]); diff --git a/calendar/classes/external/event_exporter_base.php b/calendar/classes/external/event_exporter_base.php index eeffda067c7..c3a87cf6b4f 100644 --- a/calendar/classes/external/event_exporter_base.php +++ b/calendar/classes/external/event_exporter_base.php @@ -35,6 +35,7 @@ use \core_calendar\local\event\entities\event_interface; use \core_calendar\local\event\entities\action_event_interface; use \core_course\external\course_summary_exporter; use \renderer_base; +use moodle_url; /** * Class for displaying a calendar event. @@ -178,6 +179,10 @@ class event_exporter_base extends exporter { 'type' => course_summary_exporter::read_properties_definition(), 'optional' => true, ], + 'subscription' => [ + 'type' => event_subscription_exporter::read_properties_definition(), + 'optional' => true, + ], 'canedit' => [ 'type' => PARAM_BOOL ], @@ -203,6 +208,9 @@ class event_exporter_base extends exporter { $values['icon'] = $iconexporter->export($output); + $subscriptionexporter = new event_subscription_exporter($event); + $values['subscription'] = $subscriptionexporter->export($output); + if ($course = $this->related['course']) { $coursesummaryexporter = new course_summary_exporter($course, ['context' => $context]); $values['course'] = $coursesummaryexporter->export($output); diff --git a/calendar/classes/external/event_subscription_exporter.php b/calendar/classes/external/event_subscription_exporter.php new file mode 100644 index 00000000000..5a8075bfc4f --- /dev/null +++ b/calendar/classes/external/event_subscription_exporter.php @@ -0,0 +1,85 @@ +. + +/** + * Contains event class for displaying a calendar event's subscription. + * + * @package core_calendar + * @copyright 2017 Simey Lameze + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core_calendar\external; + +defined('MOODLE_INTERNAL') || die(); + +use \core\external\exporter; +use \core_calendar\local\event\entities\event_interface; + +/** + * Class for displaying a calendar event's subscription. + * + * @package core_calendar + * @copyright 2017 Simey Lameze + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class event_subscription_exporter extends exporter { + + /** + * Constructor. + * + * @param event_interface $event + */ + public function __construct(event_interface $event) { + global $CFG; + + $data = new \stdClass(); + $data->displayeventsource = false; + if ($event->get_subscription()) { + $subscription = calendar_get_subscription($event->get_subscription()->get('id')); + if (!empty($subscription) && $CFG->calendar_showicalsource) { + $data->displayeventsource = true; + if (!empty($subscription->url)) { + $data->url = $subscription->url; + } + $data->name = $subscription->name; + } + } + + parent::__construct($data); + } + + /** + * Return the list of properties. + * + * @return array + */ + protected static function define_properties() { + return [ + 'displayeventsource' => [ + 'type' => PARAM_BOOL + ], + 'name' => [ + 'type' => PARAM_RAW, + 'optional' => true + ], + 'url' => [ + 'type' => PARAM_URL, + 'optional' => true + ], + ]; + } +} diff --git a/calendar/renderer.php b/calendar/renderer.php index c0ce327e0fb..8b93b91cb8a 100644 --- a/calendar/renderer.php +++ b/calendar/renderer.php @@ -269,10 +269,11 @@ class core_calendar_renderer extends plugin_renderer_base { // Show subscription source if needed. if (!empty($event->subscription) && $CFG->calendar_showicalsource) { if (!empty($event->subscription->url)) { - $source = html_writer::link($event->subscription->url, get_string('subsource', 'calendar', $event->subscription)); + $source = html_writer::link($event->subscription->url, + get_string('subscriptionsource', 'calendar', $event->subscription->name)); } else { // File based ical. - $source = get_string('subsource', 'calendar', $event->subscription); + $source = get_string('subscriptionsource', 'calendar', $event->subscription->name); } $output .= html_writer::tag('div', $source, array('class' => 'subscription')); } diff --git a/calendar/templates/event_subscription.mustache b/calendar/templates/event_subscription.mustache new file mode 100644 index 00000000000..e782fea819c --- /dev/null +++ b/calendar/templates/event_subscription.mustache @@ -0,0 +1,48 @@ +{{! + 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 . +}} +{{! + @template calendar/event_subscription + + Calendar event subscription. + + The purpose of this template is to render the event subscription data. + + Classes required for JS: + * none + + Data attributes required for JS: + * none + + Example context (json): + { + } +}} + +{{#subscription}} + {{#displayeventsource}} +
+ {{#name}} +

{{#str}}subscriptionsource, core_calendar, {{name}}{{/str}}

+ {{/name}} + {{#url}} +

{{#str}}subscriptionsource, core_calendar, {{name}}{{/str}}

+ {{/url}} +
+ {{/displayeventsource}} +{{/subscription}} + + diff --git a/calendar/templates/event_summary_body.mustache b/calendar/templates/event_summary_body.mustache index 70d207e4f04..b139b692dd3 100644 --- a/calendar/templates/event_summary_body.mustache +++ b/calendar/templates/event_summary_body.mustache @@ -49,9 +49,7 @@ {{#iscourseevent}} {{/iscourseevent}} - {{#source}} -
{{{source}}}
- {{/source}} + {{> core_calendar/event_subscription}} {{#groupname}}
{{{groupname}}}
diff --git a/lang/en/calendar.php b/lang/en/calendar.php index dffa73a3a15..a0bf7d0f4f5 100644 --- a/lang/en/calendar.php +++ b/lang/en/calendar.php @@ -209,8 +209,8 @@ $string['spanningevents'] = 'Events underway'; $string['subscriptions'] = 'Subscriptions'; $string['subscriptionname'] = 'Calendar name'; $string['subscriptionremoved'] = 'Calendar subscription {$a} removed'; +$string['subscriptionsource'] = 'Event source: {$a}'; $string['subscriptionupdated'] = 'Calendar subscription {$a} updated'; -$string['subsource'] = 'Event source: {$a->name}'; $string['sun'] = 'Sun'; $string['sunday'] = 'Sunday'; $string['thu'] = 'Thu'; @@ -268,3 +268,4 @@ $string['showcourseevents'] = 'Show course events'; $string['showglobalevents'] = 'Show global events'; $string['showgroupsevents'] = 'Show group events'; $string['showuserevents'] = 'Show user events'; +$string['subsource'] = 'Event source: {$a->name}'; -- 2.43.0