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_externalpage;
  27  use moodle_url;
  28  use part_of_admin_tree;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  require_once($CFG->dirroot . '/repository/lib.php');
  32  
  33  /**
  34   * Class for repositories
  35   */
  36  class repository extends base {
  37  
  38      /** @var int Repository state, when it's enabled and visible. */
  39      public const REPOSITORY_ON = 1;
  40  
  41      /** @var int Repository state, when it's enabled but hidden. */
  42      public const REPOSITORY_OFF = 0;
  43  
  44      /** @var int Repository state, when it's disabled. */
  45      public const REPOSITORY_DISABLED = -1;
  46  
  47      public static function plugintype_supports_disabling(): bool {
  48          return true;
  49      }
  50  
  51      /**
  52       * Finds all enabled plugins, the result may include missing plugins.
  53       * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
  54       */
  55      public static function get_enabled_plugins() {
  56          global $DB;
  57          return $DB->get_records_menu('repository', null, 'type ASC', 'type, type AS val');
  58      }
  59  
  60      /**
  61       * Returns current status for a pluginname.
  62       *
  63       * Repositories needs to be calculated in a different way than the default method in the base class because they need to take
  64       * into account the value of the visible column too.
  65       *
  66       * @param string $pluginname The plugin name to check.
  67       * @return int The current status (enabled, disabled...) of $pluginname.
  68       */
  69      public static function get_enabled_plugin(string $pluginname): int {
  70          global $DB;
  71  
  72          $repository = $DB->get_record('repository', ['type' => $pluginname]);
  73          if ($repository) {
  74              switch ($repository->visible) {
  75                  case 1:
  76                      $value = self::REPOSITORY_ON;
  77                      break;
  78                  default:
  79                      $value = self::REPOSITORY_OFF;
  80              }
  81          } else {
  82              $value = self::REPOSITORY_DISABLED;
  83          }
  84          return $value;
  85      }
  86  
  87      /**
  88       * Enable or disable a plugin.
  89       * When possible, the change will be stored into the config_log table, to let admins check when/who has modified it.
  90       *
  91       * @param string $pluginname The plugin name to enable/disable.
  92       * @param int $enabled Whether the pluginname should be enabled and visible (1), enabled but hidden (0) or disabled (-1).
  93       *
  94       * @return bool Whether $pluginname has been updated or not.
  95       */
  96      public static function enable_plugin(string $pluginname, int $enabled): bool {
  97          global $DB;
  98  
  99          $haschanged = false;
 100  
 101          // Enabled repositories exist in 'repository' table.
 102          // Visible = REPOSITORY_ON ==> Enabled and visible.
 103          // Visible = REPOSITORY_OFF ==> Enabled but hidden.
 104          // Disabled repositories don't exist in 'repository' table.
 105          if ($plugin = $DB->get_record('repository', ['type' => $pluginname])) {
 106              // The plugin is enabled.
 107              $oldvalue = $plugin->visible;
 108              $repositorytype = \repository::get_type_by_typename($pluginname);
 109              if ($enabled == self::REPOSITORY_DISABLED) {
 110                  $haschanged = $repositorytype->delete();
 111                  $enabled = '';
 112              } else if ($oldvalue != $enabled) {
 113                  $haschanged = $repositorytype->update_visibility($enabled);
 114              }
 115          } else {
 116              // Not all repositories have their own 'repository' record created. Disabled repositories don't have one.
 117              // Make changes only when we want to enable repository.
 118              $oldvalue = '';
 119              if ($enabled == self::REPOSITORY_ON || $enabled == self::REPOSITORY_OFF) {
 120                  $type = new \repository_type($pluginname, [], $enabled);
 121                  if (!$haschanged = $type->create()) {
 122                      throw new \moodle_exception('invalidplugin', 'repository', '', $pluginname);
 123                  }
 124              }
 125          }
 126          if ($haschanged) {
 127              // Include this information into config changes table.
 128              add_to_config_log('repository_visibility', $oldvalue, $enabled, $pluginname);
 129              \core_plugin_manager::reset_caches();
 130          }
 131  
 132          return $haschanged;
 133      }
 134  
 135      public function get_settings_section_name() {
 136          return 'repositorysettings'.$this->name;
 137      }
 138  
 139      public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
 140          if (!$this->is_installed_and_upgraded()) {
 141              return;
 142          }
 143  
 144          if ($hassiteconfig && $this->is_enabled()) {
 145              // Completely no access to repository setting when it is not enabled.
 146              $sectionname = $this->get_settings_section_name();
 147              $settings = new admin_externalpage($sectionname, $this->displayname,
 148                  new moodle_url('/admin/repository.php', ['action' => 'edit', 'repos' => $this->name]), 'moodle/site:config', false);
 149              $adminroot->add($parentnodename, $settings);
 150          }
 151      }
 152  
 153      /**
 154       * Return URL used for management of plugins of this type.
 155       * @return moodle_url
 156       */
 157      public static function get_manage_url() {
 158          return new moodle_url('/admin/repository.php');
 159      }
 160  
 161      /**
 162       * Defines if there should be a way to uninstall the plugin via the administration UI.
 163       * @return boolean
 164       */
 165      public function is_uninstall_allowed() {
 166          if ($this->name === 'upload' || $this->name === 'coursefiles' || $this->name === 'user' || $this->name === 'recent') {
 167              return false;
 168          } else {
 169              return true;
 170          }
 171      }
 172  
 173      /**
 174       * Pre-uninstall hook.
 175       * This is intended for disabling of plugin, some DB table purging, etc.
 176       * Converts all linked files to standard files when repository is removed
 177       * and cleans up all records in the DB for that repository.
 178       */
 179      public function uninstall_cleanup() {
 180          global $CFG;
 181          require_once($CFG->dirroot.'/repository/lib.php');
 182  
 183          $repo = \repository::get_type_by_typename($this->name);
 184          if ($repo) {
 185              $repo->delete(true);
 186          }
 187  
 188          parent::uninstall_cleanup();
 189      }
 190  }