Skip to content

Commit

Permalink
MDL-47900 tool_monitor: created list of rules MUC
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Nov 7, 2014
1 parent ddb4a31 commit 475635f
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 3 deletions.
5 changes: 5 additions & 0 deletions admin/tool/monitor/classes/eventobservers.php
Expand Up @@ -98,6 +98,11 @@ public static function process_event(\core\event\base $event) {
*/
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'];
Expand Down
76 changes: 76 additions & 0 deletions admin/tool/monitor/classes/subscription_manager.php
Expand Up @@ -78,6 +78,10 @@ public static function create_subscription($ruleid, $courseid, $cmid, $userid =
);
$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;
Expand Down Expand Up @@ -126,6 +130,10 @@ public static function delete_subscription($subscriptionorid, $checkuser = true)
$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;
Expand Down Expand Up @@ -198,6 +206,10 @@ public static function remove_all_subscriptions_for_rule($ruleid, $coursecontext
$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);
}
}

Expand Down Expand Up @@ -380,4 +392,68 @@ public static function count_rule_subscriptions($ruleid) {

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 changes: 35 additions & 0 deletions admin/tool/monitor/db/caches.php
@@ -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
)
);
2 changes: 1 addition & 1 deletion admin/tool/monitor/lang/en/tool_monitor.php
Expand Up @@ -29,6 +29,7 @@
$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';
Expand Down Expand Up @@ -98,4 +99,3 @@
$string['subscribeto'] = 'Subscribe to rule "{$a}"';
$string['taskcleanevents'] = 'Removes any unnecessary event monitor events';
$string['unsubscribe'] = 'Unsubscribe';

4 changes: 2 additions & 2 deletions admin/tool/monitor/version.php
Expand Up @@ -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).

0 comments on commit 475635f

Please sign in to comment.