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 if (!empty($CFG->adminsetuppending
)) {
39 // Must not use other editors before install completed!
40 return get_texteditor('textarea');
43 $enabled = editors_get_enabled();
45 $preference = get_user_preferences('htmleditor', '', $USER);
47 if (isset($enabled[$preference])) {
48 // Edit the list of editors so the users preferred editor is first in the list.
49 $editor = $enabled[$preference];
50 unset($enabled[$preference]);
51 array_unshift($enabled, $editor);
54 // now find some plugin that supports format and is available
56 foreach ($enabled as $e) {
57 if (!$e->supported_by_browser()) {
58 // bad luck, this editor is not compatible
61 if (!$supports = $e->get_supported_formats()) {
65 if (is_null($format) ||
in_array($format, $supports)) {
66 // editor supports this format, yay!
73 $editor = get_texteditor('textarea'); // must exist and can edit anything
80 * Returns users preferred text format.
81 * @return int standard text format
83 function editors_get_preferred_format() {
86 $editor = editors_get_preferred_editor();
87 return $editor->get_preferred_format();
91 * Returns list of enabled text editors
92 * @return array of name=>texteditor
94 function editors_get_enabled() {
97 if (empty($CFG->texteditors
)) {
98 $CFG->texteditors
= 'tinymce,textarea';
101 foreach(explode(',', $CFG->texteditors
) as $e) {
102 if ($editor = get_texteditor($e)) {
103 $active[$e] = $editor;
107 if (empty($active)) {
108 return array('textarea'=>get_texteditor('textarea')); // must exist and can edit anything
115 * Returns instance of text editor
117 * @param string $editorname name of editor (textarea, tinymce, ...)
118 * @return object|bool texeditor instance or false if does not exist
120 function get_texteditor($editorname) {
123 $libfile = "$CFG->libdir/editor/$editorname/lib.php";
124 if (!file_exists($libfile)) {
127 require_once($libfile);
128 $classname = $editorname.'_texteditor';
129 if (!class_exists($classname)) {
132 return new $classname();
136 * Get the list of available editors
138 * @return array Array ('editorname'=>'localised editor name')
140 function editors_get_available() {
142 foreach (core_component
::get_plugin_list('editor') as $editorname => $dir) {
143 $editors[$editorname] = get_string('pluginname', 'editor_'.$editorname);
149 * Setup all JS and CSS needed for editors.
152 function editors_head_setup() {
155 if (empty($CFG->texteditors
)) {
156 $CFG->texteditors
= 'tinymce,textarea';
158 $active = explode(',', $CFG->texteditors
);
160 foreach ($active as $editorname) {
161 if (!$editor = get_texteditor($editorname)) {
164 if (!$editor->supported_by_browser()) {
165 // bad luck, this editor is not compatible
168 $editor->head_setup();
173 * Base abstract text editor class.
175 * @copyright 2009 Petr Skoda {@link http://skodak.org}
176 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
177 * @package moodlecore
179 abstract class texteditor
{
181 * Is editor supported in current browser?
184 public abstract function supported_by_browser();
187 * Returns list of supported text formats
188 * @return array Array (FORMAT=>FORMAT)
190 public abstract function get_supported_formats();
193 * Returns main preferred text format.
194 * @return int text format
196 public abstract function get_preferred_format();
199 * Supports file picker and repos?
200 * @return object book object
202 public abstract function supports_repositories();
205 * Add required JS needed for editor
206 * @param string $elementid id of text area to be converted to editor
207 * @param array $options
208 * @param obejct $fpoptions file picker options
211 public abstract function use_editor($elementid, array $options=null, $fpoptions = null);
214 * Setup all JS and CSS needed for editor.
217 public function head_setup() {