| 1 | efrain | 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.
 | 
        
           |  |  | 13 | //
 | 
        
           |  |  | 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/>.
 | 
        
           |  |  | 16 |   | 
        
           |  |  | 17 | /**
 | 
        
           |  |  | 18 |  * Class for availability plugins.
 | 
        
           |  |  | 19 |  *
 | 
        
           |  |  | 20 |  * @package core
 | 
        
           |  |  | 21 |  * @copyright 2014 The Open University
 | 
        
           |  |  | 22 |  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 23 |  */
 | 
        
           |  |  | 24 | namespace core\plugininfo;
 | 
        
           |  |  | 25 |   | 
        
           |  |  | 26 | use admin_settingpage;
 | 
        
           |  |  | 27 |   | 
        
           |  |  | 28 | /**
 | 
        
           |  |  | 29 |  * Class for availability plugins.
 | 
        
           |  |  | 30 |  *
 | 
        
           |  |  | 31 |  * @package core
 | 
        
           |  |  | 32 |  * @copyright 2014 The Open University
 | 
        
           |  |  | 33 |  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 34 |  */
 | 
        
           |  |  | 35 | class availability extends base {
 | 
        
           |  |  | 36 |   | 
        
           |  |  | 37 |     public static function plugintype_supports_disabling(): bool {
 | 
        
           |  |  | 38 |         return true;
 | 
        
           |  |  | 39 |     }
 | 
        
           |  |  | 40 |   | 
        
           |  |  | 41 |     public static function get_enabled_plugins() {
 | 
        
           |  |  | 42 |         global $DB;
 | 
        
           |  |  | 43 |   | 
        
           |  |  | 44 |         // Get all available plugins.
 | 
        
           |  |  | 45 |         $plugins = \core_plugin_manager::instance()->get_installed_plugins('availability');
 | 
        
           |  |  | 46 |         if (!$plugins) {
 | 
        
           |  |  | 47 |             return array();
 | 
        
           |  |  | 48 |         }
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 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;
 | 
        
           |  |  | 56 |             }
 | 
        
           |  |  | 57 |         }
 | 
        
           |  |  | 58 |   | 
        
           |  |  | 59 |         return $enabled;
 | 
        
           |  |  | 60 |     }
 | 
        
           |  |  | 61 |   | 
        
           |  |  | 62 |     public static function enable_plugin(string $pluginname, int $enabled): bool {
 | 
        
           |  |  | 63 |         $haschanged = false;
 | 
        
           |  |  | 64 |   | 
        
           |  |  | 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;
 | 
        
           |  |  | 75 |         }
 | 
        
           |  |  | 76 |   | 
        
           |  |  | 77 |         if ($haschanged) {
 | 
        
           |  |  | 78 |             add_to_config_log('disabled', $oldvalue, $disabled, $plugin);
 | 
        
           |  |  | 79 |             \core_plugin_manager::reset_caches();
 | 
        
           |  |  | 80 |         }
 | 
        
           |  |  | 81 |   | 
        
           |  |  | 82 |         return $haschanged;
 | 
        
           |  |  | 83 |     }
 | 
        
           |  |  | 84 |   | 
        
           |  |  | 85 |     /**
 | 
        
           | 1441 | ariadna | 86 |      * Update the display mode for a specific plugin based on `$displaymode` parameter.
 | 
        
           |  |  | 87 |      *
 | 
        
           |  |  | 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.
 | 
        
           |  |  | 91 |      */
 | 
        
           |  |  | 92 |     public static function update_display_mode(string $pluginname, bool $displaymode): bool {
 | 
        
           |  |  | 93 |         $haschanged = false;
 | 
        
           |  |  | 94 |   | 
        
           |  |  | 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;
 | 
        
           |  |  | 105 |         }
 | 
        
           |  |  | 106 |   | 
        
           |  |  | 107 |         if ($haschanged) {
 | 
        
           |  |  | 108 |             add_to_config_log('defaultdisplaymode', $oldvalue, $disabled, $plugin);
 | 
        
           |  |  | 109 |             \core_plugin_manager::reset_caches();
 | 
        
           |  |  | 110 |         }
 | 
        
           |  |  | 111 |   | 
        
           |  |  | 112 |         return $haschanged;
 | 
        
           |  |  | 113 |     }
 | 
        
           |  |  | 114 |   | 
        
           |  |  | 115 |     /**
 | 
        
           | 1 | efrain | 116 |      * Defines if there should be a way to uninstall the plugin via the administration UI.
 | 
        
           |  |  | 117 |      *
 | 
        
           |  |  | 118 |      * @return bool
 | 
        
           |  |  | 119 |      */
 | 
        
           |  |  | 120 |     public function is_uninstall_allowed() {
 | 
        
           |  |  | 121 |         return true;
 | 
        
           |  |  | 122 |     }
 | 
        
           |  |  | 123 |   | 
        
           |  |  | 124 |     /**
 | 
        
           |  |  | 125 |      * Get the name for the settings section.
 | 
        
           |  |  | 126 |      *
 | 
        
           |  |  | 127 |      * @return string
 | 
        
           |  |  | 128 |      */
 | 
        
           |  |  | 129 |     public function get_settings_section_name() {
 | 
        
           |  |  | 130 |         return 'availabilitysetting' . $this->name;
 | 
        
           |  |  | 131 |     }
 | 
        
           |  |  | 132 |   | 
        
           |  |  | 133 |     /**
 | 
        
           |  |  | 134 |      * Load the global settings for a particular availability plugin (if there are any)
 | 
        
           |  |  | 135 |      *
 | 
        
           |  |  | 136 |      * @param \part_of_admin_tree $adminroot
 | 
        
           |  |  | 137 |      * @param string $parentnodename
 | 
        
           |  |  | 138 |      * @param bool $hassiteconfig
 | 
        
           |  |  | 139 |      */
 | 
        
           |  |  | 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.
 | 
        
           |  |  | 146 |   | 
        
           |  |  | 147 |         if (!$this->is_installed_and_upgraded()) {
 | 
        
           |  |  | 148 |             return;
 | 
        
           |  |  | 149 |         }
 | 
        
           |  |  | 150 |   | 
        
           |  |  | 151 |         if (!$hassiteconfig) {
 | 
        
           |  |  | 152 |             return;
 | 
        
           |  |  | 153 |         }
 | 
        
           |  |  | 154 |   | 
        
           |  |  | 155 |         $section = $this->get_settings_section_name();
 | 
        
           |  |  | 156 |   | 
        
           |  |  | 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.
 | 
        
           |  |  | 161 |         }
 | 
        
           |  |  | 162 |         if ($settings) {
 | 
        
           |  |  | 163 |             $ADMIN->add($parentnodename, $settings);
 | 
        
           |  |  | 164 |         }
 | 
        
           |  |  | 165 |     }
 | 
        
           |  |  | 166 | }
 |