Merge branch 'MDL-59538-master' of git://github.com/junpataleta/moodle
authorAndrew Nicols <andrew@nicols.co.uk>
Mon, 24 Jul 2017 03:57:23 +0000 (11:57 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Mon, 24 Jul 2017 03:57:23 +0000 (11:57 +0800)
calendar/amd/build/calendar.min.js
calendar/amd/src/calendar.js
calendar/externallib.php
calendar/templates/event_summary_body.mustache
calendar/tests/behat/calendar_import.feature

index 67a1cb2..d54e1ca 100644 (file)
Binary files a/calendar/amd/build/calendar.min.js and b/calendar/amd/build/calendar.min.js differ
index 59dc033..5de6318 100644 (file)
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 define(['jquery', 'core/ajax', 'core/str', 'core/templates', 'core/notification', 'core/custom_interaction_events',
-        'core/modal_factory', 'core_calendar/summary_modal', 'core_calendar/calendar_repository'],
-    function($, Ajax, Str, Templates, Notification, CustomEvents, ModalFactory, SummaryModal, CalendarRepository) {
+        'core/modal_factory', 'core_calendar/summary_modal', 'core/modal_events', 'core_calendar/calendar_repository'],
+    function($, Ajax, Str, Templates, Notification, CustomEvents, ModalFactory, SummaryModal, ModalEvents, CalendarRepository) {
 
         var SELECTORS = {
             ROOT: "[data-region='calendar']",
             EVENT_LINK: "[data-action='view-event']",
         };
 
-        var modalPromise = null;
-
         /**
          * Get the event type lang string.
          *
          * @param {String} eventType The event type.
-         * @return {String} The lang string of the event type.
+         * @return {promise} The lang string promise.
          */
         var getEventType = function(eventType) {
             var lang = 'type' + eventType;
             return Str.get_string(lang, 'core_calendar').then(function(langStr) {
                 return langStr;
-            }).fail(Notification.exception);
+            });
+        };
+
+        /**
+         * 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 '<a href="' + subscription.url + '">' + langStr + '</a>';
+                }
+                return langStr;
+            });
         };
 
         /**
          * Render the event summary modal.
          *
          * @param {Number} eventId The calendar event id.
-         * @return {promise} The summary modal promise.
          */
         var renderEventSummaryModal = function(eventId) {
+            // Calendar repository promise.
+            CalendarRepository.getEventById(eventId).then(function(getEventResponse) {
+                if (!getEventResponse.event) {
+                    throw new Error('Error encountered while trying to fetch calendar event with ID: ' + eventId);
+                }
+                var eventData = getEventResponse.event;
+                var eventTypePromise = getEventType(eventData.eventtype);
 
-            var promise = CalendarRepository.getEventById(eventId);
+                // 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 promise.then(function(result) {
-                if (!result.event) {
-                    promise.fail(Notification.exception);
-                } else {
-                    return result.event;
+                    // 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;
+                    });
                 }
-            }).then(function(eventdata) {
-                return getEventType(eventdata.eventtype).then(function(langStr) {
-                    eventdata.eventtype = langStr;
-                    return eventdata;
+
+                // Return event data with event type info.
+                return eventTypePromise.then(function(eventType) {
+                    eventData.eventtype = eventType;
+                    return eventData;
                 });
-            }).then(function(eventdata) {
-                return modalPromise.done(function(modal) {
-                    modal.setTitle(eventdata.name);
-                    modal.setBody(Templates.render('core_calendar/event_summary_body', eventdata));
-                    // Hide edit and delete buttons if I don't have permission.
-                    if (eventdata.caneditevent == false) {
-                        modal.setFooter('');
-                    }
 
-                    modal.show();
+            }).then(function(eventData) {
+                // Build the modal parameters from the event data.
+                var modalParams = {
+                    title: eventData.name,
+                    type: SummaryModal.TYPE,
+                    body: Templates.render('core_calendar/event_summary_body', eventData)
+                };
+                if (!eventData.caneditevent) {
+                    modalParams.footer = '';
+                }
+                // Create the modal.
+                return ModalFactory.create(modalParams);
+
+            }).done(function(modal) {
+                // Handle hidden event.
+                modal.getRoot().on(ModalEvents.hidden, function() {
+                    // Destroy when hidden.
+                    modal.destroy();
                 });
-            });
+
+                // Finally, render the modal!
+                modal.show();
+
+            }).fail(Notification.exception);
         };
 
         /**
          * Register event listeners for the module.
-         *
-         * @param {object} root The root element.
          */
-        var registerEventListeners = function(root) {
-            root = $(root);
-
-            var loading = false;
-            root.on('click', SELECTORS.EVENT_LINK, function(e) {
-                if (!loading) {
-                    loading = true;
-                    e.preventDefault();
-
-                    var eventElement = $(e.target).closest(SELECTORS.EVENT_LINK);
-                    var eventId = eventElement.attr('data-event-id');
-
-                    renderEventSummaryModal(eventId).done(function() {
-                        loading = false;
-                    });
-                }
+        var registerEventListeners = function() {
+            // Bind click events to event links.
+            $(SELECTORS.EVENT_LINK).click(function(e) {
+                e.preventDefault();
+                var eventId = $(this).attr('data-event-id');
+                renderEventSummaryModal(eventId);
             });
         };
 
         return {
             init: function() {
-                modalPromise = ModalFactory.create({
-                    type: SummaryModal.TYPE
-                });
-
-                registerEventListeners(SELECTORS.ROOT);
+                registerEventListeners();
             }
         };
     });
index f684ae0..6fd68d4 100644 (file)
@@ -722,6 +722,21 @@ class core_calendar_external extends external_api {
         list($event[$eventid]->description, $event[$eventid]->format) = $eventobj->format_external_text();
         $event[$eventid]->caneditevent = calendar_edit_event_allowed($eventobj);
 
+        $event[$eventid]->subscription = null;
+        $event[$eventid]->displayeventsource = false;
+        if (!empty($event[$eventid]->subscriptionid)) {
+            $subscription = calendar_get_subscription($event[$eventid]->subscriptionid);
+            if (!empty($subscription) && $CFG->calendar_showicalsource) {
+                $event[$eventid]->displayeventsource = true;
+                $subscriptiondata = new stdClass();
+                if (!empty($subscription->url)) {
+                    $subscriptiondata->url = $subscription->url;
+                }
+                $subscriptiondata->name = $subscription->name;
+                $event[$eventid]->subscription = json_encode($subscriptiondata);
+            }
+        }
+
         return array('event' => $event[$eventid], 'warnings' => $warnings);
     }
 
@@ -753,7 +768,10 @@ class core_calendar_external extends external_api {
                     'sequence' => new external_value(PARAM_INT, 'sequence'),
                     'timemodified' => new external_value(PARAM_INT, 'time modified'),
                     'subscriptionid' => new external_value(PARAM_INT, 'Subscription id', VALUE_OPTIONAL, null, NULL_ALLOWED),
+                    'subscription' => new external_value(PARAM_RAW, 'Subscription object serialized', VALUE_OPTIONAL,
+                            null, NULL_ALLOWED),
                     'caneditevent' => new external_value(PARAM_BOOL, 'Whether the user can edit the event'),
+                    'displayeventsource' => new external_value(PARAM_BOOL, 'Whether the source should be displayed'),
                 ),
                 'event'
             ),
index 6258f08..d054f4f 100644 (file)
@@ -36,4 +36,7 @@
     {{/description}}
     <h4>{{#str}} eventtype, core_calendar {{/str}}</h4>
     {{eventtype}}
+    {{#source}}
+        <div>{{{source}}}</div>
+    {{/source}}
 </div>
index 68ab388..a9cedb1 100644 (file)
@@ -31,3 +31,4 @@ Feature: Import and edit calendar events
     And I should see "Event on 2-15-2017"
     And I should see "Event on 2-25-2017"
     And I follow "Event on 2-15-2017"
+    And I should see "Event source: Test Import"