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

/report/eventlist/classes/list_generator.php

http://github.com/moodle/moodle
PHP | 285 lines | 230 code | 10 blank | 45 comment | 4 complexity | ad02b9ebefee329da24c5851188153bd MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. return array_merge(self::get_core_events_list($detail), self::get_non_core_event_list($detail));
  40. }
  41. /**
  42. * Return all of the core event files.
  43. *
  44. * @param bool $detail True will return details, but no abstract classes, False will return all events, but no details.
  45. * @return array Core events.
  46. */
  47. public static function get_core_events_list($detail = true) {
  48. global $CFG;
  49. // Disable developer debugging as deprecated events will fire warnings.
  50. // Setup backup variables to restore the following settings back to what they were when we are finished.
  51. $debuglevel = $CFG->debug;
  52. $debugdisplay = $CFG->debugdisplay;
  53. $debugdeveloper = $CFG->debugdeveloper;
  54. $CFG->debug = 0;
  55. $CFG->debugdisplay = false;
  56. $CFG->debugdeveloper = false;
  57. $eventinformation = array();
  58. $directory = $CFG->libdir . '/classes/event';
  59. $files = self::get_file_list($directory);
  60. // Remove exceptional events that will cause problems being displayed.
  61. if (isset($files['unknown_logged'])) {
  62. unset($files['unknown_logged']);
  63. }
  64. foreach ($files as $file => $location) {
  65. $functionname = '\\core\\event\\' . $file;
  66. // Check to see if this is actually a valid event.
  67. if (method_exists($functionname, 'get_static_info')) {
  68. if ($detail) {
  69. $ref = new \ReflectionClass($functionname);
  70. if (!$ref->isAbstract() && $file != 'manager') {
  71. $eventinformation = self::format_data($eventinformation, $functionname);
  72. }
  73. } else {
  74. $eventinformation[$functionname] = $file;
  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. return $eventinformation;
  83. }
  84. /**
  85. * Returns the appropriate string for the CRUD character.
  86. *
  87. * @param string $crudcharacter The CRUD character.
  88. * @return string get_string for the specific CRUD character.
  89. */
  90. public static function get_crud_string($crudcharacter) {
  91. switch ($crudcharacter) {
  92. case 'c':
  93. return get_string('create', 'report_eventlist');
  94. break;
  95. case 'u':
  96. return get_string('update', 'report_eventlist');
  97. break;
  98. case 'd':
  99. return get_string('delete', 'report_eventlist');
  100. break;
  101. case 'r':
  102. default:
  103. return get_string('read', 'report_eventlist');
  104. break;
  105. }
  106. }
  107. /**
  108. * Returns the appropriate string for the event education level.
  109. *
  110. * @param int $edulevel Takes either the edulevel constant or string.
  111. * @return string get_string for the specific education level.
  112. */
  113. public static function get_edulevel_string($edulevel) {
  114. switch ($edulevel) {
  115. case \core\event\base::LEVEL_PARTICIPATING:
  116. return get_string('participating', 'report_eventlist');
  117. break;
  118. case \core\event\base::LEVEL_TEACHING:
  119. return get_string('teaching', 'report_eventlist');
  120. break;
  121. case \core\event\base::LEVEL_OTHER:
  122. default:
  123. return get_string('other', 'report_eventlist');
  124. break;
  125. }
  126. }
  127. /**
  128. * Returns a list of files (events) with a full directory path for events in a specified directory.
  129. *
  130. * @param string $directory location of files.
  131. * @return array full location of files from the specified directory.
  132. */
  133. private static function get_file_list($directory) {
  134. global $CFG;
  135. $directoryroot = $CFG->dirroot;
  136. $finaleventfiles = array();
  137. if (is_dir($directory)) {
  138. if ($handle = opendir($directory)) {
  139. $eventfiles = scandir($directory);
  140. foreach ($eventfiles as $file) {
  141. if ($file != '.' && $file != '..') {
  142. // Ignore the file if it is external to the system.
  143. if (strrpos($directory, $directoryroot) !== false) {
  144. $location = substr($directory, strlen($directoryroot));
  145. $eventname = substr($file, 0, -4);
  146. $finaleventfiles[$eventname] = $location . '/' . $file;
  147. }
  148. }
  149. }
  150. }
  151. }
  152. return $finaleventfiles;
  153. }
  154. /**
  155. * This function returns an array of all events for the plugins of the system.
  156. *
  157. * @param bool $detail True will return details, but no abstract classes, False will return all events, but no details.
  158. * @return array A list of events from all plug-ins.
  159. */
  160. public static function get_non_core_event_list($detail = true) {
  161. global $CFG;
  162. // Disable developer debugging as deprecated events will fire warnings.
  163. // Setup backup variables to restore the following settings back to what they were when we are finished.
  164. $debuglevel = $CFG->debug;
  165. $debugdisplay = $CFG->debugdisplay;
  166. $debugdeveloper = $CFG->debugdeveloper;
  167. $CFG->debug = 0;
  168. $CFG->debugdisplay = false;
  169. $CFG->debugdeveloper = false;
  170. $noncorepluginlist = array();
  171. $plugintypes = \core_component::get_plugin_types();
  172. foreach ($plugintypes as $plugintype => $notused) {
  173. $pluginlist = \core_component::get_plugin_list($plugintype);
  174. foreach ($pluginlist as $plugin => $directory) {
  175. $plugindirectory = $directory . '/classes/event';
  176. foreach (self::get_file_list($plugindirectory) as $eventname => $notused) {
  177. $plugineventname = '\\' . $plugintype . '_' . $plugin . '\\event\\' . $eventname;
  178. // Check that this is actually an event.
  179. if (method_exists($plugineventname, 'get_static_info')) {
  180. if ($detail) {
  181. $ref = new \ReflectionClass($plugineventname);
  182. if (!$ref->isAbstract() && $plugintype . '_' . $plugin !== 'logstore_legacy') {
  183. $noncorepluginlist = self::format_data($noncorepluginlist, $plugineventname);
  184. }
  185. } else {
  186. $noncorepluginlist[$plugineventname] = $eventname;
  187. }
  188. }
  189. }
  190. }
  191. }
  192. // Now enable developer debugging as event information has been retrieved.
  193. $CFG->debug = $debuglevel;
  194. $CFG->debugdisplay = $debugdisplay;
  195. $CFG->debugdeveloper = $debugdeveloper;
  196. return $noncorepluginlist;
  197. }
  198. /**
  199. * Get the full list of observers for the system.
  200. *
  201. * @return array An array of observers in the system.
  202. */
  203. public static function get_observer_list() {
  204. $events = \core\event\manager::get_all_observers();
  205. foreach ($events as $key => $observers) {
  206. foreach ($observers as $observerskey => $observer) {
  207. $events[$key][$observerskey]->parentplugin =
  208. \core_plugin_manager::instance()->get_parent_of_subplugin($observer->plugintype);
  209. }
  210. }
  211. return $events;
  212. }
  213. /**
  214. * Returns the event data list section with url links and other formatting.
  215. *
  216. * @param array $eventdata The event data list section.
  217. * @param string $eventfullpath Full path to the events for this plugin / subplugin.
  218. * @return array The event data list section with additional formatting.
  219. */
  220. private static function format_data($eventdata, $eventfullpath) {
  221. // Get general event information.
  222. $eventdata[$eventfullpath] = $eventfullpath::get_static_info();
  223. // Create a link for further event detail.
  224. $url = new \moodle_url('eventdetail.php', array('eventname' => $eventfullpath));
  225. $link = \html_writer::link($url, $eventfullpath::get_name_with_info());
  226. $eventdata[$eventfullpath]['fulleventname'] = \html_writer::span($link);
  227. $eventdata[$eventfullpath]['fulleventname'] .= \html_writer::empty_tag('br');
  228. $eventdata[$eventfullpath]['fulleventname'] .= \html_writer::span($eventdata[$eventfullpath]['eventname'],
  229. 'report-eventlist-name');
  230. $eventdata[$eventfullpath]['crud'] = self::get_crud_string($eventdata[$eventfullpath]['crud']);
  231. $eventdata[$eventfullpath]['edulevel'] = self::get_edulevel_string($eventdata[$eventfullpath]['edulevel']);
  232. $eventdata[$eventfullpath]['legacyevent'] = $eventfullpath::get_legacy_eventname();
  233. // Mess around getting since information.
  234. $ref = new \ReflectionClass($eventdata[$eventfullpath]['eventname']);
  235. $eventdocbloc = $ref->getDocComment();
  236. $sincepattern = "/since\s*Moodle\s([0-9]+.[0-9]+)/i";
  237. preg_match($sincepattern, $eventdocbloc, $result);
  238. if (isset($result[1])) {
  239. $eventdata[$eventfullpath]['since'] = $result[1];
  240. } else {
  241. $eventdata[$eventfullpath]['since'] = null;
  242. }
  243. // Human readable plugin information to go with the component.
  244. $pluginstring = explode('\\', $eventfullpath);
  245. if ($pluginstring[1] !== 'core') {
  246. $component = $eventdata[$eventfullpath]['component'];
  247. $manager = get_string_manager();
  248. if ($manager->string_exists('pluginname', $pluginstring[1])) {
  249. $eventdata[$eventfullpath]['component'] = \html_writer::span(get_string('pluginname', $pluginstring[1]));
  250. }
  251. }
  252. // Raw event data to be used to sort the "Event name" column.
  253. $eventdata[$eventfullpath]['raweventname'] = $eventfullpath::get_name_with_info() . ' ' . $eventdata[$eventfullpath]['eventname'];
  254. // Unset information that is not currently required.
  255. unset($eventdata[$eventfullpath]['action']);
  256. unset($eventdata[$eventfullpath]['target']);
  257. return $eventdata;
  258. }
  259. }