mirror of
https://github.com/moodle/moodle.git
synced 2025-03-14 20:50:21 +01:00
MDL-47153 tool_monitor: added subscription_criteria_met event
This commit is contained in:
parent
1adb0031c0
commit
11c2bf3f73
@ -0,0 +1,86 @@
|
|||||||
|
<?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 criteria met 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 criteria met event class.
|
||||||
|
*
|
||||||
|
* @property-read array $other {
|
||||||
|
* Extra information about event.
|
||||||
|
*
|
||||||
|
* - string subscriptionid: id of the subscription.
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* @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_criteria_met extends \core\event\base {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Init method.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function init() {
|
||||||
|
$this->data['crud'] = 'c';
|
||||||
|
$this->data['edulevel'] = self::LEVEL_TEACHING;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return localised event name.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function get_name() {
|
||||||
|
return get_string('eventsubcriteriamet', 'tool_monitor');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns description of what happened.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function get_description() {
|
||||||
|
return "The criteria for the subscription with id '{$this->other['subscriptionid']}' was met.";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom validation.
|
||||||
|
*
|
||||||
|
* @throws \coding_exception
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function validate_data() {
|
||||||
|
parent::validate_data();
|
||||||
|
|
||||||
|
if (!isset($this->other['subscriptionid'])) {
|
||||||
|
throw new \coding_exception('The \'subscriptionid\' value must be set in other.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -160,6 +160,30 @@ class eventobservers {
|
|||||||
$count = $DB->count_records_sql($sql, $params);
|
$count = $DB->count_records_sql($sql, $params);
|
||||||
if (!empty($count) && $count >= $subscription->frequency) {
|
if (!empty($count) && $count >= $subscription->frequency) {
|
||||||
$idstosend[] = $subscription->id;
|
$idstosend[] = $subscription->id;
|
||||||
|
|
||||||
|
// Trigger a subscription_criteria_met event.
|
||||||
|
// It's possible that the course has been deleted since the criteria was met, so in that case use
|
||||||
|
// the system context. Set it here and change later if needed.
|
||||||
|
$context = \context_system::instance();
|
||||||
|
// We can't perform if (!empty($subscription->courseid)) below as it uses the magic method
|
||||||
|
// __get to return the variable, which will always result in being empty.
|
||||||
|
$courseid = $subscription->courseid;
|
||||||
|
if (!empty($courseid)) {
|
||||||
|
if ($coursecontext = \context_course::instance($courseid, IGNORE_MISSING)) {
|
||||||
|
$context = $coursecontext;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$params = array(
|
||||||
|
'userid' => $subscription->userid,
|
||||||
|
'courseid' => $subscription->courseid,
|
||||||
|
'context' => $context,
|
||||||
|
'other' => array(
|
||||||
|
'subscriptionid' => $subscription->id
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$event = \tool_monitor\event\subscription_criteria_met::create($params);
|
||||||
|
$event->trigger();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!empty($idstosend)) {
|
if (!empty($idstosend)) {
|
||||||
|
@ -43,6 +43,7 @@ $string['eventrulecreated'] = 'Rule created';
|
|||||||
$string['eventruledeleted'] = 'Rule deleted';
|
$string['eventruledeleted'] = 'Rule deleted';
|
||||||
$string['eventruleupdated'] = 'Rule updated';
|
$string['eventruleupdated'] = 'Rule updated';
|
||||||
$string['eventsubcreated'] = 'Subscription created';
|
$string['eventsubcreated'] = 'Subscription created';
|
||||||
|
$string['eventsubcriteriamet'] = 'Subscription criteria met';
|
||||||
$string['eventsubdeleted'] = 'Subscription deleted';
|
$string['eventsubdeleted'] = 'Subscription deleted';
|
||||||
$string['errorincorrectevent'] = 'Please select an event related to the selected plugin';
|
$string['errorincorrectevent'] = 'Please select an event related to the selected plugin';
|
||||||
$string['freqdesc'] = '{$a->freq} times in {$a->mins} minutes';
|
$string['freqdesc'] = '{$a->freq} times in {$a->mins} minutes';
|
||||||
|
@ -288,4 +288,50 @@ class tool_monitor_events_testcase extends advanced_testcase {
|
|||||||
$this->assertEquals($subscription1->id, $event->objectid);
|
$this->assertEquals($subscription1->id, $event->objectid);
|
||||||
$this->assertEventContextNotUsed($event);
|
$this->assertEventContextNotUsed($event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the subscription criteria met event.
|
||||||
|
*/
|
||||||
|
public function test_subscription_criteria_met() {
|
||||||
|
// Create the items we need to test this.
|
||||||
|
$user = $this->getDataGenerator()->create_user();
|
||||||
|
$course = $this->getDataGenerator()->create_course();
|
||||||
|
$book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
|
||||||
|
$bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
|
||||||
|
$chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
|
||||||
|
$monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
|
||||||
|
|
||||||
|
// Create a rule we want to subscribe to.
|
||||||
|
$rule = new stdClass();
|
||||||
|
$rule->userid = $user->id;
|
||||||
|
$rule->courseid = $course->id;
|
||||||
|
$rule->plugin = 'mod_book';
|
||||||
|
$rule->eventname = '\mod_book\event\chapter_viewed';
|
||||||
|
$rule->frequency = 1;
|
||||||
|
$rule->timewindow = 60;
|
||||||
|
$rule = $monitorgenerator->create_rule($rule);
|
||||||
|
|
||||||
|
// Create the subscription.
|
||||||
|
$sub = new stdClass();
|
||||||
|
$sub->courseid = $course->id;
|
||||||
|
$sub->userid = $user->id;
|
||||||
|
$sub->ruleid = $rule->id;
|
||||||
|
$monitorgenerator->create_subscription($sub);
|
||||||
|
|
||||||
|
// Now create the \mod_book\event\chapter_viewed event we are listening for.
|
||||||
|
$context = context_module::instance($book->cmid);
|
||||||
|
$event = \mod_book\event\chapter_viewed::create_from_chapter($book, $context, $chapter);
|
||||||
|
|
||||||
|
// Trigger and capture the event.
|
||||||
|
$sink = $this->redirectEvents();
|
||||||
|
\tool_monitor\eventobservers::process_event($event);
|
||||||
|
$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_criteria_met', $event);
|
||||||
|
$this->assertEquals(context_course::instance($course->id), $event->get_context());
|
||||||
|
$this->assertEventContextNotUsed($event);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user