[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/ -> eventslib.php (source)

   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.
  13  //
  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  
  17  /**
  18   * Library of functions for events manipulation.
  19   *
  20   * The public API is all at the end of this file.
  21   *
  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
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Gets the capabilities that have been cached in the database for this
  31   * component.
  32   *
  33   * @access protected To be used from eventslib only
  34   *
  35   * @param string $component examples: 'moodle', 'mod_forum', 'block_quiz_results'
  36   * @return array of events
  37   */
  38  function events_get_cached($component) {
  39      global $DB;
  40  
  41      $cachedhandlers = array();
  42  
  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);
  51          }
  52      }
  53  
  54      return $cachedhandlers;
  55  }
  56  
  57  /**
  58   * Remove all event handlers and queued events
  59   *
  60   * @category event
  61   * @param string $component examples: 'moodle', 'mod_forum', 'block_quiz_results'
  62   */
  63  function events_uninstall($component) {
  64      $cachedhandlers = events_get_cached($component);
  65      events_cleanup($component, $cachedhandlers);
  66  
  67      events_get_handlers('reset');
  68  }
  69  
  70  /**
  71   * Deletes cached events that are no longer needed by the component.
  72   *
  73   * @access protected To be used from eventslib only
  74   *
  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
  78   */
  79  function events_cleanup($component, $cachedhandlers) {
  80      global $DB;
  81  
  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);
  88              }
  89          }
  90          $DB->delete_records('events_handlers', array('eventname'=>$eventname, 'component'=>$component));
  91          $deletecount++;
  92      }
  93  
  94      return $deletecount;
  95  }
  96  
  97  /**
  98   * Removes this queued handler from the events_queued_handler table
  99   *
 100   * Removes events_queue record from events_queue if no more references to this event object exists
 101   *
 102   * @access protected To be used from eventslib only
 103   *
 104   * @param stdClass $qhandler A row from the events_queued_handler table
 105   */
 106  function events_dequeue($qhandler) {
 107      global $DB;
 108  
 109      // first delete the queue handler
 110      $DB->delete_records('events_queue_handlers', array('id'=>$qhandler->id));
 111  
 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));
 115      }
 116  }
 117  
 118  /**
 119   * Returns handlers for given event. Uses caching for better perf.
 120   *
 121   * @access protected To be used from eventslib only
 122   *
 123   * @staticvar array $handlers
 124   * @param string $eventname name of event or 'reset'
 125   * @return array|false array of handlers or false otherwise
 126   */
 127  function events_get_handlers($eventname) {
 128      global $DB;
 129      static $handlers = array();
 130  
 131      if ($eventname === 'reset') {
 132          $handlers = array();
 133          return false;
 134      }
 135  
 136      if (!array_key_exists($eventname, $handlers)) {
 137          $handlers[$eventname] = $DB->get_records('events_handlers', array('eventname'=>$eventname));
 138      }
 139  
 140      return $handlers[$eventname];
 141  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1