Skip to content

Commit

Permalink
MDL-72500 report_eventlist: group and sort list of components.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulholden committed Sep 13, 2021
1 parent 1d70079 commit a9f3cf6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
38 changes: 37 additions & 1 deletion report/eventlist/classes/filter_form.php
Expand Up @@ -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);

Expand All @@ -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;
}
}
4 changes: 2 additions & 2 deletions report/eventlist/index.php
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions report/eventlist/lang/en/report_eventlist.php
Expand Up @@ -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';
Expand Down

0 comments on commit a9f3cf6

Please sign in to comment.