Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

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