MDL-67585 core_course: add content_item_service class
[moodle.git] / lib / editor / atto / lib.php
blobbcd5c38eaf2182d1c5014738084e6bd39aed9ec0
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.
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/>.
17 /**
18 * YUI text editor integration.
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
25 defined('MOODLE_INTERNAL') || die();
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
32 class atto_texteditor extends texteditor {
34 /**
35 * Is the current browser supported by this editor?
37 * Of course!
38 * @return bool
40 public function supported_by_browser() {
41 return true;
44 /**
45 * Returns array of supported text formats.
46 * @return array
48 public function get_supported_formats() {
49 // FORMAT_MOODLE is not supported here, sorry.
50 return array(FORMAT_HTML => FORMAT_HTML);
53 /**
54 * Returns text format preferred by this editor.
55 * @return int
57 public function get_preferred_format() {
58 return FORMAT_HTML;
61 /**
62 * Does this editor support picking from repositories?
63 * @return bool
65 public function supports_repositories() {
66 return true;
69 /**
70 * Use this editor for given element.
72 * Available Atto-specific options:
73 * atto:toolbar - set to a string to override the system config editor_atto/toolbar
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
80 * Options are also passed through to the plugins.
82 * @param string $elementid
83 * @param array $options
84 * @param null $fpoptions
86 public function use_editor($elementid, array $options=null, $fpoptions=null) {
87 global $PAGE;
89 if (array_key_exists('atto:toolbar', $options)) {
90 $configstr = $options['atto:toolbar'];
91 } else {
92 $configstr = get_config('editor_atto', 'toolbar');
95 $grouplines = explode("\n", $configstr);
97 $groups = array();
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;
108 $modules = array('moodle-editor_atto-editor');
109 $options['context'] = empty($options['context']) ? context_system::instance() : $options['context'];
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;
120 // Remove manage files if requested.
121 if ($plugin == 'managefiles' && isset($options['enable_filemanagement']) && !$options['enable_filemanagement']) {
122 continue;
125 $jsplugin = array();
126 $jsplugin['name'] = $plugin;
127 $jsplugin['params'] = array();
128 $modules[] = 'moodle-atto_' . $plugin . '-button';
130 component_callback('atto_' . $plugin, 'strings_for_js');
131 $extra = component_callback('atto_' . $plugin, 'params_for_js', array($elementid, $options, $fpoptions));
133 if ($extra) {
134 $jsplugin = array_merge($jsplugin, $extra);
136 // We always need the plugin name.
137 $PAGE->requires->string_for_js('pluginname', 'atto_' . $plugin);
138 $groupplugins[] = $jsplugin;
140 $jsplugins[] = array('group'=>$group, 'plugins'=>$groupplugins);
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)));
163 * Create a params array to init the editor.
165 * @param string $elementid
166 * @param array $options
167 * @param array $fpoptions
169 protected function get_init_params($elementid, array $options = null, array $fpoptions = null, $plugins = null) {
170 global $PAGE;
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'];
181 $contentcss = $PAGE->theme->editor_css_url()->out(false);
183 // Autosave disabled for guests and not logged in users.
184 if (isguestuser() OR !isloggedin()) {
185 $autosave = false;
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,
201 if ($fpoptions) {
202 $params['filepickeroptions'] = $fpoptions;
204 return $params;