Merge branch 'MDL-41900-m' of git://github.com/micaherne/moodle
[moodle.git] / lib / eventslib.php
CommitLineData
0856223c 1<?php
117bd748
PS
2// This file is part of Moodle - http://moodle.org/
3//
11e1f828
MH
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.
117bd748 13//
11e1f828
MH
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/>.
16
0856223c 17/**
18 * Library of functions for events manipulation.
117bd748 19 *
583e93ca 20 * The public API is all at the end of this file.
d77717d7 21 *
ef6cf871 22 * @package core
1d1719f5
SH
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
0856223c 25 */
26
78bfb562
PS
27defined('MOODLE_INTERNAL') || die();
28
0856223c 29/**
30 * Loads the events definitions for the component (from file). If no
31 * events are defined for the component, we simply return an empty array.
d77717d7 32 *
ef6cf871 33 * @access protected To be used from eventslib only
11e1f828 34 *
650462df 35 * @param string $component examples: 'moodle', 'mod_forum', 'block_quiz_results'
ef6cf871 36 * @return array Array of capabilities or empty array if not exists
0856223c 37 */
38function events_load_def($component) {
650462df
PS
39 global $CFG;
40 if ($component === 'unittest') {
4ca04fb5 41 $defpath = $CFG->dirroot.'/lib/tests/fixtures/events.php';
650462df 42 } else {
b0d1d941 43 $defpath = core_component::get_component_directory($component).'/db/events.php';
650462df 44 }
0856223c 45
de420c11 46 $handlers = array();
d77717d7 47
0856223c 48 if (file_exists($defpath)) {
49 require($defpath);
0856223c 50 }
d77717d7 51
650462df
PS
52 // make sure the definitions are valid and complete; tell devs what is wrong
53 foreach ($handlers as $eventname => $handler) {
54 if ($eventname === 'reset') {
55 debugging("'reset' can not be used as event name.");
56 unset($handlers['reset']);
57 continue;
58 }
59 if (!is_array($handler)) {
60 debugging("Handler of '$eventname' must be specified as array'");
61 unset($handlers[$eventname]);
62 continue;
63 }
64 if (!isset($handler['handlerfile'])) {
65 debugging("Handler of '$eventname' must include 'handlerfile' key'");
66 unset($handlers[$eventname]);
67 continue;
68 }
69 if (!isset($handler['handlerfunction'])) {
70 debugging("Handler of '$eventname' must include 'handlerfunction' key'");
71 unset($handlers[$eventname]);
72 continue;
73 }
74 if (!isset($handler['schedule'])) {
75 $handler['schedule'] = 'instant';
76 }
77 if ($handler['schedule'] !== 'instant' and $handler['schedule'] !== 'cron') {
78 debugging("Handler of '$eventname' must include valid 'schedule' type (instant or cron)'");
79 unset($handlers[$eventname]);
80 continue;
81 }
82 if (!isset($handler['internal'])) {
83 $handler['internal'] = 1;
84 }
85 $handlers[$eventname] = $handler;
86 }
87
de420c11 88 return $handlers;
0856223c 89}
90
91/**
92 * Gets the capabilities that have been cached in the database for this
93 * component.
d77717d7 94 *
ef6cf871 95 * @access protected To be used from eventslib only
11e1f828 96 *
650462df 97 * @param string $component examples: 'moodle', 'mod_forum', 'block_quiz_results'
11e1f828 98 * @return array of events
0856223c 99 */
d46306de 100function events_get_cached($component) {
25c6a19d 101 global $DB;
102
d46306de 103 $cachedhandlers = array();
104
650462df 105 if ($storedhandlers = $DB->get_records('events_handlers', array('component'=>$component))) {
d46306de 106 foreach ($storedhandlers as $handler) {
107 $cachedhandlers[$handler->eventname] = array (
108 'id' => $handler->id,
109 'handlerfile' => $handler->handlerfile,
110 'handlerfunction' => $handler->handlerfunction,
650462df
PS
111 'schedule' => $handler->schedule,
112 'internal' => $handler->internal);
0856223c 113 }
0856223c 114 }
0856223c 115
d46306de 116 return $cachedhandlers;
d77717d7 117}
0856223c 118
119/**
ef6cf871
SH
120 * Updates all of the event definitions within the database.
121 *
122 * Unfortunately this isn't as simple as removing them all and then readding
123 * the updated event definitions. Chances are queued items are referencing the
124 * existing definitions.
9e2e5943 125 *
9e2e5943 126 * Note that the absence of the db/events.php event definition file
d46306de 127 * will cause any queued events for the component to be removed from
0856223c 128 * the database.
129 *
1d1719f5 130 * @category event
17da2e6f 131 * @param string $component examples: 'moodle', 'mod_forum', 'block_quiz_results'
650462df 132 * @return boolean always returns true
0856223c 133 */
0856223c 134function events_update_definition($component='moodle') {
25c6a19d 135 global $DB;
0856223c 136
9e2e5943 137 // load event definition from events.php
d46306de 138 $filehandlers = events_load_def($component);
d77717d7 139
0856223c 140 // load event definitions from db tables
141 // if we detect an event being already stored, we discard from this array later
142 // the remaining needs to be removed
d46306de 143 $cachedhandlers = events_get_cached($component);
d77717d7 144
d46306de 145 foreach ($filehandlers as $eventname => $filehandler) {
146 if (!empty($cachedhandlers[$eventname])) {
650462df
PS
147 if ($cachedhandlers[$eventname]['handlerfile'] === $filehandler['handlerfile'] &&
148 $cachedhandlers[$eventname]['handlerfunction'] === serialize($filehandler['handlerfunction']) &&
149 $cachedhandlers[$eventname]['schedule'] === $filehandler['schedule'] &&
150 $cachedhandlers[$eventname]['internal'] == $filehandler['internal']) {
d77717d7 151 // exact same event handler already present in db, ignore this entry
152
d46306de 153 unset($cachedhandlers[$eventname]);
d77717d7 154 continue;
155
156 } else {
157 // same event name matches, this event has been updated, update the datebase
365a5941 158 $handler = new stdClass();
d46306de 159 $handler->id = $cachedhandlers[$eventname]['id'];
160 $handler->handlerfile = $filehandler['handlerfile'];
161 $handler->handlerfunction = serialize($filehandler['handlerfunction']); // static class methods stored as array
162 $handler->schedule = $filehandler['schedule'];
650462df 163 $handler->internal = $filehandler['internal'];
d77717d7 164
25c6a19d 165 $DB->update_record('events_handlers', $handler);
d77717d7 166
d46306de 167 unset($cachedhandlers[$eventname]);
d77717d7 168 continue;
169 }
170
171 } else {
172 // if we are here, this event handler is not present in db (new)
173 // add it
365a5941 174 $handler = new stdClass();
d46306de 175 $handler->eventname = $eventname;
650462df 176 $handler->component = $component;
d46306de 177 $handler->handlerfile = $filehandler['handlerfile'];
178 $handler->handlerfunction = serialize($filehandler['handlerfunction']); // static class methods stored as array
179 $handler->schedule = $filehandler['schedule'];
650462df
PS
180 $handler->status = 0;
181 $handler->internal = $filehandler['internal'];
d46306de 182
25c6a19d 183 $DB->insert_record('events_handlers', $handler);
0856223c 184 }
185 }
d77717d7 186
d6e7a057 187 // clean up the left overs, the entries in cached events array at this points are deprecated event handlers
9e2e5943 188 // and should be removed, delete from db
d46306de 189 events_cleanup($component, $cachedhandlers);
0856223c 190
650462df
PS
191 events_get_handlers('reset');
192
0856223c 193 return true;
194}
195
d46306de 196/**
197 * Remove all event handlers and queued events
11e1f828 198 *
1d1719f5 199 * @category event
650462df 200 * @param string $component examples: 'moodle', 'mod_forum', 'block_quiz_results'
d46306de 201 */
202function events_uninstall($component) {
203 $cachedhandlers = events_get_cached($component);
204 events_cleanup($component, $cachedhandlers);
650462df
PS
205
206 events_get_handlers('reset');
d46306de 207}
208
0856223c 209/**
210 * Deletes cached events that are no longer needed by the component.
d77717d7 211 *
ef6cf871 212 * @access protected To be used from eventslib only
11e1f828 213 *
650462df 214 * @param string $component examples: 'moodle', 'mod_forum', 'block_quiz_results'
d6e7a057 215 * @param array $cachedhandlers array of the cached events definitions that will be
650462df 216 * @return int number of unused handlers that have been removed
0856223c 217 */
d46306de 218function events_cleanup($component, $cachedhandlers) {
25c6a19d 219 global $DB;
220
0856223c 221 $deletecount = 0;
d46306de 222 foreach ($cachedhandlers as $eventname => $cachedhandler) {
25c6a19d 223 if ($qhandlers = $DB->get_records('events_queue_handlers', array('handlerid'=>$cachedhandler['id']))) {
650462df 224 //debugging("Removing pending events from queue before deleting of event handler: $component - $eventname");
d46306de 225 foreach ($qhandlers as $qhandler) {
226 events_dequeue($qhandler);
227 }
228 }
650462df
PS
229 $DB->delete_records('events_handlers', array('eventname'=>$eventname, 'component'=>$component));
230 $deletecount++;
0856223c 231 }
650462df 232
0856223c 233 return $deletecount;
234}
235
8aaf935a 236/****************** End of Events handler Definition code *******************/
237
8aaf935a 238/**
1d1719f5 239 * Puts a handler on queue
d46306de 240 *
ef6cf871 241 * @access protected To be used from eventslib only
11e1f828 242 *
1d1719f5
SH
243 * @param stdClass $handler event handler object from db
244 * @param stdClass $event event data object
11e1f828 245 * @param string $errormessage The error message indicating the problem
1d1719f5 246 * @return int id number of new queue handler
8aaf935a 247 */
d46306de 248function events_queue_handler($handler, $event, $errormessage) {
25c6a19d 249 global $DB;
d77717d7 250
25c6a19d 251 if ($qhandler = $DB->get_record('events_queue_handlers', array('queuedeventid'=>$event->id, 'handlerid'=>$handler->id))) {
d46306de 252 debugging("Please check code: Event id $event->id is already queued in handler id $qhandler->id");
253 return $qhandler->id;
254 }
d77717d7 255
d46306de 256 // make a new queue handler
365a5941 257 $qhandler = new stdClass();
d46306de 258 $qhandler->queuedeventid = $event->id;
259 $qhandler->handlerid = $handler->id;
25c6a19d 260 $qhandler->errormessage = $errormessage;
d46306de 261 $qhandler->timemodified = time();
650462df 262 if ($handler->schedule === 'instant' and $handler->status == 1) {
d46306de 263 $qhandler->status = 1; //already one failed attempt to dispatch this event
264 } else {
265 $qhandler->status = 0;
8aaf935a 266 }
d46306de 267
25c6a19d 268 return $DB->insert_record('events_queue_handlers', $qhandler);
8aaf935a 269}
270
271/**
272 * trigger a single event with a specified handler
d46306de 273 *
ef6cf871 274 * @access protected To be used from eventslib only
11e1f828 275 *
ef6cf871
SH
276 * @param stdClass $handler This shoudl be a row from the events_handlers table.
277 * @param stdClass $eventdata An object containing information about the event
11e1f828 278 * @param string $errormessage error message indicating problem
1d1719f5 279 * @return bool|null True means event processed, false means retry event later; may throw exception, NULL means internal error
8aaf935a 280 */
d46306de 281function events_dispatch($handler, $eventdata, &$errormessage) {
8aaf935a 282 global $CFG;
d77717d7 283
d46306de 284 $function = unserialize($handler->handlerfunction);
285
286 if (is_callable($function)) {
287 // oki, no need for includes
288
289 } else if (file_exists($CFG->dirroot.$handler->handlerfile)) {
290 include_once($CFG->dirroot.$handler->handlerfile);
291
292 } else {
650462df
PS
293 $errormessage = "Handler file of component $handler->component: $handler->handlerfile can not be found!";
294 return null;
d46306de 295 }
296
297 // checks for handler validity
298 if (is_callable($function)) {
650462df
PS
299 $result = call_user_func($function, $eventdata);
300 if ($result === false) {
301 $errormessage = "Handler function of component $handler->component: $handler->handlerfunction requested resending of event!";
302 return false;
303 }
304 return true;
d77717d7 305
d46306de 306 } else {
650462df
PS
307 $errormessage = "Handler function of component $handler->component: $handler->handlerfunction not callable function or class method!";
308 return null;
d46306de 309 }
8aaf935a 310}
311
312/**
313 * given a queued handler, call the respective event handler to process the event
d46306de 314 *
ef6cf871 315 * @access protected To be used from eventslib only
11e1f828 316 *
1d1719f5 317 * @param stdClass $qhandler events_queued_handler row from db
650462df 318 * @return boolean true means event processed, false means retry later, NULL means fatal failure
8aaf935a 319 */
d46306de 320function events_process_queued_handler($qhandler) {
650462df 321 global $DB;
d77717d7 322
8aaf935a 323 // get handler
25c6a19d 324 if (!$handler = $DB->get_record('events_handlers', array('id'=>$qhandler->handlerid))) {
d46306de 325 debugging("Error processing queue handler $qhandler->id, missing handler id: $qhandler->handlerid");
326 //irrecoverable error, remove broken queue handler
327 events_dequeue($qhandler);
328 return NULL;
8aaf935a 329 }
d46306de 330
8aaf935a 331 // get event object
25c6a19d 332 if (!$event = $DB->get_record('events_queue', array('id'=>$qhandler->queuedeventid))) {
d46306de 333 // can't proceed with no event object - might happen when two crons running at the same time
334 debugging("Error processing queue handler $qhandler->id, missing event id: $qhandler->queuedeventid");
335 //irrecoverable error, remove broken queue handler
336 events_dequeue($qhandler);
337 return NULL;
338 }
339
340 // call the function specified by the handler
650462df
PS
341 try {
342 $errormessage = 'Unknown error';
343 if (events_dispatch($handler, unserialize(base64_decode($event->eventdata)), $errormessage)) {
344 //everything ok
345 events_dequeue($qhandler);
346 return true;
347 }
348 } catch (Exception $e) {
349 // the problem here is that we do not want one broken handler to stop all others,
350 // cron handlers are very tricky because the needed data might have been deleted before the cron execution
99fa9745
TH
351 $errormessage = "Handler function of component $handler->component: $handler->handlerfunction threw exception :" .
352 $e->getMessage() . "\n" . format_backtrace($e->getTrace(), true);
353 if (!empty($e->debuginfo)) {
354 $errormessage .= $e->debuginfo;
355 }
8aaf935a 356 }
650462df
PS
357
358 //dispatching failed
365a5941 359 $qh = new stdClass();
650462df
PS
360 $qh->id = $qhandler->id;
361 $qh->errormessage = $errormessage;
362 $qh->timemodified = time();
363 $qh->status = $qhandler->status + 1;
364 $DB->update_record('events_queue_handlers', $qh);
365
f817c41c
MA
366 debugging($errormessage);
367
650462df 368 return false;
d46306de 369}
370
371/**
d6e7a057 372 * Removes this queued handler from the events_queued_handler table
11e1f828 373 *
d6e7a057 374 * Removes events_queue record from events_queue if no more references to this event object exists
d46306de 375 *
ef6cf871 376 * @access protected To be used from eventslib only
11e1f828 377 *
ef6cf871 378 * @param stdClass $qhandler A row from the events_queued_handler table
d46306de 379 */
380function events_dequeue($qhandler) {
25c6a19d 381 global $DB;
382
d46306de 383 // first delete the queue handler
25c6a19d 384 $DB->delete_records('events_queue_handlers', array('id'=>$qhandler->id));
8aaf935a 385
d46306de 386 // if no more queued handler is pointing to the same event - delete the event too
25c6a19d 387 if (!$DB->record_exists('events_queue_handlers', array('queuedeventid'=>$qhandler->queuedeventid))) {
388 $DB->delete_records('events_queue', array('id'=>$qhandler->queuedeventid));
d46306de 389 }
8aaf935a 390}
391
1336c96e 392/**
d6e7a057 393 * Returns handlers for given event. Uses caching for better perf.
1336c96e 394 *
ef6cf871 395 * @access protected To be used from eventslib only
11e1f828 396 *
11e1f828 397 * @staticvar array $handlers
ef6cf871 398 * @param string $eventname name of event or 'reset'
1d1719f5 399 * @return array|false array of handlers or false otherwise
1336c96e 400 */
401function events_get_handlers($eventname) {
a2d51ba8 402 global $DB;
1336c96e 403 static $handlers = array();
404
650462df 405 if ($eventname === 'reset') {
1336c96e 406 $handlers = array();
407 return false;
408 }
d46306de 409
1336c96e 410 if (!array_key_exists($eventname, $handlers)) {
411 $handlers[$eventname] = $DB->get_records('events_handlers', array('eventname'=>$eventname));
412 }
413
414 return $handlers[$eventname];
415}
d46306de 416
8aaf935a 417/**
418 * Events cron will try to empty the events queue by processing all the queued events handlers
d46306de 419 *
1d1719f5
SH
420 * @access public Part of the public API
421 * @category event
11e1f828 422 * @param string $eventname empty means all
1d1719f5 423 * @return int number of dispatched events
8aaf935a 424 */
d46306de 425function events_cron($eventname='') {
25c6a19d 426 global $DB;
d77717d7 427
d46306de 428 $failed = array();
429 $processed = 0;
430
431 if ($eventname) {
25c6a19d 432 $sql = "SELECT qh.*
433 FROM {events_queue_handlers} qh, {events_handlers} h
434 WHERE qh.handlerid = h.id AND h.eventname=?
435 ORDER BY qh.id";
436 $params = array($eventname);
d46306de 437 } else {
25c6a19d 438 $sql = "SELECT *
439 FROM {events_queue_handlers}
440 ORDER BY id";
441 $params = array();
d46306de 442 }
443
b967c541
EL
444 $rs = $DB->get_recordset_sql($sql, $params);
445 foreach ($rs as $qhandler) {
446 if (isset($failed[$qhandler->handlerid])) {
447 // do not try to dispatch any later events when one already asked for retry or ended with exception
448 continue;
449 }
450 $status = events_process_queued_handler($qhandler);
451 if ($status === false) {
452 // handler is asking for retry, do not send other events to this handler now
453 $failed[$qhandler->handlerid] = $qhandler->handlerid;
454 } else if ($status === NULL) {
455 // means completely broken handler, event data was purged
456 $failed[$qhandler->handlerid] = $qhandler->handlerid;
457 } else {
458 $processed++;
d77717d7 459 }
8aaf935a 460 }
b967c541 461 $rs->close();
650462df
PS
462
463 // remove events that do not have any handlers waiting
464 $sql = "SELECT eq.id
465 FROM {events_queue} eq
466 LEFT JOIN {events_queue_handlers} qh ON qh.queuedeventid = eq.id
467 WHERE qh.id IS NULL";
468 $rs = $DB->get_recordset_sql($sql);
469 foreach ($rs as $event) {
470 //debugging('Purging stale event '.$event->id);
471 $DB->delete_records('events_queue', array('id'=>$event->id));
472 }
473 $rs->close();
474
d46306de 475 return $processed;
8aaf935a 476}
477
478/**
f0f0e1fe 479 * Do not call directly, this is intended to be used from new event base only.
d46306de 480 *
f0f0e1fe 481 * @private
11e1f828 482 * @param string $eventname name of the event
f0f0e1fe 483 * @param mixed $eventdata event data object
11e1f828 484 * @return int number of failed events
8aaf935a 485 */
f0f0e1fe 486function events_trigger_legacy($eventname, $eventdata) {
25c6a19d 487 global $CFG, $USER, $DB;
d77717d7 488
d46306de 489 $failedcount = 0; // number of failed events.
d46306de 490
491 // pull out all registered event handlers
1336c96e 492 if ($handlers = events_get_handlers($eventname)) {
d46306de 493 foreach ($handlers as $handler) {
650462df 494 $errormessage = '';
d46306de 495
650462df 496 if ($handler->schedule === 'instant') {
d46306de 497 if ($handler->status) {
498 //check if previous pending events processed
25c6a19d 499 if (!$DB->record_exists('events_queue_handlers', array('handlerid'=>$handler->id))) {
d46306de 500 // ok, queue is empty, lets reset the status back to 0 == ok
501 $handler->status = 0;
25c6a19d 502 $DB->set_field('events_handlers', 'status', 0, array('id'=>$handler->id));
779ad29f 503 // reset static handler cache
504 events_get_handlers('reset');
d46306de 505 }
506 }
507
508 // dispatch the event only if instant schedule and status ok
650462df 509 if ($handler->status or (!$handler->internal and $DB->is_transaction_started())) {
d46306de 510 // increment the error status counter
511 $handler->status++;
25c6a19d 512 $DB->set_field('events_handlers', 'status', $handler->status, array('id'=>$handler->id));
779ad29f 513 // reset static handler cache
514 events_get_handlers('reset');
650462df
PS
515
516 } else {
0e35ba6f 517 $errormessage = 'Unknown error';
650462df
PS
518 $result = events_dispatch($handler, $eventdata, $errormessage);
519 if ($result === true) {
520 // everything is fine - event dispatched
521 continue;
522 } else if ($result === false) {
523 // retry later - set error count to 1 == send next instant into cron queue
524 $DB->set_field('events_handlers', 'status', 1, array('id'=>$handler->id));
525 // reset static handler cache
526 events_get_handlers('reset');
527 } else {
528 // internal problem - ignore the event completely
529 $failedcount ++;
530 continue;
531 }
d46306de 532 }
533
534 // update the failed counter
535 $failedcount ++;
536
650462df 537 } else if ($handler->schedule === 'cron') {
d6e7a057 538 //ok - use queueing of events only
d46306de 539
540 } else {
650462df 541 // unknown schedule - ignore event completely
d46306de 542 debugging("Unknown handler schedule type: $handler->schedule");
650462df
PS
543 $failedcount ++;
544 continue;
d46306de 545 }
546
650462df 547 // if even type is not instant, or dispatch asked for retry, queue it
365a5941 548 $event = new stdClass();
650462df
PS
549 $event->userid = $USER->id;
550 $event->eventdata = base64_encode(serialize($eventdata));
551 $event->timecreated = time();
552 if (debugging()) {
553 $dump = '';
554 $callers = debug_backtrace();
555 foreach ($callers as $caller) {
556 if (!isset($caller['line'])) {
557 $caller['line'] = '?';
558 }
559 if (!isset($caller['file'])) {
560 $caller['file'] = '?';
561 }
562 $dump .= 'line ' . $caller['line'] . ' of ' . substr($caller['file'], strlen($CFG->dirroot) + 1);
563 if (isset($caller['function'])) {
564 $dump .= ': call to ';
565 if (isset($caller['class'])) {
566 $dump .= $caller['class'] . $caller['type'];
d46306de 567 }
650462df 568 $dump .= $caller['function'] . '()';
d46306de 569 }
650462df 570 $dump .= "\n";
d46306de 571 }
650462df
PS
572 $event->stackdump = $dump;
573 } else {
574 $event->stackdump = '';
d46306de 575 }
650462df 576 $event->id = $DB->insert_record('events_queue', $event);
d46306de 577 events_queue_handler($handler, $event, $errormessage);
8aaf935a 578 }
8aaf935a 579 } else {
650462df 580 // No handler found for this event name - this is ok!
8aaf935a 581 }
d46306de 582
583 return $failedcount;
8aaf935a 584}
9e2e5943 585
586/**
587 * checks if an event is registered for this component
d46306de 588 *
1d1719f5
SH
589 * @access public Part of the public API
590 *
11e1f828
MH
591 * @param string $eventname name of the event
592 * @param string $component component name, can be mod/data or moodle
593 * @return bool
9e2e5943 594 */
d46306de 595function events_is_registered($eventname, $component) {
25c6a19d 596 global $DB;
650462df 597 return $DB->record_exists('events_handlers', array('component'=>$component, 'eventname'=>$eventname));
9e2e5943 598}
d46306de 599
600/**
601 * checks if an event is queued for processing - either cron handlers attached or failed instant handlers
d46306de 602 *
1d1719f5 603 * @access public Part of the public API
11e1f828 604 *
11e1f828
MH
605 * @param string $eventname name of the event
606 * @return int number of queued events
d46306de 607 */
608function events_pending_count($eventname) {
650462df 609 global $DB;
d46306de 610
25c6a19d 611 $sql = "SELECT COUNT('x')
650462df
PS
612 FROM {events_queue_handlers} qh
613 JOIN {events_handlers} h ON h.id = qh.handlerid
614 WHERE h.eventname = ?";
615
25c6a19d 616 return $DB->count_records_sql($sql, array($eventname));
0e35ba6f 617}