Skip to content

Commit c2f883a

Browse files
committed
MDL-83766 mod_bigbluebuttonbn: Add subplugin sort
* Change sort from default alphabetical to order in manage extension list order
1 parent 7a318d5 commit c2f883a

File tree

3 files changed

+81
-19
lines changed

3 files changed

+81
-19
lines changed

mod/bigbluebuttonbn/classes/extension.php

+33-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use mod_bigbluebuttonbn\local\extension\mod_instance_helper;
2525
use stdClass;
2626
use core_plugin_manager;
27+
use core_component;
2728

2829
/**
2930
* Generic subplugin management helper
@@ -80,8 +81,7 @@ public static function action_url_addons(
8081
*/
8182
protected static function get_instances_implementing(string $classname, ?array $newparameters = []): array {
8283
$classes = self::get_classes_implementing($classname);
83-
sort($classes); // Make sure all extension classes are returned in the same order. This is arbitrarily in
84-
// alphabetical order and depends on the classname but this one way to ensure consistency across calls.
84+
ksort($classes); // Make sure all extension classes are returned in the correct order.
8585
return array_map(function($targetclassname) use ($newparameters) {
8686
// If $newparameters is null, the constructor will be called without parameters.
8787
return new $targetclassname(...$newparameters);
@@ -100,6 +100,9 @@ protected static function get_classes_implementing(string $classname): array {
100100
$classbasename = end($classnamecomponents);
101101
$allsubs = core_plugin_manager::instance()->get_plugins_of_type(self::BBB_EXTENSION_PLUGIN_NAME);
102102
$extensionclasses = [];
103+
$names = core_component::get_plugin_list(self::BBB_EXTENSION_PLUGIN_NAME);
104+
$sortedlist = self::get_sorted_plugins_list($names); // Make sure to use the most updated list.
105+
$sortedlist = array_flip($sortedlist);
103106
foreach ($allsubs as $sub) {
104107
if (!$sub->is_enabled()) {
105108
continue;
@@ -112,11 +115,38 @@ protected static function get_classes_implementing(string $classname): array {
112115
debugging("The class $targetclassname should extend $classname in the subplugin {$sub->name}. Ignoring.");
113116
continue;
114117
}
115-
$extensionclasses[] = $targetclassname;
118+
if (!isset($sortedlist[$sub->name])) {
119+
debugging("The class $targetclassname does not belong to an existing subplugin. Ignoring");
120+
continue;
121+
}
122+
// Return all extension classes based on subplugin order on manage extension page.
123+
$sortorder = $sortedlist[$sub->name];
124+
$extensionclasses[$sortorder] = $targetclassname;
116125
}
117126
return $extensionclasses;
118127
}
119128

129+
/**
130+
* Return plugin list sorted according to order from admin extension manager.
131+
* @param array $names Array of plugin names
132+
* @return array The sorted list of plugins
133+
*/
134+
public static function get_sorted_plugins_list(array $names): array {
135+
$result = [];
136+
foreach ($names as $name => $path) {
137+
$idx = get_config(self::BBB_EXTENSION_PLUGIN_NAME . '_' . $name, 'sortorder');
138+
if (!$idx) {
139+
$idx = 0;
140+
}
141+
while (array_key_exists($idx, $result)) {
142+
$idx += 1;
143+
}
144+
$result[$idx] = $name;
145+
}
146+
ksort($result);
147+
return $result;
148+
}
149+
120150
/**
121151
* Get all custom_completion addons classes.
122152
*

mod/bigbluebuttonbn/classes/local/plugins/admin_plugin_manager.php

+1-16
Original file line numberDiff line numberDiff line change
@@ -191,22 +191,7 @@ private function print_header(): void {
191191
*/
192192
public function get_sorted_plugins_list(): array {
193193
$names = core_component::get_plugin_list(extension::BBB_EXTENSION_PLUGIN_NAME);
194-
195-
$result = [];
196-
197-
foreach ($names as $name => $path) {
198-
$idx = get_config(extension::BBB_EXTENSION_PLUGIN_NAME . '_' . $name, 'sortorder');
199-
if (!$idx) {
200-
$idx = 0;
201-
}
202-
while (array_key_exists($idx, $result)) {
203-
$idx += 1;
204-
}
205-
$result[$idx] = $name;
206-
}
207-
ksort($result);
208-
209-
return $result;
194+
return extension::get_sorted_plugins_list($names);
210195
}
211196

212197
/**

mod/bigbluebuttonbn/tests/local/extension_test.php

+47
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,53 @@ public static function classes_implementing_class(): array {
440440
];
441441
}
442442

443+
/**
444+
* Test the get_sorted_plugins_list
445+
* @param array $sortorders
446+
* @param array $expected
447+
* @return void
448+
* @dataProvider sorted_plugins_list_data_provider
449+
* @covers \mod_bigbluebuttonbn\extension::get_sorted_plugins_list
450+
*/
451+
public function test_get_sorted_plugins_list(array $sortorders, array $expected): void {
452+
$this->resetAfterTest();
453+
// Enable plugin.
454+
$this->enable_plugins(true);
455+
// Create list of plugins we will then sort.
456+
$pluginlist = [
457+
'simpleone' => '/path/to/simpleone',
458+
'simpletwo' => '/path/to/simpletwo',
459+
];
460+
// Set sortorder.
461+
foreach ($sortorders as $plugin => $sortorder) {
462+
set_config('sortorder', $sortorder, 'bbbext_' . $plugin);
463+
}
464+
$sortedlist = extension::get_sorted_plugins_list($pluginlist);
465+
$this->assertSame($expected, $sortedlist);
466+
}
467+
468+
/**
469+
* Data provider for testing get_sorted_plugins_list
470+
*
471+
* @return array[]
472+
*/
473+
public static function sorted_plugins_list_data_provider(): array {
474+
return [
475+
'no sortorder' => [
476+
[],
477+
['simpleone', 'simpletwo'],
478+
],
479+
'default sortorder' => [
480+
['simpleone' => 0, 'simpletwo' => 1],
481+
['simpleone', 'simpletwo'],
482+
],
483+
'changed sortorder' => [
484+
['simpleone' => 1, 'simpletwo' => 0],
485+
['simpletwo', 'simpleone'],
486+
],
487+
];
488+
}
489+
443490
/**
444491
* Enable plugins
445492
*

0 commit comments

Comments
 (0)