Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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  defined('MOODLE_INTERNAL') || die();
  29  
  30  /**
  31   * Class for availability plugins.
  32   *
  33   * @package core
  34   * @copyright 2014 The Open University
  35   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class availability extends base {
  38      public static function get_enabled_plugins() {
  39          global $DB;
  40  
  41          // Get all available plugins.
  42          $plugins = \core_plugin_manager::instance()->get_installed_plugins('availability');
  43          if (!$plugins) {
  44              return array();
  45          }
  46  
  47          // Check they are enabled using get_config (which is cached and hopefully fast).
  48          $enabled = array();
  49          foreach ($plugins as $plugin => $version) {
  50              $disabled = get_config('availability_' . $plugin, 'disabled');
  51              if (empty($disabled)) {
  52                  $enabled[$plugin] = $plugin;
  53              }
  54          }
  55  
  56          return $enabled;
  57      }
  58  
  59      public static function enable_plugin(string $pluginname, int $enabled): bool {
  60          $haschanged = false;
  61  
  62          $plugin = 'availability_' . $pluginname;
  63          $oldvalue = get_config($plugin, 'disabled');
  64          $disabled = !$enabled;
  65          // Only set value if there is no config setting or if the value is different from the previous one.
  66          if ($oldvalue == false && $disabled) {
  67              set_config('disabled', $disabled, $plugin);
  68              $haschanged = true;
  69          } else if ($oldvalue != false && !$disabled) {
  70              unset_config('disabled', $plugin);
  71              $haschanged = true;
  72          }
  73  
  74          if ($haschanged) {
  75              add_to_config_log('disabled', $oldvalue, $disabled, $plugin);
  76              \core_plugin_manager::reset_caches();
  77          }
  78  
  79          return $haschanged;
  80      }
  81  
  82      /**
  83       * Defines if there should be a way to uninstall the plugin via the administration UI.
  84       *
  85       * @return bool
  86       */
  87      public function is_uninstall_allowed() {
  88          return true;
  89      }
  90  
  91      /**
  92       * Get the name for the settings section.
  93       *
  94       * @return string
  95       */
  96      public function get_settings_section_name() {
  97          return 'availabilitysetting' . $this->name;
  98      }
  99  
 100      /**
 101       * Load the global settings for a particular availability plugin (if there are any)
 102       *
 103       * @param \part_of_admin_tree $adminroot
 104       * @param string $parentnodename
 105       * @param bool $hassiteconfig
 106       */
 107      public function load_settings(\part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
 108          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
 109          $ADMIN = $adminroot; // May be used in settings.php.
 110          $plugininfo = $this; // Also can be used inside settings.php
 111          $availability = $this; // Also to be used inside settings.php.
 112  
 113          if (!$this->is_installed_and_upgraded()) {
 114              return;
 115          }
 116  
 117          if (!$hassiteconfig) {
 118              return;
 119          }
 120  
 121          $section = $this->get_settings_section_name();
 122  
 123          $settings = null;
 124          if (file_exists($this->full_path('settings.php'))) {
 125              $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
 126              include($this->full_path('settings.php')); // This may also set $settings to null.
 127          }
 128          if ($settings) {
 129              $ADMIN->add($parentnodename, $settings);
 130          }
 131      }
 132  }