Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 400 and 403] [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   * Defines classes used for plugin info.
  19   *
  20   * @package    core
  21   * @copyright  2021 Catalyst IT Australia Pty Ltd
  22   * @author     Safat Shahin <safatshahin@catalyst-au.net>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core\plugininfo;
  27  
  28  /**
  29   * Base class for qbank plugins.
  30   *
  31   * @package    core
  32   * @copyright  2021 Catalyst IT Australia Pty Ltd
  33   * @author     Safat Shahin <safatshahin@catalyst-au.net>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class qbank extends base {
  37  
  38      public static function plugintype_supports_disabling(): bool {
  39          return true;
  40      }
  41  
  42      public function is_uninstall_allowed(): bool {
  43          if (in_array($this->name, \core_plugin_manager::standard_plugins_list('qbank'))) {
  44              return false;
  45          }
  46          return true;
  47      }
  48  
  49      public static function get_manage_url(): \moodle_url {
  50          return new \moodle_url('/admin/settings.php', ['section' => 'manageqbanks']);
  51      }
  52  
  53      public function get_settings_section_name() {
  54          return $this->type . '_' . $this->name;
  55      }
  56  
  57      public static function get_plugins($type, $typerootdir, $typeclass, $pluginman): array {
  58          global $CFG;
  59  
  60          $qbank = parent::get_plugins($type, $typerootdir, $typeclass, $pluginman);
  61          $order = array_keys($qbank);
  62          $sortedqbanks = [];
  63          foreach ($order as $qbankname) {
  64              $sortedqbanks[$qbankname] = $qbank[$qbankname];
  65          }
  66          return $sortedqbanks;
  67      }
  68  
  69      public static function get_enabled_plugins(): ?array {
  70          global $CFG;
  71          $pluginmanager = \core_plugin_manager::instance();
  72          $plugins = $pluginmanager->get_installed_plugins('qbank');
  73  
  74          if (!$plugins) {
  75              return [];
  76          }
  77  
  78          $plugins = array_keys($plugins);
  79  
  80          // Filter to return only enabled plugins.
  81          $enabled = [];
  82          foreach ($plugins as $plugin) {
  83              $qbankinfo = $pluginmanager->get_plugin_info('qbank_'.$plugin);
  84              $qbankavailable = $qbankinfo->get_status();
  85              if ($qbankavailable === \core_plugin_manager::PLUGIN_STATUS_MISSING) {
  86                  continue;
  87              }
  88              $disabled = get_config('qbank_' . $plugin, 'disabled');
  89              if (empty($disabled)) {
  90                  $enabled[$plugin] = $plugin;
  91              }
  92          }
  93          return $enabled;
  94      }
  95  
  96      public static function enable_plugin(string $pluginname, int $enabled): bool {
  97          $haschanged = false;
  98  
  99          $plugin = 'qbank_' . $pluginname;
 100          $oldvalue = get_config($plugin, 'disabled');
 101          $disabled = !$enabled;
 102          // Only set value if there is no config setting or if the value is different from the previous one.
 103          if ($oldvalue == false && $disabled) {
 104              set_config('disabled', $disabled, $plugin);
 105              $haschanged = true;
 106          } else if ($oldvalue != false && !$disabled) {
 107              unset_config('disabled', $plugin);
 108              $haschanged = true;
 109          }
 110  
 111          if ($haschanged) {
 112              add_to_config_log('disabled', $oldvalue, $disabled, $plugin);
 113              \core_plugin_manager::reset_caches();
 114          }
 115  
 116          return $haschanged;
 117      }
 118  
 119      /**
 120       * Checks if a qbank plugin is ready to be used.
 121       * It checks the plugin status as well as the plugin is missing or not.
 122       *
 123       * @param string $fullpluginname the name of the plugin
 124       * @return bool
 125       */
 126      public static function is_plugin_enabled($fullpluginname): bool {
 127          $pluginmanager = \core_plugin_manager::instance();
 128          $qbankinfo = $pluginmanager->get_plugin_info($fullpluginname);
 129          if (empty($qbankinfo)) {
 130              return false;
 131          }
 132          $qbankavailable = $qbankinfo->get_status();
 133          if ($qbankavailable === \core_plugin_manager::PLUGIN_STATUS_MISSING ||
 134                  !empty(get_config($fullpluginname, 'disabled'))) {
 135              return false;
 136          }
 137          return true;
 138      }
 139  
 140      public function load_settings(\part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig): void {
 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  
 146          if (!$this->is_installed_and_upgraded()) {
 147              return;
 148          }
 149  
 150          if (!$hassiteconfig) {
 151              return;
 152          }
 153  
 154          $section = $this->get_settings_section_name();
 155  
 156          $settings = null;
 157          if (file_exists($this->full_path('settings.php'))) {
 158              if ($this->name !== 'columnsortorder') {
 159                  $settings = new \admin_settingpage($section, $this->displayname,
 160                                      'moodle/site:config', $this->is_enabled() === false);
 161              }
 162              include($this->full_path('settings.php')); // This may also set $settings to null.
 163          }
 164          if ($settings) {
 165              $ADMIN->add($parentnodename, $settings);
 166          }
 167      }
 168  }