Merge branch 'MDL-46133_master' of https://github.com/markn86/moodle
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Clean the tool_monitor_events table.
|
||||
*
|
||||
* @package tool_monitor
|
||||
* @copyright 2014 Mark Nelson <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace tool_monitor\task;
|
||||
|
||||
/**
|
||||
* Simple task to clean the tool_monitor_events table.
|
||||
*/
|
||||
class clean_events extends \core\task\scheduled_task {
|
||||
|
||||
/**
|
||||
* Get a descriptive name for this task.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_name() {
|
||||
return get_string('taskcleanevents', 'tool_monitor');
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the cleaning of events.
|
||||
*/
|
||||
public function execute() {
|
||||
global $DB;
|
||||
|
||||
// Array to store which events have been triggered in which course.
|
||||
$courses = array();
|
||||
|
||||
// Firstly, let's go through the site wide rules. There may be multiple rules for the site that rely on
|
||||
// the same event being triggered, so we only remove the events when they reach the max timewindow.
|
||||
if ($siterules = $DB->get_recordset('tool_monitor_rules', array('courseid' => 0), 'timewindow DESC')) {
|
||||
// Go through each rule and check if there are any events we can remove.
|
||||
foreach ($siterules as $rule) {
|
||||
// Check if we have already processed this event.
|
||||
if (isset($courses[$rule->courseid][$rule->eventname])) {
|
||||
continue;
|
||||
}
|
||||
// Store the timewindow for this event.
|
||||
$courses[$rule->courseid][$rule->eventname] = $rule->timewindow;
|
||||
// Delete any events that may exist that have exceeded the timewindow.
|
||||
$DB->delete_records_select('tool_monitor_events', 'eventname = :eventname AND
|
||||
courseid = :courseid AND timecreated <= :timewindow',
|
||||
array('eventname' => $rule->eventname, 'courseid' => $rule->courseid,
|
||||
'timewindow' => time() - $rule->timewindow));
|
||||
}
|
||||
// Free resources.
|
||||
$siterules->close();
|
||||
}
|
||||
|
||||
// Now, get the course rules. The same applies here - there may be multiple rules for the course that rely on
|
||||
// the same event being triggered, so we only remove the events when they reach the max timewindow.
|
||||
if ($rules = $DB->get_recordset_select('tool_monitor_rules', 'courseid != 0', array(), 'timewindow DESC')) {
|
||||
// Go through each rule and check if there are any events we can remove.
|
||||
foreach ($rules as $rule) {
|
||||
// Check if we have already processed this event for this particular course.
|
||||
if (isset($courses[$rule->courseid][$rule->eventname])) {
|
||||
continue;
|
||||
}
|
||||
// Add the course and event to the list.
|
||||
$courses[$rule->courseid][$rule->eventname] = $rule->timewindow;
|
||||
// If there is a site wide rule listening for this event do not remove it unless the maximum
|
||||
// timewindow between the two has exceeded.
|
||||
$timewindow = $rule->timewindow;
|
||||
if (isset($courses[0][$rule->eventname]) && ($courses[0][$rule->eventname] > $timewindow)) {
|
||||
$timewindow = $courses[0][$rule->eventname];
|
||||
}
|
||||
// Delete any events that may exist that have exceeded the timewindow.
|
||||
$DB->delete_records_select('tool_monitor_events', 'eventname = :eventname AND
|
||||
courseid = :courseid AND timecreated <= :timewindow',
|
||||
array('eventname' => $rule->eventname, 'courseid' => $rule->courseid,
|
||||
'timewindow' => time() - $timewindow));
|
||||
}
|
||||
// Free resources.
|
||||
$rules->close();
|
||||
}
|
||||
|
||||
if ($siterules || $rules) { // Check that there are rules present.
|
||||
// Get a list of all the events we have been through.
|
||||
$allevents = array();
|
||||
foreach ($courses as $key => $value) {
|
||||
foreach ($courses[$key] as $event => $notused) {
|
||||
$allevents[] = $event;
|
||||
}
|
||||
}
|
||||
// Remove all the events in the table that are not applicable to any rule. There may be a rule in one course
|
||||
// listening for a certain event, but not in another course, so we can delete the event from the course
|
||||
// where there is no rule. We also have to consider site wide rules. We may have an event that is triggered
|
||||
// in a course we can't remove because there is a site wide rule for this event.
|
||||
if ($events = $DB->get_recordset('tool_monitor_events')) {
|
||||
// Array to store which events we need to remove.
|
||||
$eventstodelete = array();
|
||||
// Store the current time.
|
||||
$now = time();
|
||||
foreach ($events as $event) {
|
||||
// If the event is not required for a course rule and there is no site wide rule for it, or
|
||||
// it has extended past or equal to the timewindow for the site rule - it can be deleted.
|
||||
if (!isset($courses[$event->courseid][$event->eventname]) && (!isset($courses[0][$event->eventname])
|
||||
|| $courses[0][$event->eventname] <= ($now - $event->timecreated))) {
|
||||
$eventstodelete[] = $event->id;
|
||||
}
|
||||
}
|
||||
// Free resources.
|
||||
$events->close();
|
||||
|
||||
// Remove the events.
|
||||
if (!empty($eventstodelete)) {
|
||||
list($eventidsql, $params) = $DB->get_in_or_equal($eventstodelete);
|
||||
$DB->delete_records_select('tool_monitor_events', "id $eventidsql", $params);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove all the events in the table that are not used by any rule.
|
||||
if (!empty($allevents)) {
|
||||
list($eventnamesql, $params) = $DB->get_in_or_equal($allevents, SQL_PARAMS_QM, 'param', false);
|
||||
$DB->delete_records_select('tool_monitor_events', "eventname $eventnamesql", $params);
|
||||
}
|
||||
} else { // No rules, just remove everything.
|
||||
$DB->delete_records('tool_monitor_events');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* This file defines tasks performed by the tool.
|
||||
*
|
||||
* @package tool_monitor
|
||||
* @copyright 2014 Mark Nelson <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
// List of tasks.
|
||||
$tasks = array(
|
||||
array(
|
||||
'classname' => 'tool_monitor\task\clean_events',
|
||||
'blocking' => 0,
|
||||
'minute' => '*',
|
||||
'hour' => '*',
|
||||
'day' => '*',
|
||||
'dayofweek' => '*',
|
||||
'month' => '*'
|
||||
)
|
||||
);
|
||||
@@ -84,6 +84,7 @@ $string['subcreatesuccess'] = "Subscription successfully created";
|
||||
$string['subdeletesuccess'] = "Subscription successfully removed";
|
||||
$string['subhelp'] = 'Subscription details';
|
||||
$string['subhelp_help'] = 'This subscription listens for when the event \'{$a->eventname}\' has been triggered in \'{$a->moduleinstance}\' {$a->frequency} time(s) in {$a->minutes} minute(s).';
|
||||
$string['taskcleanevents'] = 'Removes any unnecessary event monitor events';
|
||||
$string['title'] = '{$a->coursename} : {$a->reportname}';
|
||||
$string['monitor:managerules'] = 'Manage event monitor rules';
|
||||
$string['monitor:subscribe'] = 'Subscribe to event monitor rules';
|
||||
|
||||
@@ -0,0 +1,221 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Unit tests for the tool_monitor clean events task.
|
||||
*
|
||||
* @package tool_monitor
|
||||
* @category test
|
||||
* @copyright 2014 Mark Nelson <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
|
||||
/**
|
||||
* Class used to test the tool_monitor clean events task.
|
||||
*/
|
||||
class tool_monitor_task_clean_events_testcase extends advanced_testcase {
|
||||
|
||||
/**
|
||||
* Test set up.
|
||||
*/
|
||||
public function setUp() {
|
||||
$this->resetAfterTest(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the cleaning up of events.
|
||||
*/
|
||||
public function test_clean_events() {
|
||||
global $DB;
|
||||
|
||||
// Create the necessary items for testing.
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
|
||||
$book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
|
||||
$bookcontext = context_module::instance($book->cmid);
|
||||
$bookchapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
|
||||
$course2 = $this->getDataGenerator()->create_course();
|
||||
$book2 = $this->getDataGenerator()->create_module('book', array('course' => $course2->id));
|
||||
$book2context = context_module::instance($book2->cmid);
|
||||
$book2chapter = $bookgenerator->create_chapter(array('bookid' => $book2->id));
|
||||
$monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
|
||||
|
||||
// Let's set some data for the rules we need before we can generate them.
|
||||
$rule = new stdClass();
|
||||
$rule->userid = $user->id;
|
||||
$rule->courseid = $course->id;
|
||||
$rule->plugin = 'mod_book';
|
||||
$rule->eventname = '\mod_book\event\course_module_viewed';
|
||||
$rule->timewindow = 500;
|
||||
|
||||
// Let's add a few rules we want to monitor.
|
||||
$rule1 = $monitorgenerator->create_rule($rule);
|
||||
|
||||
$rule->eventname = '\mod_book\event\course_module_instance_list_viewed';
|
||||
$rule2 = $monitorgenerator->create_rule($rule);
|
||||
|
||||
// Add the same rules for the same course, but this time with a lower timewindow (used to test that we do not
|
||||
// remove an event for a course if there is still a rule where the maximum timewindow has not been reached).
|
||||
$rule->eventname = '\mod_book\event\course_module_viewed';
|
||||
$rule->timewindow = 200;
|
||||
$rule3 = $monitorgenerator->create_rule($rule);
|
||||
|
||||
$rule->eventname = '\mod_book\event\course_module_instance_list_viewed';
|
||||
$rule4 = $monitorgenerator->create_rule($rule);
|
||||
|
||||
// Add another rule in a different course.
|
||||
$rule->courseid = $course2->id;
|
||||
$rule->eventname = '\mod_book\event\chapter_viewed';
|
||||
$rule->timewindow = 200;
|
||||
$rule5 = $monitorgenerator->create_rule($rule);
|
||||
|
||||
// Add a site wide rule.
|
||||
$rule->courseid = 0;
|
||||
$rule->eventname = '\mod_book\event\chapter_viewed';
|
||||
$rule->timewindow = 500;
|
||||
$rule6 = $monitorgenerator->create_rule($rule);
|
||||
|
||||
// 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();
|
||||
\mod_book\event\chapter_viewed::create_from_chapter($book, $bookcontext, $bookchapter)->trigger();
|
||||
|
||||
// Let's trigger the viewed events again, but in another course. The rules created for these events are
|
||||
// associated with another course, so these events should get deleted when we trigger the cleanup task.
|
||||
\mod_book\event\course_module_viewed::create_from_book($book2, $book2context)->trigger();
|
||||
\mod_book\event\course_module_instance_list_viewed::create_from_course($course2)->trigger();
|
||||
// Trigger a chapter_viewed event in this course - this should not get deleted as the rule is site wide.
|
||||
\mod_book\event\chapter_viewed::create_from_chapter($book2, $book2context, $book2chapter)->trigger();
|
||||
|
||||
// Trigger a bunch of other events.
|
||||
$eventparams = array(
|
||||
'context' => context_course::instance($course->id)
|
||||
);
|
||||
for ($i = 0; $i < 5; $i++) {
|
||||
\mod_quiz\event\course_module_instance_list_viewed::create($eventparams)->trigger();
|
||||
\mod_scorm\event\course_module_instance_list_viewed::create($eventparams)->trigger();
|
||||
}
|
||||
|
||||
// Check that there are a bunch of events now. There will be additional events for creating courses and modules.
|
||||
$this->assertEquals(20, $DB->count_records('tool_monitor_events'));
|
||||
|
||||
// Run the task and check that all the mod_quiz/mod_scorm events are removed as well as the course_module_*
|
||||
// viewed events in the second course. The chapter_viewed event in the second course should remain though as
|
||||
// there is a rule associated with that event in that course. The chapter_viewed event in the first course
|
||||
// should also remain as there is a site wide rule.
|
||||
$task = new \tool_monitor\task\clean_events();
|
||||
$task->execute();
|
||||
|
||||
$events = $DB->get_records('tool_monitor_events');
|
||||
$this->assertEquals(4, count($events));
|
||||
$event1 = array_shift($events);
|
||||
$event2 = array_shift($events);
|
||||
$event3 = array_shift($events);
|
||||
$event4 = array_shift($events);
|
||||
$this->assertEquals('\mod_book\event\course_module_viewed', $event1->eventname);
|
||||
$this->assertEquals($course->id, $event1->courseid);
|
||||
$this->assertEquals('\mod_book\event\course_module_instance_list_viewed', $event2->eventname);
|
||||
$this->assertEquals($course->id, $event2->courseid);
|
||||
$this->assertEquals('\mod_book\event\chapter_viewed', $event3->eventname);
|
||||
$this->assertEquals($course->id, $event3->courseid);
|
||||
$this->assertEquals('\mod_book\event\chapter_viewed', $event4->eventname);
|
||||
$this->assertEquals($course2->id, $event4->courseid);
|
||||
|
||||
// Update the timewindow for two of the rules.
|
||||
$updaterule = new stdClass();
|
||||
$updaterule->id = $rule1->id;
|
||||
$updaterule->timewindow = 0;
|
||||
\tool_monitor\rule_manager::update_rule($updaterule);
|
||||
$updaterule->id = $rule2->id;
|
||||
\tool_monitor\rule_manager::update_rule($updaterule);
|
||||
|
||||
// Run the task and check that the events remain as we still have not reached the maximum timewindow.
|
||||
$task = new \tool_monitor\task\clean_events();
|
||||
$task->execute();
|
||||
|
||||
$this->assertEquals(4, $DB->count_records('tool_monitor_events'));
|
||||
|
||||
// Now, remove the rules associated with course_module_* events so they get deleted.
|
||||
\tool_monitor\rule_manager::delete_rule($rule1->id);
|
||||
\tool_monitor\rule_manager::delete_rule($rule2->id);
|
||||
\tool_monitor\rule_manager::delete_rule($rule3->id);
|
||||
\tool_monitor\rule_manager::delete_rule($rule4->id);
|
||||
|
||||
// Run the task and check all the course_module_* events are gone.
|
||||
$task = new \tool_monitor\task\clean_events();
|
||||
$task->execute();
|
||||
|
||||
// We now should only have the chapter_viewed events.
|
||||
$events = $DB->get_records('tool_monitor_events');
|
||||
$this->assertEquals(2, count($events));
|
||||
$event1 = array_shift($events);
|
||||
$event2 = array_shift($events);
|
||||
$this->assertEquals('\mod_book\event\chapter_viewed', $event1->eventname);
|
||||
$this->assertEquals($course->id, $event1->courseid);
|
||||
$this->assertEquals('\mod_book\event\chapter_viewed', $event2->eventname);
|
||||
$this->assertEquals($course2->id, $event2->courseid);
|
||||
|
||||
// Set the timewindow of the rule for the event chapter_viewed in the second course to 0.
|
||||
$updaterule->id = $rule5->id;
|
||||
\tool_monitor\rule_manager::update_rule($updaterule);
|
||||
|
||||
// Run the task.
|
||||
$task = new \tool_monitor\task\clean_events();
|
||||
$task->execute();
|
||||
|
||||
// Check that nothing was deleted as we still have a site wide rule for the chapter_viewed event.
|
||||
$this->assertEquals(2, $DB->count_records('tool_monitor_events'));
|
||||
|
||||
// Set the timewindow back to 500.
|
||||
$updaterule->id = $rule5->id;
|
||||
$updaterule->timewindow = 500;
|
||||
\tool_monitor\rule_manager::update_rule($updaterule);
|
||||
|
||||
// Set the site rule timewindow to 0.
|
||||
$updaterule->id = $rule6->id;
|
||||
$updaterule->timewindow = 0;
|
||||
\tool_monitor\rule_manager::update_rule($updaterule);
|
||||
|
||||
// Run the task.
|
||||
$task = new \tool_monitor\task\clean_events();
|
||||
$task->execute();
|
||||
|
||||
// We now should only have one chapter_viewed event for the second course.
|
||||
$events = $DB->get_records('tool_monitor_events');
|
||||
$this->assertEquals(1, count($events));
|
||||
$event1 = array_shift($events);
|
||||
$this->assertEquals('\mod_book\event\chapter_viewed', $event1->eventname);
|
||||
$this->assertEquals($course2->id, $event1->courseid);
|
||||
|
||||
// Remove the site rule.
|
||||
\tool_monitor\rule_manager::delete_rule($rule6->id);
|
||||
|
||||
// Remove the last remaining rule.
|
||||
\tool_monitor\rule_manager::delete_rule($rule5->id);
|
||||
|
||||
// Run the task.
|
||||
$task = new \tool_monitor\task\clean_events();
|
||||
$task->execute();
|
||||
|
||||
// There should be no events left.
|
||||
$this->assertEquals(0, $DB->count_records('tool_monitor_events'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user