Merge branch 'MDL-47153_master' of https://github.com/markn86/moodle

This commit is contained in:
Damyon Wiese
2014-10-23 15:47:10 +08:00
14 changed files with 1028 additions and 39 deletions
@@ -0,0 +1,77 @@
<?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 created 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 created 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_created extends \core\event\base {
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['objecttable'] = 'tool_monitor_rules';
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_TEACHING;
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventrulecreated', 'tool_monitor');
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' created 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/edit.php', array('ruleid' => $this->objectid,
'courseid' => $this->courseid));
}
}
@@ -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));
}
}
@@ -0,0 +1,77 @@
<?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 updated 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 updated 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_updated extends \core\event\base {
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['objecttable'] = 'tool_monitor_rules';
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_TEACHING;
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventruleupdated', 'tool_monitor');
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' updated 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/edit.php', array('ruleid' => $this->objectid,
'courseid' => $this->courseid));
}
}
@@ -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 <[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 subscription created 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 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'.";
}
}
@@ -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 <[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 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 <[email protected]>
* @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.');
}
}
}
@@ -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 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 subscription 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 subscription_deleted extends \core\event\base {
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['objecttable'] = 'tool_monitor_subscriptions';
$this->data['crud'] = 'd';
$this->data['edulevel'] = self::LEVEL_TEACHING;
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventsubdeleted', 'tool_monitor');
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' deleted the event monitor subscription with id '$this->objectid'.";
}
}
+29 -1
View File
@@ -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);
}
}
@@ -156,6 +160,30 @@ class eventobservers {
$count = $DB->count_records_sql($sql, $params);
if (!empty($count) && $count >= $subscription->frequency) {
$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)) {
+84 -4
View File
@@ -49,6 +49,26 @@ class rule_manager {
$ruledata->timemodified = $now;
$ruledata->id = $DB->insert_record('tool_monitor_rules', $ruledata);
// Trigger a rule created event.
if ($ruledata->id) {
if (!empty($ruledata->courseid)) {
$courseid = $ruledata->courseid;
$context = \context_course::instance($ruledata->courseid);
} else {
$courseid = 0;
$context = \context_system::instance();
}
$params = array(
'objectid' => $ruledata->id,
'courseid' => $courseid,
'context' => $context
);
$event = \tool_monitor\event\rule_created::create($params);
$event->trigger();
}
return new rule($ruledata);
}
@@ -85,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));
subscription_manager::remove_all_subscriptions_for_rule($ruleid, $coursecontext);
// 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;
}
/**
@@ -127,7 +180,34 @@ class rule_manager {
throw new \coding_exception('Invalid rule ID.');
}
$ruledata->timemodified = time();
return $DB->update_record('tool_monitor_rules', $ruledata);
$success = $DB->update_record('tool_monitor_rules', $ruledata);
// If successful trigger a rule updated event.
if ($success) {
// If we do not have the course id we need to retrieve it.
if (!isset($ruledata->courseid)) {
$courseid = $DB->get_field('tool_monitor_rules', 'courseid', array('id' => $ruledata->id), MUST_EXIST);
} else {
$courseid = $ruledata->courseid;
}
if (!empty($courseid)) {
$context = \context_course::instance($courseid);
} else {
$context = \context_system::instance();
}
$params = array(
'objectid' => $ruledata->id,
'courseid' => $courseid,
'context' => $context
);
$event = \tool_monitor\event\rule_updated::create($params);
$event->trigger();
}
return $success;
}
/**
@@ -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;
}
/**
@@ -81,7 +102,33 @@ class subscription_manager {
if ($checkuser && $subscription->userid != $USER->id) {
throw new \coding_exception('Invalid subscription supplied');
}
return $DB->delete_records('tool_monitor_subscriptions', array('id' => $subscription->id));
// Store the subscription before we delete it.
$subscription = $DB->get_record('tool_monitor_subscriptions', array('id' => $subscription->id));
$success = $DB->delete_records('tool_monitor_subscriptions', array('id' => $subscription->id));
// If successful trigger a subscription_deleted event.
if ($success) {
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_deleted::create($params);
$event->add_record_snapshot('tool_monitor_subscriptions', $subscription);
$event->trigger();
}
return $success;
}
/**
@@ -112,12 +159,49 @@ class subscription_manager {
* Delete all subscribers for a given rule.
*
* @param int $ruleid rule id.
* @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 remove_all_subscriptions_for_rule($ruleid) {
public static function remove_all_subscriptions_for_rule($ruleid, $coursecontext = null) {
global $DB;
return $DB->delete_records('tool_monitor_subscriptions', array('ruleid' => $ruleid));
// Store all the subscriptions we have to delete.
$subscriptions = $DB->get_recordset('tool_monitor_subscriptions', array('ruleid' => $ruleid));
// Now delete them.
$success = $DB->delete_records('tool_monitor_subscriptions', array('ruleid' => $ruleid));
// If successful and there were subscriptions that were deleted trigger a subscription deleted event.
if ($success && $subscriptions) {
foreach ($subscriptions as $subscription) {
// 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 = $subscription->courseid;
} else if (!empty($subscription->courseid) && ($coursecontext =
\context_course::instance($subscription->courseid, IGNORE_MISSING))) {
$courseid = $subscription->courseid;
$context = $coursecontext;
} else {
$courseid = 0;
$context = \context_system::instance();
}
$params = array(
'objectid' => $subscription->id,
'courseid' => $courseid,
'context' => $context
);
$event = \tool_monitor\event\subscription_deleted::create($params);
$event->add_record_snapshot('tool_monitor_subscriptions', $subscription);
$event->trigger();
}
}
return $success;
}
/**
@@ -39,6 +39,12 @@ $string['description'] = 'Description:';
$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['eventsubcreated'] = 'Subscription created';
$string['eventsubcriteriamet'] = 'Subscription criteria met';
$string['eventsubdeleted'] = 'Subscription deleted';
$string['errorincorrectevent'] = 'Please select an event related to the selected plugin';
$string['freqdesc'] = '{$a->freq} times in {$a->mins} minutes';
$string['frequency'] = 'Frequency';
@@ -46,16 +46,17 @@ class tool_monitor_eventobservers_testcase extends advanced_testcase {
$this->resetAfterTest(true);
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
$rule = new stdClass();
$rule->userid = $user->id;
$rule->courseid = $course->id;
$rule->courseid = $course1->id;
$rule->plugin = 'test';
$sub = new stdClass();
$sub->courseid = $course->id;
$sub->courseid = $course1->id;
$sub->userid = $user->id;
// Add 10 rules for this course with subscriptions.
@@ -65,9 +66,9 @@ class tool_monitor_eventobservers_testcase extends advanced_testcase {
$monitorgenerator->create_subscription($sub);
}
// Add 10 random rules for random courses.
// Add 10 random rules for course 2.
$rule->courseid = $course2->id;
for ($i = 0; $i < 10; $i++) {
$rule->courseid = rand(10000000, 50000000);
$createdrule = $monitorgenerator->create_rule($rule);
$sub->courseid = $rule->courseid;
$sub->ruleid = $createdrule->id;
@@ -77,24 +78,24 @@ class tool_monitor_eventobservers_testcase extends advanced_testcase {
// Verify data before course delete.
$totalrules = \tool_monitor\rule_manager::get_rules_by_plugin('test');
$this->assertCount(20, $totalrules);
$courserules = \tool_monitor\rule_manager::get_rules_by_courseid($course->id);
$courserules = \tool_monitor\rule_manager::get_rules_by_courseid($course1->id);
$this->assertCount(10, $courserules);
$totalsubs = $DB->get_records('tool_monitor_subscriptions');
$this->assertCount(20, $totalsubs);
$coursesubs = \tool_monitor\subscription_manager::get_user_subscriptions_for_course($course->id, 0, 0, $user->id);
$coursesubs = \tool_monitor\subscription_manager::get_user_subscriptions_for_course($course1->id, 0, 0, $user->id);
$this->assertCount(10, $coursesubs);
// Let us delete the course now.
delete_course($course->id, false);
delete_course($course1->id, false);
// Verify data after course delete.
$totalrules = \tool_monitor\rule_manager::get_rules_by_plugin('test');
$this->assertCount(10, $totalrules);
$courserules = \tool_monitor\rule_manager::get_rules_by_courseid($course->id);
$courserules = \tool_monitor\rule_manager::get_rules_by_courseid($course1->id);
$this->assertCount(0, $courserules); // Making sure all rules are deleted.
$totalsubs = $DB->get_records('tool_monitor_subscriptions');
$this->assertCount(10, $totalsubs);
$coursesubs = \tool_monitor\subscription_manager::get_user_subscriptions_for_course($course->id, 0, 0, $user->id);
$coursesubs = \tool_monitor\subscription_manager::get_user_subscriptions_for_course($course1->id, 0, 0, $user->id);
$this->assertCount(0, $coursesubs); // Making sure all subscriptions are deleted.
}
@@ -374,16 +375,17 @@ class tool_monitor_eventobservers_testcase extends advanced_testcase {
$this->resetAfterTest(true);
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
$rule = new stdClass();
$rule->userid = $user->id;
$rule->courseid = $course->id;
$rule->courseid = $course1->id;
$rule->plugin = 'test';
$sub = new stdClass();
$sub->courseid = $course->id;
$sub->courseid = $course1->id;
$sub->userid = $user->id;
// Add 10 rules for this course with subscriptions.
@@ -393,9 +395,9 @@ class tool_monitor_eventobservers_testcase extends advanced_testcase {
$monitorgenerator->create_subscription($sub);
}
// Add 10 random rules for random courses.
// Add 10 random rules for course 2.
$rule->courseid = $course2->id;
for ($i = 0; $i < 10; $i++) {
$rule->courseid = rand(10000000, 50000000);
$createdrule = $monitorgenerator->create_rule($rule);
$sub->courseid = $rule->courseid;
$sub->ruleid = $createdrule->id;
@@ -428,21 +430,22 @@ class tool_monitor_eventobservers_testcase extends advanced_testcase {
$this->resetAfterTest(true);
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
// Now let us create a rule specific to a module instance.
$cm = new stdClass();
$cm->course = $course->id;
$cm->course = $course1->id;
$book = $this->getDataGenerator()->create_module('book', $cm);
$rule = new stdClass();
$rule->userid = $user->id;
$rule->courseid = $course->id;
$rule->courseid = $course1->id;
$rule->plugin = 'test';
$sub = new stdClass();
$sub->courseid = $course->id;
$sub->courseid = $course1->id;
$sub->userid = $user->id;
$sub->cmid = $book->cmid;
@@ -453,9 +456,9 @@ class tool_monitor_eventobservers_testcase extends advanced_testcase {
$monitorgenerator->create_subscription($sub);
}
// Add 10 random rules for random courses.
// Add 10 random rules for course 2.
$rule->courseid = $course2->id;
for ($i = 0; $i < 10; $i++) {
$rule->courseid = rand(10000000, 50000000);
$createdrule = $monitorgenerator->create_rule($rule);
$sub->courseid = $rule->courseid;
$sub->ruleid = $createdrule->id;
+337
View File
@@ -0,0 +1,337 @@
<?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/>.
/**
* Events tests.
*
* @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();
/**
* Tests that the tool_monitor events are valid and triggered correctly.
*/
class tool_monitor_events_testcase extends advanced_testcase {
/**
* Tests set up.
*/
public function setUp() {
$this->resetAfterTest();
}
/**
* Test the rule created event.
*/
public function test_rule_created() {
// Create the items we need to create a rule.
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_user();
// Create the variables for the rule we want to create.
$ruledata = new stdClass();
$ruledata->userid = $user->id;
$ruledata->courseid = $course->id;
$ruledata->description = 'Rule description';
$ruledata->descriptionformat = FORMAT_HTML;
$ruledata->template = 'A message template';
$ruledata->templateformat = FORMAT_HTML;
$ruledata->frequency = 1;
$ruledata->timewindow = 60;
// Trigger and capture the event.
$sink = $this->redirectEvents();
$rule = \tool_monitor\rule_manager::add_rule($ruledata);
$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_created', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$this->assertEquals($rule->id, $event->objectid);
$this->assertEventContextNotUsed($event);
// Now let's add a system rule (courseid = 0).
$ruledata->courseid = 0;
// Trigger and capture the event.
$sink = $this->redirectEvents();
\tool_monitor\rule_manager::add_rule($ruledata);
$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_created', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
}
/**
* Test the rule updated event.
*/
public function test_rule_updated() {
// 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 update.
$createrule = new stdClass();
$createrule->courseid = $course->id;
$rule = $monitorgenerator->create_rule($createrule);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$updaterule = new stdClass();
$updaterule->id = $rule->id;
\tool_monitor\rule_manager::update_rule($updaterule);
$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_updated', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$this->assertEquals($rule->id, $event->objectid);
$this->assertEventContextNotUsed($event);
// Now let's update a system rule (courseid = 0).
$createrule->courseid = 0;
$rule = $monitorgenerator->create_rule($createrule);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$updaterule = new stdClass();
$updaterule->id = $rule->id;
\tool_monitor\rule_manager::update_rule($updaterule);
$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_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());
}
/**
* 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());
}
/**
* Test the subscription deleted event.
*/
public function test_subscription_deleted() {
// 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();
$sub = new stdClass();
$sub->courseid = $course->id;
$sub->userid = $user->id;
$sub->ruleid = $rule->id;
// Create the subscription we are going to delete.
$subscription = $monitorgenerator->create_subscription($sub);
// Trigger and capture the event.
$sink = $this->redirectEvents();
\tool_monitor\subscription_manager::delete_subscription($subscription->id, false);
$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_deleted', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$this->assertEquals($subscription->id, $event->objectid);
$this->assertEventContextNotUsed($event);
// Now let's delete a system subscription.
$sub = new stdClass();
$sub->courseid = 0;
$sub->userid = $user->id;
$sub->ruleid = $rule->id;
// Create the subscription we are going to delete.
$subscription = $monitorgenerator->create_subscription($sub);
// Trigger and capture the event.
$sink = $this->redirectEvents();
\tool_monitor\subscription_manager::delete_subscription($subscription->id, false);
$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_deleted', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
// Now, create a bunch of subscriptions for the rule we created.
$sub->courseid = $course->id;
for ($i = 1; $i <= 10; $i++) {
$sub->userid = $i;
$subscription = $monitorgenerator->create_subscription($sub);
if ($i == 1) {
$subscription1 = $subscription;
}
}
// Trigger and capture the events.
$sink = $this->redirectEvents();
\tool_monitor\subscription_manager::remove_all_subscriptions_for_rule($rule->id);
$events = $sink->get_events();
// Check that there were 10 events in total.
$this->assertCount(10, $events);
// Get the first event and ensure it is valid (we can assume the rest are the same).
$event = reset($events);
$this->assertInstanceOf('\tool_monitor\event\subscription_deleted', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$this->assertEquals($subscription1->id, $event->objectid);
$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);
}
}
@@ -108,11 +108,14 @@ class tool_monitor_rule_manager_testcase extends advanced_testcase {
$monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$record = new stdClass();
$record->courseid = 3;
$record->courseid = $course1->id;
$record2 = new stdClass();
$record2->courseid = 4;
$record2->courseid = $course2->id;
$ruleids = array();
for ($i = 0; $i < 10; $i++) {
@@ -122,7 +125,7 @@ class tool_monitor_rule_manager_testcase extends advanced_testcase {
$ruleids[] = $rule->id;
$rule = $monitorgenerator->create_rule($record2); // Create rules in a different course.
}
$ruledata = \tool_monitor\rule_manager::get_rules_by_courseid(3);
$ruledata = \tool_monitor\rule_manager::get_rules_by_courseid($course1->id);
$this->assertEquals($ruleids, array_keys($ruledata));
$this->assertCount(20, $ruledata);
}
@@ -114,13 +114,11 @@ class tool_monitor_task_clean_events_testcase extends advanced_testcase {
\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'));
// 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'));
// 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.
// 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.
$task = new \tool_monitor\task\clean_events();
$task->execute();