Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
/lib/ -> editorlib.php (source)

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Utility classes and functions for text editor integration.
  20   *
  21   * @package    core
  22   * @subpackage editor
  23   * @copyright  2009 Petr Skoda {@link http://skodak.org}
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Returns users preferred editor for given format
  31   *
  32   * @param int $format text format or null of none
  33   * @return texteditor object
  34   */
  35  function editors_get_preferred_editor($format = NULL) {
  36      global $USER, $CFG;
  37  
  38      if (!empty($CFG->adminsetuppending)) {
  39          // Must not use other editors before install completed!
  40          return get_texteditor('textarea');
  41      }
  42  
  43      $enabled = editors_get_enabled();
  44  
  45      $preference = get_user_preferences('htmleditor', '', $USER);
  46  
  47      if (isset($enabled[$preference])) {
  48          // Edit the list of editors so the users preferred editor is first in the list.
  49          $editor = $enabled[$preference];
  50          unset($enabled[$preference]);
  51          array_unshift($enabled, $editor);
  52      }
  53  
  54      // now find some plugin that supports format and is available
  55      $editor = false;
  56      foreach ($enabled as $e) {
  57          if (!$e->supported_by_browser()) {
  58              // bad luck, this editor is not compatible
  59              continue;
  60          }
  61          if (!$supports = $e->get_supported_formats()) {
  62              // buggy editor!
  63              continue;
  64          }
  65          if (is_null($format) || in_array($format, $supports)) {
  66              // editor supports this format, yay!
  67              $editor = $e;
  68              break;
  69          }
  70      }
  71  
  72      if (!$editor) {
  73          $editor = get_texteditor('textarea'); // must exist and can edit anything
  74      }
  75  
  76      return $editor;
  77  }
  78  
  79  /**
  80   * Returns users preferred text format.
  81   * @return int standard text format
  82   */
  83  function editors_get_preferred_format() {
  84      global $USER;
  85  
  86      $editor = editors_get_preferred_editor();
  87      return $editor->get_preferred_format();
  88  }
  89  
  90  /**
  91   * Returns list of enabled text editors
  92   * @return array of name=>texteditor
  93   */
  94  function editors_get_enabled() {
  95      global $CFG;
  96  
  97      if (empty($CFG->texteditors)) {
  98          $CFG->texteditors = 'atto,tiny,textarea';
  99      }
 100      $active = array();
 101      foreach(explode(',', $CFG->texteditors) as $e) {
 102          if ($editor = get_texteditor($e)) {
 103              $active[$e] = $editor;
 104          }
 105      }
 106  
 107      if (empty($active)) {
 108          return array('textarea'=>get_texteditor('textarea')); // must exist and can edit anything
 109      }
 110  
 111      return $active;
 112  }
 113  
 114  /**
 115   * Returns instance of text editor
 116   *
 117   * @param string $editorname name of editor (textarea, tiny, ...)
 118   * @return object|bool texeditor instance or false if does not exist
 119   */
 120  function get_texteditor($editorname) {
 121      global $CFG;
 122  
 123      $libfile = "$CFG->libdir/editor/$editorname/lib.php";
 124      if (!file_exists($libfile)) {
 125          return false;
 126      }
 127      require_once($libfile);
 128      $classname = $editorname.'_texteditor';
 129      if (!class_exists($classname)) {
 130          return false;
 131      }
 132      return new $classname();
 133  }
 134  
 135  /**
 136   * Get the list of available editors
 137   *
 138   * @return array Array ('editorname'=>'localised editor name')
 139   */
 140  function editors_get_available() {
 141      $editors = array();
 142      foreach (core_component::get_plugin_list('editor') as $editorname => $dir) {
 143          $editors[$editorname] = get_string('pluginname', 'editor_'.$editorname);
 144      }
 145      return $editors;
 146  }
 147  
 148  /**
 149   * Setup all JS and CSS needed for editors.
 150   * @return void
 151   */
 152  function editors_head_setup() {
 153      global $CFG;
 154  
 155      if (empty($CFG->texteditors)) {
 156          $CFG->texteditors = 'atto,tiny,textarea';
 157      }
 158      $active = explode(',', $CFG->texteditors);
 159  
 160      foreach ($active as $editorname) {
 161          if (!$editor = get_texteditor($editorname)) {
 162              continue;
 163          }
 164          if (!$editor->supported_by_browser()) {
 165              // bad luck, this editor is not compatible
 166              continue;
 167          }
 168          $editor->head_setup();
 169      }
 170  }
 171  
 172  /**
 173   * Base abstract text editor class.
 174   *
 175   * @copyright  2009 Petr Skoda {@link http://skodak.org}
 176   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 177   * @package moodlecore
 178   */
 179  abstract class texteditor {
 180      /**
 181       * Is editor supported in current browser?
 182       * @return bool
 183       */
 184      public abstract function supported_by_browser();
 185  
 186      /**
 187       * Returns list of supported text formats
 188       * @return array Array (FORMAT=>FORMAT)
 189       */
 190      public abstract function get_supported_formats();
 191  
 192      /**
 193       * Returns main preferred text format.
 194       * @return int text format
 195       */
 196      public abstract function get_preferred_format();
 197  
 198      /**
 199       * Supports file picker and repos?
 200       * @return object book object
 201       */
 202      public abstract function supports_repositories();
 203  
 204      /**
 205       * @var string $text The text set to the editor in the form.
 206       * @since 3.0
 207       */
 208      protected $text = '';
 209  
 210      /**
 211       * Set the text set for this form field. Will be called before "use_editor".
 212       * @param string $text The text for the form field.
 213       */
 214      public function set_text($text) {
 215          $this->text = $text;
 216      }
 217  
 218      /**
 219       * Get the text set for this form field. Can be called from "use_editor".
 220       * @return string
 221       */
 222      public function get_text() {
 223          return $this->text;
 224      }
 225  
 226      /**
 227       * Add required JS needed for editor
 228       *
 229       * Valid options may vary by editor. See the individual editor
 230       * implementations of this function for documentation.
 231       *
 232       * @param string $elementid id of text area to be converted to editor
 233       * @param array $options Editor options
 234       * @param obejct $fpoptions file picker options
 235       * @return void
 236       */
 237      public abstract function use_editor($elementid, array $options=null, $fpoptions = null);
 238  
 239      /**
 240       * Setup all JS and CSS needed for editor.
 241       * @return void
 242       */
 243      public function head_setup() {
 244      }
 245  }