MDL-39888 mnet: Removal of an old sql statement invloving the mdl_log table.
[moodle.git] / lib / form / editor.php
bloba463d2313626202e44b409c3efd599442b7f6b99
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.
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/>.
18 /**
19 * Editor input element
21 * Contains class to create preffered editor form element
23 * @package core_form
24 * @copyright 2009 Petr Skoda {@link http://skodak.org}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 global $CFG;
30 require_once('HTML/QuickForm/element.php');
31 require_once($CFG->dirroot.'/lib/filelib.php');
32 require_once($CFG->dirroot.'/repository/lib.php');
34 /**
35 * Editor element
37 * It creates preffered editor (textbox/TinyMce) form element for the format (Text/HTML) selected.
39 * @package core_form
40 * @category form
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);
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);
62 /**
63 * Constructor
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) {
72 global $CFG, $PAGE;
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;
87 } else {
88 $this->_options['context'] = context_system::instance();
91 $this->_options['trusted'] = trusttext_trusted($this->_options['context']);
92 parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
94 // Note: for some reason the code using this setting does not like bools.
95 $this->_options['subdirs'] = (int)($this->_options['subdirs'] == 1);
97 editors_head_setup();
101 * Called by HTML_QuickForm whenever form event is made on this element
103 * @param string $event Name of event
104 * @param mixed $arg event arguments
105 * @param object $caller calling object
106 * @return bool
108 function onQuickFormEvent($event, $arg, &$caller)
110 switch ($event) {
111 case 'createElement':
112 $caller->setType($arg[0] . '[format]', PARAM_ALPHANUM);
113 $caller->setType($arg[0] . '[itemid]', PARAM_INT);
114 break;
116 return parent::onQuickFormEvent($event, $arg, $caller);
120 * Sets name of editor
122 * @param string $name name of the editor
124 function setName($name) {
125 $this->updateAttributes(array('name'=>$name));
129 * Returns name of element
131 * @return string
133 function getName() {
134 return $this->getAttribute('name');
138 * Updates editor values, if part of $_values
140 * @param array $values associative array of values to set
142 function setValue($values) {
143 $values = (array)$values;
144 foreach ($values as $name=>$value) {
145 if (array_key_exists($name, $this->_values)) {
146 $this->_values[$name] = $value;
152 * Returns editor values
154 * @return array
156 function getValue() {
157 return $this->_values;
161 * Returns maximum file size which can be uploaded
163 * @return int
165 function getMaxbytes() {
166 return $this->_options['maxbytes'];
170 * Sets maximum file size which can be uploaded
172 * @param int $maxbytes file size
174 function setMaxbytes($maxbytes) {
175 global $CFG;
176 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $maxbytes);
180 * Returns the maximum size of the area.
182 * @return int
184 function getAreamaxbytes() {
185 return $this->_options['areamaxbytes'];
189 * Sets the maximum size of the area.
191 * @param int $areamaxbytes size limit
193 function setAreamaxbytes($areamaxbytes) {
194 $this->_options['areamaxbytes'] = $areamaxbytes;
198 * Returns maximum number of files which can be uploaded
200 * @return int
202 function getMaxfiles() {
203 return $this->_options['maxfiles'];
207 * Sets maximum number of files which can be uploaded.
209 * @param int $num number of files
211 function setMaxfiles($num) {
212 $this->_options['maxfiles'] = $num;
216 * Returns true if subdirectoy can be created, else false
218 * @return bool
220 function getSubdirs() {
221 return $this->_options['subdirs'];
225 * Set option to create sub directory, while uploading file
227 * @param bool $allow true if sub directory can be created.
229 function setSubdirs($allow) {
230 $this->_options['subdirs'] = (int)($allow == 1);
234 * Returns editor format
236 * @return int.
238 function getFormat() {
239 return $this->_values['format'];
243 * Checks if editor used is a required field
245 * @return bool true if required field.
247 function isRequired() {
248 return (isset($this->_options['required']) && $this->_options['required']);
252 * @deprecated since Moodle 2.0
254 function setHelpButton($_helpbuttonargs, $function='_helpbutton') {
255 throw new coding_exception('setHelpButton() can not be used any more, please see MoodleQuickForm::addHelpButton().');
259 * Returns html for help button.
261 * @return string html for help button
263 function getHelpButton() {
264 return $this->_helpbutton;
268 * Returns type of editor element
270 * @return string
272 function getElementTemplateType() {
273 if ($this->_flagFrozen){
274 return 'nodisplay';
275 } else {
276 return 'default';
281 * Returns HTML for editor form element.
283 * @return string
285 function toHtml() {
286 global $CFG, $PAGE;
287 require_once($CFG->dirroot.'/repository/lib.php');
289 if ($this->_flagFrozen) {
290 return $this->getFrozenHtml();
293 $ctx = $this->_options['context'];
295 $id = $this->_attributes['id'];
296 $elname = $this->_attributes['name'];
298 $subdirs = $this->_options['subdirs'];
299 $maxbytes = $this->_options['maxbytes'];
300 $areamaxbytes = $this->_options['areamaxbytes'];
301 $maxfiles = $this->_options['maxfiles'];
302 $changeformat = $this->_options['changeformat']; // TO DO: implement as ajax calls
304 $text = $this->_values['text'];
305 $format = $this->_values['format'];
306 $draftitemid = $this->_values['itemid'];
308 // security - never ever allow guest/not logged in user to upload anything
309 if (isguestuser() or !isloggedin()) {
310 $maxfiles = 0;
313 $str = $this->_getTabs();
314 $str .= '<div>';
316 $editor = editors_get_preferred_editor($format);
317 $strformats = format_text_menu();
318 $formats = $editor->get_supported_formats();
319 foreach ($formats as $fid) {
320 $formats[$fid] = $strformats[$fid];
323 // get filepicker info
325 $fpoptions = array();
326 if ($maxfiles != 0 ) {
327 if (empty($draftitemid)) {
328 // no existing area info provided - let's use fresh new draft area
329 require_once("$CFG->libdir/filelib.php");
330 $this->setValue(array('itemid'=>file_get_unused_draft_itemid()));
331 $draftitemid = $this->_values['itemid'];
334 $args = new stdClass();
335 // need these three to filter repositories list
336 $args->accepted_types = array('web_image');
337 $args->return_types = $this->_options['return_types'];
338 $args->context = $ctx;
339 $args->env = 'filepicker';
340 // advimage plugin
341 $image_options = initialise_filepicker($args);
342 $image_options->context = $ctx;
343 $image_options->client_id = uniqid();
344 $image_options->maxbytes = $this->_options['maxbytes'];
345 $image_options->areamaxbytes = $this->_options['areamaxbytes'];
346 $image_options->env = 'editor';
347 $image_options->itemid = $draftitemid;
349 // moodlemedia plugin
350 $args->accepted_types = array('video', 'audio');
351 $media_options = initialise_filepicker($args);
352 $media_options->context = $ctx;
353 $media_options->client_id = uniqid();
354 $media_options->maxbytes = $this->_options['maxbytes'];
355 $media_options->areamaxbytes = $this->_options['areamaxbytes'];
356 $media_options->env = 'editor';
357 $media_options->itemid = $draftitemid;
359 // advlink plugin
360 $args->accepted_types = '*';
361 $link_options = initialise_filepicker($args);
362 $link_options->context = $ctx;
363 $link_options->client_id = uniqid();
364 $link_options->maxbytes = $this->_options['maxbytes'];
365 $link_options->areamaxbytes = $this->_options['areamaxbytes'];
366 $link_options->env = 'editor';
367 $link_options->itemid = $draftitemid;
369 $fpoptions['image'] = $image_options;
370 $fpoptions['media'] = $media_options;
371 $fpoptions['link'] = $link_options;
374 //If editor is required and tinymce, then set required_tinymce option to initalize tinymce validation.
375 if (($editor instanceof tinymce_texteditor) && !is_null($this->getAttribute('onchange'))) {
376 $this->_options['required'] = true;
379 // print text area - TODO: add on-the-fly switching, size configuration, etc.
380 $editor->use_editor($id, $this->_options, $fpoptions);
382 $rows = empty($this->_attributes['rows']) ? 15 : $this->_attributes['rows'];
383 $cols = empty($this->_attributes['cols']) ? 80 : $this->_attributes['cols'];
385 //Apply editor validation if required field
386 $editorrules = '';
387 if (!is_null($this->getAttribute('onblur')) && !is_null($this->getAttribute('onchange'))) {
388 $editorrules = ' onblur="'.htmlspecialchars($this->getAttribute('onblur')).'" onchange="'.htmlspecialchars($this->getAttribute('onchange')).'"';
390 $str .= '<div><textarea id="'.$id.'" name="'.$elname.'[text]" rows="'.$rows.'" cols="'.$cols.'" spellcheck="true"'.$editorrules.'>';
391 $str .= s($text);
392 $str .= '</textarea></div>';
394 $str .= '<div>';
395 if (count($formats)>1) {
396 $str .= html_writer::label(get_string('format'), 'menu'. $elname. 'format', false, array('class' => 'accesshide'));
397 $str .= html_writer::select($formats, $elname.'[format]', $format, false, array('id' => 'menu'. $elname. 'format'));
398 } else {
399 $keys = array_keys($formats);
400 $str .= html_writer::empty_tag('input',
401 array('name'=>$elname.'[format]', 'type'=> 'hidden', 'value' => array_pop($keys)));
403 $str .= '</div>';
405 // during moodle installation, user area doesn't exist
406 // so we need to disable filepicker here.
407 if (!during_initial_install() && empty($CFG->adminsetuppending)) {
408 // 0 means no files, -1 unlimited
409 if ($maxfiles != 0 ) {
410 $str .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $elname.'[itemid]',
411 'value' => $draftitemid));
413 // used by non js editor only
414 $editorurl = new moodle_url("$CFG->wwwroot/repository/draftfiles_manager.php", array(
415 'action'=>'browse',
416 'env'=>'editor',
417 'itemid'=>$draftitemid,
418 'subdirs'=>$subdirs,
419 'maxbytes'=>$maxbytes,
420 'areamaxbytes' => $areamaxbytes,
421 'maxfiles'=>$maxfiles,
422 'ctx_id'=>$ctx->id,
423 'course'=>$PAGE->course->id,
424 'sesskey'=>sesskey(),
426 $str .= '<noscript>';
427 $str .= "<div><object type='text/html' data='$editorurl' height='160' width='600' style='border:1px solid #000'></object></div>";
428 $str .= '</noscript>';
433 $str .= '</div>';
435 return $str;
439 * What to display when element is frozen.
441 * @return empty string
443 function getFrozenHtml() {
445 return '';