MDL-59750 core_calendar: better handling of event subscription
authorSimey Lameze <simey@moodle.com>
Tue, 12 Sep 2017 02:47:01 +0000 (10:47 +0800)
committerSimey Lameze <simey@moodle.com>
Wed, 20 Sep 2017 04:47:27 +0000 (12:47 +0800)
calendar/amd/build/calendar.min.js
calendar/amd/src/calendar.js
calendar/classes/external/day_exporter.php
calendar/classes/external/event_exporter.php
calendar/classes/external/event_exporter_base.php
calendar/classes/external/event_subscription_exporter.php [new file with mode: 0644]
calendar/renderer.php
calendar/templates/event_subscription.mustache [new file with mode: 0644]
calendar/templates/event_summary_body.mustache
lang/en/calendar.php

index 1999702..8e4150a 100644 (file)
Binary files a/calendar/amd/build/calendar.min.js and b/calendar/amd/build/calendar.min.js differ
index 2958c83..10af845 100644 (file)
@@ -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 '<a href="' + subscription.url + '">' + langStr + '</a>';
-            }
-            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 = {
index 0f5b92b..2e56f45 100644 (file)
@@ -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;
index 917f07e..35ce4f9 100644 (file)
@@ -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'))]);
index eeffda0..c3a87cf 100644 (file)
@@ -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 (file)
index 0000000..5a8075b
--- /dev/null
@@ -0,0 +1,85 @@
+<?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/>.
+
+/**
+ * Contains event class for displaying a calendar event's subscription.
+ *
+ * @package   core_calendar
+ * @copyright 2017 Simey Lameze <simey@moodle.com>
+ * @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 <simey@moodle.com>
+ * @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
+            ],
+        ];
+    }
+}
index c0ce327..8b93b91 100644 (file)
@@ -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 (file)
index 0000000..e782fea
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+}}
+{{!
+    @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}}
+        <div>
+            {{#name}}
+                <p>{{#str}}subscriptionsource, core_calendar, {{name}}{{/str}}</p>
+            {{/name}}
+            {{#url}}
+                <p><a href="{{url}}">{{#str}}subscriptionsource, core_calendar, {{name}}{{/str}}</a></p>
+            {{/url}}
+        </div>
+    {{/displayeventsource}}
+{{/subscription}}
+
+
index 70d207e..b139b69 100644 (file)
@@ -49,9 +49,7 @@
     {{#iscourseevent}}
         <div><a href="{{url}}">{{course.fullname}}</a></div>
     {{/iscourseevent}}
-    {{#source}}
-        <div>{{{source}}}</div>
-    {{/source}}
+    {{> core_calendar/event_subscription}}
     {{#groupname}}
         <div><a href="{{url}}">{{{course.fullname}}}</a></div>
         <div>{{{groupname}}}</div>
index dffa73a..a0bf7d0 100644 (file)
@@ -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}';