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 402] [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   * Event documentation
  19   *
  20   * @package   report_eventlist
  21   * @copyright 2014 Adrian Greeve <adrian@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Class for returning system event information.
  29   *
  30   * @package   report_eventlist
  31   * @copyright 2014 Adrian Greeve <adrian@moodle.com>
  32   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class report_eventlist_list_generator {
  35  
  36      /**
  37       * Convenience method. Returns all of the core events either with or without details.
  38       *
  39       * @param bool $detail True will return details, but no abstract classes, False will return all events, but no details.
  40       * @return array All events.
  41       */
  42      public static function get_all_events_list($detail = true) {
  43          global $CFG;
  44  
  45          // Disable developer debugging as deprecated events will fire warnings.
  46          // Setup backup variables to restore the following settings back to what they were when we are finished.
  47          $debuglevel          = $CFG->debug;
  48          $debugdisplay        = $CFG->debugdisplay;
  49          $debugdeveloper      = $CFG->debugdeveloper;
  50          $CFG->debug          = 0;
  51          $CFG->debugdisplay   = false;
  52          $CFG->debugdeveloper = false;
  53  
  54          // List of exceptional events that will cause problems if displayed.
  55          $eventsignore = [
  56              \core\event\unknown_logged::class,
  57              \logstore_legacy\event\legacy_logged::class,
  58          ];
  59  
  60          $eventinformation = [];
  61  
  62          $events = core_component::get_component_classes_in_namespace(null, 'event');
  63          foreach (array_keys($events) as $event) {
  64              // We need to filter all classes that extend event base, or the base class itself.
  65              if (is_a($event, \core\event\base::class, true) && !in_array($event, $eventsignore)) {
  66                  if ($detail) {
  67                      $reflectionclass = new ReflectionClass($event);
  68                      if (!$reflectionclass->isAbstract()) {
  69                          $eventinformation = self::format_data($eventinformation, "\\$event}");
  70                      }
  71                  } else {
  72                      $parts = explode('\\', $event);
  73                      $eventinformation["\\$event}"] = array_shift($parts);
  74                  }
  75              }
  76          }
  77  
  78          // Now enable developer debugging as event information has been retrieved.
  79          $CFG->debug          = $debuglevel;
  80          $CFG->debugdisplay   = $debugdisplay;
  81          $CFG->debugdeveloper = $debugdeveloper;
  82  
  83          return $eventinformation;
  84      }
  85  
  86      /**
  87       * Return all of the core event files.
  88       *
  89       * @param bool $detail True will return details, but no abstract classes, False will return all events, but no details.
  90       * @return array Core events.
  91       *
  92       * @deprecated since 4.0 use {@see get_all_events_list} instead
  93       */
  94      public static function get_core_events_list($detail = true) {
  95          global $CFG;
  96  
  97          debugging(__FUNCTION__ . '() is deprecated, please use report_eventlist_list_generator::get_all_events_list() instead',
  98              DEBUG_DEVELOPER);
  99  
 100          // Disable developer debugging as deprecated events will fire warnings.
 101          // Setup backup variables to restore the following settings back to what they were when we are finished.
 102          $debuglevel          = $CFG->debug;
 103          $debugdisplay        = $CFG->debugdisplay;
 104          $debugdeveloper      = $CFG->debugdeveloper;
 105          $CFG->debug          = 0;
 106          $CFG->debugdisplay   = false;
 107          $CFG->debugdeveloper = false;
 108  
 109          $eventinformation = array();
 110          $directory = $CFG->libdir . '/classes/event';
 111          $files = self::get_file_list($directory);
 112  
 113          // Remove exceptional events that will cause problems being displayed.
 114          if (isset($files['unknown_logged'])) {
 115              unset($files['unknown_logged']);
 116          }
 117          foreach ($files as $file => $location) {
 118              $functionname = '\\core\\event\\' . $file;
 119              // Check to see if this is actually a valid event.
 120              if (method_exists($functionname, 'get_static_info')) {
 121                  if ($detail) {
 122                      $ref = new \ReflectionClass($functionname);
 123                      if (!$ref->isAbstract() && $file != 'manager') {
 124                          $eventinformation = self::format_data($eventinformation, $functionname);
 125                      }
 126                  } else {
 127                      $eventinformation[$functionname] = $file;
 128                  }
 129              }
 130          }
 131          // Now enable developer debugging as event information has been retrieved.
 132          $CFG->debug          = $debuglevel;
 133          $CFG->debugdisplay   = $debugdisplay;
 134          $CFG->debugdeveloper = $debugdeveloper;
 135          return $eventinformation;
 136      }
 137  
 138      /**
 139       * Returns the appropriate string for the CRUD character.
 140       *
 141       * @param string $crudcharacter The CRUD character.
 142       * @return string get_string for the specific CRUD character.
 143       */
 144      public static function get_crud_string($crudcharacter) {
 145          switch ($crudcharacter) {
 146              case 'c':
 147                  return get_string('create', 'report_eventlist');
 148                  break;
 149  
 150              case 'u':
 151                  return get_string('update', 'report_eventlist');
 152                  break;
 153  
 154              case 'd':
 155                  return get_string('delete', 'report_eventlist');
 156                  break;
 157  
 158              case 'r':
 159              default:
 160                  return get_string('read', 'report_eventlist');
 161                  break;
 162          }
 163      }
 164  
 165      /**
 166       * Returns the appropriate string for the event education level.
 167       *
 168       * @param int $edulevel Takes either the edulevel constant or string.
 169       * @return string get_string for the specific education level.
 170       */
 171      public static function get_edulevel_string($edulevel) {
 172          switch ($edulevel) {
 173              case \core\event\base::LEVEL_PARTICIPATING:
 174                  return get_string('participating', 'report_eventlist');
 175                  break;
 176  
 177              case \core\event\base::LEVEL_TEACHING:
 178                  return get_string('teaching', 'report_eventlist');
 179                  break;
 180  
 181              case \core\event\base::LEVEL_OTHER:
 182              default:
 183                  return get_string('other', 'report_eventlist');
 184                  break;
 185          }
 186      }
 187  
 188      /**
 189       * Returns a list of files (events) with a full directory path for events in a specified directory.
 190       *
 191       * @param string $directory location of files.
 192       * @return array full location of files from the specified directory.
 193       */
 194      private static function get_file_list($directory) {
 195          global $CFG;
 196          $directoryroot = $CFG->dirroot;
 197          $finaleventfiles = array();
 198          if (is_dir($directory)) {
 199              if ($handle = opendir($directory)) {
 200                  $eventfiles = scandir($directory);
 201                  foreach ($eventfiles as $file) {
 202                      if ($file != '.' && $file != '..') {
 203                          // Ignore the file if it is external to the system.
 204                          if (strrpos($directory, $directoryroot) !== false) {
 205                              $location = substr($directory, strlen($directoryroot));
 206                              $eventname = substr($file, 0, -4);
 207                              $finaleventfiles[$eventname] = $location  . '/' . $file;
 208                          }
 209                      }
 210                  }
 211              }
 212          }
 213          return $finaleventfiles;
 214      }
 215  
 216      /**
 217       * This function returns an array of all events for the plugins of the system.
 218       *
 219       * @param bool $detail True will return details, but no abstract classes, False will return all events, but no details.
 220       * @return array A list of events from all plug-ins.
 221       *
 222       * @deprecated since 4.0 use {@see get_all_events_list} instead
 223       */
 224      public static function get_non_core_event_list($detail = true) {
 225          global $CFG;
 226  
 227          debugging(__FUNCTION__ . '() is deprecated, please use report_eventlist_list_generator::get_all_events_list() instead',
 228              DEBUG_DEVELOPER);
 229  
 230          // Disable developer debugging as deprecated events will fire warnings.
 231          // Setup backup variables to restore the following settings back to what they were when we are finished.
 232          $debuglevel          = $CFG->debug;
 233          $debugdisplay        = $CFG->debugdisplay;
 234          $debugdeveloper      = $CFG->debugdeveloper;
 235          $CFG->debug          = 0;
 236          $CFG->debugdisplay   = false;
 237          $CFG->debugdeveloper = false;
 238  
 239          $noncorepluginlist = array();
 240          $plugintypes = \core_component::get_plugin_types();
 241          foreach ($plugintypes as $plugintype => $notused) {
 242              $pluginlist = \core_component::get_plugin_list($plugintype);
 243              foreach ($pluginlist as $plugin => $directory) {
 244                  $plugindirectory = $directory . '/classes/event';
 245                  foreach (self::get_file_list($plugindirectory) as $eventname => $notused) {
 246                      $plugineventname = '\\' . $plugintype . '_' . $plugin . '\\event\\' . $eventname;
 247                      // Check that this is actually an event.
 248                      if (method_exists($plugineventname, 'get_static_info')) {
 249                          if ($detail) {
 250                              $ref = new \ReflectionClass($plugineventname);
 251                              if (!$ref->isAbstract() && $plugintype . '_' . $plugin !== 'logstore_legacy') {
 252                                  $noncorepluginlist = self::format_data($noncorepluginlist, $plugineventname);
 253                              }
 254                          } else {
 255                              $noncorepluginlist[$plugineventname] = $eventname;
 256                          }
 257                      }
 258                  }
 259              }
 260          }
 261          // Now enable developer debugging as event information has been retrieved.
 262          $CFG->debug          = $debuglevel;
 263          $CFG->debugdisplay   = $debugdisplay;
 264          $CFG->debugdeveloper = $debugdeveloper;
 265  
 266          return $noncorepluginlist;
 267      }
 268  
 269      /**
 270       * Get the full list of observers for the system.
 271       *
 272       * @return array An array of observers in the system.
 273       */
 274      public static function get_observer_list() {
 275          $events = \core\event\manager::get_all_observers();
 276          foreach ($events as $key => $observers) {
 277              foreach ($observers as $observerskey => $observer) {
 278                  $events[$key][$observerskey]->parentplugin =
 279                          \core_plugin_manager::instance()->get_parent_of_subplugin($observer->plugintype);
 280              }
 281          }
 282          return $events;
 283      }
 284  
 285      /**
 286       * Returns the event data list section with url links and other formatting.
 287       *
 288       * @param array $eventdata The event data list section.
 289       * @param string $eventfullpath Full path to the events for this plugin / subplugin.
 290       * @return array The event data list section with additional formatting.
 291       */
 292      private static function format_data($eventdata, $eventfullpath) {
 293          // Get general event information.
 294          $eventdata[$eventfullpath] = $eventfullpath::get_static_info();
 295          // Create a link for further event detail.
 296          $url = new \moodle_url('eventdetail.php', array('eventname' => $eventfullpath));
 297          $link = \html_writer::link($url, $eventfullpath::get_name_with_info());
 298          $eventdata[$eventfullpath]['fulleventname'] = \html_writer::span($link);
 299          $eventdata[$eventfullpath]['fulleventname'] .= \html_writer::empty_tag('br');
 300          $eventdata[$eventfullpath]['fulleventname'] .= \html_writer::span($eventdata[$eventfullpath]['eventname'],
 301                  'report-eventlist-name');
 302  
 303          $eventdata[$eventfullpath]['crud'] = self::get_crud_string($eventdata[$eventfullpath]['crud']);
 304          $eventdata[$eventfullpath]['edulevel'] = self::get_edulevel_string($eventdata[$eventfullpath]['edulevel']);
 305          $eventdata[$eventfullpath]['legacyevent'] = $eventfullpath::get_legacy_eventname();
 306  
 307          // Mess around getting since information.
 308          $ref = new \ReflectionClass($eventdata[$eventfullpath]['eventname']);
 309          $eventdocbloc = $ref->getDocComment();
 310          $sincepattern = "/since\s*Moodle\s([0-9]+.[0-9]+)/i";
 311          preg_match($sincepattern, $eventdocbloc, $result);
 312          if (isset($result[1])) {
 313              $eventdata[$eventfullpath]['since'] = $result[1];
 314          } else {
 315              $eventdata[$eventfullpath]['since'] = null;
 316          }
 317  
 318          // Human readable plugin information to go with the component.
 319          $pluginstring = explode('\\', $eventfullpath);
 320          if ($pluginstring[1] !== 'core') {
 321              $component = $eventdata[$eventfullpath]['component'];
 322              $manager = get_string_manager();
 323              if ($manager->string_exists('pluginname', $pluginstring[1])) {
 324                  $eventdata[$eventfullpath]['component'] = \html_writer::span(get_string('pluginname', $pluginstring[1]));
 325              }
 326          }
 327  
 328          // Raw event data to be used to sort the "Event name" column.
 329          $eventdata[$eventfullpath]['raweventname'] = $eventfullpath::get_name_with_info() . ' ' . $eventdata[$eventfullpath]['eventname'];
 330  
 331          // Unset information that is not currently required.
 332          unset($eventdata[$eventfullpath]['action']);
 333          unset($eventdata[$eventfullpath]['target']);
 334          return $eventdata;
 335      }
 336  }