3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Utility classes and functions for text editor integration.
23 * @copyright 2009 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
30 * Returns users preferred editor for given format
32 * @param int $format text format or null of none
33 * @return texteditor object
35 function editors_get_preferred_editor($format = NULL) {
38 $enabled = editors_get_enabled();
40 $preventhtml = (count($enabled) > 1 and empty($USER->htmleditor
));
42 // now find some plugin that supports format and is available
44 foreach ($enabled as $e) {
45 if (!$e->supported_by_browser()) {
46 // bad luck, this editor is not compatible
49 if ($preventhtml and $format == FORMAT_HTML
and $e->get_preferred_format() == FORMAT_HTML
) {
50 // this is really not what we want but we could use it if nothing better found
54 if (!$supports = $e->get_supported_formats()) {
58 if (is_null($format)) {
59 // format does not matter
60 if ($preventhtml and $e->get_preferred_format() == FORMAT_HTML
) {
61 // this is really not what we want but we could use it if nothing better found
69 if (in_array($format, $supports)) {
70 // editor supports this format, yay!
77 $editor = get_texteditor('textarea'); // must exist and can edit anything
84 * Returns users preferred text format.
85 * @return int standard text format
87 function editors_get_preferred_format() {
90 $editors = editors_get_enabled();
91 if (count($editors) == 1) {
92 $editor = reset($editors);
93 return $editor->get_preferred_format();
96 foreach ($editors as $editor) {
97 if (empty($USER->htmleditor
) and $editor->get_preferred_format() == FORMAT_HTML
) {
98 // we do not prefer this one
101 return $editor->get_preferred_format();
104 // user did not want html editor, but there is no other choice, sorry
105 $editor = reset($editors);
106 return $editor->get_preferred_format();
110 * Returns list of enabled text editors
111 * @return array of name=>texteditor
113 function editors_get_enabled() {
116 if (empty($CFG->texteditors
)) {
117 $CFG->texteditors
= 'tinymce,atto,textarea';
120 foreach(explode(',', $CFG->texteditors
) as $e) {
121 if ($editor = get_texteditor($e)) {
122 $active[$e] = $editor;
126 if (empty($active)) {
127 return array('textarea'=>get_texteditor('textarea')); // must exist and can edit anything
134 * Returns instance of text editor
136 * @param string $editorname name of editor (textarea, tinymce, ...)
137 * @return object|bool texeditor instance or false if does not exist
139 function get_texteditor($editorname) {
142 $libfile = "$CFG->libdir/editor/$editorname/lib.php";
143 if (!file_exists($libfile)) {
146 require_once($libfile);
147 $classname = $editorname.'_texteditor';
148 if (!class_exists($classname)) {
151 return new $classname();
155 * Get the list of available editors
157 * @return array Array ('editorname'=>'localised editor name')
159 function editors_get_available() {
161 foreach (core_component
::get_plugin_list('editor') as $editorname => $dir) {
162 $editors[$editorname] = get_string('pluginname', 'editor_'.$editorname);
168 * Setup all JS and CSS needed for editors.
171 function editors_head_setup() {
174 if (empty($CFG->texteditors
)) {
175 $CFG->texteditors
= 'tinymce,textarea';
177 $active = explode(',', $CFG->texteditors
);
179 foreach ($active as $editorname) {
180 if (!$editor = get_texteditor($editorname)) {
183 if (!$editor->supported_by_browser()) {
184 // bad luck, this editor is not compatible
187 $editor->head_setup();
192 * Base abstract text editor class.
194 * @copyright 2009 Petr Skoda {@link http://skodak.org}
195 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
196 * @package moodlecore
198 abstract class texteditor
{
200 * Is editor supported in current browser?
203 public abstract function supported_by_browser();
206 * Returns list of supported text formats
207 * @return array Array (FORMAT=>FORMAT)
209 public abstract function get_supported_formats();
212 * Returns main preferred text format.
213 * @return int text format
215 public abstract function get_preferred_format();
218 * Supports file picker and repos?
219 * @return object book object
221 public abstract function supports_repositories();
224 * Add required JS needed for editor
225 * @param string $elementid id of text area to be converted to editor
226 * @param array $options
227 * @param obejct $fpoptions file picker options
230 public abstract function use_editor($elementid, array $options=null, $fpoptions = null);
233 * Setup all JS and CSS needed for editor.
236 public function head_setup() {
240 //=== TO BE DEPRECATED in 2.1 =====================
243 * Does the user want and can edit using rich text html editor?
244 * @todo Deprecate: eradicate completely, replace with something else in the future
247 function can_use_html_editor() {
250 $editors = editors_get_enabled();
251 if (count($editors) > 1) {
252 if (empty($USER->htmleditor
)) {
257 foreach ($editors as $editor) {
258 if ($editor->get_preferred_format() == FORMAT_HTML
) {