Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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      /**
  86       * Defines if there should be a way to uninstall the plugin via the administration UI.
  87       *
  88       * @return bool
  89       */
  90      public function is_uninstall_allowed() {
  91          return true;
  92      }
  93  
  94      /**
  95       * Get the name for the settings section.
  96       *
  97       * @return string
  98       */
  99      public function get_settings_section_name() {
 100          return 'availabilitysetting' . $this->name;
 101      }
 102  
 103      /**
 104       * Load the global settings for a particular availability plugin (if there are any)
 105       *
 106       * @param \part_of_admin_tree $adminroot
 107       * @param string $parentnodename
 108       * @param bool $hassiteconfig
 109       */
 110      public function load_settings(\part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
 111          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
 112          /** @var \admin_root $ADMIN */
 113          $ADMIN = $adminroot; // May be used in settings.php.
 114          $plugininfo = $this; // Also can be used inside settings.php
 115          $availability = $this; // Also to be used inside settings.php.
 116  
 117          if (!$this->is_installed_and_upgraded()) {
 118              return;
 119          }
 120  
 121          if (!$hassiteconfig) {
 122              return;
 123          }
 124  
 125          $section = $this->get_settings_section_name();
 126  
 127          $settings = null;
 128          if (file_exists($this->full_path('settings.php'))) {
 129              $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
 130              include($this->full_path('settings.php')); // This may also set $settings to null.
 131          }
 132          if ($settings) {
 133              $ADMIN->add($parentnodename, $settings);
 134          }
 135      }
 136  }