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 'areamaxbytes' => FILE_AREA_MAX_BYTES_UNLIMITED, 'context' => null, 'noclean' => 0, 'trusttext' => 0,
56 'return_types' => 7, 'collapsible' => 0, 'collapsed' => 0);
57 // $_options['return_types'] = FILE_INTERNAL | FILE_EXTERNAL | FILE_REFERENCE
59 /** @var array values for editor */
60 protected $_values = array('text'=>null, 'format'=>null, 'itemid'=>null);
65 * @param string $elementName (optional) name of the editor
66 * @param string $elementLabel (optional) editor label
67 * @param array $attributes (optional) Either a typical HTML attribute string
68 * or an associative array
69 * @param array $options set of options to initalize filepicker
71 function MoodleQuickForm_editor($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
74 $options = (array)$options;
75 foreach ($options as $name=>$value) {
76 if (array_key_exists($name, $this->_options)) {
77 $this->_options[$name] = $value;
80 if (!empty($options['maxbytes'])) {
81 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']);
83 if (!$this->_options['context']) {
84 // trying to set context to the current page context to make legacy files show in filepicker (e.g. forum post)
85 if (!empty($PAGE->context->id)) {
86 $this->_options['context'] = $PAGE->context;
88 $this->_options['context'] = context_system::instance();
91 $this->_options['trusted'] = trusttext_trusted($this->_options['context']);
92 parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
100 * @param string $name name of the editor
102 function setName($name) {
103 $this->updateAttributes(array('name'=>$name));
107 * Returns name of element
112 return $this->getAttribute('name');
116 * Updates editor values, if part of $_values
118 * @param array $values associative array of values to set
120 function setValue($values) {
121 $values = (array)$values;
122 foreach ($values as $name=>$value) {
123 if (array_key_exists($name, $this->_values)) {
124 $this->_values[$name] = $value;
130 * Returns editor values
134 function getValue() {
135 return $this->_values;
139 * Returns maximum file size which can be uploaded
143 function getMaxbytes() {
144 return $this->_options['maxbytes'];
148 * Sets maximum file size which can be uploaded
150 * @param int $maxbytes file size
152 function setMaxbytes($maxbytes) {
154 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $maxbytes);
158 * Returns the maximum size of the area.
162 function getAreamaxbytes() {
163 return $this->_options['areamaxbytes'];
167 * Sets the maximum size of the area.
169 * @param int $areamaxbytes size limit
171 function setAreamaxbytes($areamaxbytes) {
172 $this->_options['areamaxbytes'] = $areamaxbytes;
176 * Returns maximum number of files which can be uploaded
180 function getMaxfiles() {
181 return $this->_options['maxfiles'];
185 * Sets maximum number of files which can be uploaded.
187 * @param int $num number of files
189 function setMaxfiles($num) {
190 $this->_options['maxfiles'] = $num;
194 * Returns true if subdirectoy can be created, else false
198 function getSubdirs() {
199 return $this->_options['subdirs'];
203 * Set option to create sub directory, while uploading file
205 * @param bool $allow true if sub directory can be created.
207 function setSubdirs($allow) {
208 $this->_options['subdirs'] = $allow;
212 * Returns editor format
216 function getFormat() {
217 return $this->_values['format'];
221 * Checks if editor used is a required field
223 * @return bool true if required field.
225 function isRequired() {
226 return (isset($this->_options['required']) && $this->_options['required']);
230 * @deprecated since Moodle 2.0
232 function setHelpButton($_helpbuttonargs, $function='_helpbutton') {
233 throw new coding_exception('setHelpButton() can not be used any more, please see MoodleQuickForm::addHelpButton().');
237 * Returns html for help button.
239 * @return string html for help button
241 function getHelpButton() {
242 return $this->_helpbutton;
246 * Returns type of editor element
250 function getElementTemplateType() {
251 if ($this->_flagFrozen){
259 * Returns HTML for editor form element.
265 require_once($CFG->dirroot.'/repository/lib.php');
267 if ($this->_flagFrozen) {
268 return $this->getFrozenHtml();
271 $ctx = $this->_options['context'];
273 $id = $this->_attributes['id'];
274 $elname = $this->_attributes['name'];
276 $subdirs = $this->_options['subdirs'];
277 $maxbytes = $this->_options['maxbytes'];
278 $areamaxbytes = $this->_options['areamaxbytes'];
279 $maxfiles = $this->_options['maxfiles'];
280 $changeformat = $this->_options['changeformat']; // TO DO: implement as ajax calls
282 $text = $this->_values['text'];
283 $format = $this->_values['format'];
284 $draftitemid = $this->_values['itemid'];
286 // security - never ever allow guest/not logged in user to upload anything
287 if (isguestuser() or !isloggedin()) {
291 $str = $this->_getTabs();
294 $editor = editors_get_preferred_editor($format);
295 $strformats = format_text_menu();
296 $formats = $editor->get_supported_formats();
297 foreach ($formats as $fid) {
298 $formats[$fid] = $strformats[$fid];
301 // get filepicker info
303 $fpoptions = array();
304 if ($maxfiles != 0 ) {
305 if (empty($draftitemid)) {
306 // no existing area info provided - let's use fresh new draft area
307 require_once("$CFG->libdir/filelib.php");
308 $this->setValue(array('itemid'=>file_get_unused_draft_itemid()));
309 $draftitemid = $this->_values['itemid'];
312 $args = new stdClass();
313 // need these three to filter repositories list
314 $args->accepted_types = array('web_image');
315 $args->return_types = $this->_options['return_types'];
316 $args->context = $ctx;
317 $args->env = 'filepicker';
319 $image_options = initialise_filepicker($args);
320 $image_options->context = $ctx;
321 $image_options->client_id = uniqid();
322 $image_options->maxbytes = $this->_options['maxbytes'];
323 $image_options->areamaxbytes = $this->_options['areamaxbytes'];
324 $image_options->env = 'editor';
325 $image_options->itemid = $draftitemid;
327 // moodlemedia plugin
328 $args->accepted_types = array('video', 'audio');
329 $media_options = initialise_filepicker($args);
330 $media_options->context = $ctx;
331 $media_options->client_id = uniqid();
332 $media_options->maxbytes = $this->_options['maxbytes'];
333 $media_options->areamaxbytes = $this->_options['areamaxbytes'];
334 $media_options->env = 'editor';
335 $media_options->itemid = $draftitemid;
338 $args->accepted_types = '*';
339 $link_options = initialise_filepicker($args);
340 $link_options->context = $ctx;
341 $link_options->client_id = uniqid();
342 $link_options->maxbytes = $this->_options['maxbytes'];
343 $link_options->areamaxbytes = $this->_options['areamaxbytes'];
344 $link_options->env = 'editor';
345 $link_options->itemid = $draftitemid;
347 $fpoptions['image'] = $image_options;
348 $fpoptions['media'] = $media_options;
349 $fpoptions['link'] = $link_options;
352 //If editor is required and tinymce, then set required_tinymce option to initalize tinymce validation.
353 if (($editor instanceof tinymce_texteditor) && !is_null($this->getAttribute('onchange'))) {
354 $this->_options['required'] = true;
357 // print text area - TODO: add on-the-fly switching, size configuration, etc.
358 $editor->use_editor($id, $this->_options, $fpoptions);
360 $rows = empty($this->_attributes['rows']) ? 15 : $this->_attributes['rows'];
361 $cols = empty($this->_attributes['cols']) ? 80 : $this->_attributes['cols'];
363 //Apply editor validation if required field
365 if (!is_null($this->getAttribute('onblur')) && !is_null($this->getAttribute('onchange'))) {
366 $editorrules = ' onblur="'.htmlspecialchars($this->getAttribute('onblur')).'" onchange="'.htmlspecialchars($this->getAttribute('onchange')).'"';
368 $str .= '<div><textarea id="'.$id.'" name="'.$elname.'[text]" rows="'.$rows.'" cols="'.$cols.'" spellcheck="true"';
370 if (isset($this->_options['collapsed']) && $this->_options['collapsed']) {
371 $this->_options['collapsible'] = 1;
372 $classes[] = 'collapsed';
374 if (isset($this->_options['collapsible']) && $this->_options['collapsible']) {
375 $classes[] = 'collapsible';
377 $str .= ' class="' . implode(' ', $classes) . '"';
378 $str .= $editorrules.'>';
380 $str .= '</textarea></div>';
383 if (count($formats)>1) {
384 $str .= html_writer::label(get_string('format'), 'menu'. $elname. 'format', false, array('class' => 'accesshide'));
385 $str .= html_writer::select($formats, $elname.'[format]', $format, false, array('id' => 'menu'. $elname. 'format'));
387 $keys = array_keys($formats);
388 $str .= html_writer::empty_tag('input',
389 array('name'=>$elname.'[format]', 'type'=> 'hidden', 'value' => array_pop($keys)));
393 // during moodle installation, user area doesn't exist
394 // so we need to disable filepicker here.
395 if (!during_initial_install() && empty($CFG->adminsetuppending)) {
396 // 0 means no files, -1 unlimited
397 if ($maxfiles != 0 ) {
398 $str .= '<input type="hidden" name="'.$elname.'[itemid]" value="'.$draftitemid.'" />';
400 // used by non js editor only
401 $editorurl = new moodle_url("$CFG->wwwroot/repository/draftfiles_manager.php", array(
404 'itemid'=>$draftitemid,
406 'maxbytes'=>$maxbytes,
407 'areamaxbytes' => $areamaxbytes,
408 'maxfiles'=>$maxfiles,
410 'course'=>$PAGE->course->id,
411 'sesskey'=>sesskey(),
413 $str .= '<noscript>';
414 $str .= "<div><object type='text/html' data='$editorurl' height='160' width='600' style='border:1px solid #000'></object></div>";
415 $str .= '</noscript>';
426 * What to display when element is frozen.
428 * @return empty string
430 function getFrozenHtml() {