fix git cvs drift
[moodle.git] / lib / editorlib.php
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.
14 //
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.
20  *
21  * @package    moodlecore
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
25  */
27 /**
28  * Returns users preferred editor for given format
29  *
30  * @todo  implement user preferences for text editors
31  *
32  * @global object
33  * @global object
34  * @param int $format text format or null of none
35  * @return object texteditor object
36  */
37 function get_preferred_texteditor($format=null) {
38     global $CFG, $USER;
40     if (empty($CFG->texteditors)) {
41         $CFG->texteditors = 'tinymce,textarea';
42     }
43     $active = explode(',', $CFG->texteditors);
45     // TODO: implement user preferences for text editors
47     // now find some plugin that supports format and is available
48     $editor = false;
49     foreach ($active as $editorname) {
50         if (!$e = get_texteditor($editorname)) {
51             continue;
52         }
53         if (!$e->supported_by_browser()) {
54             // bad luck, this editor is not compatible
55             continue;
56         }
57         if (!$supports = $e->get_supported_formats()) {
58             continue;
59         }
60         if (is_null($format)) {
61             // format does not matter
62             $editor = $e;
63             break;
64         }
65         if (in_array($format, $supports)) {
66             // editor supports this format, yay!
67             $editor = $e;
68             break;
69         }
70     }
72     if (!$editor) {
73         $editor = get_texteditor('textarea'); // must exist and can edit anything
74     }
76     return $editor;
77 }
79 /**
80  * Returns instance of text editor
81  *
82  * @global object
83  * @param string $editorname name of editor (textarea, tinymce, ...)
84  * @return object|bool texeditor instance or false if does not exist
85  */
86 function get_texteditor($editorname) {
87     global $CFG;
89     $libfile = "$CFG->libdir/editor/$editorname/lib.php";
90     if (!file_exists($libfile)) {
91         return false;
92     }
93     require_once($libfile);
94     $classname = $editorname.'_texteditor';
95     if (!class_exists($classname)) {
96         return false;
97     }
98     return new $classname();
99 }
101 /**
102  * Get the list of available editors
103  *
104  * @return array Array ('editorname'=>'localised editor name')
105  */
106 function get_available_editors() {
107     $editors = array();
108     foreach (get_list_of_plugins('lib/editor') as $editorname) {
109         $editors[$editorname] = get_string('modulename', 'editor_'.$editorname);
110     }
111     return $editors;
114 /**
115  * Base abstract text editor class.
116  *
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
120  */
121 abstract class texteditor {
122     /**
123      * Is editor supported in current browser?
124      * @return bool
125      */
126     public abstract function supported_by_browser();
128     /**
129      * Returns list of supported text formats
130      * @return array Array (FORMAT=>FORMAT)
131      */
132     public abstract function get_supported_formats();
134     /**
135      * Returns main preferred text format.
136      * @return int text format
137      */
138     public abstract function get_preferred_format();
140     /**
141      * Supports file picker and repos?
142      * @return object book object
143      */
144     public abstract function supports_repositories();
146     /**
147      * Returns textarea class in formslib editor element
148      * @return string
149      */
150     public abstract function get_editor_element_class();
152     /**
153      * Returns textarea class for legacy text editor
154      * @return string
155      */
156     public abstract function get_legacy_textarea_class();
158     /**
159      * Returns js script statements needed in html head.
160      * @return string
161      */
162     public abstract function header_js();
167 //=== DEPRECATED =====================
168 /**
169  * can_use_html_editor is deprecated...
170  * @deprecated
171  * @todo Deprecated: eradicate completely, replace with something else
172  * @return bool
173  */
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();