mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 22:08:20 +01:00
MDL-9843 get_cached_events does not handle components properly;
whitespace cleanup; minor coding style changes - such as always returning arrays from get cached and file events; marked some functions as eventslib internal only
This commit is contained in:
parent
4230372f19
commit
d77717d786
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
/**
|
||||
* Library of functions for events manipulation.
|
||||
*
|
||||
*
|
||||
* @author Martin Dougiamas and many others
|
||||
* @version $Id$
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
||||
@ -13,7 +13,9 @@
|
||||
* Loads the events definitions for the component (from file). If no
|
||||
* events are defined for the component, we simply return an empty array.
|
||||
* @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results'
|
||||
* @return array of capabilities
|
||||
* @return array of capabilities or empty array if not exists
|
||||
*
|
||||
* INTERNAL - to be used from eventslib only
|
||||
*/
|
||||
function events_load_def($component) {
|
||||
global $CFG;
|
||||
@ -27,21 +29,25 @@ function events_load_def($component) {
|
||||
if ($compparts[0] == 'block') {
|
||||
// Blocks are an exception. Blocks directory is 'blocks', and not
|
||||
// 'block'. So we need to jump through hoops.
|
||||
$defpath = $CFG->dirroot.'/'.$compparts[0].
|
||||
's/'.$compparts[1].'/db/events.php';
|
||||
$defpath = $CFG->dirroot.'/blocks/'.$compparts[1].'/db/events.php';
|
||||
|
||||
} else if ($compparts[0] == 'format') {
|
||||
// Similar to the above, course formats are 'format' while they
|
||||
// Similar to the above, course formats are 'format' while they
|
||||
// are stored in 'course/format'.
|
||||
$defpath = $CFG->dirroot.'/course/'.$component.'/db/events.php';
|
||||
$defpath = $CFG->dirroot.'/course/format/'.$compparts[1].'/db/events.php';
|
||||
|
||||
} else {
|
||||
$defpath = $CFG->dirroot.'/'.$component.'/db/events.php';
|
||||
}
|
||||
}
|
||||
|
||||
$events = array();
|
||||
|
||||
if (file_exists($defpath)) {
|
||||
require($defpath);
|
||||
return $events;
|
||||
}
|
||||
|
||||
return $events;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -49,29 +55,24 @@ function events_load_def($component) {
|
||||
* component.
|
||||
* @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results'
|
||||
* @return array of events
|
||||
*
|
||||
* INTERNAL - to be used from eventslib only
|
||||
*/
|
||||
function get_cached_events($component='moodle') {
|
||||
if ($component == 'moodle') {
|
||||
$storedevents = get_records_select('events_handlers',
|
||||
"handlermodule LIKE 'moodle/%'");
|
||||
} else {
|
||||
$storedevents = get_records_select('events_handlers',
|
||||
"handlermodule LIKE '$component%'");
|
||||
}
|
||||
|
||||
if (!empty($storedevents)) {
|
||||
foreach ($storedevents as $storedevent) {
|
||||
$eventname = $storedevent->eventname;
|
||||
// not needed for comparisons
|
||||
unset($storedevent->handlermodule);
|
||||
//unset($storedevent->id);
|
||||
unset($storedevent->eventname);
|
||||
$cachedevents[$eventname] = (array)$storedevent;
|
||||
}
|
||||
return $cachedevents;
|
||||
}
|
||||
}
|
||||
function get_cached_events($component) {
|
||||
$cachedevents = array();
|
||||
|
||||
if ($storedevents = get_records('events_handlers', 'handlermodule', $component)) {
|
||||
foreach ($storedevents as $event) {
|
||||
$cachedevents[$event->eventname] = array (
|
||||
'id' => $event->id,
|
||||
'handlerfile' => $event->handlerfile,
|
||||
'handlerfunction' => $event->handlerfunction,
|
||||
'schedule' => $event->schedule);
|
||||
}
|
||||
}
|
||||
|
||||
return $cachedevents;
|
||||
}
|
||||
|
||||
/**
|
||||
* We can not removed all event handlers in table, then add them again
|
||||
@ -91,58 +92,52 @@ function get_cached_events($component='moodle') {
|
||||
|
||||
function events_update_definition($component='moodle') {
|
||||
|
||||
$storedevents = array();
|
||||
|
||||
// load event definition from events.php
|
||||
$fileevents = events_load_def($component);
|
||||
|
||||
|
||||
// load event definitions from db tables
|
||||
// if we detect an event being already stored, we discard from this array later
|
||||
// the remaining needs to be removed
|
||||
|
||||
$cachedevents = get_cached_events($component);
|
||||
|
||||
if ($fileevents) {
|
||||
foreach ($fileevents as $eventname => $fileevent) {
|
||||
if (!empty($cachedevents[$eventname])) {
|
||||
// exact same event handler already present in db,
|
||||
// ignore this entry
|
||||
if ($cachedevents[$eventname]['handlerfile'] == $fileevent['handlerfile'] &&
|
||||
$cachedevents[$eventname]['handlerfunction'] == $fileevent['handlerfunction'] &&
|
||||
$cachedevents[$eventname]['schedule'] == $fileevent['schedule']) {
|
||||
|
||||
unset($cachedevents[$eventname]);
|
||||
continue;
|
||||
|
||||
} else {
|
||||
// same event name matches, this event has been updated,
|
||||
// update the datebase
|
||||
$event = new object;
|
||||
$event->id = $cachedevents[$eventname]['id'];
|
||||
$event->handlerfile = $fileevent['handlerfile'];
|
||||
$event->handlerfunction = $fileevent['handlerfunction'];
|
||||
$event->schedule = $fileevent['schedule'];
|
||||
|
||||
update_record('events_handlers', $event);
|
||||
|
||||
unset($cachedevents[$eventname]);
|
||||
continue;
|
||||
}
|
||||
|
||||
} else {
|
||||
// if we are here, this event handler is not present in db (new)
|
||||
// add it
|
||||
$event = new object;
|
||||
$event->eventname = $eventname;
|
||||
$event->handlermodule = $component;
|
||||
$event->handlerfile = $fileevent['handlerfile'];
|
||||
|
||||
foreach ($fileevents as $eventname => $fileevent) {
|
||||
if (!empty($cachedevents[$eventname])) {
|
||||
if ($cachedevents[$eventname]['handlerfile'] == $fileevent['handlerfile'] &&
|
||||
$cachedevents[$eventname]['handlerfunction'] == $fileevent['handlerfunction'] &&
|
||||
$cachedevents[$eventname]['schedule'] == $fileevent['schedule']) {
|
||||
// exact same event handler already present in db, ignore this entry
|
||||
|
||||
unset($cachedevents[$eventname]);
|
||||
continue;
|
||||
|
||||
} else {
|
||||
// same event name matches, this event has been updated, update the datebase
|
||||
$event = new object();
|
||||
$event->id = $cachedevents[$eventname]['id'];
|
||||
$event->handlerfile = $fileevent['handlerfile'];
|
||||
$event->handlerfunction = $fileevent['handlerfunction'];
|
||||
$event->schedule = $fileevent['schedule'];
|
||||
insert_record('events_handlers', $event);
|
||||
}
|
||||
$event->schedule = $fileevent['schedule'];
|
||||
|
||||
update_record('events_handlers', $event);
|
||||
|
||||
unset($cachedevents[$eventname]);
|
||||
continue;
|
||||
}
|
||||
|
||||
} else {
|
||||
// if we are here, this event handler is not present in db (new)
|
||||
// add it
|
||||
$event = new object();
|
||||
$event->eventname = $eventname;
|
||||
$event->handlermodule = $component;
|
||||
$event->handlerfile = $fileevent['handlerfile'];
|
||||
$event->handlerfunction = $fileevent['handlerfunction'];
|
||||
$event->schedule = $fileevent['schedule'];
|
||||
|
||||
insert_record('events_handlers', $event);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// clean up the left overs, the entries in cachedevents array at this points are deprecated event handlers
|
||||
// and should be removed, delete from db
|
||||
events_cleanup($component, $cachedevents);
|
||||
@ -155,14 +150,14 @@ function events_update_definition($component='moodle') {
|
||||
* @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results'
|
||||
* @param $chachedevents - array of the cached events definitions that will be
|
||||
* @return int - number of deprecated capabilities that have been removed
|
||||
*
|
||||
* INTERNAL - to be used from eventslib only
|
||||
*/
|
||||
function events_cleanup($component, $cachedevents) {
|
||||
$deletecount = 0;
|
||||
if ($cachedevents) {
|
||||
foreach ($cachedevents as $eventname => $cachedevent) {
|
||||
if (delete_records('events_handlers', 'eventname', $eventname, 'handlermodule', $component)) {
|
||||
$deletecount++;
|
||||
}
|
||||
foreach ($cachedevents as $eventname => $cachedevent) {
|
||||
if (delete_records('events_handlers', 'eventname', $eventname, 'handlermodule', $component)) {
|
||||
$deletecount++;
|
||||
}
|
||||
}
|
||||
return $deletecount;
|
||||
@ -179,7 +174,7 @@ function events_cleanup($component, $cachedevents) {
|
||||
*/
|
||||
function queue_handler($handler, $eventid) {
|
||||
global $USER;
|
||||
|
||||
|
||||
// check if this event handler is already queued
|
||||
if (!$qh = get_record('events_queue_handlers', 'queuedeventid', $eventid, 'handlerid', $handler->id)) {
|
||||
// make a new one
|
||||
@ -208,13 +203,13 @@ function queue_handler($handler, $eventid) {
|
||||
function trigger_event($eventname, $eventdata) {
|
||||
$failedevent = 0; // number of failed events.
|
||||
$eventid = 0;
|
||||
|
||||
|
||||
// pull out all registered event handlers
|
||||
if ($handlers = get_records('events_handlers', 'eventname', $eventname)) {
|
||||
foreach ($handlers as $handler) {
|
||||
// either excute it now
|
||||
|
||||
// if event type is
|
||||
|
||||
// if event type is
|
||||
if ($handler->schedule == 'instant') {
|
||||
if (dispatch_event($handler, $eventdata)) {
|
||||
continue;
|
||||
@ -226,7 +221,7 @@ function trigger_event($eventname, $eventdata) {
|
||||
// if even type is not instant, or trigger failed, queue it
|
||||
$queuedevent++;
|
||||
// make and queue the event object here
|
||||
|
||||
|
||||
if (!$eventid) {
|
||||
$eq = new object;
|
||||
$eq->userid = $USER->id;
|
||||
@ -236,8 +231,8 @@ function trigger_event($eventname, $eventdata) {
|
||||
$eq->timecreated = time();
|
||||
$eventid = insert_record('events_queue', $eq);
|
||||
}
|
||||
queue_handler($handler, $eventid);
|
||||
}
|
||||
queue_handler($handler, $eventid);
|
||||
}
|
||||
}
|
||||
return $failedevent;
|
||||
}
|
||||
@ -252,10 +247,10 @@ function dispatch_event($handler, $eventdata) {
|
||||
|
||||
global $CFG;
|
||||
// checks for handler validity
|
||||
|
||||
|
||||
// check if the same handler is queued already, if so, return false so we can queue it
|
||||
// TODO
|
||||
|
||||
// TODO
|
||||
|
||||
include_once($CFG->dirroot.$handler->handlerfile);
|
||||
return call_user_func($handler->handlerfunction, $eventdata);
|
||||
}
|
||||
@ -268,16 +263,16 @@ function dispatch_event($handler, $eventdata) {
|
||||
function events_process_queued_handler($handler) {
|
||||
// checks for handler validity
|
||||
global $CFG;
|
||||
|
||||
|
||||
// get handler
|
||||
if (!$eventhandler = get_record('events_handlers', 'id', $handler->handlerid)) {
|
||||
// can't proceed with no handler
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
// get event object
|
||||
if (!$eventobject = get_record('events_queue', 'id', $handler->queuedeventid)) {
|
||||
// can't proceed with no event object
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
// call the function sepcified by the handler
|
||||
|
||||
@ -288,21 +283,21 @@ function events_process_queued_handler($handler) {
|
||||
* Events cron will try to empty the events queue by processing all the queued events handlers
|
||||
*/
|
||||
function events_cron() {
|
||||
|
||||
|
||||
global $CFG;
|
||||
|
||||
|
||||
if ($handlers = get_records_select('events_queue_handlers', '', 'timemodified')) {
|
||||
foreach ($handlers as $handler) {
|
||||
if (events_process_queued_handler($handler)) {
|
||||
// dequeue();
|
||||
events_dequeue($handler);
|
||||
events_dequeue($handler);
|
||||
} else {
|
||||
// failed again, put back on queue
|
||||
$handler->timemodified = time();
|
||||
$handler->status++;
|
||||
update_record('events_queue_handlers', $handler);
|
||||
update_record('events_queue_handlers', $handler);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -312,15 +307,15 @@ function events_cron() {
|
||||
* @input object handler - events_queued_handler object from db
|
||||
*/
|
||||
function events_dequeue($handler) {
|
||||
|
||||
|
||||
if (delete_records('events_queue_handlers', 'id', $handler->id)) {
|
||||
// if no more queued handler is pointing to the same event
|
||||
if (!record_exists('events_queue_handlers', 'queuedeventid', $handler->queuedeventid)) {
|
||||
delete_records('events_queue', 'id', $handler->queuedeventid);
|
||||
delete_records('events_queue', 'id', $handler->queuedeventid);
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -330,6 +325,6 @@ function events_dequeue($handler) {
|
||||
* @return bool
|
||||
*/
|
||||
function event_is_registered($component, $eventname) {
|
||||
return record_exists('events_handlers', 'handlermodule', $component, 'eventname', $eventname);
|
||||
return record_exists('events_handlers', 'handlermodule', $component, 'eventname', $eventname);
|
||||
}
|
||||
?>
|
||||
|
Loading…
x
Reference in New Issue
Block a user