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 401] [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   * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
  21   * @copyright  2016 Brendan Heywood (brendan@catalyst-au.net)
  22   * @package    core
  23   */
  24  namespace core\plugininfo;
  25  
  26  use moodle_url, part_of_admin_tree, admin_settingpage, admin_externalpage, core_plugin_manager;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  /**
  31   * Class for dataformats
  32   *
  33   * @package    core
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
  35   * @copyright  2016 Brendan Heywood (brendan@catalyst-au.net)
  36   */
  37  class dataformat extends base {
  38  
  39      /**
  40       * Display name
  41       */
  42      public function init_display_name() {
  43          if (!get_string_manager()->string_exists('dataformat', $this->component)) {
  44              $this->displayname = '[dataformat,' . $this->component . ']';
  45          } else {
  46              $this->displayname = get_string('dataformat', $this->component);
  47          }
  48      }
  49  
  50      /**
  51       * Gathers and returns the information about all plugins of the given type
  52       *
  53       * @param string $type the name of the plugintype, eg. mod, auth or workshopform
  54       * @param string $typerootdir full path to the location of the plugin dir
  55       * @param string $typeclass the name of the actually called class
  56       * @param core_plugin_manager $pluginman the plugin manager calling this method
  57       * @return array of plugintype classes, indexed by the plugin name
  58       */
  59      public static function get_plugins($type, $typerootdir, $typeclass, $pluginman) {
  60          global $CFG;
  61          $formats = parent::get_plugins($type, $typerootdir, $typeclass, $pluginman);
  62  
  63          if (!empty($CFG->dataformat_plugins_sortorder)) {
  64              $order = explode(',', $CFG->dataformat_plugins_sortorder);
  65              $order = array_merge(array_intersect($order, array_keys($formats)),
  66                          array_diff(array_keys($formats), $order));
  67          } else {
  68              $order = array_keys($formats);
  69          }
  70          $sortedformats = array();
  71          foreach ($order as $formatname) {
  72              $sortedformats[$formatname] = $formats[$formatname];
  73          }
  74          return $sortedformats;
  75      }
  76  
  77      /**
  78       * Finds all enabled plugins, the result may include missing plugins.
  79       * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
  80       */
  81      public static function get_enabled_plugins() {
  82          $enabled = array();
  83          $plugins = core_plugin_manager::instance()->get_installed_plugins('dataformat');
  84  
  85          if (!$plugins) {
  86              return array();
  87          }
  88  
  89          $enabled = array();
  90          foreach ($plugins as $plugin => $version) {
  91              $disabled = get_config('dataformat_' . $plugin, 'disabled');
  92              if (empty($disabled)) {
  93                  $enabled[$plugin] = $plugin;
  94              }
  95          }
  96          return $enabled;
  97      }
  98  
  99      public static function enable_plugin(string $pluginname, int $enabled): bool {
 100          $haschanged = false;
 101  
 102          $plugin = 'dataformat_' . $pluginname;
 103          $oldvalue = get_config($plugin, 'disabled');
 104          $disabled = !$enabled;
 105          // Only set value if there is no config setting or if the value is different from the previous one.
 106          if ($oldvalue == false && $disabled) {
 107              set_config('disabled', $disabled, $plugin);
 108              $haschanged = true;
 109          } else if ($oldvalue != false && !$disabled) {
 110              unset_config('disabled', $plugin);
 111              $haschanged = true;
 112          }
 113  
 114          if ($haschanged) {
 115              add_to_config_log('disabled', $oldvalue, $disabled, $plugin);
 116              \core_plugin_manager::reset_caches();
 117          }
 118  
 119          return $haschanged;
 120      }
 121  
 122      /**
 123       * Returns the node name used in admin settings menu for this plugin settings (if applicable)
 124       *
 125       * @return null|string node name or null if plugin does not create settings node (default)
 126       */
 127      public function get_settings_section_name() {
 128          return 'dataformatsetting' . $this->name;
 129      }
 130  
 131      /**
 132       * Loads plugin settings to the settings tree
 133       *
 134       * This function usually includes settings.php file in plugins folder.
 135       * Alternatively it can create a link to some settings page (instance of admin_externalpage)
 136       *
 137       * @param \part_of_admin_tree $adminroot
 138       * @param string $parentnodename
 139       * @param bool $hassiteconfig whether the current user has moodle/site:config capability
 140       */
 141      public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
 142          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
 143          $ADMIN = $adminroot; // May be used in settings.php.
 144          $plugininfo = $this; // Also can be used inside settings.php.
 145          $dataformat = $this;     // Also can be used inside settings.php.
 146  
 147          if (!$this->is_installed_and_upgraded()) {
 148              return;
 149          }
 150  
 151          if (!$hassiteconfig) {
 152              return;
 153          }
 154          if (file_exists($this->full_path('settings.php'))) {
 155              $fullpath = $this->full_path('settings.php');
 156          } else if (file_exists($this->full_path('dataformatsettings.php'))) {
 157              $fullpath = $this->full_path('dataformatsettings.php');
 158          } else {
 159              return;
 160          }
 161  
 162          $section = $this->get_settings_section_name();
 163          $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
 164          include($fullpath); // This may also set $settings to null.
 165  
 166          if ($settings) {
 167              $ADMIN->add($parentnodename, $settings);
 168          }
 169      }
 170  
 171      /**
 172       * dataformats can be uninstalled
 173       *
 174       * @return bool
 175       */
 176      public function is_uninstall_allowed() {
 177          return true;
 178      }
 179  
 180      /**
 181       * Return URL used for management of plugins of this type.
 182       * @return moodle_url
 183       */
 184      public static function get_manage_url() {
 185          return new moodle_url('/admin/settings.php?section=managedataformats');
 186      }
 187  
 188  }