Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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