[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 18 /** 19 * Editor input element 20 * 21 * Contains class to create preffered editor form element 22 * 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 26 */ 27 28 global $CFG; 29 30 require_once('HTML/QuickForm/element.php'); 31 require_once($CFG->dirroot.'/lib/filelib.php'); 32 require_once($CFG->dirroot.'/repository/lib.php'); 33 34 /** 35 * Editor element 36 * 37 * It creates preffered editor (textbox/TinyMce) form element for the format (Text/HTML) selected. 38 * 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 45 */ 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 = ''; 49 50 /** @var string defines the type of editor */ 51 public $_type = 'editor'; 52 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 58 59 /** @var array values for editor */ 60 protected $_values = array('text'=>null, 'format'=>null, 'itemid'=>null); 61 62 /** 63 * Constructor 64 * 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 70 */ 71 function MoodleQuickForm_editor($elementName=null, $elementLabel=null, $attributes=null, $options=null) { 72 global $CFG, $PAGE; 73 74 $options = (array)$options; 75 foreach ($options as $name=>$value) { 76 if (array_key_exists($name, $this->_options)) { 77 $this->_options[$name] = $value; 78 } 79 } 80 if (!empty($options['maxbytes'])) { 81 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']); 82 } 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(); 89 } 90 } 91 $this->_options['trusted'] = trusttext_trusted($this->_options['context']); 92 parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes); 93 94 // Note: for some reason the code using this setting does not like bools. 95 $this->_options['subdirs'] = (int)($this->_options['subdirs'] == 1); 96 97 editors_head_setup(); 98 } 99 100 /** 101 * Called by HTML_QuickForm whenever form event is made on this element 102 * 103 * @param string $event Name of event 104 * @param mixed $arg event arguments 105 * @param object $caller calling object 106 * @return bool 107 */ 108 function onQuickFormEvent($event, $arg, &$caller) 109 { 110 switch ($event) { 111 case 'createElement': 112 $caller->setType($arg[0] . '[format]', PARAM_ALPHANUM); 113 $caller->setType($arg[0] . '[itemid]', PARAM_INT); 114 break; 115 } 116 return parent::onQuickFormEvent($event, $arg, $caller); 117 } 118 119 /** 120 * Sets name of editor 121 * 122 * @param string $name name of the editor 123 */ 124 function setName($name) { 125 $this->updateAttributes(array('name'=>$name)); 126 } 127 128 /** 129 * Returns name of element 130 * 131 * @return string 132 */ 133 function getName() { 134 return $this->getAttribute('name'); 135 } 136 137 /** 138 * Updates editor values, if part of $_values 139 * 140 * @param array $values associative array of values to set 141 */ 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; 147 } 148 } 149 } 150 151 /** 152 * Returns editor values 153 * 154 * @return array 155 */ 156 function getValue() { 157 return $this->_values; 158 } 159 160 /** 161 * Returns maximum file size which can be uploaded 162 * 163 * @return int 164 */ 165 function getMaxbytes() { 166 return $this->_options['maxbytes']; 167 } 168 169 /** 170 * Sets maximum file size which can be uploaded 171 * 172 * @param int $maxbytes file size 173 */ 174 function setMaxbytes($maxbytes) { 175 global $CFG; 176 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $maxbytes); 177 } 178 179 /** 180 * Returns the maximum size of the area. 181 * 182 * @return int 183 */ 184 function getAreamaxbytes() { 185 return $this->_options['areamaxbytes']; 186 } 187 188 /** 189 * Sets the maximum size of the area. 190 * 191 * @param int $areamaxbytes size limit 192 */ 193 function setAreamaxbytes($areamaxbytes) { 194 $this->_options['areamaxbytes'] = $areamaxbytes; 195 } 196 197 /** 198 * Returns maximum number of files which can be uploaded 199 * 200 * @return int 201 */ 202 function getMaxfiles() { 203 return $this->_options['maxfiles']; 204 } 205 206 /** 207 * Sets maximum number of files which can be uploaded. 208 * 209 * @param int $num number of files 210 */ 211 function setMaxfiles($num) { 212 $this->_options['maxfiles'] = $num; 213 } 214 215 /** 216 * Returns true if subdirectoy can be created, else false 217 * 218 * @return bool 219 */ 220 function getSubdirs() { 221 return $this->_options['subdirs']; 222 } 223 224 /** 225 * Set option to create sub directory, while uploading file 226 * 227 * @param bool $allow true if sub directory can be created. 228 */ 229 function setSubdirs($allow) { 230 $this->_options['subdirs'] = (int)($allow == 1); 231 } 232 233 /** 234 * Returns editor format 235 * 236 * @return int. 237 */ 238 function getFormat() { 239 return $this->_values['format']; 240 } 241 242 /** 243 * Checks if editor used is a required field 244 * 245 * @return bool true if required field. 246 */ 247 function isRequired() { 248 return (isset($this->_options['required']) && $this->_options['required']); 249 } 250 251 /** 252 * @deprecated since Moodle 2.0 253 */ 254 function setHelpButton($_helpbuttonargs, $function='_helpbutton') { 255 throw new coding_exception('setHelpButton() can not be used any more, please see MoodleQuickForm::addHelpButton().'); 256 } 257 258 /** 259 * Returns html for help button. 260 * 261 * @return string html for help button 262 */ 263 function getHelpButton() { 264 return $this->_helpbutton; 265 } 266 267 /** 268 * Returns type of editor element 269 * 270 * @return string 271 */ 272 function getElementTemplateType() { 273 if ($this->_flagFrozen){ 274 return 'nodisplay'; 275 } else { 276 return 'default'; 277 } 278 } 279 280 /** 281 * Returns HTML for editor form element. 282 * 283 * @return string 284 */ 285 function toHtml() { 286 global $CFG, $PAGE; 287 require_once($CFG->dirroot.'/repository/lib.php'); 288 289 if ($this->_flagFrozen) { 290 return $this->getFrozenHtml(); 291 } 292 293 $ctx = $this->_options['context']; 294 295 $id = $this->_attributes['id']; 296 $elname = $this->_attributes['name']; 297 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 303 304 $text = $this->_values['text']; 305 $format = $this->_values['format']; 306 $draftitemid = $this->_values['itemid']; 307 308 // security - never ever allow guest/not logged in user to upload anything 309 if (isguestuser() or !isloggedin()) { 310 $maxfiles = 0; 311 } 312 313 $str = $this->_getTabs(); 314 $str .= '<div>'; 315 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]; 321 } 322 323 // get filepicker info 324 // 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']; 332 } 333 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; 348 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; 358 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; 368 369 $fpoptions['image'] = $image_options; 370 $fpoptions['media'] = $media_options; 371 $fpoptions['link'] = $link_options; 372 } 373 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; 377 } 378 379 // print text area - TODO: add on-the-fly switching, size configuration, etc. 380 $editor->use_editor($id, $this->_options, $fpoptions); 381 382 $rows = empty($this->_attributes['rows']) ? 15 : $this->_attributes['rows']; 383 $cols = empty($this->_attributes['cols']) ? 80 : $this->_attributes['cols']; 384 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')).'"'; 389 } 390 $str .= '<div><textarea id="'.$id.'" name="'.$elname.'[text]" rows="'.$rows.'" cols="'.$cols.'" spellcheck="true"'.$editorrules.'>'; 391 $str .= s($text); 392 $str .= '</textarea></div>'; 393 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))); 402 } 403 $str .= '</div>'; 404 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)); 412 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(), 425 )); 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>'; 429 } 430 } 431 432 433 $str .= '</div>'; 434 435 return $str; 436 } 437 438 /** 439 * What to display when element is frozen. 440 * 441 * @return empty string 442 */ 443 function getFrozenHtml() { 444 445 return ''; 446 } 447 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |