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
  21   * @copyright  2017 Damyon Wiese
  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 document converter plugins
  30   *
  31   * @package    core
  32   * @copyright  2017 Damyon Wiese
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class fileconverter extends base {
  36  
  37      /**
  38       * Should there be a way to uninstall the plugin via the administration UI.
  39       *
  40       * Uninstallation is allowed for fileconverter plugins.
  41       *
  42       * @return bool
  43       */
  44      public function is_uninstall_allowed() {
  45          return true;
  46      }
  47  
  48      /**
  49       * Get the name for the settings section.
  50       *
  51       * @return string
  52       */
  53      public function get_settings_section_name() {
  54          return 'fileconverter' . $this->name;
  55      }
  56  
  57      /**
  58       * Load the global settings for a particular availability plugin (if there are any)
  59       *
  60       * @param \part_of_admin_tree $adminroot
  61       * @param string $parentnodename
  62       * @param bool $hassiteconfig
  63       */
  64      public function load_settings(\part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
  65          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
  66          $ADMIN = $adminroot; // May be used in settings.php.
  67          $plugininfo = $this; // Also can 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' => 'managefileconverterplugins'));
  95      }
  96  
  97      /**
  98       * Finds all enabled plugins, the result may include missing plugins.
  99       *
 100       * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
 101       */
 102      public static function get_enabled_plugins() {
 103          global $CFG;
 104  
 105          $order = (!empty($CFG->converter_plugins_sortorder)) ? explode(',', $CFG->converter_plugins_sortorder) : [];
 106          if ($order) {
 107              $plugins = \core_plugin_manager::instance()->get_installed_plugins('fileconverter');
 108              $order = array_intersect($order, array_keys($plugins));
 109          }
 110  
 111          return array_combine($order, $order);
 112      }
 113  
 114      /**
 115       * Sets the current plugin as enabled or disabled
 116       * When enabling tries to guess the sortorder based on default rank returned by the plugin.
 117       * @param bool $newstate
 118       */
 119      public function set_enabled($newstate = true) {
 120          $enabled = self::get_enabled_plugins();
 121          if (array_key_exists($this->name, $enabled) == $newstate) {
 122              // Nothing to do.
 123              return;
 124          }
 125          if ($newstate) {
 126              // Enable converter plugin.
 127              $plugins = \core_plugin_manager::instance()->get_plugins_of_type('fileconverter');
 128              if (!array_key_exists($this->name, $plugins)) {
 129                  // Can not be enabled.
 130                  return;
 131              }
 132              $enabled[$this->name] = $this->name;
 133              self::set_enabled_plugins($enabled);
 134          } else {
 135              // Disable converter plugin.
 136              unset($enabled[$this->name]);
 137              self::set_enabled_plugins($enabled);
 138          }
 139      }
 140  
 141      /**
 142       * Set the list of enabled converter players in the specified sort order
 143       * To be used when changing settings or in unit tests
 144       * @param string|array $list list of plugin names without frankenstyle prefix - comma-separated string or an array
 145       */
 146      public static function set_enabled_plugins($list) {
 147          if (empty($list)) {
 148              $list = [];
 149          } else if (!is_array($list)) {
 150              $list = explode(',', $list);
 151          }
 152          if ($list) {
 153              $plugins = \core_plugin_manager::instance()->get_installed_plugins('fileconverter');
 154              $list = array_intersect($list, array_keys($plugins));
 155          }
 156          set_config('converter_plugins_sortorder', join(',', $list));
 157          \core_plugin_manager::reset_caches();
 158      }
 159  
 160      /**
 161       * Returns a string describing the formats this engine can converter from / to.
 162       *
 163       * @return string
 164       */
 165      public function get_supported_conversions() {
 166          $classname = self::get_classname($this->name);
 167          if (class_exists($classname)) {
 168              $object = new $classname();
 169              return $object->get_supported_conversions();
 170          }
 171          return '';
 172      }
 173  
 174      /**
 175       * Return the class name for the plugin.
 176       *
 177       * @param   string $plugin
 178       * @return  string
 179       */
 180      public static function get_classname($plugin) {
 181          return "\\fileconverter_{$plugin}\\converter";
 182      }
 183  }