MDL-47153 tool_monitor: added subscription_created event

This commit is contained in:
Mark Nelson 2014-10-05 15:59:29 -07:00
parent 91df5f7e37
commit 229f841907
4 changed files with 127 additions and 1 deletions

View File

@ -0,0 +1,67 @@
<?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/>.
/**
* The tool_monitor subscription created event.
*
* @package tool_monitor
* @copyright 2014 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_monitor\event;
defined('MOODLE_INTERNAL') || die();
/**
* The tool_monitor subscription created event class.
*
* @package tool_monitor
* @since Moodle 2.8
* @copyright 2014 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class subscription_created extends \core\event\base {
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['objecttable'] = 'tool_monitor_subscriptions';
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_TEACHING;
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventsubcreated', 'tool_monitor');
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' created the event monitor subscription with id '$this->objectid'.";
}
}

View File

@ -59,7 +59,28 @@ class subscription_manager {
}
$subscription->timecreated = time();
return $DB->insert_record('tool_monitor_subscriptions', $subscription);
$subscription->id = $DB->insert_record('tool_monitor_subscriptions', $subscription);
// Trigger a subscription created event.
if ($subscription->id) {
if (!empty($subscription->courseid)) {
$courseid = $subscription->courseid;
$context = \context_course::instance($subscription->courseid);
} else {
$courseid = 0;
$context = \context_system::instance();
}
$params = array(
'objectid' => $subscription->id,
'courseid' => $courseid,
'context' => $context
);
$event = \tool_monitor\event\subscription_created::create($params);
$event->trigger();
}
return $subscription->id;
}
/**

View File

@ -42,6 +42,7 @@ $string['eventnotfound'] = 'Event not found';
$string['eventrulecreated'] = 'Rule created';
$string['eventruledeleted'] = 'Rule deleted';
$string['eventruleupdated'] = 'Rule updated';
$string['eventsubcreated'] = 'Subscription created';
$string['errorincorrectevent'] = 'Please select an event related to the selected plugin';
$string['freqdesc'] = '{$a->freq} times in {$a->mins} minutes';
$string['frequency'] = 'Frequency';

View File

@ -172,4 +172,41 @@ class tool_monitor_events_testcase extends advanced_testcase {
$this->assertInstanceOf('\tool_monitor\event\rule_deleted', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
}
/**
* Test the subscription created event.
*/
public function test_subscription_created() {
// Create the items we need to test this.
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
// Create a rule to subscribe to.
$rule = $monitorgenerator->create_rule();
// Trigger and capture the event.
$sink = $this->redirectEvents();
$subscriptionid = \tool_monitor\subscription_manager::create_subscription($rule->id, $course->id, 0, $user->id);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Confirm that the event contains the expected values.
$this->assertInstanceOf('\tool_monitor\event\subscription_created', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$this->assertEquals($subscriptionid, $event->objectid);
$this->assertEventContextNotUsed($event);
// Create a system subscription - trigger and capture the event.
$sink = $this->redirectEvents();
\tool_monitor\subscription_manager::create_subscription($rule->id, 0, 0, $user->id);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Confirm that the event uses the system context.
$this->assertInstanceOf('\tool_monitor\event\subscription_created', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
}
}