PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/tool/monitor/classes/eventlist.php

https://bitbucket.org/moodle/moodle
PHP | 222 lines | 178 code | 7 blank | 37 comment | 2 complexity | 68132d1e99204757d35d044c244dd6ca MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. * Event documentation
  18. *
  19. * @package tool_monitor
  20. * @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. namespace tool_monitor;
  24. defined('MOODLE_INTERNAL') || die();
  25. /**
  26. * Class for returning event information.
  27. *
  28. * @since Moodle 2.8
  29. * @package tool_monitor
  30. * @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class eventlist {
  34. /**
  35. * Return all of the core event files.
  36. *
  37. * @return array Core events.
  38. */
  39. protected static function get_core_eventlist() {
  40. global $CFG;
  41. // Disable developer debugging as deprecated events will fire warnings.
  42. // Setup backup variables to restore the following settings back to what they were when we are finished.
  43. $debuglevel = $CFG->debug;
  44. $debugdisplay = $CFG->debugdisplay;
  45. $debugdeveloper = $CFG->debugdeveloper;
  46. $CFG->debug = 0;
  47. $CFG->debugdisplay = false;
  48. $CFG->debugdeveloper = false;
  49. $eventinformation = array();
  50. $directory = $CFG->libdir . '/classes/event';
  51. $files = self::get_file_list($directory);
  52. // Remove exceptional events that will cause problems being displayed.
  53. if (isset($files['unknown_logged'])) {
  54. unset($files['unknown_logged']);
  55. }
  56. foreach ($files as $file => $location) {
  57. $classname = '\\core\\event\\' . $file;
  58. // Check to see if this is actually a valid event.
  59. if (method_exists($classname, 'get_static_info')) {
  60. $ref = new \ReflectionClass($classname);
  61. // Ignore abstracts.
  62. if (!$ref->isAbstract() && $file != 'manager') {
  63. $eventinformation[$classname] = $classname::get_name_with_info();
  64. }
  65. }
  66. }
  67. // Now enable developer debugging as event information has been retrieved.
  68. $CFG->debug = $debuglevel;
  69. $CFG->debugdisplay = $debugdisplay;
  70. $CFG->debugdeveloper = $debugdeveloper;
  71. return $eventinformation;
  72. }
  73. /**
  74. * This function returns an array of all events for the plugins of the system.
  75. *
  76. * @param bool $withoutcomponent Return an eventlist without associated components.
  77. *
  78. * @return array A list of events from all plug-ins.
  79. */
  80. protected static function get_non_core_eventlist($withoutcomponent = false) {
  81. global $CFG;
  82. // Disable developer debugging as deprecated events will fire warnings.
  83. // Setup backup variables to restore the following settings back to what they were when we are finished.
  84. $debuglevel = $CFG->debug;
  85. $debugdisplay = $CFG->debugdisplay;
  86. $debugdeveloper = $CFG->debugdeveloper;
  87. $CFG->debug = 0;
  88. $CFG->debugdisplay = false;
  89. $CFG->debugdeveloper = false;
  90. $noncorepluginlist = array();
  91. $plugintypes = \core_component::get_plugin_types();
  92. foreach ($plugintypes as $plugintype => $notused) {
  93. $pluginlist = \core_component::get_plugin_list($plugintype);
  94. foreach ($pluginlist as $plugin => $directory) {
  95. $plugindirectory = $directory . '/classes/event';
  96. foreach (self::get_file_list($plugindirectory) as $eventname => $notused) {
  97. $fullpluginname = $plugintype . '_' . $plugin;
  98. $plugineventname = '\\' . $fullpluginname . '\\event\\' . $eventname;
  99. // Check that this is actually an event.
  100. if (method_exists($plugineventname, 'get_static_info') && $fullpluginname !== 'tool_monitor') { // No selfie here.
  101. $ref = new \ReflectionClass($plugineventname);
  102. if (!$ref->isAbstract() && $fullpluginname !== 'logstore_legacy') {
  103. if ($withoutcomponent) {
  104. $noncorepluginlist[$plugineventname] = $plugineventname::get_name_with_info();
  105. } else {
  106. $noncorepluginlist[$fullpluginname][$plugineventname] = $plugineventname::get_name_with_info();
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. // Now enable developer debugging as event information has been retrieved.
  114. $CFG->debug = $debuglevel;
  115. $CFG->debugdisplay = $debugdisplay;
  116. $CFG->debugdeveloper = $debugdeveloper;
  117. return $noncorepluginlist;
  118. }
  119. /**
  120. * Returns a list of files with a full directory path in a specified directory.
  121. *
  122. * @param string $directory location of files.
  123. * @return array full location of files from the specified directory.
  124. */
  125. protected static function get_file_list($directory) {
  126. global $CFG;
  127. $directoryroot = $CFG->dirroot;
  128. $finalfiles = array();
  129. if (is_dir($directory)) {
  130. if ($handle = opendir($directory)) {
  131. $files = scandir($directory);
  132. foreach ($files as $file) {
  133. if ($file != '.' && $file != '..') {
  134. // Ignore the file if it is external to the system.
  135. if (strrpos($directory, $directoryroot) !== false) {
  136. $location = substr($directory, strlen($directoryroot));
  137. $name = substr($file, 0, -4);
  138. $finalfiles[$name] = $location . '/' . $file;
  139. }
  140. }
  141. }
  142. }
  143. }
  144. return $finalfiles;
  145. }
  146. /**
  147. * Get a list of events present in the system.
  148. *
  149. * @param bool $withoutcomponent Return an eventlist without associated components.
  150. *
  151. * @return array list of events present in the system.
  152. */
  153. public static function get_all_eventlist($withoutcomponent = false) {
  154. if ($withoutcomponent) {
  155. $return = array_merge(self::get_core_eventlist(), self::get_non_core_eventlist($withoutcomponent));
  156. array_multisort($return, SORT_NATURAL);
  157. } else {
  158. $return = array_merge(array('core' => self::get_core_eventlist()),
  159. self::get_non_core_eventlist($withoutcomponent = false));
  160. }
  161. return $return;
  162. }
  163. /**
  164. * Return list of plugins that have events.
  165. *
  166. * @param array $eventlist a list of events present in the system {@link eventlist::get_all_eventlist}.
  167. *
  168. * @return array list of plugins with human readable name.
  169. */
  170. public static function get_plugin_list($eventlist = array()) {
  171. if (empty($eventlist)) {
  172. $eventlist = self::get_all_eventlist();
  173. }
  174. $plugins = array_keys($eventlist);
  175. $return = array();
  176. foreach ($plugins as $plugin) {
  177. if ($plugin === 'core') {
  178. $return[$plugin] = get_string('core', 'tool_monitor');
  179. } else if (get_string_manager()->string_exists('pluginname', $plugin)) {
  180. $return[$plugin] = get_string('pluginname', $plugin);
  181. } else {
  182. $return[$plugin] = $plugin;
  183. }
  184. }
  185. return $return;
  186. }
  187. /**
  188. * validate if the given event belongs to the given plugin.
  189. *
  190. * @param string $plugin Frankenstyle name of the plugin.
  191. * @param string $eventname Full qualified event name.
  192. * @param array $eventlist List of events generated by {@link eventlist::get_all_eventlist}
  193. *
  194. * @return bool Returns true if the selected event belongs to the selected plugin, false otherwise.
  195. */
  196. public static function validate_event_plugin($plugin, $eventname, $eventlist = array()) {
  197. if (empty($eventlist)) {
  198. $eventlist = self::get_all_eventlist();
  199. }
  200. if (isset($eventlist[$plugin][$eventname])) {
  201. return true;
  202. }
  203. return false;
  204. }
  205. }