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 core_plugin_manager;
  27  use moodle_url;
  28  
  29  /**
  30   * Class for question behaviours.
  31   */
  32  class qbehaviour extends base {
  33  
  34      public static function plugintype_supports_disabling(): bool {
  35          return true;
  36      }
  37  
  38      /**
  39       * Finds all enabled plugins, the result may include missing plugins.
  40       * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
  41       */
  42      public static function get_enabled_plugins() {
  43          $plugins = core_plugin_manager::instance()->get_installed_plugins('qbehaviour');
  44          if (!$plugins) {
  45              return array();
  46          }
  47          if ($disabled = get_config('question', 'disabledbehaviours')) {
  48              $disabled = explode(',', $disabled);
  49          } else {
  50              $disabled = array();
  51          }
  52  
  53          $enabled = array();
  54          foreach ($plugins as $plugin => $version) {
  55              if (in_array($plugin, $disabled)) {
  56                  continue;
  57              }
  58              $enabled[$plugin] = $plugin;
  59          }
  60  
  61          return $enabled;
  62      }
  63  
  64      public static function enable_plugin(string $pluginname, int $enabled): bool {
  65          $haschanged = false;
  66          $plugins = [];
  67          $oldvalue = get_config('question', 'disabledbehaviours');
  68          if (!empty($oldvalue)) {
  69              $plugins = array_flip(explode(',', $oldvalue));
  70          }
  71          // Only set visibility if it's different from the current value.
  72          if ($enabled && array_key_exists($pluginname, $plugins)) {
  73              unset($plugins[$pluginname]);
  74              $haschanged = true;
  75          } else if (!$enabled && !array_key_exists($pluginname, $plugins)) {
  76              $plugins[$pluginname] = $pluginname;
  77              $haschanged = true;
  78          }
  79  
  80          if ($haschanged) {
  81              $new = implode(',', array_flip($plugins));
  82              add_to_config_log('disabledbehaviours', $oldvalue, $new, 'question');
  83              set_config('disabledbehaviours', $new, 'question');
  84              // Reset caches.
  85              \core_plugin_manager::reset_caches();
  86          }
  87  
  88          return $haschanged;
  89      }
  90  
  91      public function is_uninstall_allowed() {
  92          global $DB;
  93  
  94          if ($this->name === 'missing') {
  95              // qbehaviour_missing is used by the system. It cannot be uninstalled.
  96              return false;
  97          }
  98  
  99          return !$DB->record_exists('question_attempts', array('behaviour' => $this->name));
 100      }
 101  
 102      /**
 103       * Pre-uninstall hook.
 104       *
 105       * This is intended for disabling of plugin, some DB table purging, etc.
 106       *
 107       * NOTE: to be called from uninstall_plugin() only.
 108       * @private
 109       */
 110      public function uninstall_cleanup() {
 111          if ($disabledbehaviours = get_config('question', 'disabledbehaviours')) {
 112              $disabledbehaviours = explode(',', $disabledbehaviours);
 113              $disabledbehaviours = array_unique($disabledbehaviours);
 114          } else {
 115              $disabledbehaviours = array();
 116          }
 117          if (($key = array_search($this->name, $disabledbehaviours)) !== false) {
 118              unset($disabledbehaviours[$key]);
 119              set_config('disabledbehaviours', implode(',', $disabledbehaviours), 'question');
 120          }
 121  
 122          if ($behaviourorder = get_config('question', 'behavioursortorder')) {
 123              $behaviourorder = explode(',', $behaviourorder);
 124              $behaviourorder = array_unique($behaviourorder);
 125          } else {
 126              $behaviourorder = array();
 127          }
 128          if (($key = array_search($this->name, $behaviourorder)) !== false) {
 129              unset($behaviourorder[$key]);
 130              set_config('behavioursortorder', implode(',', $behaviourorder), 'question');
 131          }
 132  
 133          parent::uninstall_cleanup();
 134      }
 135  
 136      /**
 137       * Return URL used for management of plugins of this type.
 138       * @return moodle_url
 139       */
 140      public static function get_manage_url() {
 141          return new moodle_url('/admin/qbehaviours.php');
 142      }
 143  }