PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/report/eventlist/classes/list_generator.php

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