|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 defined('MOODLE_INTERNAL') || die(); 00028 00035 function editors_get_preferred_editor($format = NULL) { 00036 global $USER; 00037 00038 $enabled = editors_get_enabled(); 00039 00040 $preventhtml = (count($enabled) > 1 and empty($USER->htmleditor)); 00041 00042 // now find some plugin that supports format and is available 00043 $editor = false; 00044 foreach ($enabled as $e) { 00045 if (!$e->supported_by_browser()) { 00046 // bad luck, this editor is not compatible 00047 continue; 00048 } 00049 if ($preventhtml and $format == FORMAT_HTML and $e->get_preferred_format() == FORMAT_HTML) { 00050 // this is really not what we want but we could use it if nothing better found 00051 $editor = $e; 00052 continue; 00053 } 00054 if (!$supports = $e->get_supported_formats()) { 00055 // buggy editor! 00056 continue; 00057 } 00058 if (is_null($format)) { 00059 // format does not matter 00060 if ($preventhtml and $e->get_preferred_format() == FORMAT_HTML) { 00061 // this is really not what we want but we could use it if nothing better found 00062 $editor = $e; 00063 continue; 00064 } else { 00065 $editor = $e; 00066 break; 00067 } 00068 } 00069 if (in_array($format, $supports)) { 00070 // editor supports this format, yay! 00071 $editor = $e; 00072 break; 00073 } 00074 } 00075 00076 if (!$editor) { 00077 $editor = get_texteditor('textarea'); // must exist and can edit anything 00078 } 00079 00080 return $editor; 00081 } 00082 00087 function editors_get_preferred_format() { 00088 global $USER; 00089 00090 $editors = editors_get_enabled(); 00091 if (count($editors) == 1) { 00092 $editor = reset($editors); 00093 return $editor->get_preferred_format(); 00094 } 00095 00096 foreach ($editors as $editor) { 00097 if (empty($USER->htmleditor) and $editor->get_preferred_format() == FORMAT_HTML) { 00098 // we do not prefer this one 00099 continue; 00100 } 00101 return $editor->get_preferred_format(); 00102 } 00103 00104 // user did not want html editor, but there is no other choice, sorry 00105 $editor = reset($editors); 00106 return $editor->get_preferred_format(); 00107 } 00108 00113 function editors_get_enabled() { 00114 global $CFG; 00115 00116 if (empty($CFG->texteditors)) { 00117 $CFG->texteditors = 'tinymce,textarea'; 00118 } 00119 $active = array(); 00120 foreach(explode(',', $CFG->texteditors) as $e) { 00121 if ($editor = get_texteditor($e)) { 00122 $active[$e] = $editor; 00123 } 00124 } 00125 00126 if (empty($active)) { 00127 return array('textarea'=>get_texteditor('textarea')); // must exist and can edit anything 00128 } 00129 00130 return $active; 00131 } 00132 00139 function get_texteditor($editorname) { 00140 global $CFG; 00141 00142 $libfile = "$CFG->libdir/editor/$editorname/lib.php"; 00143 if (!file_exists($libfile)) { 00144 return false; 00145 } 00146 require_once($libfile); 00147 $classname = $editorname.'_texteditor'; 00148 if (!class_exists($classname)) { 00149 return false; 00150 } 00151 return new $classname(); 00152 } 00153 00159 function editors_get_available() { 00160 $editors = array(); 00161 foreach (get_plugin_list('editor') as $editorname => $dir) { 00162 $editors[$editorname] = get_string('pluginname', 'editor_'.$editorname); 00163 } 00164 return $editors; 00165 } 00166 00171 function editors_head_setup() { 00172 global $CFG; 00173 00174 if (empty($CFG->texteditors)) { 00175 $CFG->texteditors = 'tinymce,textarea'; 00176 } 00177 $active = explode(',', $CFG->texteditors); 00178 00179 foreach ($active as $editorname) { 00180 if (!$editor = get_texteditor($editorname)) { 00181 continue; 00182 } 00183 if (!$editor->supported_by_browser()) { 00184 // bad luck, this editor is not compatible 00185 continue; 00186 } 00187 $editor->head_setup(); 00188 } 00189 } 00190 00198 abstract class texteditor { 00203 public abstract function supported_by_browser(); 00204 00209 public abstract function get_supported_formats(); 00210 00215 public abstract function get_preferred_format(); 00216 00221 public abstract function supports_repositories(); 00222 00230 public abstract function use_editor($elementid, array $options=null, $fpoptions = null); 00231 00236 public function head_setup() { 00237 } 00238 } 00239 00240 //=== TO BE DEPRECATED in 2.1 ===================== 00241 00247 function can_use_html_editor() { 00248 global $USER; 00249 00250 $editors = editors_get_enabled(); 00251 if (count($editors) > 1) { 00252 if (empty($USER->htmleditor)) { 00253 return false; 00254 } 00255 } 00256 00257 foreach ($editors as $editor) { 00258 if ($editor->get_preferred_format() == FORMAT_HTML) { 00259 return true; 00260 } 00261 } 00262 00263 return false; 00264 }