Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [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  2013 Petr Skoda {@link http://skodak.org}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core\plugininfo;
  25  
  26  use admin_settingpage;
  27  use moodle_url;
  28  use part_of_admin_tree;
  29  
  30  /**
  31   * Class for HTML editors
  32   */
  33  class editor extends base {
  34  
  35      public static function plugintype_supports_disabling(): bool {
  36          return true;
  37      }
  38  
  39      /**
  40       * Finds all enabled plugins, the result may include missing plugins.
  41       * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
  42       */
  43      public static function get_enabled_plugins() {
  44          global $CFG;
  45  
  46          if (empty($CFG->texteditors)) {
  47              return array('atto'=>'atto', 'tinymce'=>'tinymce', 'textarea'=>'textarea');
  48          }
  49  
  50          $enabled = array();
  51          foreach (explode(',', $CFG->texteditors) as $editor) {
  52              $enabled[$editor] = $editor;
  53          }
  54  
  55          return $enabled;
  56      }
  57  
  58      public static function enable_plugin(string $pluginname, int $enabled): bool {
  59          global $CFG;
  60  
  61          $haschanged = false;
  62          if (!empty($CFG->texteditors)) {
  63              $plugins = array_flip(explode(',', $CFG->texteditors));
  64          } else {
  65              $plugins = [];
  66          }
  67  
  68          // Only set visibility if it's different from the current value.
  69          if ($enabled && !array_key_exists($pluginname, $plugins)) {
  70              $plugins[$pluginname] = $pluginname;
  71              $haschanged = true;
  72          } else if (!$enabled && array_key_exists($pluginname, $plugins)) {
  73              unset($plugins[$pluginname]);
  74              $haschanged = true;
  75          }
  76  
  77          // At least one editor must be active.
  78          if (empty($plugins)) {
  79              $plugins['textarea'] = 'textarea';
  80              $haschanged = true;
  81          }
  82  
  83          if ($haschanged) {
  84              $new = implode(',', array_flip($plugins));
  85              add_to_config_log('editor_visibility', !$enabled, $enabled, $pluginname);
  86              set_config('texteditors', $new);
  87              // Reset caches.
  88              \core_plugin_manager::reset_caches();
  89          }
  90  
  91          return $haschanged;
  92      }
  93  
  94      public function get_settings_section_name() {
  95          return 'editorsettings' . $this->name;
  96      }
  97  
  98      public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
  99          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
 100          /** @var \admin_root $ADMIN */
 101          $ADMIN = $adminroot; // May be used in settings.php.
 102          $plugininfo = $this; // Also can be used inside settings.php.
 103          $editor = $this;     // Also can be used inside settings.php.
 104  
 105          if (!$this->is_installed_and_upgraded()) {
 106              return;
 107          }
 108  
 109          if (!$hassiteconfig or !file_exists($this->full_path('settings.php'))) {
 110              return;
 111          }
 112  
 113          $section = $this->get_settings_section_name();
 114  
 115          $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
 116          include($this->full_path('settings.php')); // This may also set $settings to null.
 117  
 118          if ($settings) {
 119              $ADMIN->add($parentnodename, $settings);
 120          }
 121      }
 122  
 123      /**
 124       * Basic textarea editor can not be uninstalled.
 125       */
 126      public function is_uninstall_allowed() {
 127          if ($this->name === 'textarea') {
 128              return false;
 129          } else {
 130              return true;
 131          }
 132      }
 133  
 134      /**
 135       * Return URL used for management of plugins of this type.
 136       * @return moodle_url
 137       */
 138      public static function get_manage_url() {
 139          return new moodle_url('/admin/settings.php', array('section'=>'manageeditors'));
 140      }
 141  
 142      public static function plugintype_supports_ordering(): bool {
 143          return true;
 144      }
 145  
 146      public static function get_sorted_plugins(bool $enabledonly = false): ?array {
 147          global $CFG;
 148  
 149          $pluginmanager = \core_plugin_manager::instance();
 150          $plugins = $pluginmanager->get_plugins_of_type('editor');
 151  
 152          // The Editor list is stored in an ordered string.
 153          $activeeditors = explode(',', $CFG->texteditors);
 154  
 155          $sortedplugins = [];
 156          foreach ($activeeditors as $editor) {
 157              if (isset($plugins[$editor])) {
 158                  $sortedplugins[$editor] = $plugins[$editor];
 159                  unset($plugins[$editor]);
 160              }
 161          }
 162  
 163          if ($enabledonly) {
 164              return $sortedplugins;
 165          }
 166  
 167          // Sort the rest of the plugins lexically.
 168          uasort($plugins, function ($a, $b) {
 169              return strnatcasecmp($a->name, $b->name);
 170          });
 171  
 172          return array_merge(
 173              $sortedplugins,
 174              $plugins,
 175          );
 176      }
 177  
 178      public static function change_plugin_order(string $pluginname, int $direction): bool {
 179          $activeeditors = array_keys(self::get_sorted_plugins(true));
 180          $key = array_search($pluginname, $activeeditors);
 181  
 182          if ($key === false) {
 183              return false;
 184          }
 185  
 186          if ($direction === self::MOVE_DOWN) {
 187              // Move down the list.
 188              if ($key < (count($activeeditors) - 1)) {
 189                  $fsave = $activeeditors[$key];
 190                  $activeeditors[$key] = $activeeditors[$key + 1];
 191                  $activeeditors[$key + 1] = $fsave;
 192                  add_to_config_log('editor_position', $key, $key + 1, $pluginname);
 193                  set_config('texteditors', implode(',', $activeeditors));
 194                  \core_plugin_manager::reset_caches();
 195  
 196                  return true;
 197              }
 198          } else if ($direction === self::MOVE_UP) {
 199              if ($key >= 1) {
 200                  $fsave = $activeeditors[$key];
 201                  $activeeditors[$key] = $activeeditors[$key - 1];
 202                  $activeeditors[$key - 1] = $fsave;
 203                  add_to_config_log('editor_position', $key, $key - 1, $pluginname);
 204                  set_config('texteditors', implode(',', $activeeditors));
 205                  \core_plugin_manager::reset_caches();
 206  
 207                  return true;
 208              }
 209          }
 210  
 211          return false;
 212      }
 213  }