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]

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