Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 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_contentbank
  21   * @copyright  2020 Amaia Anabitarte <amaia@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core\plugininfo;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Class for contentbank plugins
  30   *
  31   * @package    core_contentbank
  32   * @copyright  2020 Amaia Anabitarte <amaia@moodle.com>
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class contenttype extends base {
  36  
  37  
  38      /**
  39       * Defines if there should be a way to uninstall the plugin via the administration UI.
  40       *
  41       * @return bool
  42       */
  43      public function is_uninstall_allowed() {
  44          return true;
  45      }
  46  
  47      /**
  48       * Get the name for the settings section.
  49       *
  50       * @return string
  51       */
  52      public function get_settings_section_name() {
  53          return 'contentbanksetting' . $this->name;
  54      }
  55  
  56      /**
  57       * Load the global settings for a particular contentbank plugin (if there are any)
  58       *
  59       * @param \part_of_admin_tree $adminroot
  60       * @param string $parentnodename
  61       * @param bool $hassiteconfig
  62       */
  63      public function load_settings(\part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
  64          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
  65          $ADMIN = $adminroot; // May be used in settings.php.
  66          $plugininfo = $this; // Also can be used inside settings.php
  67          $contenttype = $this; // Also to be used inside settings.php.
  68  
  69          if (!$this->is_installed_and_upgraded()) {
  70              return;
  71          }
  72  
  73          if (!$hassiteconfig) {
  74              return;
  75          }
  76  
  77          $section = $this->get_settings_section_name();
  78  
  79          $settings = null;
  80          if (file_exists($this->full_path('settings.php'))) {
  81              $settings = new \admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
  82              include($this->full_path('settings.php')); // This may also set $settings to null.
  83          }
  84          if ($settings) {
  85              $ADMIN->add($parentnodename, $settings);
  86          }
  87      }
  88  
  89      /**
  90       * Return URL used for management of plugins of this type.
  91       * @return \moodle_url
  92       */
  93      public static function get_manage_url() {
  94          return new \moodle_url('/admin/settings.php', array('section' => 'managecontentbanktypes'));
  95      }
  96  
  97  
  98      /**
  99       * Gathers and returns the information about all plugins of the given type
 100       *
 101       * @param string $type the name of the plugintype, eg. mod, auth or workshopform
 102       * @param string $typerootdir full path to the location of the plugin dir
 103       * @param string $typeclass the name of the actually called class
 104       * @param core_plugin_manager $pluginman the plugin manager calling this method
 105       * @return array of plugintype classes, indexed by the plugin name
 106       */
 107      public static function get_plugins($type, $typerootdir, $typeclass, $pluginman) {
 108          global $CFG;
 109  
 110          $contents = parent::get_plugins($type, $typerootdir, $typeclass, $pluginman);
 111          if (!empty($CFG->contentbank_plugins_sortorder)) {
 112              $order = explode(',', $CFG->contentbank_plugins_sortorder);
 113              $order = array_merge(array_intersect($order, array_keys($contents)),
 114                  array_diff(array_keys($contents), $order));
 115          } else {
 116              $order = array_keys($contents);
 117          }
 118          $sortedcontents = array();
 119          foreach ($order as $contentname) {
 120              $sortedcontents[$contentname] = $contents[$contentname];
 121          }
 122          return $sortedcontents;
 123      }
 124  
 125      /**
 126       * Finds all enabled plugins, the result may include missing plugins.
 127       * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
 128       */
 129      public static function get_enabled_plugins() {
 130          global $CFG;
 131  
 132          $plugins = \core_plugin_manager::instance()->get_installed_plugins('contenttype');
 133  
 134          if (!$plugins) {
 135              return array();
 136          }
 137  
 138          $plugins = array_keys($plugins);
 139          // Order the plugins.
 140          if (!empty($CFG->contentbank_plugins_sortorder)) {
 141              $order = explode(',', $CFG->contentbank_plugins_sortorder);
 142              $order = array_merge(array_intersect($order, $plugins),
 143                  array_diff($plugins, $order));
 144          } else {
 145              $order = $plugins;
 146          }
 147  
 148          // Filter to return only enabled plugins.
 149          $enabled = array();
 150          foreach ($order as $plugin) {
 151              $disabled = get_config('contentbank_' . $plugin, 'disabled');
 152              if (empty($disabled)) {
 153                  $enabled[$plugin] = $plugin;
 154              }
 155          }
 156          return $enabled;
 157      }
 158  
 159      public static function enable_plugin(string $pluginname, int $enabled): bool {
 160          $haschanged = false;
 161  
 162          $plugin = 'contentbank_' . $pluginname;
 163          $oldvalue = get_config($plugin, 'disabled');
 164          $disabled = !$enabled;
 165          // Only set value if there is no config setting or if the value is different from the previous one.
 166          if ($oldvalue == false && $disabled) {
 167              set_config('disabled', $disabled, $plugin);
 168              $haschanged = true;
 169          } else if ($oldvalue != false && !$disabled) {
 170              unset_config('disabled', $plugin);
 171              $haschanged = true;
 172          }
 173  
 174          if ($haschanged) {
 175              add_to_config_log('disabled', $oldvalue, $disabled, $plugin);
 176              \core_plugin_manager::reset_caches();
 177          }
 178  
 179          return $haschanged;
 180      }
 181  
 182      /**
 183       * Optional extra warning before uninstallation adding number of existing contenttype contents.
 184       *
 185       * @return string
 186       */
 187      public function get_uninstall_extra_warning() {
 188          global $DB;
 189  
 190          $contentcount = $DB->count_records('contentbank_content', ['contenttype' => "contenttype_$this->name"]);
 191          if (!$contentcount) {
 192              return '';
 193          }
 194  
 195          $message = get_string('contenttypeuninstalling',
 196              'core_admin',
 197              (object)['count' => $contentcount, 'type' => $this->displayname]
 198          );
 199  
 200          return $message;
 201      }
 202  
 203      /**
 204       * Pre-uninstall hook.
 205       *
 206       * This is intended for disabling of plugin, some DB table purging, etc.
 207       *
 208       * NOTE: to be called from uninstall_plugin() only.
 209       */
 210      public function uninstall_cleanup() {
 211          global $DB;
 212  
 213          $records = $DB->get_records('contentbank_content', ['contenttype' => 'contenttype_'.$this->name]);
 214          $contenttypename = 'contenttype_'.$this->name;
 215          $contenttypeclass = "\\$contenttypename\\contenttype";
 216          foreach ($records as $record) {
 217              $context = \context::instance_by_id($record->contextid, MUST_EXIST);
 218              $contenttype = new $contenttypeclass($context);
 219              $contentclass = "\\$contenttypename\\content";
 220              $content = new $contentclass($record);
 221              $contenttype->delete_content($content);
 222          }
 223  
 224          parent::uninstall_cleanup();
 225      }
 226  }