Merge branch 'MDL-43230-master' of git://github.com/ryanwyllie/moodle
[moodle.git] / lib / eventslib.php
blob2474aa1313bf863931e327d95e9258eac27bb83e
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Library of functions for events manipulation.
20 * The public API is all at the end of this file.
22 * @package core
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Gets the capabilities that have been cached in the database for this
31 * component.
33 * @access protected To be used from eventslib only
35 * @param string $component examples: 'moodle', 'mod_forum', 'block_quiz_results'
36 * @return array of events
38 function events_get_cached($component) {
39 global $DB;
41 $cachedhandlers = array();
43 if ($storedhandlers = $DB->get_records('events_handlers', array('component'=>$component))) {
44 foreach ($storedhandlers as $handler) {
45 $cachedhandlers[$handler->eventname] = array (
46 'id' => $handler->id,
47 'handlerfile' => $handler->handlerfile,
48 'handlerfunction' => $handler->handlerfunction,
49 'schedule' => $handler->schedule,
50 'internal' => $handler->internal);
54 return $cachedhandlers;
57 /**
58 * Remove all event handlers and queued events
60 * @category event
61 * @param string $component examples: 'moodle', 'mod_forum', 'block_quiz_results'
63 function events_uninstall($component) {
64 $cachedhandlers = events_get_cached($component);
65 events_cleanup($component, $cachedhandlers);
67 events_get_handlers('reset');
70 /**
71 * Deletes cached events that are no longer needed by the component.
73 * @access protected To be used from eventslib only
75 * @param string $component examples: 'moodle', 'mod_forum', 'block_quiz_results'
76 * @param array $cachedhandlers array of the cached events definitions that will be
77 * @return int number of unused handlers that have been removed
79 function events_cleanup($component, $cachedhandlers) {
80 global $DB;
82 $deletecount = 0;
83 foreach ($cachedhandlers as $eventname => $cachedhandler) {
84 if ($qhandlers = $DB->get_records('events_queue_handlers', array('handlerid'=>$cachedhandler['id']))) {
85 //debugging("Removing pending events from queue before deleting of event handler: $component - $eventname");
86 foreach ($qhandlers as $qhandler) {
87 events_dequeue($qhandler);
90 $DB->delete_records('events_handlers', array('eventname'=>$eventname, 'component'=>$component));
91 $deletecount++;
94 return $deletecount;
97 /**
98 * Removes this queued handler from the events_queued_handler table
100 * Removes events_queue record from events_queue if no more references to this event object exists
102 * @access protected To be used from eventslib only
104 * @param stdClass $qhandler A row from the events_queued_handler table
106 function events_dequeue($qhandler) {
107 global $DB;
109 // first delete the queue handler
110 $DB->delete_records('events_queue_handlers', array('id'=>$qhandler->id));
112 // if no more queued handler is pointing to the same event - delete the event too
113 if (!$DB->record_exists('events_queue_handlers', array('queuedeventid'=>$qhandler->queuedeventid))) {
114 $DB->delete_records('events_queue', array('id'=>$qhandler->queuedeventid));
119 * Returns handlers for given event. Uses caching for better perf.
121 * @access protected To be used from eventslib only
123 * @staticvar array $handlers
124 * @param string $eventname name of event or 'reset'
125 * @return array|false array of handlers or false otherwise
127 function events_get_handlers($eventname) {
128 global $DB;
129 static $handlers = array();
131 if ($eventname === 'reset') {
132 $handlers = array();
133 return false;
136 if (!array_key_exists($eventname, $handlers)) {
137 $handlers[$eventname] = $DB->get_records('events_handlers', array('eventname'=>$eventname));
140 return $handlers[$eventname];