[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/editor/atto/ -> lib.php (source)

   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  <[email protected]>
  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  <[email protected]>
  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       * @param string $elementid
  73       * @param array $options
  74       * @param null $fpoptions
  75       */
  76      public function use_editor($elementid, array $options=null, $fpoptions=null) {
  77          global $PAGE;
  78  
  79          $configstr = get_config('editor_atto', 'toolbar');
  80  
  81          $grouplines = explode("\n", $configstr);
  82  
  83          $groups = array();
  84  
  85          foreach ($grouplines as $groupline) {
  86              $line = explode('=', $groupline);
  87              if (count($line) > 1) {
  88                  $group = trim(array_shift($line));
  89                  $plugins = array_map('trim', explode(',', array_shift($line)));
  90                  $groups[$group] = $plugins;
  91              }
  92          }
  93  
  94          $modules = array('moodle-editor_atto-editor');
  95          $options['context'] = empty($options['context']) ? context_system::instance() : $options['context'];
  96  
  97          $jsplugins = array();
  98          foreach ($groups as $group => $plugins) {
  99              $groupplugins = array();
 100              foreach ($plugins as $plugin) {
 101                  // Do not die on missing plugin.
 102                  if (!core_component::get_component_directory('atto_' . $plugin))  {
 103                      continue;
 104                  }
 105  
 106                  $jsplugin = array();
 107                  $jsplugin['name'] = $plugin;
 108                  $jsplugin['params'] = array();
 109                  $modules[] = 'moodle-atto_' . $plugin . '-button';
 110  
 111                  component_callback('atto_' . $plugin, 'strings_for_js');
 112                  $extra = component_callback('atto_' . $plugin, 'params_for_js', array($elementid, $options, $fpoptions));
 113  
 114                  if ($extra) {
 115                      $jsplugin = array_merge($jsplugin, $extra);
 116                  }
 117                  // We always need the plugin name.
 118                  $PAGE->requires->string_for_js('pluginname', 'atto_' . $plugin);
 119                  $groupplugins[] = $jsplugin;
 120              }
 121              $jsplugins[] = array('group'=>$group, 'plugins'=>$groupplugins);
 122          }
 123  
 124          $PAGE->requires->strings_for_js(array(
 125                  'editor_command_keycode',
 126                  'editor_control_keycode',
 127                  'plugin_title_shortcut',
 128                  'textrecovered',
 129                  'autosavefailed',
 130                  'autosavesucceeded',
 131                  'errortextrecovery'
 132              ), 'editor_atto');
 133          $PAGE->requires->strings_for_js(array(
 134                  'warning',
 135                  'info'
 136              ), 'moodle');
 137          $PAGE->requires->yui_module($modules,
 138                                      'Y.M.editor_atto.Editor.init',
 139                                      array($this->get_init_params($elementid, $options, $fpoptions, $jsplugins)));
 140  
 141      }
 142  
 143      /**
 144       * Create a params array to init the editor.
 145       *
 146       * @param string $elementid
 147       * @param array $options
 148       * @param array $fpoptions
 149       */
 150      protected function get_init_params($elementid, array $options = null, array $fpoptions = null, $plugins = null) {
 151          global $PAGE;
 152  
 153          $directionality = get_string('thisdirection', 'langconfig');
 154          $strtime        = get_string('strftimetime');
 155          $strdate        = get_string('strftimedaydate');
 156          $lang           = current_language();
 157          $autosave       = true;
 158          $autosavefrequency = get_config('editor_atto', 'autosavefrequency');
 159          if (isset($options['autosave'])) {
 160              $autosave       = $options['autosave'];
 161          }
 162          $contentcss     = $PAGE->theme->editor_css_url()->out(false);
 163  
 164          $params = array(
 165              'elementid' => $elementid,
 166              'content_css' => $contentcss,
 167              'contextid' => $options['context']->id,
 168              'autosaveEnabled' => $autosave,
 169              'autosaveFrequency' => $autosavefrequency,
 170              'language' => $lang,
 171              'directionality' => $directionality,
 172              'filepickeroptions' => array(),
 173              'plugins' => $plugins,
 174              'pageHash' => sha1($PAGE->url)
 175          );
 176          if ($fpoptions) {
 177              $params['filepickeroptions'] = $fpoptions;
 178          }
 179          return $params;
 180      }
 181  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1