[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/monitor/classes/ -> eventlist.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   * Event documentation
  19   *
  20   * @package    tool_monitor
  21   * @copyright  2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_monitor;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Class for returning event information.
  31   *
  32   * @since      Moodle 2.8
  33   * @package    tool_monitor
  34   * @copyright  2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class eventlist {
  38      /**
  39       * Return all of the core event files.
  40       *
  41       * @return array Core events.
  42       */
  43      protected static function get_core_eventlist() {
  44          global $CFG;
  45  
  46          // Disable developer debugging as deprecated events will fire warnings.
  47          // Setup backup variables to restore the following settings back to what they were when we are finished.
  48          $debuglevel          = $CFG->debug;
  49          $debugdisplay        = $CFG->debugdisplay;
  50          $debugdeveloper      = $CFG->debugdeveloper;
  51          $CFG->debug          = 0;
  52          $CFG->debugdisplay   = false;
  53          $CFG->debugdeveloper = false;
  54  
  55          $eventinformation = array();
  56          $directory = $CFG->libdir . '/classes/event';
  57          $files = self::get_file_list($directory);
  58  
  59          // Remove exceptional events that will cause problems being displayed.
  60          if (isset($files['unknown_logged'])) {
  61              unset($files['unknown_logged']);
  62          }
  63          foreach ($files as $file => $location) {
  64              $classname = '\\core\\event\\' . $file;
  65              // Check to see if this is actually a valid event.
  66              if (method_exists($classname, 'get_static_info')) {
  67                  $ref = new \ReflectionClass($classname);
  68                  // Ignore abstracts.
  69                  if (!$ref->isAbstract() && $file != 'manager') {
  70                      $eventinformation[$classname] = $classname::get_name();
  71                  }
  72              }
  73          }
  74          // Now enable developer debugging as event information has been retrieved.
  75          $CFG->debug          = $debuglevel;
  76          $CFG->debugdisplay   = $debugdisplay;
  77          $CFG->debugdeveloper = $debugdeveloper;
  78          return $eventinformation;
  79      }
  80  
  81      /**
  82       * This function returns an array of all events for the plugins of the system.
  83       *
  84       * @param bool $withoutcomponent Return an eventlist without associated components.
  85       *
  86       * @return array A list of events from all plug-ins.
  87       */
  88      protected static function get_non_core_eventlist($withoutcomponent = false) {
  89          global $CFG;
  90          // Disable developer debugging as deprecated events will fire warnings.
  91          // Setup backup variables to restore the following settings back to what they were when we are finished.
  92          $debuglevel          = $CFG->debug;
  93          $debugdisplay        = $CFG->debugdisplay;
  94          $debugdeveloper      = $CFG->debugdeveloper;
  95          $CFG->debug          = 0;
  96          $CFG->debugdisplay   = false;
  97          $CFG->debugdeveloper = false;
  98  
  99          $noncorepluginlist = array();
 100          $plugintypes = \core_component::get_plugin_types();
 101          foreach ($plugintypes as $plugintype => $notused) {
 102              $pluginlist = \core_component::get_plugin_list($plugintype);
 103              foreach ($pluginlist as $plugin => $directory) {
 104                  $plugindirectory = $directory . '/classes/event';
 105                  foreach (self::get_file_list($plugindirectory) as $eventname => $notused) {
 106                      $plugineventname = '\\' . $plugintype . '_' . $plugin . '\\event\\' . $eventname;
 107                      // Check that this is actually an event.
 108                      if (method_exists($plugineventname, 'get_static_info')  && $plugin != 'monitor') { // No selfie here.
 109                          $ref = new \ReflectionClass($plugineventname);
 110                          if (!$ref->isAbstract() && $plugin != 'legacy') {
 111                              if ($withoutcomponent) {
 112                                  $noncorepluginlist[$plugineventname] = $plugineventname::get_name();
 113                              } else {
 114                                  $noncorepluginlist[$plugintype . '_' . $plugin][$plugineventname] = $plugineventname::get_name();
 115                              }
 116                          }
 117                      }
 118                  }
 119              }
 120          }
 121  
 122          // Now enable developer debugging as event information has been retrieved.
 123          $CFG->debug          = $debuglevel;
 124          $CFG->debugdisplay   = $debugdisplay;
 125          $CFG->debugdeveloper = $debugdeveloper;
 126  
 127          return $noncorepluginlist;
 128      }
 129  
 130      /**
 131       * Returns a list of files with a full directory path in a specified directory.
 132       *
 133       * @param string $directory location of files.
 134       * @return array full location of files from the specified directory.
 135       */
 136      protected static function get_file_list($directory) {
 137          global $CFG;
 138          $directoryroot = $CFG->dirroot;
 139          $finalfiles = array();
 140          if (is_dir($directory)) {
 141              if ($handle = opendir($directory)) {
 142                  $files = scandir($directory);
 143                  foreach ($files as $file) {
 144                      if ($file != '.' && $file != '..') {
 145                          // Ignore the file if it is external to the system.
 146                          if (strrpos($directory, $directoryroot) !== false) {
 147                              $location = substr($directory, strlen($directoryroot));
 148                              $name = substr($file, 0, -4);
 149                              $finalfiles[$name] = $location  . '/' . $file;
 150                          }
 151                      }
 152                  }
 153              }
 154          }
 155          return $finalfiles;
 156      }
 157  
 158      /**
 159       * Get a list of events present in the system.
 160       *
 161       * @param bool $withoutcomponent Return an eventlist without associated components.
 162       *
 163       * @return array list of events present in the system.
 164       */
 165      public static function get_all_eventlist($withoutcomponent = false) {
 166          if ($withoutcomponent) {
 167              $return = array_merge(self::get_core_eventlist(), self::get_non_core_eventlist($withoutcomponent));
 168              array_multisort($return, SORT_NATURAL);
 169          } else {
 170              $return = array_merge(array('core' => self::get_core_eventlist()),
 171                      self::get_non_core_eventlist($withoutcomponent = false));
 172          }
 173          return $return;
 174      }
 175  
 176      /**
 177       * Return list of plugins that have events.
 178       *
 179       * @param array $eventlist a list of events present in the system {@link eventlist::get_all_eventlist}.
 180       *
 181       * @return array list of plugins with human readable name.
 182       */
 183      public static function get_plugin_list($eventlist = array()) {
 184          if (empty($eventlist)) {
 185              $eventlist = self::get_all_eventlist();
 186          }
 187          $plugins = array_keys($eventlist);
 188          $return = array();
 189          foreach ($plugins as $plugin) {
 190              if ($plugin === 'core') {
 191                  $return[$plugin] = get_string('core', 'tool_monitor');
 192              } else if (get_string_manager()->string_exists('pluginname', $plugin)) {
 193                  $return[$plugin] = get_string('pluginname', $plugin);
 194              } else {
 195                  $return[$plugin] = $plugin;
 196              }
 197          }
 198  
 199          return $return;
 200      }
 201  
 202      /**
 203       * validate if the given event belongs to the given plugin.
 204       *
 205       * @param string $plugin Frankenstyle name of the plugin.
 206       * @param string $eventname Full qualified event name.
 207       * @param array $eventlist List of events generated by {@link eventlist::get_all_eventlist}
 208       *
 209       * @return bool Returns true if the selected event belongs to the selected plugin, false otherwise.
 210       */
 211      public static function validate_event_plugin($plugin, $eventname, $eventlist = array()) {
 212          if (empty($eventlist)) {
 213              $eventlist = self::get_all_eventlist();
 214          }
 215          if (isset($eventlist[$plugin][$eventname])) {
 216              return true;
 217          }
 218  
 219          return false;
 220      }
 221  }


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