MDL-73532 navigation: Secondary navigation for single activity course
[moodle.git] / lib / editorlib.php
blob383f2bf6e5288536410286a2e37f1f714a2b3198
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, $CFG;
38 if (!empty($CFG->adminsetuppending)) {
39 // Must not use other editors before install completed!
40 return get_texteditor('textarea');
43 $enabled = editors_get_enabled();
45 $preference = get_user_preferences('htmleditor', '', $USER);
47 if (isset($enabled[$preference])) {
48 // Edit the list of editors so the users preferred editor is first in the list.
49 $editor = $enabled[$preference];
50 unset($enabled[$preference]);
51 array_unshift($enabled, $editor);
54 // now find some plugin that supports format and is available
55 $editor = false;
56 foreach ($enabled as $e) {
57 if (!$e->supported_by_browser()) {
58 // bad luck, this editor is not compatible
59 continue;
61 if (!$supports = $e->get_supported_formats()) {
62 // buggy editor!
63 continue;
65 if (is_null($format) || in_array($format, $supports)) {
66 // editor supports this format, yay!
67 $editor = $e;
68 break;
72 if (!$editor) {
73 $editor = get_texteditor('textarea'); // must exist and can edit anything
76 return $editor;
79 /**
80 * Returns users preferred text format.
81 * @return int standard text format
83 function editors_get_preferred_format() {
84 global $USER;
86 $editor = editors_get_preferred_editor();
87 return $editor->get_preferred_format();
90 /**
91 * Returns list of enabled text editors
92 * @return array of name=>texteditor
94 function editors_get_enabled() {
95 global $CFG;
97 if (empty($CFG->texteditors)) {
98 $CFG->texteditors = 'atto,tinymce,textarea';
100 $active = array();
101 foreach(explode(',', $CFG->texteditors) as $e) {
102 if ($editor = get_texteditor($e)) {
103 $active[$e] = $editor;
107 if (empty($active)) {
108 return array('textarea'=>get_texteditor('textarea')); // must exist and can edit anything
111 return $active;
115 * Returns instance of text editor
117 * @param string $editorname name of editor (textarea, tinymce, ...)
118 * @return object|bool texeditor instance or false if does not exist
120 function get_texteditor($editorname) {
121 global $CFG;
123 $libfile = "$CFG->libdir/editor/$editorname/lib.php";
124 if (!file_exists($libfile)) {
125 return false;
127 require_once($libfile);
128 $classname = $editorname.'_texteditor';
129 if (!class_exists($classname)) {
130 return false;
132 return new $classname();
136 * Get the list of available editors
138 * @return array Array ('editorname'=>'localised editor name')
140 function editors_get_available() {
141 $editors = array();
142 foreach (core_component::get_plugin_list('editor') as $editorname => $dir) {
143 $editors[$editorname] = get_string('pluginname', 'editor_'.$editorname);
145 return $editors;
149 * Setup all JS and CSS needed for editors.
150 * @return void
152 function editors_head_setup() {
153 global $CFG;
155 if (empty($CFG->texteditors)) {
156 $CFG->texteditors = 'atto,tinymce,textarea';
158 $active = explode(',', $CFG->texteditors);
160 foreach ($active as $editorname) {
161 if (!$editor = get_texteditor($editorname)) {
162 continue;
164 if (!$editor->supported_by_browser()) {
165 // bad luck, this editor is not compatible
166 continue;
168 $editor->head_setup();
173 * Base abstract text editor class.
175 * @copyright 2009 Petr Skoda {@link http://skodak.org}
176 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
177 * @package moodlecore
179 abstract class texteditor {
181 * Is editor supported in current browser?
182 * @return bool
184 public abstract function supported_by_browser();
187 * Returns list of supported text formats
188 * @return array Array (FORMAT=>FORMAT)
190 public abstract function get_supported_formats();
193 * Returns main preferred text format.
194 * @return int text format
196 public abstract function get_preferred_format();
199 * Supports file picker and repos?
200 * @return object book object
202 public abstract function supports_repositories();
205 * @var string $text The text set to the editor in the form.
206 * @since 3.0
208 protected $text = '';
211 * Set the text set for this form field. Will be called before "use_editor".
212 * @param string $text The text for the form field.
214 public function set_text($text) {
215 $this->text = $text;
219 * Get the text set for this form field. Can be called from "use_editor".
220 * @return string
222 public function get_text() {
223 return $this->text;
227 * Add required JS needed for editor
229 * Valid options may vary by editor. See the individual editor
230 * implementations of this function for documentation.
232 * @param string $elementid id of text area to be converted to editor
233 * @param array $options Editor options
234 * @param obejct $fpoptions file picker options
235 * @return void
237 public abstract function use_editor($elementid, array $options=null, $fpoptions = null);
240 * Setup all JS and CSS needed for editor.
241 * @return void
243 public function head_setup() {