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 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_plugin_list('editor') as $editorname => $dir) {
109 $editors[$editorname] = get_string('pluginname', 'editor_'.$editorname);
115 * Setup all JS and CSS needed for editors.
118 function editors_head_setup() {
121 if (empty($CFG->texteditors)) {
122 $CFG->texteditors = 'tinymce,textarea';
124 $active = explode(',', $CFG->texteditors);
126 foreach ($active as $editorname) {
127 if (!$editor = get_texteditor($editorname)) {
130 if (!$editor->supported_by_browser()) {
131 // bad luck, this editor is not compatible
134 $editor->head_setup();
139 * Base abstract text editor class.
141 * @copyright 2009 Petr Skoda {@link http://skodak.org}
142 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
143 * @package moodlecore
145 abstract class texteditor {
147 * Is editor supported in current browser?
150 public abstract function supported_by_browser();
153 * Returns list of supported text formats
154 * @return array Array (FORMAT=>FORMAT)
156 public abstract function get_supported_formats();
159 * Returns main preferred text format.
160 * @return int text format
162 public abstract function get_preferred_format();
165 * Supports file picker and repos?
166 * @return object book object
168 public abstract function supports_repositories();
171 * Add required JS needed for editor
172 * @param string $elementid id of text area to be converted to editor
173 * @param array $options
174 * @param obejct $fpoptions file picker options
177 public abstract function use_editor($elementid, array $options=null, $fpoptions = null);
180 * Setup all JS and CSS needed for editor.
183 public function head_setup() {
187 //TODO: this is very wrong way to do admin settings - this has to be rewritten
188 require_once($CFG->libdir.'/formslib.php');
190 * Editor settings moodle form class.
192 * @copyright 2010 Dongsheng Cai
193 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
194 * @package moodlecore
196 class editor_settings_form extends moodleform {
197 public function definition() {
198 $mform =& $this->_form;
200 $mform->addElement('hidden', 'action', 'edit');
201 $this->add_action_buttons(true, get_string('savechanges'));
205 //=== DEPRECATED =====================
207 * can_use_html_editor is deprecated...
209 * @todo Deprecated: eradicate completely, replace with something else
212 function can_use_html_editor() {
213 //TODO: eradicate completely, replace with something else
215 $tinymyce = get_texteditor('tinymce');
216 return $tinymyce ->supported_by_browser();