fix git cvs drift
[moodle.git] / lib / editorlib.php
CommitLineData
5ca3c838 1<?php
2
8fd57583 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/>.
17
18/**
19 * Utility classes and functions for text editor integration.
20 *
21 * @package moodlecore
22 * @subpackage editor
11e1f828 23 * @copyright 2009 Petr Skoda {@link http://skodak.org}
8fd57583 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
5ca3c838 26
1905b5f6 27/**
28 * Returns users preferred editor for given format
11e1f828
MH
29 *
30 * @todo implement user preferences for text editors
31 *
32 * @global object
33 * @global object
1905b5f6 34 * @param int $format text format or null of none
11e1f828 35 * @return object texteditor object
1905b5f6 36 */
5ca3c838 37function get_preferred_texteditor($format=null) {
38 global $CFG, $USER;
39
40 if (empty($CFG->texteditors)) {
41 $CFG->texteditors = 'tinymce,textarea';
42 }
43 $active = explode(',', $CFG->texteditors);
44
45 // TODO: implement user preferences for text editors
46
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 }
71
72 if (!$editor) {
73 $editor = get_texteditor('textarea'); // must exist and can edit anything
74 }
75
76 return $editor;
77}
78
1905b5f6 79/**
80 * Returns instance of text editor
11e1f828
MH
81 *
82 * @global object
1905b5f6 83 * @param string $editorname name of editor (textarea, tinymce, ...)
11e1f828 84 * @return object|bool texeditor instance or false if does not exist
1905b5f6 85 */
86function get_texteditor($editorname) {
5ca3c838 87 global $CFG;
88
1905b5f6 89 $libfile = "$CFG->libdir/editor/$editorname/lib.php";
5ca3c838 90 if (!file_exists($libfile)) {
91 return false;
92 }
93 require_once($libfile);
1905b5f6 94 $classname = $editorname.'_texteditor';
5ca3c838 95 if (!class_exists($classname)) {
96 return false;
97 }
98 return new $classname();
99}
100
1905b5f6 101/**
5ca3c838 102 * Get the list of available editors
11e1f828
MH
103 *
104 * @return array Array ('editorname'=>'localised editor name')
5ca3c838 105 */
106function get_available_editors() {
107 $editors = array();
1905b5f6 108 foreach (get_list_of_plugins('lib/editor') as $editorname) {
109 $editors[$editorname] = get_string('modulename', 'editor_'.$editorname);
5ca3c838 110 }
111 return $editors;
112}
113
114/**
1905b5f6 115 * Base abstract text editor class.
11e1f828
MH
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
5ca3c838 120 */
121abstract class texteditor {
1905b5f6 122 /**
123 * Is editor supported in current browser?
124 * @return bool
125 */
5ca3c838 126 public abstract function supported_by_browser();
1905b5f6 127
128 /**
129 * Returns list of supported text formats
11e1f828 130 * @return array Array (FORMAT=>FORMAT)
1905b5f6 131 */
5ca3c838 132 public abstract function get_supported_formats();
1905b5f6 133
134 /**
135 * Returns main preferred text format.
136 * @return int text format
137 */
5ca3c838 138 public abstract function get_preferred_format();
1905b5f6 139
140 /**
141 * Supports file picker and repos?
11e1f828 142 * @return object book object
1905b5f6 143 */
5ca3c838 144 public abstract function supports_repositories();
1905b5f6 145
146 /**
147 * Returns textarea class in formslib editor element
148 * @return string
149 */
5ca3c838 150 public abstract function get_editor_element_class();
1905b5f6 151
152 /**
153 * Returns textarea class for legacy text editor
154 * @return string
155 */
5ca3c838 156 public abstract function get_legacy_textarea_class();
1905b5f6 157
158 /**
159 * Returns js script statements needed in html head.
160 * @return string
161 */
5ca3c838 162 public abstract function header_js();
163}
164
165
166
167//=== DEPRECATED =====================
168/**
1905b5f6 169 * can_use_html_editor is deprecated...
11e1f828
MH
170 * @deprecated
171 * @todo Deprecated: eradicate completely, replace with something else
172 * @return bool
5ca3c838 173 */
174function can_use_html_editor() {
1905b5f6 175 //TODO: eradicate completely, replace with something else
5ca3c838 176
1905b5f6 177 $tinymyce = get_texteditor('tinymce');
178 return $tinymyce ->supported_by_browser();
5ca3c838 179}