Skip to content

Commit

Permalink
MDL-72720 qbehaviour: Implement enable_plugin() method
Browse files Browse the repository at this point in the history
  • Loading branch information
sarjona committed Oct 15, 2021
1 parent 5009021 commit a22093d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
11 changes: 4 additions & 7 deletions admin/qbehaviours.php
Expand Up @@ -88,10 +88,9 @@
}

if (array_search($disable, $disabledbehaviours) === false) {
$disabledbehaviours[] = $disable;
set_config('disabledbehaviours', implode(',', $disabledbehaviours), 'question');
$class = \core_plugin_manager::resolve_plugininfo_class('qbehaviour');
$class::enable_plugin($disable, false);
}
core_plugin_manager::reset_caches();
redirect($thispageurl);
}

Expand All @@ -106,10 +105,9 @@
}

if (($key = array_search($enable, $disabledbehaviours)) !== false) {
unset($disabledbehaviours[$key]);
set_config('disabledbehaviours', implode(',', $disabledbehaviours), 'question');
$class = \core_plugin_manager::resolve_plugininfo_class('qbehaviour');
$class::enable_plugin($enable, true);
}
core_plugin_manager::reset_caches();
redirect($thispageurl);
}

Expand Down Expand Up @@ -238,4 +236,3 @@ function question_behaviour_icon_html($action, $behaviour, $icon, $alt, $tip) {
new pix_icon($icon, $alt, 'moodle', array('title' => '', 'class' => 'iconsmall')),
null, array('title' => $tip));
}

28 changes: 27 additions & 1 deletion lib/classes/plugininfo/qbehaviour.php
Expand Up @@ -57,6 +57,33 @@ public static function get_enabled_plugins() {
return $enabled;
}

public static function enable_plugin(string $pluginname, int $enabled): bool {
$haschanged = false;
$plugins = [];
$oldvalue = get_config('question', 'disabledbehaviours');
if (!empty($oldvalue)) {
$plugins = array_flip(explode(',', $oldvalue));
}
// Only set visibility if it's different from the current value.
if ($enabled && array_key_exists($pluginname, $plugins)) {
unset($plugins[$pluginname]);
$haschanged = true;
} else if (!$enabled && !array_key_exists($pluginname, $plugins)) {
$plugins[$pluginname] = $pluginname;
$haschanged = true;
}

if ($haschanged) {
$new = implode(',', array_flip($plugins));
add_to_config_log('disabledbehaviours', $oldvalue, $new, 'question');
set_config('disabledbehaviours', $new, 'question');
// Reset caches.
\core_plugin_manager::reset_caches();
}

return $haschanged;
}

public function is_uninstall_allowed() {
global $DB;

Expand Down Expand Up @@ -110,4 +137,3 @@ public static function get_manage_url() {
return new moodle_url('/admin/qbehaviours.php');
}
}

0 comments on commit a22093d

Please sign in to comment.