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  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 moodle_url, part_of_admin_tree, admin_settingpage;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  
  31  /**
  32   * Class for HTML editors
  33   */
  34  class editor extends base {
  35      /**
  36       * Finds all enabled plugins, the result may include missing plugins.
  37       * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
  38       */
  39      public static function get_enabled_plugins() {
  40          global $CFG;
  41  
  42          if (empty($CFG->texteditors)) {
  43              return array('atto'=>'atto', 'tinymce'=>'tinymce', 'textarea'=>'textarea');
  44          }
  45  
  46          $enabled = array();
  47          foreach (explode(',', $CFG->texteditors) as $editor) {
  48              $enabled[$editor] = $editor;
  49          }
  50  
  51          return $enabled;
  52      }
  53  
  54      public static function enable_plugin(string $pluginname, int $enabled): bool {
  55          global $CFG;
  56  
  57          $haschanged = false;
  58          if (!empty($CFG->texteditors)) {
  59              $plugins = array_flip(explode(',', $CFG->texteditors));
  60          } else {
  61              $plugins = [];
  62          }
  63  
  64          // Only set visibility if it's different from the current value.
  65          if ($enabled && !array_key_exists($pluginname, $plugins)) {
  66              $plugins[$pluginname] = $pluginname;
  67              $haschanged = true;
  68          } else if (!$enabled && array_key_exists($pluginname, $plugins)) {
  69              unset($plugins[$pluginname]);
  70              $haschanged = true;
  71          }
  72  
  73          // At least one editor must be active.
  74          if (empty($plugins)) {
  75              $plugins['textarea'] = 'textarea';
  76              $haschanged = true;
  77          }
  78  
  79          if ($haschanged) {
  80              $new = implode(',', array_flip($plugins));
  81              add_to_config_log('editor_visibility', !$enabled, $enabled, $pluginname);
  82              set_config('texteditors', $new);
  83              // Reset caches.
  84              \core_plugin_manager::reset_caches();
  85          }
  86  
  87          return $haschanged;
  88      }
  89  
  90      public function get_settings_section_name() {
  91          return 'editorsettings' . $this->name;
  92      }
  93  
  94      public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
  95          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
  96          $ADMIN = $adminroot; // May be used in settings.php.
  97          $plugininfo = $this; // Also can be used inside settings.php.
  98          $editor = $this;     // Also can be used inside settings.php.
  99  
 100          if (!$this->is_installed_and_upgraded()) {
 101              return;
 102          }
 103  
 104          if (!$hassiteconfig or !file_exists($this->full_path('settings.php'))) {
 105              return;
 106          }
 107  
 108          $section = $this->get_settings_section_name();
 109  
 110          $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
 111          include($this->full_path('settings.php')); // This may also set $settings to null.
 112  
 113          if ($settings) {
 114              $ADMIN->add($parentnodename, $settings);
 115          }
 116      }
 117  
 118      /**
 119       * Basic textarea editor can not be uninstalled.
 120       */
 121      public function is_uninstall_allowed() {
 122          if ($this->name === 'textarea') {
 123              return false;
 124          } else {
 125              return true;
 126          }
 127      }
 128  
 129      /**
 130       * Return URL used for management of plugins of this type.
 131       * @return moodle_url
 132       */
 133      public static function get_manage_url() {
 134          return new moodle_url('/admin/settings.php', array('section'=>'manageeditors'));
 135      }
 136  }