Skip to content

Commit

Permalink
MDL-59750 core_calendar: better handling of event subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
lameze committed Sep 20, 2017
1 parent e98f053 commit 8afe9f8
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 66 deletions.
2 changes: 1 addition & 1 deletion calendar/amd/build/calendar.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 1 addition & 36 deletions calendar/amd/src/calendar.js
Expand Up @@ -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.
*
Expand All @@ -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 = {
Expand Down
2 changes: 2 additions & 0 deletions calendar/classes/external/day_exporter.php
Expand Up @@ -26,6 +26,8 @@

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot . '/calendar/lib.php');

use core\external\exporter;
use renderer_base;
use moodle_url;
Expand Down
23 changes: 0 additions & 23 deletions calendar/classes/external/event_exporter.php
Expand Up @@ -51,13 +51,6 @@ protected static function define_other_properties() {

$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];
Expand Down Expand Up @@ -130,22 +123,6 @@ protected function get_other_values(renderer_base $output) {
$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'))]);
Expand Down
8 changes: 8 additions & 0 deletions calendar/classes/external/event_exporter_base.php
Expand Up @@ -35,6 +35,7 @@
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.
Expand Down Expand Up @@ -178,6 +179,10 @@ protected static function define_other_properties() {
'type' => course_summary_exporter::read_properties_definition(),
'optional' => true,
],
'subscription' => [
'type' => event_subscription_exporter::read_properties_definition(),
'optional' => true,
],
'canedit' => [
'type' => PARAM_BOOL
],
Expand All @@ -203,6 +208,9 @@ protected function get_other_values(renderer_base $output) {

$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);
Expand Down
85 changes: 85 additions & 0 deletions calendar/classes/external/event_subscription_exporter.php
@@ -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
],
];
}
}
5 changes: 3 additions & 2 deletions calendar/renderer.php
Expand Up @@ -269,10 +269,11 @@ public function event(calendar_event $event, $showactions=true) {
// 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'));
}
Expand Down
48 changes: 48 additions & 0 deletions 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 <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}}


4 changes: 1 addition & 3 deletions calendar/templates/event_summary_body.mustache
Expand Up @@ -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>
Expand Down
3 changes: 2 additions & 1 deletion lang/en/calendar.php
Expand Up @@ -209,8 +209,8 @@
$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';
Expand Down Expand Up @@ -268,3 +268,4 @@
$string['showglobalevents'] = 'Show global events';
$string['showgroupsevents'] = 'Show group events';
$string['showuserevents'] = 'Show user events';
$string['subsource'] = 'Event source: {$a->name}';

0 comments on commit 8afe9f8

Please sign in to comment.