MDL-47153 tool_monitor: added rule_deleted event

This commit is contained in:
Mark Nelson
2014-10-21 15:48:09 -07:00
parent 12b9969ef9
commit 91df5f7e37
5 changed files with 160 additions and 3 deletions
@@ -0,0 +1,76 @@
<?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 rule deleted event.
*
* @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\event;
defined('MOODLE_INTERNAL') || die();
/**
* The tool_monitor rule deleted event class.
*
* @package tool_monitor
* @since Moodle 2.8
* @copyright 2014 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class rule_deleted extends \core\event\base {
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['objecttable'] = 'tool_monitor_rules';
$this->data['crud'] = 'd';
$this->data['edulevel'] = self::LEVEL_TEACHING;
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventruledeleted', 'tool_monitor');
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' deleted the event monitor rule with id '$this->objectid'.";
}
/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/admin/tool/monitor/managerules.php', array('courseid' => $this->courseid));
}
}
@@ -54,8 +54,12 @@ class eventobservers {
*/
public static function course_deleted(\core\event\course_deleted $event) {
$rules = rule_manager::get_rules_by_courseid($event->courseid);
$context = null;
if ($event->contextlevel == CONTEXT_COURSE) {
$context = $event->get_context();
}
foreach ($rules as $rule) {
rule_manager::delete_rule($rule->id);
rule_manager::delete_rule($rule->id, $context);
}
}
+35 -2
View File
@@ -105,14 +105,47 @@ class rule_manager {
* Delete a rule and associated subscriptions, by rule id.
*
* @param int $ruleid id of rule to be deleted.
* @param \context|null $coursecontext the context of the course - this is passed when we
* can not get the context via \context_course as the course has been deleted.
*
* @return bool
*/
public static function delete_rule($ruleid) {
public static function delete_rule($ruleid, $coursecontext = null) {
global $DB;
subscription_manager::remove_all_subscriptions_for_rule($ruleid);
return $DB->delete_records('tool_monitor_rules', array('id' => $ruleid));
// Retrieve the rule from the DB before we delete it, so we have a record when we trigger a rule deleted event.
$rule = $DB->get_record('tool_monitor_rules', array('id' => $ruleid));
$success = $DB->delete_records('tool_monitor_rules', array('id' => $ruleid));
// If successful trigger a rule deleted event.
if ($success) {
// It is possible that we are deleting rules associated with a deleted course, so we should be
// passing the context as the second parameter.
if (!is_null($coursecontext)) {
$context = $coursecontext;
$courseid = $rule->courseid;
} else if (!empty($rule->courseid) && ($context = \context_course::instance($rule->courseid,
IGNORE_MISSING))) {
$courseid = $rule->courseid;
} else {
$courseid = 0;
$context = \context_system::instance();
}
$params = array(
'objectid' => $rule->id,
'courseid' => $courseid,
'context' => $context
);
$event = \tool_monitor\event\rule_deleted::create($params);
$event->add_record_snapshot('tool_monitor_rules', $rule);
$event->trigger();
}
return $success;
}
/**
@@ -40,6 +40,7 @@ $string['duplicaterule'] = 'Duplicate rule';
$string['editrule'] = 'Edit rule';
$string['eventnotfound'] = 'Event not found';
$string['eventrulecreated'] = 'Rule created';
$string['eventruledeleted'] = 'Rule deleted';
$string['eventruleupdated'] = 'Rule updated';
$string['errorincorrectevent'] = 'Please select an event related to the selected plugin';
$string['freqdesc'] = '{$a->freq} times in {$a->mins} minutes';
+43
View File
@@ -129,4 +129,47 @@ class tool_monitor_events_testcase extends advanced_testcase {
$this->assertInstanceOf('\tool_monitor\event\rule_updated', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
}
/**
* Test the rule deleted event.
*/
public function test_rule_deleted() {
// Create the items we need.
$monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
$course = $this->getDataGenerator()->create_course();
// Create the rule we are going to delete.
$createrule = new stdClass();
$createrule->courseid = $course->id;
$rule = $monitorgenerator->create_rule($createrule);
// Trigger and capture the event.
$sink = $this->redirectEvents();
\tool_monitor\rule_manager::delete_rule($rule->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\rule_deleted', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$this->assertEquals($rule->id, $event->objectid);
$this->assertEventContextNotUsed($event);
// Now let's delete a system rule (courseid = 0).
$createrule = new stdClass();
$createrule->courseid = 0;
$rule = $monitorgenerator->create_rule($createrule);
// Trigger and capture the event.
$sink = $this->redirectEvents();
\tool_monitor\rule_manager::delete_rule($rule->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\rule_deleted', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
}
}