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 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   * @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      public static function enable_plugin(string $pluginname, int $enabled): bool {
 115          global $CFG;
 116  
 117          $haschanged = false;
 118          $plugins = [];
 119          if (!empty($CFG->converter_plugins_sortorder)) {
 120              $plugins = array_flip(explode(',', $CFG->converter_plugins_sortorder));
 121          }
 122          // Only set visibility if it's different from the current value.
 123          if ($enabled && !array_key_exists($pluginname, $plugins)) {
 124              $plugins[$pluginname] = $pluginname;
 125              $haschanged = true;
 126          } else if (!$enabled && array_key_exists($pluginname, $plugins)) {
 127              unset($plugins[$pluginname]);
 128              $haschanged = true;
 129          }
 130  
 131          if ($haschanged) {
 132              add_to_config_log('converter_plugins_sortorder', !$enabled, $enabled, $pluginname);
 133              self::set_enabled_plugins(array_flip($plugins));
 134          }
 135  
 136          return $haschanged;
 137      }
 138  
 139      /**
 140       * Sets the current plugin as enabled or disabled
 141       * When enabling tries to guess the sortorder based on default rank returned by the plugin.
 142       * @param bool $newstate
 143       */
 144      public function set_enabled($newstate = true) {
 145          self::enable_plugin($this->name, $newstate);
 146      }
 147  
 148      /**
 149       * Set the list of enabled converter players in the specified sort order
 150       * To be used when changing settings or in unit tests
 151       * @param string|array $list list of plugin names without frankenstyle prefix - comma-separated string or an array
 152       */
 153      public static function set_enabled_plugins($list) {
 154          if (empty($list)) {
 155              $list = [];
 156          } else if (!is_array($list)) {
 157              $list = explode(',', $list);
 158          }
 159          if ($list) {
 160              $plugins = \core_plugin_manager::instance()->get_installed_plugins('fileconverter');
 161              $list = array_intersect($list, array_keys($plugins));
 162          }
 163          set_config('converter_plugins_sortorder', join(',', $list));
 164          \core_plugin_manager::reset_caches();
 165      }
 166  
 167      /**
 168       * Returns a string describing the formats this engine can converter from / to.
 169       *
 170       * @return string
 171       */
 172      public function get_supported_conversions() {
 173          $classname = self::get_classname($this->name);
 174          if (class_exists($classname)) {
 175              $object = new $classname();
 176              return $object->get_supported_conversions();
 177          }
 178          return '';
 179      }
 180  
 181      /**
 182       * Return the class name for the plugin.
 183       *
 184       * @param   string $plugin
 185       * @return  string
 186       */
 187      public static function get_classname($plugin) {
 188          return "\\fileconverter_{$plugin}\\converter";
 189      }
 190  }