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]

   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   * YUI text editor integration.
  19   *
  20   * @package    editor_atto
  21   * @copyright  2013 Damyon Wiese  <damyon@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * This is the texteditor implementation.
  29   * @copyright  2013 Damyon Wiese  <damyon@moodle.com>
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class atto_texteditor extends texteditor {
  33  
  34      /**
  35       * Is the current browser supported by this editor?
  36       *
  37       * Of course!
  38       * @return bool
  39       */
  40      public function supported_by_browser() {
  41          return true;
  42      }
  43  
  44      /**
  45       * Returns array of supported text formats.
  46       * @return array
  47       */
  48      public function get_supported_formats() {
  49          // FORMAT_MOODLE is not supported here, sorry.
  50          return array(FORMAT_HTML => FORMAT_HTML);
  51      }
  52  
  53      /**
  54       * Returns text format preferred by this editor.
  55       * @return int
  56       */
  57      public function get_preferred_format() {
  58          return FORMAT_HTML;
  59      }
  60  
  61      /**
  62       * Does this editor support picking from repositories?
  63       * @return bool
  64       */
  65      public function supports_repositories() {
  66          return true;
  67      }
  68  
  69      /**
  70       * Use this editor for given element.
  71       *
  72       * Available Atto-specific options:
  73       *   atto:toolbar - set to a string to override the system config editor_atto/toolbar
  74       *
  75       * Available general options:
  76       *   context - set to the current context object
  77       *   enable_filemanagement - set false to get rid of the managefiles plugin
  78       *   autosave - true/false to control autosave
  79       *
  80       * Options are also passed through to the plugins.
  81       *
  82       * @param string $elementid
  83       * @param array $options
  84       * @param null $fpoptions
  85       */
  86      public function use_editor($elementid, array $options=null, $fpoptions=null) {
  87          global $PAGE;
  88  
  89          if (array_key_exists('atto:toolbar', $options)) {
  90              $configstr = $options['atto:toolbar'];
  91          } else {
  92              $configstr = get_config('editor_atto', 'toolbar');
  93          }
  94  
  95          $grouplines = explode("\n", $configstr);
  96  
  97          $groups = array();
  98  
  99          foreach ($grouplines as $groupline) {
 100              $line = explode('=', $groupline);
 101              if (count($line) > 1) {
 102                  $group = trim(array_shift($line));
 103                  $plugins = array_map('trim', explode(',', array_shift($line)));
 104                  $groups[$group] = $plugins;
 105              }
 106          }
 107  
 108          $modules = array('moodle-editor_atto-editor');
 109          $options['context'] = empty($options['context']) ? context_system::instance() : $options['context'];
 110  
 111          $jsplugins = array();
 112          foreach ($groups as $group => $plugins) {
 113              $groupplugins = array();
 114              foreach ($plugins as $plugin) {
 115                  // Do not die on missing plugin.
 116                  if (!core_component::get_component_directory('atto_' . $plugin))  {
 117                      continue;
 118                  }
 119  
 120                  // Remove manage files if requested.
 121                  if ($plugin == 'managefiles' && isset($options['enable_filemanagement']) && !$options['enable_filemanagement']) {
 122                      continue;
 123                  }
 124  
 125                  $jsplugin = array();
 126                  $jsplugin['name'] = $plugin;
 127                  $jsplugin['params'] = array();
 128                  $modules[] = 'moodle-atto_' . $plugin . '-button';
 129  
 130                  component_callback('atto_' . $plugin, 'strings_for_js');
 131                  $extra = component_callback('atto_' . $plugin, 'params_for_js', array($elementid, $options, $fpoptions));
 132  
 133                  if ($extra) {
 134                      $jsplugin = array_merge($jsplugin, $extra);
 135                  }
 136                  // We always need the plugin name.
 137                  $PAGE->requires->string_for_js('pluginname', 'atto_' . $plugin);
 138                  $groupplugins[] = $jsplugin;
 139              }
 140              $jsplugins[] = array('group'=>$group, 'plugins'=>$groupplugins);
 141          }
 142  
 143          $PAGE->requires->strings_for_js([
 144                  'editor_command_keycode',
 145                  'editor_control_keycode',
 146                  'plugin_title_shortcut',
 147                  'textrecovered',
 148                  'autosavefailed',
 149                  'autosavesucceeded',
 150                  'errortextrecovery',
 151                  'richtexteditor',
 152              ], 'editor_atto');
 153          $PAGE->requires->strings_for_js(array(
 154                  'warning',
 155                  'info'
 156              ), 'moodle');
 157          $PAGE->requires->yui_module($modules,
 158                                      'Y.M.editor_atto.Editor.init',
 159                                      array($this->get_init_params($elementid, $options, $fpoptions, $jsplugins)));
 160  
 161      }
 162  
 163      /**
 164       * Create a params array to init the editor.
 165       *
 166       * @param string $elementid
 167       * @param array $options
 168       * @param array $fpoptions
 169       */
 170      protected function get_init_params($elementid, array $options = null, array $fpoptions = null, $plugins = null) {
 171          global $PAGE;
 172  
 173          $directionality = get_string('thisdirection', 'langconfig');
 174          $strtime        = get_string('strftimetime');
 175          $strdate        = get_string('strftimedaydate');
 176          $lang           = current_language();
 177          $autosave       = true;
 178          $autosavefrequency = get_config('editor_atto', 'autosavefrequency');
 179          if (isset($options['autosave'])) {
 180              $autosave       = $options['autosave'];
 181          }
 182          $contentcss     = $PAGE->theme->editor_css_url()->out(false);
 183  
 184          // Autosave disabled for guests and not logged in users.
 185          if (isguestuser() OR !isloggedin()) {
 186              $autosave = false;
 187          }
 188          // Note <> is a safe separator, because it will not appear in the output of s().
 189          $pagehash = sha1($PAGE->url . '<>' . s($this->get_text()));
 190          $params = array(
 191              'elementid' => $elementid,
 192              'content_css' => $contentcss,
 193              'contextid' => $options['context']->id,
 194              'autosaveEnabled' => $autosave,
 195              'autosaveFrequency' => $autosavefrequency,
 196              'language' => $lang,
 197              'directionality' => $directionality,
 198              'filepickeroptions' => array(),
 199              'plugins' => $plugins,
 200              'pageHash' => $pagehash,
 201          );
 202          if ($fpoptions) {
 203              $params['filepickeroptions'] = $fpoptions;
 204          }
 205          return $params;
 206      }
 207  }