Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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