Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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(array(
 144                  'editor_command_keycode',
 145                  'editor_control_keycode',
 146                  'plugin_title_shortcut',
 147                  'textrecovered',
 148                  'autosavefailed',
 149                  'autosavesucceeded',
 150                  'errortextrecovery'
 151              ), 'editor_atto');
 152          $PAGE->requires->strings_for_js(array(
 153                  'warning',
 154                  'info'
 155              ), 'moodle');
 156          $PAGE->requires->yui_module($modules,
 157                                      'Y.M.editor_atto.Editor.init',
 158                                      array($this->get_init_params($elementid, $options, $fpoptions, $jsplugins)));
 159  
 160      }
 161  
 162      /**
 163       * Create a params array to init the editor.
 164       *
 165       * @param string $elementid
 166       * @param array $options
 167       * @param array $fpoptions
 168       */
 169      protected function get_init_params($elementid, array $options = null, array $fpoptions = null, $plugins = null) {
 170          global $PAGE;
 171  
 172          $directionality = get_string('thisdirection', 'langconfig');
 173          $strtime        = get_string('strftimetime');
 174          $strdate        = get_string('strftimedaydate');
 175          $lang           = current_language();
 176          $autosave       = true;
 177          $autosavefrequency = get_config('editor_atto', 'autosavefrequency');
 178          if (isset($options['autosave'])) {
 179              $autosave       = $options['autosave'];
 180          }
 181          $contentcss     = $PAGE->theme->editor_css_url()->out(false);
 182  
 183          // Autosave disabled for guests and not logged in users.
 184          if (isguestuser() OR !isloggedin()) {
 185              $autosave = false;
 186          }
 187          // Note <> is a safe separator, because it will not appear in the output of s().
 188          $pagehash = sha1($PAGE->url . '<>' . s($this->get_text()));
 189          $params = array(
 190              'elementid' => $elementid,
 191              'content_css' => $contentcss,
 192              'contextid' => $options['context']->id,
 193              'autosaveEnabled' => $autosave,
 194              'autosaveFrequency' => $autosavefrequency,
 195              'language' => $lang,
 196              'directionality' => $directionality,
 197              'filepickeroptions' => array(),
 198              'plugins' => $plugins,
 199              'pageHash' => $pagehash,
 200          );
 201          if ($fpoptions) {
 202              $params['filepickeroptions'] = $fpoptions;
 203          }
 204          return $params;
 205      }
 206  }