Merge branch 'MDL-33441' of git://github.com/danpoltawski/moodle
[moodle.git] / lib / editorlib.php
blob4c73e1d341813a304abc2f4a5c1ca392089a3571
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
18 /**
19 * Utility classes and functions for text editor integration.
21 * @package core
22 * @subpackage editor
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();
29 /**
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) {
36 global $USER;
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
43 $editor = false;
44 foreach ($enabled as $e) {
45 if (!$e->supported_by_browser()) {
46 // bad luck, this editor is not compatible
47 continue;
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
51 $editor = $e;
52 continue;
54 if (!$supports = $e->get_supported_formats()) {
55 // buggy editor!
56 continue;
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
62 $editor = $e;
63 continue;
64 } else {
65 $editor = $e;
66 break;
69 if (in_array($format, $supports)) {
70 // editor supports this format, yay!
71 $editor = $e;
72 break;
76 if (!$editor) {
77 $editor = get_texteditor('textarea'); // must exist and can edit anything
80 return $editor;
83 /**
84 * Returns users preferred text format.
85 * @return int standard text format
87 function editors_get_preferred_format() {
88 global $USER;
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
99 continue;
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() {
114 global $CFG;
116 if (empty($CFG->texteditors)) {
117 $CFG->texteditors = 'tinymce,textarea';
119 $active = array();
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
130 return $active;
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) {
140 global $CFG;
142 $libfile = "$CFG->libdir/editor/$editorname/lib.php";
143 if (!file_exists($libfile)) {
144 return false;
146 require_once($libfile);
147 $classname = $editorname.'_texteditor';
148 if (!class_exists($classname)) {
149 return false;
151 return new $classname();
155 * Get the list of available editors
157 * @return array Array ('editorname'=>'localised editor name')
159 function editors_get_available() {
160 $editors = array();
161 foreach (get_plugin_list('editor') as $editorname => $dir) {
162 $editors[$editorname] = get_string('pluginname', 'editor_'.$editorname);
164 return $editors;
168 * Setup all JS and CSS needed for editors.
169 * @return void
171 function editors_head_setup() {
172 global $CFG;
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)) {
181 continue;
183 if (!$editor->supported_by_browser()) {
184 // bad luck, this editor is not compatible
185 continue;
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?
201 * @return bool
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
228 * @return void
230 public abstract function use_editor($elementid, array $options=null, $fpoptions = null);
233 * Setup all JS and CSS needed for editor.
234 * @return void
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
245 * @return bool
247 function can_use_html_editor() {
248 global $USER;
250 $editors = editors_get_enabled();
251 if (count($editors) > 1) {
252 if (empty($USER->htmleditor)) {
253 return false;
257 foreach ($editors as $editor) {
258 if ($editor->get_preferred_format() == FORMAT_HTML) {
259 return true;
263 return false;