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   * Defines classes used for plugin info.
  19   *
  20   * @package    core
  21   * @copyright  2011 David Mudrak <david@moodle.com>
  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  use core_plugin_manager;
  28  use moodle_url;
  29  use part_of_admin_tree;
  30  
  31  /**
  32   * Class for question types
  33   */
  34  class qtype extends base {
  35  
  36      public static function plugintype_supports_disabling(): bool {
  37          return true;
  38      }
  39  
  40      /**
  41       * Finds all enabled plugins, the result may include missing plugins.
  42       * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
  43       */
  44      public static function get_enabled_plugins() {
  45          global $DB;
  46  
  47          $plugins = core_plugin_manager::instance()->get_installed_plugins('qtype');
  48          if (!$plugins) {
  49              return array();
  50          }
  51          $installed = array();
  52          foreach ($plugins as $plugin => $version) {
  53              $installed[] = $plugin.'_disabled';
  54          }
  55  
  56          list($installed, $params) = $DB->get_in_or_equal($installed, SQL_PARAMS_NAMED);
  57          $disabled = $DB->get_records_select('config_plugins', "name $installed AND plugin = 'question'", $params, 'plugin ASC');
  58          foreach ($disabled as $conf) {
  59              if (empty($conf->value)) {
  60                  continue;
  61              }
  62              $name = substr($conf->name, 0, -9);
  63              unset($plugins[$name]);
  64          }
  65  
  66          $enabled = array();
  67          foreach ($plugins as $plugin => $version) {
  68              $enabled[$plugin] = $plugin;
  69          }
  70  
  71          return $enabled;
  72      }
  73  
  74      public static function enable_plugin(string $pluginname, int $enabled): bool {
  75          $haschanged = false;
  76  
  77          $settingname = $pluginname . '_disabled';
  78          $oldvalue = get_config('question', $settingname);
  79          $disabled = !$enabled;
  80          // Only set value if there is no config setting or if the value is different from the previous one.
  81          if ($oldvalue == false && $disabled) {
  82              set_config($settingname, $disabled, 'question');
  83              $haschanged = true;
  84          } else if ($oldvalue != false && !$disabled) {
  85              unset_config($settingname, 'question');
  86              $haschanged = true;
  87          }
  88  
  89          if ($haschanged) {
  90              add_to_config_log($settingname, $oldvalue, $disabled, 'question');
  91              \core_plugin_manager::reset_caches();
  92          }
  93  
  94          return $haschanged;
  95      }
  96  
  97      public function is_uninstall_allowed() {
  98          global $DB;
  99  
 100          if ($this->name === 'missingtype') {
 101              // qtype_missingtype is used by the system. It cannot be uninstalled.
 102              return false;
 103          }
 104  
 105          return !$DB->record_exists('question', array('qtype' => $this->name));
 106      }
 107  
 108      /**
 109       * Return URL used for management of plugins of this type.
 110       * @return moodle_url
 111       */
 112      public static function get_manage_url() {
 113          return new moodle_url('/admin/qtypes.php');
 114      }
 115  
 116      /**
 117       * Pre-uninstall hook.
 118       *
 119       * This is intended for disabling of plugin, some DB table purging, etc.
 120       *
 121       * NOTE: to be called from uninstall_plugin() only.
 122       * @private
 123       */
 124      public function uninstall_cleanup() {
 125          // Delete any question configuration records mentioning this plugin.
 126          unset_config($this->name . '_disabled', 'question');
 127          unset_config($this->name . '_sortorder', 'question');
 128  
 129          parent::uninstall_cleanup();
 130      }
 131  
 132      public function get_settings_section_name() {
 133          return 'qtypesetting' . $this->name;
 134      }
 135  
 136      public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
 137          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
 138          /** @var \admin_root $ADMIN */
 139          $ADMIN = $adminroot; // May be used in settings.php.
 140          $plugininfo = $this; // Also can be used inside settings.php.
 141          $qtype = $this;      // Also can be used inside settings.php.
 142  
 143          if (!$this->is_installed_and_upgraded()) {
 144              return;
 145          }
 146  
 147          $section = $this->get_settings_section_name();
 148  
 149          $settings = null;
 150          $systemcontext = \context_system::instance();
 151          if (($hassiteconfig || has_capability('moodle/question:config', $systemcontext)) &&
 152              file_exists($this->full_path('settings.php'))) {
 153              $settings = new admin_settingpage($section, $this->displayname,
 154                  'moodle/question:config', $this->is_enabled() === false);
 155              include($this->full_path('settings.php')); // This may also set $settings to null.
 156          }
 157          if ($settings) {
 158              $ADMIN->add($parentnodename, $settings);
 159          }
 160      }
 161  }