MDL-72720 media: Implement enable_plugin() method

This commit is contained in:
Sara Arjona 2021-10-04 15:32:48 +02:00
parent 98bee59968
commit 0543ea7f02
2 changed files with 49 additions and 30 deletions

View File

@ -49,11 +49,13 @@ if (!array_key_exists($media, $plugins)) {
switch ($action) {
case 'disable':
$plugins[$media]->set_enabled(false);
$class = \core_plugin_manager::resolve_plugininfo_class('media');
$class::enable_plugin($media, false);
break;
case 'enable':
$plugins[$media]->set_enabled(true);
$class = \core_plugin_manager::resolve_plugininfo_class('media');
$class::enable_plugin($media, true);
break;
case 'up':

View File

@ -99,40 +99,57 @@ class media extends base {
return array_combine($order, $order);
}
public static function enable_plugin(string $pluginname, int $enabled): bool {
global $CFG;
$haschanged = false;
$plugins = [];
if (!empty($CFG->media_plugins_sortorder)) {
$plugins = explode(',', $CFG->media_plugins_sortorder);
}
// Only set visibility if it's different from the current value.
if ($enabled && !in_array($pluginname, $plugins)) {
// Enable media plugin.
$pluginsbytype = \core_plugin_manager::instance()->get_plugins_of_type('media');
if (!array_key_exists($pluginname, $pluginsbytype)) {
// Can not be enabled.
return false;
}
$rank = $pluginsbytype[$pluginname]->get_rank();
$position = 0;
// Insert before the first enabled plugin which default rank is smaller than the default rank of this one.
foreach ($plugins as $playername) {
if (($player = $pluginsbytype[$playername]) && ($rank > $player->get_rank())) {
break;
}
$position++;
}
array_splice($plugins, $position, 0, [$pluginname]);
$haschanged = true;
} else if (!$enabled && in_array($pluginname, $plugins)) {
// Disable media plugin.
$key = array_search($pluginname, $plugins);
unset($plugins[$key]);
$haschanged = true;
}
if ($haschanged) {
add_to_config_log('media_plugins_sortorder', !$enabled, $enabled, $pluginname);
self::set_enabled_plugins($plugins);
}
return $haschanged;
}
/**
* Sets the current plugin as enabled or disabled
* When enabling tries to guess the sortorder based on default rank returned by the plugin.
* @param bool $newstate
*/
public function set_enabled($newstate = true) {
$enabled = self::get_enabled_plugins();
if (array_key_exists($this->name, $enabled) == $newstate) {
// Nothing to do.
return;
}
if ($newstate) {
// Enable media plugin.
$plugins = \core_plugin_manager::instance()->get_plugins_of_type('media');
if (!array_key_exists($this->name, $plugins)) {
// Can not be enabled.
return;
}
$rank = $this->get_rank();
$position = 0;
// Insert before the first enabled plugin which default rank is smaller than the default rank of this one.
foreach ($enabled as $playername) {
if (($player = $plugins[$playername]) && ($rank > $player->get_rank())) {
break;
}
$position++;
}
array_splice($enabled, $position, 0, [$this->name]);
self::set_enabled_plugins($enabled);
} else {
// Disable media plugin.
unset($enabled[$this->name]);
self::set_enabled_plugins($enabled);
}
self::enable_plugin($this->name, $newstate);
}
/**