Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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      /**
 160       * Optional extra warning before uninstallation adding number of existing contenttype contents.
 161       *
 162       * @return string
 163       */
 164      public function get_uninstall_extra_warning() {
 165          global $DB;
 166  
 167          $contentcount = $DB->count_records('contentbank_content', ['contenttype' => "contenttype_$this->name"]);
 168          if (!$contentcount) {
 169              return '';
 170          }
 171  
 172          $message = get_string('contenttypeuninstalling',
 173              'core_admin',
 174              (object)['count' => $contentcount, 'type' => $this->displayname]
 175          );
 176  
 177          return $message;
 178      }
 179  
 180      /**
 181       * Pre-uninstall hook.
 182       *
 183       * This is intended for disabling of plugin, some DB table purging, etc.
 184       *
 185       * NOTE: to be called from uninstall_plugin() only.
 186       */
 187      public function uninstall_cleanup() {
 188          global $DB;
 189  
 190          $records = $DB->get_records('contentbank_content', ['contenttype' => 'contenttype_'.$this->name]);
 191          $contenttypename = 'contenttype_'.$this->name;
 192          $contenttypeclass = "\\$contenttypename\\contenttype";
 193          foreach ($records as $record) {
 194              $context = \context::instance_by_id($record->contextid, MUST_EXIST);
 195              $contenttype = new $contenttypeclass($context);
 196              $contentclass = "\\$contenttypename\\content";
 197              $content = new $contentclass($record);
 198              $contenttype->delete_content($content);
 199          }
 200  
 201          parent::uninstall_cleanup();
 202      }
 203  }