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 | 2 +- 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, 151 insertions(+), 66 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 19997029788..8e4150affbb 100644 --- a/calendar/amd/build/calendar.min.js +++ b/calendar/amd/build/calendar.min.js @@ -1 +1 @@ -define(["jquery","core/ajax","core/str","core/templates","core/notification","core/custom_interaction_events","core/modal_events","core/modal_factory","core_calendar/modal_event_form","core_calendar/summary_modal","core_calendar/repository","core_calendar/events","core_calendar/view_manager"],function(a,b,c,d,e,f,g,h,i,j,k,l,m){var n={ROOT:"[data-region='calendar']",DAY:"[data-region='day']",EVENT_ITEM:"[data-region='event-item']",EVENT_LINK:"[data-action='view-event']",NEW_EVENT_BUTTON:"[data-action='new-event-button']",DAY_CONTENT:"[data-region='day-content']",LOADING_ICON:".loading-icon",VIEW_DAY_LINK:"[data-action='view-day-link']",CALENDAR_MONTH_WRAPPER:".calendarwrapper",COURSE_SELECTOR:'select[name="course"]'},o=function(a){var b="type"+a;return c.get_string(b,"core_calendar").then(function(a){return a})},p=function(a){return c.get_string("subsource","core_calendar",a).then(function(b){return a.url?''+b+"":b})},q=function(b){k.getEventById(b).then(function(c){if(!c.event)throw new Error("Error encountered while trying to fetch calendar event with ID: "+b);var d=c.event,e=o(d.eventtype);if(d.displayeventsource){d.subscription=JSON.parse(d.subscription);var f={url:d.subscription.url,name:d.subscription.name},g=p(f);return a.when(e,g).then(function(a,b){return d.eventtype=a,d.source=b,d})}return e.then(function(a){return d.eventtype=a,d})}).then(function(a){var b={title:a.name,type:j.TYPE,body:d.render("core_calendar/event_summary_body",a),templateContext:{canedit:a.canedit,candelete:a.candelete,isactionevent:a.isactionevent,url:a.url}};return h.create(b)}).done(function(a){a.getRoot().on(g.hidden,function(){a.destroy()}),a.show()}).fail(e.exception)},r=function(b,c,f,g){var h=null,i=g.attr("data-day-timestamp");f&&(h=f.attr("data-day-timestamp")),f&&h==i||d.render("core/loading",{}).then(function(a,b){g.find(n.DAY_CONTENT).addClass("hidden"),d.appendNodeContents(g,a,b),f&&(f.find(n.DAY_CONTENT).addClass("hidden"),d.appendNodeContents(f,a,b))}).then(function(){return k.updateEventStartDay(c,i)}).then(function(){a("body").trigger(l.eventMoved,[c,f,g])}).always(function(){var a=g.find(n.LOADING_ICON);if(g.find(n.DAY_CONTENT).removeClass("hidden"),d.replaceNode(a,"",""),f){var b=f.find(n.LOADING_ICON);f.find(n.DAY_CONTENT).removeClass("hidden"),d.replaceNode(b,"","")}}).fail(e.exception)},s=function(a){var b=a.find(n.NEW_EVENT_BUTTON),c=b.attr("data-context-id");return h.create({type:i.TYPE,large:!0,templateContext:{contextid:c}},[a,n.NEW_EVENT_BUTTON])},t=function(b,c){var d=a("body"),e=a(b).find(n.CALENDAR_MONTH_WRAPPER).data("courseid");d.on(l.created,function(){m.reloadCurrentMonth(b)}),d.on(l.deleted,function(){m.reloadCurrentMonth(b)}),d.on(l.updated,function(){m.reloadCurrentMonth(b)}),d.on(l.editActionEvent,function(a,b){window.location.assign(b)}),d.on(l.moveEvent,r),d.on(l.eventMoved,function(){m.reloadCurrentMonth(b)}),c.then(function(a){d.on(l.editEvent,function(b,c){a.setEventId(c),a.show()}),a.setCourseId(e)})},u=function(b){b.on("click",n.EVENT_ITEM,function(b){b.preventDefault(),b.stopPropagation();var c=a(b.target),d=null;d=c.is(n.EVENT_LINK)?c.attr("data-event-id"):c.find(n.EVENT_LINK).attr("data-event-id"),q(d)}),b.on("change",n.COURSE_SELECTOR,function(){var c=a(this),d=c.val();m.reloadCurrentMonth(b,d).then(function(){return b.find(n.COURSE_SELECTOR).val(d)}).fail(e.exception)});var c=s(b);t(b,c),b.on("click",n.DAY,function(b){var d=a(b.target);if(!d.is(n.VIEW_DAY_LINK)){var e=a(this).attr("data-new-event-timestamp");c.then(function(a){a.setStartTime(e),a.show()}),b.preventDefault()}})};return{init:function(b){b=a(b),m.init(b),u(b)}}}); \ No newline at end of file +define(["jquery","core/ajax","core/str","core/templates","core/notification","core/custom_interaction_events","core/modal_events","core/modal_factory","core_calendar/modal_event_form","core_calendar/summary_modal","core_calendar/repository","core_calendar/events","core_calendar/view_manager"],function(a,b,c,d,e,f,g,h,i,j,k,l,m){var n={ROOT:"[data-region='calendar']",DAY:"[data-region='day']",EVENT_ITEM:"[data-region='event-item']",EVENT_LINK:"[data-action='view-event']",NEW_EVENT_BUTTON:"[data-action='new-event-button']",DAY_CONTENT:"[data-region='day-content']",LOADING_ICON:".loading-icon",VIEW_DAY_LINK:"[data-action='view-day-link']",CALENDAR_MONTH_WRAPPER:".calendarwrapper",COURSE_SELECTOR:'select[name="course"]'},o=function(a){var b="type"+a;return c.get_string(b,"core_calendar").then(function(a){return a})},p=function(a){k.getEventById(a).then(function(b){if(!b.event)throw new Error("Error encountered while trying to fetch calendar event with ID: "+a);var c=b.event;return o(c.eventtype).then(function(a){return c.eventtype=a,c})}).then(function(a){var b={title:a.name,type:j.TYPE,body:d.render("core_calendar/event_summary_body",a),templateContext:{canedit:a.canedit,candelete:a.candelete,isactionevent:a.isactionevent,url:a.url}};return h.create(b)}).done(function(a){a.getRoot().on(g.hidden,function(){a.destroy()}),a.show()}).fail(e.exception)},q=function(b,c,f,g){var h=null,i=g.attr("data-day-timestamp");f&&(h=f.attr("data-day-timestamp")),f&&h==i||d.render("core/loading",{}).then(function(a,b){g.find(n.DAY_CONTENT).addClass("hidden"),d.appendNodeContents(g,a,b),f&&(f.find(n.DAY_CONTENT).addClass("hidden"),d.appendNodeContents(f,a,b))}).then(function(){return k.updateEventStartDay(c,i)}).then(function(){a("body").trigger(l.eventMoved,[c,f,g])}).always(function(){var a=g.find(n.LOADING_ICON);if(g.find(n.DAY_CONTENT).removeClass("hidden"),d.replaceNode(a,"",""),f){var b=f.find(n.LOADING_ICON);f.find(n.DAY_CONTENT).removeClass("hidden"),d.replaceNode(b,"","")}}).fail(e.exception)},r=function(a){var b=a.find(n.NEW_EVENT_BUTTON),c=b.attr("data-context-id");return h.create({type:i.TYPE,large:!0,templateContext:{contextid:c}},[a,n.NEW_EVENT_BUTTON])},s=function(b,c){var d=a("body"),e=a(b).find(n.CALENDAR_MONTH_WRAPPER).data("courseid");d.on(l.created,function(){m.reloadCurrentMonth(b)}),d.on(l.deleted,function(){m.reloadCurrentMonth(b)}),d.on(l.updated,function(){m.reloadCurrentMonth(b)}),d.on(l.editActionEvent,function(a,b){window.location.assign(b)}),d.on(l.moveEvent,q),d.on(l.eventMoved,function(){m.reloadCurrentMonth(b)}),c.then(function(a){d.on(l.editEvent,function(b,c){a.setEventId(c),a.show()}),a.setCourseId(e)})},t=function(b){b.on("click",n.EVENT_ITEM,function(b){b.preventDefault(),b.stopPropagation();var c=a(b.target),d=null;d=c.is(n.EVENT_LINK)?c.attr("data-event-id"):c.find(n.EVENT_LINK).attr("data-event-id"),p(d)}),b.on("change",n.COURSE_SELECTOR,function(){var c=a(this),d=c.val();m.reloadCurrentMonth(b,d).then(function(){return b.find(n.COURSE_SELECTOR).val(d)}).fail(e.exception)});var c=r(b);s(b,c),b.on("click",n.DAY,function(b){var d=a(b.target);if(!d.is(n.VIEW_DAY_LINK)){var e=a(this).attr("data-new-event-timestamp");c.then(function(a){a.setStartTime(e),a.show()}),b.preventDefault()}})};return{init:function(b){b=a(b),m.init(b),t(b)}}}); \ No newline at end of file diff --git a/calendar/amd/src/calendar.js b/calendar/amd/src/calendar.js index 2958c837534..10af84553ae 100644 --- a/calendar/amd/src/calendar.js +++ b/calendar/amd/src/calendar.js @@ -82,21 +82,6 @@ define([ }); }; - /** - * Get the event source. - * - * @param {Object} subscription The event subscription object. - * @return {promise} The lang string promise. - */ - var getEventSource = function(subscription) { - return Str.get_string('subsource', 'core_calendar', subscription).then(function(langStr) { - if (subscription.url) { - return '' + 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}}
{{course.fullname}}
{{/iscourseevent}} - {{#source}} -
{{{source}}}
- {{/source}} + {{> core_calendar/event_subscription}} {{#groupname}}
{{{course.fullname}}}
{{{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}';