Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 401 and 403]

   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  namespace tool_monitor;
  19  
  20  use core_component;
  21  use ReflectionClass;
  22  
  23  /**
  24   * Class for returning event information.
  25   *
  26   * @since      Moodle 2.8
  27   * @package    tool_monitor
  28   * @copyright  2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class eventlist {
  32  
  33      /**
  34       * Get a list of events present in the system.
  35       *
  36       * @param bool $withoutcomponent Return an eventlist without associated components.
  37       *
  38       * @return array list of events present in the system.
  39       */
  40      public static function get_all_eventlist($withoutcomponent = false) {
  41          global $CFG;
  42  
  43          // Disable developer debugging as deprecated events will fire warnings.
  44          // Setup backup variables to restore the following settings back to what they were when we are finished.
  45          $debuglevel          = $CFG->debug;
  46          $debugdisplay        = $CFG->debugdisplay;
  47          $debugdeveloper      = $CFG->debugdeveloper;
  48          $CFG->debug          = 0;
  49          $CFG->debugdisplay   = false;
  50          $CFG->debugdeveloper = false;
  51  
  52          // List of exceptional events that will cause problems if displayed.
  53          $eventsignore = [
  54              \core\event\unknown_logged::class,
  55              \logstore_legacy\event\legacy_logged::class,
  56          ];
  57  
  58          $return = [];
  59  
  60          $events = core_component::get_component_classes_in_namespace(null, 'event');
  61          foreach (array_keys($events) as $event) {
  62              // We need to filter all classes that extend event base, or the base class itself.
  63              if (is_a($event, \core\event\base::class, true) && !in_array($event, $eventsignore)) {
  64  
  65                  $reflectionclass = new ReflectionClass($event);
  66                  if ($reflectionclass->isAbstract()) {
  67                      continue;
  68                  }
  69  
  70                  // We can't choose this component's own events.
  71                  [$component] = explode('\\', $event);
  72                  if ($component === 'tool_monitor') {
  73                      continue;
  74                  }
  75  
  76                  if ($withoutcomponent) {
  77                      $return["\\{$event}"] = $event::get_name_with_info();
  78                  } else {
  79                      $return[$component]["\\{$event}"] = $event::get_name_with_info();
  80                  }
  81              }
  82          }
  83  
  84          // Now enable developer debugging as event information has been retrieved.
  85          $CFG->debug          = $debuglevel;
  86          $CFG->debugdisplay   = $debugdisplay;
  87          $CFG->debugdeveloper = $debugdeveloper;
  88  
  89          if ($withoutcomponent) {
  90              array_multisort($return, SORT_NATURAL);
  91          }
  92  
  93          return $return;
  94      }
  95  
  96      /**
  97       * Return list of plugins that have events.
  98       *
  99       * @param array $eventlist a list of events present in the system {@link eventlist::get_all_eventlist}.
 100       *
 101       * @return array list of plugins with human readable name.
 102       */
 103      public static function get_plugin_list($eventlist = array()) {
 104          if (empty($eventlist)) {
 105              $eventlist = self::get_all_eventlist();
 106          }
 107          $plugins = array_keys($eventlist);
 108          $return = array();
 109          foreach ($plugins as $plugin) {
 110              if ($plugin === 'core') {
 111                  $return[$plugin] = get_string('core', 'tool_monitor');
 112              } else if (get_string_manager()->string_exists('pluginname', $plugin)) {
 113                  $return[$plugin] = get_string('pluginname', $plugin);
 114              } else {
 115                  $return[$plugin] = $plugin;
 116              }
 117          }
 118  
 119          return $return;
 120      }
 121  
 122      /**
 123       * validate if the given event belongs to the given plugin.
 124       *
 125       * @param string $plugin Frankenstyle name of the plugin.
 126       * @param string $eventname Full qualified event name.
 127       * @param array $eventlist List of events generated by {@link eventlist::get_all_eventlist}
 128       *
 129       * @return bool Returns true if the selected event belongs to the selected plugin, false otherwise.
 130       */
 131      public static function validate_event_plugin($plugin, $eventname, $eventlist = array()) {
 132          if (empty($eventlist)) {
 133              $eventlist = self::get_all_eventlist();
 134          }
 135          if (isset($eventlist[$plugin][$eventname])) {
 136              return true;
 137          }
 138  
 139          return false;
 140      }
 141  }