Skip to content

Commit

Permalink
Merge branch 'MDL-44369_m32v3' of https://github.com/sbourget/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Aug 2, 2016
2 parents de741b1 + 756e3c5 commit babfc98
Show file tree
Hide file tree
Showing 8 changed files with 499 additions and 8 deletions.
54 changes: 49 additions & 5 deletions calendar/lib.php
Expand Up @@ -2956,6 +2956,14 @@ function calendar_add_subscription($sub) {
if (empty($sub->id)) {
$id = $DB->insert_record('event_subscriptions', $sub);
// we cannot cache the data here because $sub is not complete.
$sub->id = $id;
// Trigger event, calendar subscription added.
$eventparams = array('objectid' => $sub->id,
'context' => calendar_get_calendar_context($sub),
'other' => array('eventtype' => $sub->eventtype, 'courseid' => $sub->courseid)
);
$event = \core\event\calendar_subscription_created::create($eventparams);
$event->trigger();
return $id;
} else {
// Why are we doing an update here?
Expand Down Expand Up @@ -3112,13 +3120,21 @@ function calendar_process_subscription_row($subscriptionid, $pollinterval, $acti
function calendar_delete_subscription($subscription) {
global $DB;

if (is_object($subscription)) {
$subscription = $subscription->id;
if (!is_object($subscription)) {
$subscription = $DB->get_record('event_subscriptions', array('id' => $subscription), '*', MUST_EXIST);
}
// Delete subscription and related events.
$DB->delete_records('event', array('subscriptionid' => $subscription));
$DB->delete_records('event_subscriptions', array('id' => $subscription));
cache_helper::invalidate_by_definition('core', 'calendar_subscriptions', array(), array($subscription));
$DB->delete_records('event', array('subscriptionid' => $subscription->id));
$DB->delete_records('event_subscriptions', array('id' => $subscription->id));
cache_helper::invalidate_by_definition('core', 'calendar_subscriptions', array(), array($subscription->id));

// Trigger event, calendar subscription deleted.
$eventparams = array('objectid' => $subscription->id,
'context' => calendar_get_calendar_context($subscription),
'other' => array('courseid' => $subscription->courseid)
);
$event = \core\event\calendar_subscription_deleted::create($eventparams);
$event->trigger();
}
/**
* From a URL, fetch the calendar and return an iCalendar object.
Expand Down Expand Up @@ -3246,6 +3262,14 @@ function calendar_update_subscription($subscription) {
// Update cache.
$cache = cache::make('core', 'calendar_subscriptions');
$cache->set($subscription->id, $subscription);
// Trigger event, calendar subscription updated.
$eventparams = array('userid' => $subscription->userid,
'objectid' => $subscription->id,
'context' => calendar_get_calendar_context($subscription),
'other' => array('eventtype' => $subscription->eventtype, 'courseid' => $subscription->courseid)
);
$event = \core\event\calendar_subscription_updated::create($eventparams);
$event->trigger();
}

/**
Expand Down Expand Up @@ -3320,3 +3344,23 @@ function calendar_cron() {

return true;
}

/**
* Helper function to determine the context of a calendar subscription.
* Subscriptions can be created in two contexts COURSE, or USER.
*
* @param stdClass $subscription
* @return context instance
*/
function calendar_get_calendar_context($subscription) {

// Determine context based on calendar type.
if ($subscription->eventtype === 'site') {
$context = context_course::instance(SITEID);
} else if ($subscription->eventtype === 'group' || $subscription->eventtype === 'course') {
$context = context_course::instance($subscription->courseid);
} else {
$context = context_user::instance($subscription->userid);
}
return $context;
}
91 changes: 91 additions & 0 deletions calendar/tests/events_test.php
Expand Up @@ -386,4 +386,95 @@ public function test_calendar_event_deleted_validations() {
$this->assertContains('The \'timestart\' value must be set in other.', $e->getMessage());
}
}

/**
* Tests for calendar_subscription_added event.
*/
public function test_calendar_subscription_created() {
global $CFG;
require_once($CFG->dirroot . '/calendar/lib.php');
$this->resetAfterTest(true);

// Create a mock subscription.
$subscription = new stdClass();
$subscription->eventtype = 'site';
$subscription->name = 'test';
$subscription->courseid = $this->course->id;

// Trigger and capture the event.
$sink = $this->redirectEvents();
$id = calendar_add_subscription($subscription);

$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
$this->assertInstanceOf('\core\event\calendar_subscription_created', $event);
$this->assertEquals($id, $event->objectid);
$this->assertEquals($subscription->courseid, $event->other['courseid']);
$this->assertEquals($subscription->eventtype, $event->other['eventtype']);
$this->assertDebuggingNotCalled();
$sink->close();

}

/**
* Tests for calendar_subscription_updated event.
*/
public function test_calendar_subscription_updated() {
global $CFG;
require_once($CFG->dirroot . '/calendar/lib.php');
$this->resetAfterTest(true);

// Create a mock subscription.
$subscription = new stdClass();
$subscription->eventtype = 'site';
$subscription->name = 'test';
$subscription->courseid = $this->course->id;
$subscription->id = calendar_add_subscription($subscription);
// Now edit it.
$subscription->name = 'awesome';

// Trigger and capture the event.
$sink = $this->redirectEvents();
calendar_update_subscription($subscription);
$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
$this->assertInstanceOf('\core\event\calendar_subscription_updated', $event);
$this->assertEquals($subscription->id, $event->objectid);
$this->assertEquals($subscription->courseid, $event->other['courseid']);
$this->assertEquals($subscription->eventtype, $event->other['eventtype']);
$this->assertDebuggingNotCalled();
$sink->close();

}

/**
* Tests for calendar_subscription_deleted event.
*/
public function test_calendar_subscription_deleted() {
global $CFG;
require_once($CFG->dirroot . '/calendar/lib.php');
$this->resetAfterTest(true);

// Create a mock subscription.
$subscription = new stdClass();
$subscription->eventtype = 'site';
$subscription->name = 'test';
$subscription->courseid = $this->course->id;
$subscription->id = calendar_add_subscription($subscription);

// Trigger and capture the event.
$sink = $this->redirectEvents();
calendar_delete_subscription($subscription);
$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
$this->assertInstanceOf('\core\event\calendar_subscription_deleted', $event);
$this->assertEquals($subscription->id, $event->objectid);
$this->assertEquals($subscription->courseid, $event->other['courseid']);
$this->assertDebuggingNotCalled();
$sink->close();

}
}
4 changes: 2 additions & 2 deletions calendar/tests/ical_test.php
Expand Up @@ -57,14 +57,14 @@ public function test_calendar_update_subscription() {
$id = calendar_add_subscription($subscription);

$subscription = new stdClass();
$subscription->id = $id;
$subscription = calendar_get_subscription($id);
$subscription->name = 'awesome';
calendar_update_subscription($subscription);
$sub = calendar_get_subscription($id);
$this->assertEquals($subscription->name, $sub->name);

$subscription = new stdClass();
$subscription->id = $id;
$subscription = calendar_get_subscription($id);
$subscription->name = 'awesome2';
$subscription->pollinterval = 604800;
calendar_update_subscription($subscription);
Expand Down
3 changes: 3 additions & 0 deletions lang/en/calendar.php
Expand Up @@ -95,6 +95,9 @@
$string['eventcalendareventcreated'] = 'Calendar event created';
$string['eventcalendareventupdated'] = 'Calendar event updated';
$string['eventcalendareventdeleted'] = 'Calendar event deleted';
$string['eventsubscriptioncreated'] = 'Calendar subscription created';
$string['eventsubscriptionupdated'] = 'Calendar subscription updated';
$string['eventsubscriptiondeleted'] = 'Calendar subscription deleted';
$string['expired'] = 'Expired';
$string['explain_site_timeformat'] = 'You can choose to see times in either 12 or 24 hour format for the whole site. If you choose "default", then the format will be automatically chosen according to the language you use in the site. This setting can be overridden by user preferences.';
$string['export'] = 'Export';
Expand Down
119 changes: 119 additions & 0 deletions lib/classes/event/calendar_subscription_created.php
@@ -0,0 +1,119 @@
<?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/>.

/**
* calendar subscription added event.
*
* @package core
* @copyright 2016 Stephen Bourget
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core\event;
defined('MOODLE_INTERNAL') || die();

/**
* Event triggered after a calendar subscription is added.
*
* @property-read array $other {
* Extra information about the event.
*
* - string eventtype: the type of events (site, course, group, user).
* - int courseid: The ID of the course (SITEID, User(0) or actual course)
* }
*
* @package core
* @since Moodle 3.2
* @copyright 2016 Stephen Bourget
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class calendar_subscription_created extends base
{

/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'event_subscriptions';
}

/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventsubscriptioncreated', 'calendar');
}

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "User {$this->userid} has added a calendar
subscription with id {$this->objectid} of event type {$this->other['eventtype']}.";
}

/**
* Returns relevant URL.
*
* @return \moodle_url
*/
public function get_url() {
if (($this->other['courseid'] == SITEID) || ($this->other['courseid'] == 0)) {
return new \moodle_url('calendar/managesubscriptions.php');
} else {
return new \moodle_url('calendar/managesubscriptions.php', array('course' => $this->other['courseid']));
}
}

/**
* Custom validations.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
parent::validate_data();
if (!isset($this->context)) {
throw new \coding_exception('The \'context\' must be set.');
}
if (!isset($this->objectid)) {
throw new \coding_exception('The \'objectid\' must be set.');
}
if (!isset($this->other['eventtype'])) {
throw new \coding_exception('The \'eventtype\' value must be set in other.');
}
if (!isset($this->other['courseid'])) {
throw new \coding_exception('The \'courseid\' value must be set in other.');
}
}

/**
* Returns mappings for restore
*
* @return array
*/
public static function get_objectid_mapping() {
return array('db' => 'event_subscriptions', 'restore' => 'event_subscriptions');
}
}

0 comments on commit babfc98

Please sign in to comment.