/lib/editorlib.php

https://github.com/mnoorenberghe/moodle · PHP · 264 lines · 129 code · 34 blank · 101 comment · 28 complexity · af6599bd6e61ebc40c33857873060195 MD5 · raw file

  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Utility classes and functions for text editor integration.
  18. *
  19. * @package core
  20. * @subpackage editor
  21. * @copyright 2009 Petr Skoda {@link http://skodak.org}
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. /**
  26. * Returns users preferred editor for given format
  27. *
  28. * @param int $format text format or null of none
  29. * @return texteditor object
  30. */
  31. function editors_get_preferred_editor($format = NULL) {
  32. global $USER;
  33. $enabled = editors_get_enabled();
  34. $preventhtml = (count($enabled) > 1 and empty($USER->htmleditor));
  35. // now find some plugin that supports format and is available
  36. $editor = false;
  37. foreach ($enabled as $e) {
  38. if (!$e->supported_by_browser()) {
  39. // bad luck, this editor is not compatible
  40. continue;
  41. }
  42. if ($preventhtml and $format == FORMAT_HTML and $e->get_preferred_format() == FORMAT_HTML) {
  43. // this is really not what we want but we could use it if nothing better found
  44. $editor = $e;
  45. continue;
  46. }
  47. if (!$supports = $e->get_supported_formats()) {
  48. // buggy editor!
  49. continue;
  50. }
  51. if (is_null($format)) {
  52. // format does not matter
  53. if ($preventhtml and $e->get_preferred_format() == FORMAT_HTML) {
  54. // this is really not what we want but we could use it if nothing better found
  55. $editor = $e;
  56. continue;
  57. } else {
  58. $editor = $e;
  59. break;
  60. }
  61. }
  62. if (in_array($format, $supports)) {
  63. // editor supports this format, yay!
  64. $editor = $e;
  65. break;
  66. }
  67. }
  68. if (!$editor) {
  69. $editor = get_texteditor('textarea'); // must exist and can edit anything
  70. }
  71. return $editor;
  72. }
  73. /**
  74. * Returns users preferred text format.
  75. * @return int standard text format
  76. */
  77. function editors_get_preferred_format() {
  78. global $USER;
  79. $editors = editors_get_enabled();
  80. if (count($editors) == 1) {
  81. $editor = reset($editors);
  82. return $editor->get_preferred_format();
  83. }
  84. foreach ($editors as $editor) {
  85. if (empty($USER->htmleditor) and $editor->get_preferred_format() == FORMAT_HTML) {
  86. // we do not prefer this one
  87. continue;
  88. }
  89. return $editor->get_preferred_format();
  90. }
  91. // user did not want html editor, but there is no other choice, sorry
  92. $editor = reset($editors);
  93. return $editor->get_preferred_format();
  94. }
  95. /**
  96. * Returns list of enabled text editors
  97. * @return array of name=>texteditor
  98. */
  99. function editors_get_enabled() {
  100. global $CFG;
  101. if (empty($CFG->texteditors)) {
  102. $CFG->texteditors = 'tinymce,atto,textarea';
  103. }
  104. $active = array();
  105. foreach(explode(',', $CFG->texteditors) as $e) {
  106. if ($editor = get_texteditor($e)) {
  107. $active[$e] = $editor;
  108. }
  109. }
  110. if (empty($active)) {
  111. return array('textarea'=>get_texteditor('textarea')); // must exist and can edit anything
  112. }
  113. return $active;
  114. }
  115. /**
  116. * Returns instance of text editor
  117. *
  118. * @param string $editorname name of editor (textarea, tinymce, ...)
  119. * @return object|bool texeditor instance or false if does not exist
  120. */
  121. function get_texteditor($editorname) {
  122. global $CFG;
  123. $libfile = "$CFG->libdir/editor/$editorname/lib.php";
  124. if (!file_exists($libfile)) {
  125. return false;
  126. }
  127. require_once($libfile);
  128. $classname = $editorname.'_texteditor';
  129. if (!class_exists($classname)) {
  130. return false;
  131. }
  132. return new $classname();
  133. }
  134. /**
  135. * Get the list of available editors
  136. *
  137. * @return array Array ('editorname'=>'localised editor name')
  138. */
  139. function editors_get_available() {
  140. $editors = array();
  141. foreach (core_component::get_plugin_list('editor') as $editorname => $dir) {
  142. $editors[$editorname] = get_string('pluginname', 'editor_'.$editorname);
  143. }
  144. return $editors;
  145. }
  146. /**
  147. * Setup all JS and CSS needed for editors.
  148. * @return void
  149. */
  150. function editors_head_setup() {
  151. global $CFG;
  152. if (empty($CFG->texteditors)) {
  153. $CFG->texteditors = 'tinymce,textarea';
  154. }
  155. $active = explode(',', $CFG->texteditors);
  156. foreach ($active as $editorname) {
  157. if (!$editor = get_texteditor($editorname)) {
  158. continue;
  159. }
  160. if (!$editor->supported_by_browser()) {
  161. // bad luck, this editor is not compatible
  162. continue;
  163. }
  164. $editor->head_setup();
  165. }
  166. }
  167. /**
  168. * Base abstract text editor class.
  169. *
  170. * @copyright 2009 Petr Skoda {@link http://skodak.org}
  171. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  172. * @package moodlecore
  173. */
  174. abstract class texteditor {
  175. /**
  176. * Is editor supported in current browser?
  177. * @return bool
  178. */
  179. public abstract function supported_by_browser();
  180. /**
  181. * Returns list of supported text formats
  182. * @return array Array (FORMAT=>FORMAT)
  183. */
  184. public abstract function get_supported_formats();
  185. /**
  186. * Returns main preferred text format.
  187. * @return int text format
  188. */
  189. public abstract function get_preferred_format();
  190. /**
  191. * Supports file picker and repos?
  192. * @return object book object
  193. */
  194. public abstract function supports_repositories();
  195. /**
  196. * Add required JS needed for editor
  197. * @param string $elementid id of text area to be converted to editor
  198. * @param array $options
  199. * @param obejct $fpoptions file picker options
  200. * @return void
  201. */
  202. public abstract function use_editor($elementid, array $options=null, $fpoptions = null);
  203. /**
  204. * Setup all JS and CSS needed for editor.
  205. * @return void
  206. */
  207. public function head_setup() {
  208. }
  209. }
  210. //=== TO BE DEPRECATED in 2.1 =====================
  211. /**
  212. * Does the user want and can edit using rich text html editor?
  213. * @todo Deprecate: eradicate completely, replace with something else in the future
  214. * @return bool
  215. */
  216. function can_use_html_editor() {
  217. global $USER;
  218. $editors = editors_get_enabled();
  219. if (count($editors) > 1) {
  220. if (empty($USER->htmleditor)) {
  221. return false;
  222. }
  223. }
  224. foreach ($editors as $editor) {
  225. if ($editor->get_preferred_format() == FORMAT_HTML) {
  226. return true;
  227. }
  228. }
  229. return false;
  230. }