From a9f3cf6d75f5ad3cc27b451847784720dd1cddd9 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Mon, 6 Sep 2021 11:31:53 +0100 Subject: [PATCH] MDL-72500 report_eventlist: group and sort list of components. --- report/eventlist/classes/filter_form.php | 38 ++++++++++++++++++- report/eventlist/index.php | 4 +- report/eventlist/lang/en/report_eventlist.php | 2 + 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/report/eventlist/classes/filter_form.php b/report/eventlist/classes/filter_form.php index a7ebdc429fca4..616950294ff34 100644 --- a/report/eventlist/classes/filter_form.php +++ b/report/eventlist/classes/filter_form.php @@ -43,7 +43,8 @@ public function definition() { $mform->addElement('text', 'eventname', get_string('name', 'report_eventlist')); $mform->setType('eventname', PARAM_RAW); - $mform->addElement('select', 'eventcomponent', get_string('component', 'report_eventlist'), $componentarray); + $mform->addElement('selectgroups', 'eventcomponent', get_string('component', 'report_eventlist'), + self::group_components_by_type($componentarray)); $mform->addElement('select', 'eventedulevel', get_string('edulevel', 'report_eventlist'), $edulevelarray); $mform->addElement('select', 'eventcrud', get_string('crud', 'report_eventlist'), $crudarray); @@ -52,4 +53,39 @@ public function definition() { $buttonarray[] = $mform->createElement('button', 'clearbutton', get_string('clear', 'report_eventlist')); $mform->addGroup($buttonarray, 'filterbuttons', '', array(' '), false); } + + /** + * Group list of component names by type for use in grouped select element + * + * @param string[] $components + * @return array[] Component type => [...Components] + */ + private static function group_components_by_type(array $components): array { + $pluginmanager = core_plugin_manager::instance(); + + $result = []; + foreach ($components as $component) { + // Core sub-systems are grouped together and are denoted by a distinct lang string. + if (strpos($component, 'core') === 0) { + $componenttype = get_string('core', 'report_eventlist'); + $componentname = get_string('coresubsystem', 'report_eventlist', $component); + } else { + [$type] = core_component::normalize_component($component); + $componenttype = $pluginmanager->plugintype_name_plural($type); + $componentname = $pluginmanager->plugin_name($component); + } + + $result[$componenttype][$component] = $componentname; + } + + // Sort returned components according to their type, followed by name. + core_collator::ksort($result); + array_walk($result, function(array &$componenttype) { + core_collator::asort($componenttype); + }); + + // Prepend "All" option. + array_unshift($result, [0 => get_string('all', 'report_eventlist')]); + return $result; + } } diff --git a/report/eventlist/index.php b/report/eventlist/index.php index 9d1e40c4a39d6..3474f85b74dad 100644 --- a/report/eventlist/index.php +++ b/report/eventlist/index.php @@ -30,11 +30,11 @@ $completelist = report_eventlist_list_generator::get_all_events_list(); $tabledata = array(); -$components = array('0' => get_string('all', 'report_eventlist')); +$components = array(); $edulevel = array('0' => get_string('all', 'report_eventlist')); $crud = array('0' => get_string('all', 'report_eventlist')); foreach ($completelist as $value) { - $components[] = $value['component']; + $components[] = explode('\\', $value['eventname'])[1]; $edulevel[] = $value['edulevel']; $crud[] = $value['crud']; $tabledata[] = (object)$value; diff --git a/report/eventlist/lang/en/report_eventlist.php b/report/eventlist/lang/en/report_eventlist.php index 892ea16c45206..edff5f308ce5f 100644 --- a/report/eventlist/lang/en/report_eventlist.php +++ b/report/eventlist/lang/en/report_eventlist.php @@ -28,6 +28,8 @@ $string['all'] = 'All'; $string['clear'] = 'Clear'; $string['component'] = 'Component'; +$string['core'] = 'Core'; +$string['coresubsystem'] = 'Subsystem ({$a})'; $string['create'] = 'create'; $string['crud'] = 'Database query type'; $string['delete'] = 'delete';