From 475635f52dbd9d42e25602131e2ad8bed6b5ecd0 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Tue, 28 Oct 2014 14:28:35 -0700 Subject: [PATCH] MDL-47900 tool_monitor: created list of rules MUC --- admin/tool/monitor/classes/eventobservers.php | 5 ++ .../monitor/classes/subscription_manager.php | 76 +++++++++++++++++++ admin/tool/monitor/db/caches.php | 35 +++++++++ admin/tool/monitor/lang/en/tool_monitor.php | 2 +- admin/tool/monitor/version.php | 4 +- 5 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 admin/tool/monitor/db/caches.php diff --git a/admin/tool/monitor/classes/eventobservers.php b/admin/tool/monitor/classes/eventobservers.php index f6c7939e906..bd5fc54fb7b 100644 --- a/admin/tool/monitor/classes/eventobservers.php +++ b/admin/tool/monitor/classes/eventobservers.php @@ -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']; diff --git a/admin/tool/monitor/classes/subscription_manager.php b/admin/tool/monitor/classes/subscription_manager.php index 1ecf5656177..342d36b2f57 100644 --- a/admin/tool/monitor/classes/subscription_manager.php +++ b/admin/tool/monitor/classes/subscription_manager.php @@ -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; + } } diff --git a/admin/tool/monitor/db/caches.php b/admin/tool/monitor/db/caches.php new file mode 100644 index 00000000000..323b5972646 --- /dev/null +++ b/admin/tool/monitor/db/caches.php @@ -0,0 +1,35 @@ +. + +/** + * Event monitor cache definitions. + * + * @package tool_monitor + * @copyright 2014 Mark Nelson + * @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 + ) +); diff --git a/admin/tool/monitor/lang/en/tool_monitor.php b/admin/tool/monitor/lang/en/tool_monitor.php index 474629e8918..04ac4982eb6 100644 --- a/admin/tool/monitor/lang/en/tool_monitor.php +++ b/admin/tool/monitor/lang/en/tool_monitor.php @@ -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'; - diff --git a/admin/tool/monitor/version.php b/admin/tool/monitor/version.php index 98589eb5bf3..42c28c16a14 100644 --- a/admin/tool/monitor/version.php +++ b/admin/tool/monitor/version.php @@ -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).