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
28 * Returns users preferred editor for given format
30 * @todo implement user preferences for text editors
34 * @param int $format text format or null of none
35 * @return object texteditor object
37 function get_preferred_texteditor($format=null) {
40 if (empty($CFG->texteditors)) {
41 $CFG->texteditors = 'tinymce,textarea';
43 $active = explode(',', $CFG->texteditors);
45 // TODO: implement user preferences for text editors
47 // now find some plugin that supports format and is available
49 foreach ($active as $editorname) {
50 if (!$e = get_texteditor($editorname)) {
53 if (!$e->supported_by_browser()) {
54 // bad luck, this editor is not compatible
57 if (!$supports = $e->get_supported_formats()) {
60 if (is_null($format)) {
61 // format does not matter
65 if (in_array($format, $supports)) {
66 // editor supports this format, yay!
73 $editor = get_texteditor('textarea'); // must exist and can edit anything
80 * Returns instance of text editor
83 * @param string $editorname name of editor (textarea, tinymce, ...)
84 * @return object|bool texeditor instance or false if does not exist
86 function get_texteditor($editorname) {
89 $libfile = "$CFG->libdir/editor/$editorname/lib.php";
90 if (!file_exists($libfile)) {
93 require_once($libfile);
94 $classname = $editorname.'_texteditor';
95 if (!class_exists($classname)) {
98 return new $classname();
102 * Get the list of available editors
104 * @return array Array ('editorname'=>'localised editor name')
106 function get_available_editors() {
108 foreach (get_list_of_plugins('lib/editor') as $editorname) {
109 $editors[$editorname] = get_string('modulename', 'editor_'.$editorname);
115 * Base abstract text editor class.
117 * @copyright 2009 Petr Skoda {@link http://skodak.org}
118 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
119 * @package moodlecore
121 abstract class texteditor {
123 * Is editor supported in current browser?
126 public abstract function supported_by_browser();
129 * Returns list of supported text formats
130 * @return array Array (FORMAT=>FORMAT)
132 public abstract function get_supported_formats();
135 * Returns main preferred text format.
136 * @return int text format
138 public abstract function get_preferred_format();
141 * Supports file picker and repos?
142 * @return object book object
144 public abstract function supports_repositories();
147 * Returns textarea class in formslib editor element
150 public abstract function get_editor_element_class();
153 * Returns textarea class for legacy text editor
156 public abstract function get_legacy_textarea_class();
159 * Returns js script statements needed in html head.
162 public abstract function header_js();
167 //=== DEPRECATED =====================
169 * can_use_html_editor is deprecated...
171 * @todo Deprecated: eradicate completely, replace with something else
174 function can_use_html_editor() {
175 //TODO: eradicate completely, replace with something else
177 $tinymyce = get_texteditor('tinymce');
178 return $tinymyce ->supported_by_browser();