2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Defines classes used for plugin info.
21 * @copyright 2011 David Mudrak <david@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace core\plugininfo
;
26 use core_plugin_manager
;
30 * Class for question behaviours.
32 class qbehaviour
extends base
{
34 public static function plugintype_supports_disabling(): bool {
39 * Finds all enabled plugins, the result may include missing plugins.
40 * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
42 public static function get_enabled_plugins() {
43 $plugins = core_plugin_manager
::instance()->get_installed_plugins('qbehaviour');
47 if ($disabled = get_config('question', 'disabledbehaviours')) {
48 $disabled = explode(',', $disabled);
54 foreach ($plugins as $plugin => $version) {
55 if (in_array($plugin, $disabled)) {
58 $enabled[$plugin] = $plugin;
64 public static function enable_plugin(string $pluginname, int $enabled): bool {
67 $oldvalue = get_config('question', 'disabledbehaviours');
68 if (!empty($oldvalue)) {
69 $plugins = array_flip(explode(',', $oldvalue));
71 // Only set visibility if it's different from the current value.
72 if ($enabled && array_key_exists($pluginname, $plugins)) {
73 unset($plugins[$pluginname]);
75 } else if (!$enabled && !array_key_exists($pluginname, $plugins)) {
76 $plugins[$pluginname] = $pluginname;
81 $new = implode(',', array_flip($plugins));
82 add_to_config_log('disabledbehaviours', $oldvalue, $new, 'question');
83 set_config('disabledbehaviours', $new, 'question');
85 \core_plugin_manager
::reset_caches();
91 public function is_uninstall_allowed() {
94 if ($this->name
=== 'missing') {
95 // qbehaviour_missing is used by the system. It cannot be uninstalled.
99 return !$DB->record_exists('question_attempts', array('behaviour' => $this->name
));
103 * Pre-uninstall hook.
105 * This is intended for disabling of plugin, some DB table purging, etc.
107 * NOTE: to be called from uninstall_plugin() only.
110 public function uninstall_cleanup() {
111 if ($disabledbehaviours = get_config('question', 'disabledbehaviours')) {
112 $disabledbehaviours = explode(',', $disabledbehaviours);
113 $disabledbehaviours = array_unique($disabledbehaviours);
115 $disabledbehaviours = array();
117 if (($key = array_search($this->name
, $disabledbehaviours)) !== false) {
118 unset($disabledbehaviours[$key]);
119 set_config('disabledbehaviours', implode(',', $disabledbehaviours), 'question');
122 if ($behaviourorder = get_config('question', 'behavioursortorder')) {
123 $behaviourorder = explode(',', $behaviourorder);
124 $behaviourorder = array_unique($behaviourorder);
126 $behaviourorder = array();
128 if (($key = array_search($this->name
, $behaviourorder)) !== false) {
129 unset($behaviourorder[$key]);
130 set_config('behavioursortorder', implode(',', $behaviourorder), 'question');
133 parent
::uninstall_cleanup();
137 * Return URL used for management of plugins of this type.
140 public static function get_manage_url() {
141 return new moodle_url('/admin/qbehaviours.php');