weekly back-to-dev release 5.0dev
[moodle.git] / lib / classes / plugininfo / availability.php
blobe4a60cce4989021282acd31cbf2040951c8b8540
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 * Class for availability plugins.
20 * @package core
21 * @copyright 2014 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace core\plugininfo;
26 use admin_settingpage;
28 /**
29 * Class for availability plugins.
31 * @package core
32 * @copyright 2014 The Open University
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class availability extends base {
37 public static function plugintype_supports_disabling(): bool {
38 return true;
41 public static function get_enabled_plugins() {
42 global $DB;
44 // Get all available plugins.
45 $plugins = \core_plugin_manager::instance()->get_installed_plugins('availability');
46 if (!$plugins) {
47 return array();
50 // Check they are enabled using get_config (which is cached and hopefully fast).
51 $enabled = array();
52 foreach ($plugins as $plugin => $version) {
53 $disabled = get_config('availability_' . $plugin, 'disabled');
54 if (empty($disabled)) {
55 $enabled[$plugin] = $plugin;
59 return $enabled;
62 public static function enable_plugin(string $pluginname, int $enabled): bool {
63 $haschanged = false;
65 $plugin = 'availability_' . $pluginname;
66 $oldvalue = get_config($plugin, 'disabled');
67 $disabled = !$enabled;
68 // Only set value if there is no config setting or if the value is different from the previous one.
69 if ($oldvalue == false && $disabled) {
70 set_config('disabled', $disabled, $plugin);
71 $haschanged = true;
72 } else if ($oldvalue != false && !$disabled) {
73 unset_config('disabled', $plugin);
74 $haschanged = true;
77 if ($haschanged) {
78 add_to_config_log('disabled', $oldvalue, $disabled, $plugin);
79 \core_plugin_manager::reset_caches();
82 return $haschanged;
85 /**
86 * Update the display mode for a specific plugin based on `$displaymode` parameter.
88 * @param string $pluginname The plugin name.
89 * @param bool $displaymode whether the eye icon of display mode is enabled or disabled.
90 * @return bool Returns true if the configuration has been changed, false otherwise.
92 public static function update_display_mode(string $pluginname, bool $displaymode): bool {
93 $haschanged = false;
95 $plugin = 'availability_' . $pluginname;
96 $oldvalue = get_config($plugin, 'defaultdisplaymode');
97 $disabled = !$displaymode;
98 // Only set value if there is no config setting or if the value is different from the previous one.
99 if ($oldvalue == false && $disabled) {
100 set_config('defaultdisplaymode', $disabled, $plugin);
101 $haschanged = true;
102 } else if ($oldvalue != false && !$disabled) {
103 unset_config('defaultdisplaymode', $plugin);
104 $haschanged = true;
107 if ($haschanged) {
108 add_to_config_log('defaultdisplaymode', $oldvalue, $disabled, $plugin);
109 \core_plugin_manager::reset_caches();
112 return $haschanged;
116 * Defines if there should be a way to uninstall the plugin via the administration UI.
118 * @return bool
120 public function is_uninstall_allowed() {
121 return true;
125 * Get the name for the settings section.
127 * @return string
129 public function get_settings_section_name() {
130 return 'availabilitysetting' . $this->name;
134 * Load the global settings for a particular availability plugin (if there are any)
136 * @param \part_of_admin_tree $adminroot
137 * @param string $parentnodename
138 * @param bool $hassiteconfig
140 public function load_settings(\part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
141 global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
142 /** @var \admin_root $ADMIN */
143 $ADMIN = $adminroot; // May be used in settings.php.
144 $plugininfo = $this; // Also can be used inside settings.php
145 $availability = $this; // Also to be used inside settings.php.
147 if (!$this->is_installed_and_upgraded()) {
148 return;
151 if (!$hassiteconfig) {
152 return;
155 $section = $this->get_settings_section_name();
157 $settings = null;
158 if (file_exists($this->full_path('settings.php'))) {
159 $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
160 include($this->full_path('settings.php')); // This may also set $settings to null.
162 if ($settings) {
163 $ADMIN->add($parentnodename, $settings);