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   * @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      /**
 100       * Returns the node name used in admin settings menu for this plugin settings (if applicable)
 101       *
 102       * @return null|string node name or null if plugin does not create settings node (default)
 103       */
 104      public function get_settings_section_name() {
 105          return 'dataformatsetting' . $this->name;
 106      }
 107  
 108      /**
 109       * Loads plugin settings to the settings tree
 110       *
 111       * This function usually includes settings.php file in plugins folder.
 112       * Alternatively it can create a link to some settings page (instance of admin_externalpage)
 113       *
 114       * @param \part_of_admin_tree $adminroot
 115       * @param string $parentnodename
 116       * @param bool $hassiteconfig whether the current user has moodle/site:config capability
 117       */
 118      public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
 119          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
 120          $ADMIN = $adminroot; // May be used in settings.php.
 121          $plugininfo = $this; // Also can be used inside settings.php.
 122          $dataformat = $this;     // Also can be used inside settings.php.
 123  
 124          if (!$this->is_installed_and_upgraded()) {
 125              return;
 126          }
 127  
 128          if (!$hassiteconfig) {
 129              return;
 130          }
 131          if (file_exists($this->full_path('settings.php'))) {
 132              $fullpath = $this->full_path('settings.php');
 133          } else if (file_exists($this->full_path('dataformatsettings.php'))) {
 134              $fullpath = $this->full_path('dataformatsettings.php');
 135          } else {
 136              return;
 137          }
 138  
 139          $section = $this->get_settings_section_name();
 140          $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
 141          include($fullpath); // This may also set $settings to null.
 142  
 143          if ($settings) {
 144              $ADMIN->add($parentnodename, $settings);
 145          }
 146      }
 147  
 148      /**
 149       * dataformats can be uninstalled
 150       *
 151       * @return bool
 152       */
 153      public function is_uninstall_allowed() {
 154          return true;
 155      }
 156  
 157      /**
 158       * Return URL used for management of plugins of this type.
 159       * @return moodle_url
 160       */
 161      public static function get_manage_url() {
 162          return new moodle_url('/admin/settings.php?section=managedataformats');
 163      }
 164  
 165  }
 166