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 400 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   * @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       * Given a list of dataformat types, return them sorted according to site configuration (if set)
  52       *
  53       * @param string[] $formats List of formats, ['csv', 'pdf', etc]
  54       * @return string[] List of formats according to configured sort, ['csv', 'odf', etc]
  55       */
  56      private static function get_plugins_sortorder(array $formats): array {
  57          global $CFG;
  58  
  59          if (!empty($CFG->dataformat_plugins_sortorder)) {
  60              $order = explode(',', $CFG->dataformat_plugins_sortorder);
  61              $order = array_merge(array_intersect($order, $formats), array_diff($formats, $order));
  62          } else {
  63              $order = $formats;
  64          }
  65  
  66          return $order;
  67      }
  68  
  69      /**
  70       * Gathers and returns the information about all plugins of the given type
  71       *
  72       * @param string $type the name of the plugintype, eg. mod, auth or workshopform
  73       * @param string $typerootdir full path to the location of the plugin dir
  74       * @param string $typeclass the name of the actually called class
  75       * @param core_plugin_manager $pluginman the plugin manager calling this method
  76       * @return array of plugintype classes, indexed by the plugin name
  77       */
  78      public static function get_plugins($type, $typerootdir, $typeclass, $pluginman) {
  79          $formats = parent::get_plugins($type, $typerootdir, $typeclass, $pluginman);
  80  
  81          $order = static::get_plugins_sortorder(array_keys($formats));
  82          $sortedformats = array();
  83          foreach ($order as $formatname) {
  84              $sortedformats[$formatname] = $formats[$formatname];
  85          }
  86          return $sortedformats;
  87      }
  88  
  89      /**
  90       * Finds all enabled plugins, the result may include missing plugins.
  91       * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
  92       */
  93      public static function get_enabled_plugins() {
  94          $plugins = core_plugin_manager::instance()->get_installed_plugins('dataformat');
  95          if (!$plugins) {
  96              return array();
  97          }
  98  
  99          $order = static::get_plugins_sortorder(array_keys($plugins));
 100          $enabled = array();
 101          foreach ($order as $formatname) {
 102              $disabled = get_config('dataformat_' . $formatname, 'disabled');
 103              if (empty($disabled)) {
 104                  $enabled[$formatname] = $formatname;
 105              }
 106          }
 107          return $enabled;
 108      }
 109  
 110      public static function enable_plugin(string $pluginname, int $enabled): bool {
 111          $haschanged = false;
 112  
 113          $plugin = 'dataformat_' . $pluginname;
 114          $oldvalue = get_config($plugin, 'disabled');
 115          $disabled = !$enabled;
 116          // Only set value if there is no config setting or if the value is different from the previous one.
 117          if ($oldvalue == false && $disabled) {
 118              set_config('disabled', $disabled, $plugin);
 119              $haschanged = true;
 120          } else if ($oldvalue != false && !$disabled) {
 121              unset_config('disabled', $plugin);
 122              $haschanged = true;
 123          }
 124  
 125          if ($haschanged) {
 126              add_to_config_log('disabled', $oldvalue, $disabled, $plugin);
 127              \core_plugin_manager::reset_caches();
 128          }
 129  
 130          return $haschanged;
 131      }
 132  
 133      /**
 134       * Returns the node name used in admin settings menu for this plugin settings (if applicable)
 135       *
 136       * @return null|string node name or null if plugin does not create settings node (default)
 137       */
 138      public function get_settings_section_name() {
 139          return 'dataformatsetting' . $this->name;
 140      }
 141  
 142      /**
 143       * Loads plugin settings to the settings tree
 144       *
 145       * This function usually includes settings.php file in plugins folder.
 146       * Alternatively it can create a link to some settings page (instance of admin_externalpage)
 147       *
 148       * @param \part_of_admin_tree $adminroot
 149       * @param string $parentnodename
 150       * @param bool $hassiteconfig whether the current user has moodle/site:config capability
 151       */
 152      public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
 153          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
 154          $ADMIN = $adminroot; // May be used in settings.php.
 155          $plugininfo = $this; // Also can be used inside settings.php.
 156          $dataformat = $this;     // Also can be used inside settings.php.
 157  
 158          if (!$this->is_installed_and_upgraded()) {
 159              return;
 160          }
 161  
 162          if (!$hassiteconfig) {
 163              return;
 164          }
 165          if (file_exists($this->full_path('settings.php'))) {
 166              $fullpath = $this->full_path('settings.php');
 167          } else if (file_exists($this->full_path('dataformatsettings.php'))) {
 168              $fullpath = $this->full_path('dataformatsettings.php');
 169          } else {
 170              return;
 171          }
 172  
 173          $section = $this->get_settings_section_name();
 174          $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
 175          include($fullpath); // This may also set $settings to null.
 176  
 177          if ($settings) {
 178              $ADMIN->add($parentnodename, $settings);
 179          }
 180      }
 181  
 182      /**
 183       * dataformats can be uninstalled
 184       *
 185       * @return bool
 186       */
 187      public function is_uninstall_allowed() {
 188          return true;
 189      }
 190  
 191      /**
 192       * Return URL used for management of plugins of this type.
 193       * @return moodle_url
 194       */
 195      public static function get_manage_url() {
 196          return new moodle_url('/admin/settings.php?section=managedataformats');
 197      }
 198  
 199  }