mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 20:42:22 +02:00
Merge branch 'MDL-47900_master' of https://github.com/markn86/moodle
This commit is contained in:
commit
d04803595c
@ -98,6 +98,11 @@ class eventobservers {
|
||||
*/
|
||||
protected function buffer_event(\core\event\base $event) {
|
||||
|
||||
// If there are no subscriptions for this event do not buffer it.
|
||||
if (!\tool_monitor\subscription_manager::event_has_subscriptions($event->eventname, $event->courseid)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$eventdata = $event->get_data();
|
||||
$eventobj = new \stdClass();
|
||||
$eventobj->eventname = $eventdata['eventname'];
|
||||
|
@ -78,6 +78,10 @@ class subscription_manager {
|
||||
);
|
||||
$event = \tool_monitor\event\subscription_created::create($params);
|
||||
$event->trigger();
|
||||
|
||||
// Let's invalidate the cache.
|
||||
$cache = \cache::make('tool_monitor', 'eventsubscriptions');
|
||||
$cache->delete($courseid);
|
||||
}
|
||||
|
||||
return $subscription->id;
|
||||
@ -126,6 +130,10 @@ class subscription_manager {
|
||||
$event = \tool_monitor\event\subscription_deleted::create($params);
|
||||
$event->add_record_snapshot('tool_monitor_subscriptions', $subscription);
|
||||
$event->trigger();
|
||||
|
||||
// Let's invalidate the cache.
|
||||
$cache = \cache::make('tool_monitor', 'eventsubscriptions');
|
||||
$cache->delete($courseid);
|
||||
}
|
||||
|
||||
return $success;
|
||||
@ -198,6 +206,10 @@ class subscription_manager {
|
||||
$event = \tool_monitor\event\subscription_deleted::create($params);
|
||||
$event->add_record_snapshot('tool_monitor_subscriptions', $subscription);
|
||||
$event->trigger();
|
||||
|
||||
// Let's invalidate the cache.
|
||||
$cache = \cache::make('tool_monitor', 'eventsubscriptions');
|
||||
$cache->delete($courseid);
|
||||
}
|
||||
}
|
||||
|
||||
@ -380,4 +392,68 @@ class subscription_manager {
|
||||
|
||||
return $DB->count_records_sql($sql, array('ruleid' => $ruleid));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if an event in a particular course has a subscription.
|
||||
*
|
||||
* @param string $eventname the name of the event
|
||||
* @param int $courseid the course id
|
||||
* @return bool returns true if the event has subscriptions in a given course, false otherwise.
|
||||
*/
|
||||
public static function event_has_subscriptions($eventname, $courseid) {
|
||||
global $DB;
|
||||
|
||||
// Check if we can return these from cache.
|
||||
$cache = \cache::make('tool_monitor', 'eventsubscriptions');
|
||||
|
||||
// The SQL we will be using to fill the cache if it is empty.
|
||||
$sql = "SELECT DISTINCT(r.eventname)
|
||||
FROM {tool_monitor_subscriptions} s
|
||||
INNER JOIN {tool_monitor_rules} r
|
||||
ON s.ruleid = r.id
|
||||
WHERE s.courseid = :courseid";
|
||||
|
||||
$sitesubscriptions = $cache->get(0);
|
||||
// If we do not have the site subscriptions in the cache then return them from the DB.
|
||||
if ($sitesubscriptions === false) {
|
||||
// Set the array for the cache.
|
||||
$sitesubscriptions = array();
|
||||
if ($subscriptions = $DB->get_records_sql($sql, array('courseid' => 0))) {
|
||||
foreach ($subscriptions as $subscription) {
|
||||
$sitesubscriptions[$subscription->eventname] = true;
|
||||
}
|
||||
}
|
||||
$cache->set(0, $sitesubscriptions);
|
||||
}
|
||||
|
||||
// Check if a subscription exists for this event site wide.
|
||||
if (isset($sitesubscriptions[$eventname])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// If the course id is for the site, and we reached here then there is no site wide subscription for this event.
|
||||
if (empty($courseid)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$coursesubscriptions = $cache->get($courseid);
|
||||
// If we do not have the course subscriptions in the cache then return them from the DB.
|
||||
if ($coursesubscriptions === false) {
|
||||
// Set the array for the cache.
|
||||
$coursesubscriptions = array();
|
||||
if ($subscriptions = $DB->get_records_sql($sql, array('courseid' => $courseid))) {
|
||||
foreach ($subscriptions as $subscription) {
|
||||
$coursesubscriptions[$subscription->eventname] = true;
|
||||
}
|
||||
}
|
||||
$cache->set($courseid, $coursesubscriptions);
|
||||
}
|
||||
|
||||
// Check if a subscription exists for this event in this course.
|
||||
if (isset($coursesubscriptions[$eventname])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
35
admin/tool/monitor/db/caches.php
Normal file
35
admin/tool/monitor/db/caches.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Event monitor cache definitions.
|
||||
*
|
||||
* @package tool_monitor
|
||||
* @copyright 2014 Mark Nelson <markn@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die;
|
||||
|
||||
$definitions = array(
|
||||
'eventsubscriptions' => array(
|
||||
'mode' => cache_store::MODE_APPLICATION,
|
||||
'simplekeys' => true,
|
||||
'simpledata' => true,
|
||||
'staticacceleration' => true,
|
||||
'staticaccelerationsize' => 10
|
||||
)
|
||||
);
|
@ -29,6 +29,7 @@ $string['allevents'] = 'All events';
|
||||
$string['allmodules'] = 'All instances';
|
||||
$string['area'] = 'Area';
|
||||
$string['areatomonitor'] = 'Area to monitor';
|
||||
$string['cachedef_rules'] = 'This stores the list of event subscriptions for individual courses';
|
||||
$string['contactadmin'] = 'Contact your administrator to enable it.';
|
||||
$string['core'] = 'Core';
|
||||
$string['currentsubscriptions'] = 'Your current subscriptions';
|
||||
@ -98,4 +99,3 @@ $string['subhelp_help'] = 'This subscription listens for when the event \'{$a->e
|
||||
$string['subscribeto'] = 'Subscribe to rule "{$a}"';
|
||||
$string['taskcleanevents'] = 'Removes any unnecessary event monitor events';
|
||||
$string['unsubscribe'] = 'Unsubscribe';
|
||||
|
||||
|
@ -114,20 +114,88 @@ class tool_monitor_eventobservers_testcase extends advanced_testcase {
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Create events and verify data is fine.
|
||||
// Create the necessary items for testing.
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$course2 = $this->getDataGenerator()->create_course();
|
||||
$monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
|
||||
|
||||
$initialevents = $DB->get_records('tool_monitor_events');
|
||||
$initalcount = count($initialevents);
|
||||
// Fire a bunch of events.
|
||||
// Trigger a bunch of other events.
|
||||
$eventparams = array(
|
||||
'context' => context_course::instance($course->id)
|
||||
);
|
||||
for ($i = 0; $i < 5; $i++) {
|
||||
\core\event\course_viewed::create($eventparams)->trigger();
|
||||
\mod_quiz\event\course_module_instance_list_viewed::create($eventparams)->trigger();
|
||||
\mod_scorm\event\course_module_instance_list_viewed::create($eventparams)->trigger();
|
||||
}
|
||||
|
||||
// Confirm that nothing was stored in the tool_monitor_events table
|
||||
// as we do not have any subscriptions associated for the above events.
|
||||
$this->assertEquals(0, $DB->count_records('tool_monitor_events'));
|
||||
|
||||
// Now, let's create a rule so an event can be stored.
|
||||
$rule = new stdClass();
|
||||
$rule->courseid = $course->id;
|
||||
$rule->plugin = 'mod_book';
|
||||
$rule->eventname = '\mod_book\event\course_module_instance_list_viewed';
|
||||
$rule = $monitorgenerator->create_rule($rule);
|
||||
|
||||
// Let's subscribe to this rule.
|
||||
$sub = new stdClass;
|
||||
$sub->courseid = $course->id;
|
||||
$sub->ruleid = $rule->id;
|
||||
$sub->userid = $user->id;
|
||||
$monitorgenerator->create_subscription($sub);
|
||||
|
||||
// Again, let's just fire more events to make sure they still aren't stored.
|
||||
for ($i = 0; $i < 5; $i++) {
|
||||
\core\event\course_viewed::create($eventparams)->trigger();
|
||||
\mod_quiz\event\course_module_instance_list_viewed::create($eventparams)->trigger();
|
||||
\mod_scorm\event\course_module_instance_list_viewed::create($eventparams)->trigger();
|
||||
}
|
||||
|
||||
// Fire the event we want to capture.
|
||||
$event = \mod_book\event\course_module_instance_list_viewed::create_from_course($course);
|
||||
$event->trigger();
|
||||
|
||||
// Check that the event data is valid.
|
||||
$events = $DB->get_records('tool_monitor_events');
|
||||
$count = count($events);
|
||||
$this->assertEquals($initalcount + 1, $count);
|
||||
$this->assertEquals(1, count($events));
|
||||
$monitorevent = array_pop($events);
|
||||
$this->assertEquals($event->eventname, $monitorevent->eventname);
|
||||
$this->assertEquals($event->contextid, $monitorevent->contextid);
|
||||
$this->assertEquals($event->contextlevel, $monitorevent->contextlevel);
|
||||
$this->assertEquals($event->get_url()->out(), $monitorevent->link);
|
||||
$this->assertEquals($event->courseid, $monitorevent->courseid);
|
||||
$this->assertEquals($event->timecreated, $monitorevent->timecreated);
|
||||
|
||||
// Match values.
|
||||
// Remove the stored events.
|
||||
$DB->delete_records('tool_monitor_events');
|
||||
|
||||
// Now, let's create a site wide rule.
|
||||
$rule = new stdClass();
|
||||
$rule->courseid = 0;
|
||||
$rule->plugin = 'mod_book';
|
||||
$rule->eventname = '\mod_book\event\course_module_instance_list_viewed';
|
||||
$rule = $monitorgenerator->create_rule($rule);
|
||||
|
||||
// Let's subscribe to this rule.
|
||||
$sub = new stdClass;
|
||||
$sub->courseid = 0;
|
||||
$sub->ruleid = $rule->id;
|
||||
$sub->userid = $user->id;
|
||||
$monitorgenerator->create_subscription($sub);
|
||||
|
||||
// Fire the event we want to capture - but in a different course.
|
||||
$event = \mod_book\event\course_module_instance_list_viewed::create_from_course($course2);
|
||||
$event->trigger();
|
||||
|
||||
// Check that the event data is valid.
|
||||
$events = $DB->get_records('tool_monitor_events');
|
||||
$this->assertEquals(1, count($events));
|
||||
$monitorevent = array_pop($events);
|
||||
$this->assertEquals($event->eventname, $monitorevent->eventname);
|
||||
$this->assertEquals($event->contextid, $monitorevent->contextid);
|
||||
$this->assertEquals($event->contextlevel, $monitorevent->contextlevel);
|
||||
|
@ -50,6 +50,8 @@ class tool_monitor_events_testcase extends advanced_testcase {
|
||||
$ruledata = new stdClass();
|
||||
$ruledata->userid = $user->id;
|
||||
$ruledata->courseid = $course->id;
|
||||
$ruledata->plugin = 'mod_assign';
|
||||
$ruledata->eventname = '\mod_assign\event\submission_viewed';
|
||||
$ruledata->description = 'Rule description';
|
||||
$ruledata->descriptionformat = FORMAT_HTML;
|
||||
$ruledata->template = 'A message template';
|
||||
|
@ -94,6 +94,31 @@ class tool_monitor_task_clean_events_testcase extends advanced_testcase {
|
||||
$rule->timewindow = 500;
|
||||
$rule6 = $monitorgenerator->create_rule($rule);
|
||||
|
||||
|
||||
// Let's subscribe to these rules.
|
||||
$sub = new stdClass;
|
||||
$sub->courseid = $course->id;
|
||||
$sub->ruleid = $rule1->id;
|
||||
$sub->userid = $user->id;
|
||||
$monitorgenerator->create_subscription($sub);
|
||||
|
||||
$sub->ruleid = $rule2->id;
|
||||
$monitorgenerator->create_subscription($sub);
|
||||
|
||||
$sub->ruleid = $rule3->id;
|
||||
$monitorgenerator->create_subscription($sub);
|
||||
|
||||
$sub->ruleid = $rule4->id;
|
||||
$monitorgenerator->create_subscription($sub);
|
||||
|
||||
$sub->ruleid = $rule5->id;
|
||||
$sub->courseid = $course2->id;
|
||||
$monitorgenerator->create_subscription($sub);
|
||||
|
||||
$sub->ruleid = $rule6->id;
|
||||
$sub->courseid = 0;
|
||||
$monitorgenerator->create_subscription($sub);
|
||||
|
||||
// Now let's populate the tool_monitor table with the events associated with those rules.
|
||||
\mod_book\event\course_module_viewed::create_from_book($book, $bookcontext)->trigger();
|
||||
\mod_book\event\course_module_instance_list_viewed::create_from_course($course)->trigger();
|
||||
@ -115,8 +140,8 @@ class tool_monitor_task_clean_events_testcase extends advanced_testcase {
|
||||
\mod_scorm\event\course_module_instance_list_viewed::create($eventparams)->trigger();
|
||||
}
|
||||
|
||||
// Check that the events exist - there will be additional events for creating courses, modules and rules.
|
||||
$this->assertEquals(26, $DB->count_records('tool_monitor_events'));
|
||||
// We do not store events that have no subscriptions - so there will be only 4 events.
|
||||
$this->assertEquals(4, $DB->count_records('tool_monitor_events'));
|
||||
|
||||
// Run the task and check that all the quiz, scorm and rule events are removed as well as the course_module_*
|
||||
// viewed events in the second course.
|
||||
|
@ -26,6 +26,6 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die;
|
||||
|
||||
$plugin->version = 2014111000; // The current plugin version (Date: YYYYMMDDXX).
|
||||
$plugin->requires = 2014110400; // Requires this Moodle version.
|
||||
$plugin->version = 2014111001; // The current plugin version (Date: YYYYMMDDXX).
|
||||
$plugin->requires = 2014110400; // Requires this Moodle version.
|
||||
$plugin->component = 'tool_monitor'; // Full name of the plugin (used for diagnostics).
|
||||
|
Loading…
x
Reference in New Issue
Block a user