2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Editor input element
21 * Contains class to create preffered editor form element
24 * @copyright 2009 Petr Skoda {@link http://skodak.org}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 require_once('HTML/QuickForm/element.php');
31 require_once($CFG->dirroot.'/lib/filelib.php');
32 require_once($CFG->dirroot.'/repository/lib.php');
37 * It creates preffered editor (textbox/TinyMce) form element for the format (Text/HTML) selected.
41 * @copyright 2009 Petr Skoda {@link http://skodak.org}
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 * @todo MDL-29421 element Freezing
44 * @todo MDL-29426 ajax format conversion
46 class MoodleQuickForm_editor extends HTML_QuickForm_element {
47 /** @var string html for help button, if empty then no help will icon will be dispalyed. */
48 public $_helpbutton = '';
50 /** @var string defines the type of editor */
51 public $_type = 'editor';
53 /** @var array options provided to initalize filepicker */
54 protected $_options = array('subdirs'=>0, 'maxbytes'=>0, 'maxfiles'=>0, 'changeformat'=>0,
55 'context'=>null, 'noclean'=>0, 'trusttext'=>0, 'return_types'=>7);
56 // $_options['return_types'] = FILE_INTERNAL | FILE_EXTERNAL | FILE_REFERENCE
58 /** @var array values for editor */
59 protected $_values = array('text'=>null, 'format'=>null, 'itemid'=>null);
64 * @param string $elementName (optional) name of the editor
65 * @param string $elementLabel (optional) editor label
66 * @param array $attributes (optional) Either a typical HTML attribute string
67 * or an associative array
68 * @param array $options set of options to initalize filepicker
70 function MoodleQuickForm_editor($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
73 $options = (array)$options;
74 foreach ($options as $name=>$value) {
75 if (array_key_exists($name, $this->_options)) {
76 $this->_options[$name] = $value;
79 if (!empty($options['maxbytes'])) {
80 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']);
82 if (!$this->_options['context']) {
83 // trying to set context to the current page context to make legacy files show in filepicker (e.g. forum post)
84 if (!empty($PAGE->context->id)) {
85 $this->_options['context'] = $PAGE->context;
87 $this->_options['context'] = context_system::instance();
90 $this->_options['trusted'] = trusttext_trusted($this->_options['context']);
91 parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
99 * @param string $name name of the editor
101 function setName($name) {
102 $this->updateAttributes(array('name'=>$name));
106 * Returns name of element
111 return $this->getAttribute('name');
115 * Updates editor values, if part of $_values
117 * @param array $values associative array of values to set
119 function setValue($values) {
120 $values = (array)$values;
121 foreach ($values as $name=>$value) {
122 if (array_key_exists($name, $this->_values)) {
123 $this->_values[$name] = $value;
129 * Returns editor values
133 function getValue() {
134 return $this->_values;
138 * Returns maximum file size which can be uploaded
142 function getMaxbytes() {
143 return $this->_options['maxbytes'];
147 * Sets maximum file size which can be uploaded
149 * @param int $maxbytes file size
151 function setMaxbytes($maxbytes) {
153 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $maxbytes);
157 * Returns maximum number of files which can be uploaded
161 function getMaxfiles() {
162 return $this->_options['maxfiles'];
166 * Sets maximum number of files which can be uploaded.
168 * @param int $num number of files
170 function setMaxfiles($num) {
171 $this->_options['maxfiles'] = $num;
175 * Returns true if subdirectoy can be created, else false
179 function getSubdirs() {
180 return $this->_options['subdirs'];
184 * Set option to create sub directory, while uploading file
186 * @param bool $allow true if sub directory can be created.
188 function setSubdirs($allow) {
189 $this->_options['subdirs'] = $allow;
193 * Returns editor format
197 function getFormat() {
198 return $this->_values['format'];
202 * Checks if editor used is a required field
204 * @return bool true if required field.
206 function isRequired() {
207 return (isset($this->_options['required']) && $this->_options['required']);
211 * Sets help button for editor
213 * @param mixed $_helpbuttonargs arguments to create help button
214 * @param string $function name of the callback function
215 * @deprecated since Moodle 2.0. Please do not call this function any more.
216 * @todo MDL-34508 this api will be removed.
217 * @see MoodleQuickForm::addHelpButton()
219 function setHelpButton($_helpbuttonargs, $function='_helpbutton') {
220 debugging('setHelpButton() is deprecated, please use $mform->addHelpButton() instead');
221 if (!is_array($_helpbuttonargs)) {
222 $_helpbuttonargs = array($_helpbuttonargs);
224 $_helpbuttonargs = $_helpbuttonargs;
226 //we do this to to return html instead of printing it
227 //without having to specify it in every call to make a button.
228 if ('_helpbutton' == $function){
229 $defaultargs = array('', '', 'moodle', true, false, '', true);
230 $_helpbuttonargs = $_helpbuttonargs + $defaultargs ;
232 $this->_helpbutton=call_user_func_array($function, $_helpbuttonargs);
236 * Returns html for help button.
238 * @return string html for help button
240 function getHelpButton() {
241 return $this->_helpbutton;
245 * Returns type of editor element
249 function getElementTemplateType() {
250 if ($this->_flagFrozen){
258 * Returns HTML for editor form element.
264 require_once($CFG->dirroot.'/repository/lib.php');
266 if ($this->_flagFrozen) {
267 return $this->getFrozenHtml();
270 $ctx = $this->_options['context'];
272 $id = $this->_attributes['id'];
273 $elname = $this->_attributes['name'];
275 $subdirs = $this->_options['subdirs'];
276 $maxbytes = $this->_options['maxbytes'];
277 $maxfiles = $this->_options['maxfiles'];
278 $changeformat = $this->_options['changeformat']; // TO DO: implement as ajax calls
280 $text = $this->_values['text'];
281 $format = $this->_values['format'];
282 $draftitemid = $this->_values['itemid'];
284 // security - never ever allow guest/not logged in user to upload anything
285 if (isguestuser() or !isloggedin()) {
289 $str = $this->_getTabs();
292 $editor = editors_get_preferred_editor($format);
293 $strformats = format_text_menu();
294 $formats = $editor->get_supported_formats();
295 foreach ($formats as $fid) {
296 $formats[$fid] = $strformats[$fid];
299 // get filepicker info
301 $fpoptions = array();
302 if ($maxfiles != 0 ) {
303 if (empty($draftitemid)) {
304 // no existing area info provided - let's use fresh new draft area
305 require_once("$CFG->libdir/filelib.php");
306 $this->setValue(array('itemid'=>file_get_unused_draft_itemid()));
307 $draftitemid = $this->_values['itemid'];
310 $args = new stdClass();
311 // need these three to filter repositories list
312 $args->accepted_types = array('web_image');
313 $args->return_types = $this->_options['return_types'];
314 $args->context = $ctx;
315 $args->env = 'filepicker';
317 $image_options = initialise_filepicker($args);
318 $image_options->context = $ctx;
319 $image_options->client_id = uniqid();
320 $image_options->maxbytes = $this->_options['maxbytes'];
321 $image_options->env = 'editor';
322 $image_options->itemid = $draftitemid;
324 // moodlemedia plugin
325 $args->accepted_types = array('video', 'audio');
326 $media_options = initialise_filepicker($args);
327 $media_options->context = $ctx;
328 $media_options->client_id = uniqid();
329 $media_options->maxbytes = $this->_options['maxbytes'];
330 $media_options->env = 'editor';
331 $media_options->itemid = $draftitemid;
334 $args->accepted_types = '*';
335 $link_options = initialise_filepicker($args);
336 $link_options->context = $ctx;
337 $link_options->client_id = uniqid();
338 $link_options->maxbytes = $this->_options['maxbytes'];
339 $link_options->env = 'editor';
340 $link_options->itemid = $draftitemid;
342 $fpoptions['image'] = $image_options;
343 $fpoptions['media'] = $media_options;
344 $fpoptions['link'] = $link_options;
347 //If editor is required and tinymce, then set required_tinymce option to initalize tinymce validation.
348 if (($editor instanceof tinymce_texteditor) && !is_null($this->getAttribute('onchange'))) {
349 $this->_options['required'] = true;
352 // print text area - TODO: add on-the-fly switching, size configuration, etc.
353 $editor->use_editor($id, $this->_options, $fpoptions);
355 $rows = empty($this->_attributes['rows']) ? 15 : $this->_attributes['rows'];
356 $cols = empty($this->_attributes['cols']) ? 80 : $this->_attributes['cols'];
358 //Apply editor validation if required field
360 if (!is_null($this->getAttribute('onblur')) && !is_null($this->getAttribute('onchange'))) {
361 $editorrules = ' onblur="'.htmlspecialchars($this->getAttribute('onblur')).'" onchange="'.htmlspecialchars($this->getAttribute('onchange')).'"';
363 $str .= '<div><textarea id="'.$id.'" name="'.$elname.'[text]" rows="'.$rows.'" cols="'.$cols.'"'.$editorrules.'>';
365 $str .= '</textarea></div>';
368 if (count($formats)>1) {
369 $str .= html_writer::label(get_string('format'), 'menu'. $elname. 'format', false, array('class' => 'accesshide'));
370 $str .= html_writer::select($formats, $elname.'[format]', $format, false, array('id' => 'menu'. $elname. 'format'));
372 $keys = array_keys($formats);
373 $str .= html_writer::empty_tag('input',
374 array('name'=>$elname.'[format]', 'type'=> 'hidden', 'value' => array_pop($keys)));
378 // during moodle installation, user area doesn't exist
379 // so we need to disable filepicker here.
380 if (!during_initial_install() && empty($CFG->adminsetuppending)) {
381 // 0 means no files, -1 unlimited
382 if ($maxfiles != 0 ) {
383 $str .= '<input type="hidden" name="'.$elname.'[itemid]" value="'.$draftitemid.'" />';
385 // used by non js editor only
386 $editorurl = new moodle_url("$CFG->wwwroot/repository/draftfiles_manager.php", array(
389 'itemid'=>$draftitemid,
391 'maxbytes'=>$maxbytes,
392 'maxfiles'=>$maxfiles,
394 'course'=>$PAGE->course->id,
395 'sesskey'=>sesskey(),
397 $str .= '<noscript>';
398 $str .= "<div><object type='text/html' data='$editorurl' height='160' width='600' style='border:1px solid #000'></object></div>";
399 $str .= '</noscript>';
410 * What to display when element is frozen.
412 * @return empty string
414 function getFrozenHtml() {