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