weekly release 4.5dev
[moodle.git] / lib / classes / plugininfo / qbehaviour.php
blob2b1fdf2316f8393925f707451b94effd63170bbc
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * Defines classes used for plugin info.
20 * @package core
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;
27 use moodle_url;
29 /**
30 * Class for question behaviours.
32 class qbehaviour extends base {
34 public static function plugintype_supports_disabling(): bool {
35 return true;
38 /**
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');
44 if (!$plugins) {
45 return array();
47 if ($disabled = get_config('question', 'disabledbehaviours')) {
48 $disabled = explode(',', $disabled);
49 } else {
50 $disabled = array();
53 $enabled = array();
54 foreach ($plugins as $plugin => $version) {
55 if (in_array($plugin, $disabled)) {
56 continue;
58 $enabled[$plugin] = $plugin;
61 return $enabled;
64 public static function enable_plugin(string $pluginname, int $enabled): bool {
65 $haschanged = false;
66 $plugins = [];
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]);
74 $haschanged = true;
75 } else if (!$enabled && !array_key_exists($pluginname, $plugins)) {
76 $plugins[$pluginname] = $pluginname;
77 $haschanged = true;
80 if ($haschanged) {
81 $new = implode(',', array_flip($plugins));
82 add_to_config_log('disabledbehaviours', $oldvalue, $new, 'question');
83 set_config('disabledbehaviours', $new, 'question');
84 // Reset caches.
85 \core_plugin_manager::reset_caches();
88 return $haschanged;
91 public function is_uninstall_allowed() {
92 global $DB;
94 if ($this->name === 'missing') {
95 // qbehaviour_missing is used by the system. It cannot be uninstalled.
96 return false;
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.
108 * @private
110 public function uninstall_cleanup() {
111 if ($disabledbehaviours = get_config('question', 'disabledbehaviours')) {
112 $disabledbehaviours = explode(',', $disabledbehaviours);
113 $disabledbehaviours = array_unique($disabledbehaviours);
114 } else {
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);
125 } else {
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.
138 * @return moodle_url
140 public static function get_manage_url() {
141 return new moodle_url('/admin/qbehaviours.php');