5 require_once('HTML/QuickForm/element.php');
6 require_once($CFG->dirroot.'/lib/filelib.php');
7 require_once($CFG->dirroot.'/repository/lib.php');
12 // * ajax format conversion
14 class MoodleQuickForm_editor extends HTML_QuickForm_element {
15 public $_helpbutton = '';
17 public $_type = 'editor';
18 protected $_options = array('subdirs'=>0, 'maxbytes'=>0, 'maxfiles'=>0, 'changeformat'=>0,
19 'context'=>null, 'noclean'=>0, 'trusttext'=>0);
20 protected $_values = array('text'=>null, 'format'=>null, 'itemid'=>null);
22 function MoodleQuickForm_editor($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
25 $options = (array)$options;
26 foreach ($options as $name=>$value) {
27 if (array_key_exists($name, $this->_options)) {
28 $this->_options[$name] = $value;
31 if (!empty($options['maxbytes'])) {
32 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']);
34 if (!$this->_options['context']) {
35 $this->_options['context'] = get_context_instance(CONTEXT_SYSTEM);
37 $this->_options['trusted'] = trusttext_trusted($this->_options['context']);
38 parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
43 function setName($name) {
44 $this->updateAttributes(array('name'=>$name));
48 return $this->getAttribute('name');
51 function setValue($values) {
52 $values = (array)$values;
53 foreach ($values as $name=>$value) {
54 if (array_key_exists($name, $this->_values)) {
55 $this->_values[$name] = $value;
61 return $this->getAttribute('value');
64 function getMaxbytes() {
65 return $this->_options['maxbytes'];
68 function setMaxbytes($maxbytes) {
70 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $maxbytes);
73 function getMaxfiles() {
74 return $this->_options['maxfiles'];
77 function setMaxfiles($num) {
78 $this->_options['maxfiles'] = $num;
81 function getSubdirs() {
82 return $this->_options['subdirs'];
85 function setSubdirs($allow) {
86 $this->_options['subdirs'] = $allow;
90 * Returns editor format
94 function getFormat() {
95 return $this->_values['format'];
99 * Checks if editor used is tinymce and is required field
101 * @return true if required field.
103 function isRequired() {
104 return (isset($this->_options['required']) && $this->_options['required']);
107 function setHelpButton($_helpbuttonargs, $function='_helpbutton') {
108 if (!is_array($_helpbuttonargs)) {
109 $_helpbuttonargs = array($_helpbuttonargs);
111 $_helpbuttonargs = $_helpbuttonargs;
113 //we do this to to return html instead of printing it
114 //without having to specify it in every call to make a button.
115 if ('_helpbutton' == $function){
116 $defaultargs = array('', '', 'moodle', true, false, '', true);
117 $_helpbuttonargs = $_helpbuttonargs + $defaultargs ;
119 $this->_helpbutton=call_user_func_array($function, $_helpbuttonargs);
122 function getHelpButton() {
123 return $this->_helpbutton;
126 function getElementTemplateType() {
127 if ($this->_flagFrozen){
136 require_once($CFG->dirroot.'/repository/lib.php');
138 if ($this->_flagFrozen) {
139 return $this->getFrozenHtml();
142 $ctx = $this->_options['context'];
144 $id = $this->_attributes['id'];
145 $elname = $this->_attributes['name'];
147 $subdirs = $this->_options['subdirs'];
148 $maxbytes = $this->_options['maxbytes'];
149 $maxfiles = $this->_options['maxfiles'];
150 $changeformat = $this->_options['changeformat']; // TO DO: implement as ajax calls
152 $text = $this->_values['text'];
153 $format = $this->_values['format'];
154 $draftitemid = $this->_values['itemid'];
156 // security - never ever allow guest/not logged in user to upload anything
157 if (isguestuser() or !isloggedin()) {
161 $str = $this->_getTabs();
164 $editor = editors_get_preferred_editor($format);
165 $strformats = format_text_menu();
166 $formats = $editor->get_supported_formats();
167 foreach ($formats as $fid) {
168 $formats[$fid] = $strformats[$fid];
171 // get filepicker info
173 $fpoptions = array();
174 if ($maxfiles != 0 ) {
175 if (empty($draftitemid)) {
176 // no existing area info provided - let's use fresh new draft area
177 require_once("$CFG->libdir/filelib.php");
178 $this->setValue(array('itemid'=>file_get_unused_draft_itemid()));
179 $draftitemid = $this->_values['itemid'];
182 $args = new stdClass();
183 // need these three to filter repositories list
184 $args->accepted_types = array('image');
185 $args->return_types = (FILE_INTERNAL | FILE_EXTERNAL);
186 $args->context = $ctx;
187 $args->env = 'filepicker';
189 $image_options = initialise_filepicker($args);
190 $image_options->context = $ctx;
191 $image_options->client_id = uniqid();
192 $image_options->maxbytes = $this->_options['maxbytes'];
193 $image_options->env = 'editor';
194 $image_options->itemid = $draftitemid;
196 // moodlemedia plugin
197 $args->accepted_types = array('video', 'audio');
198 $media_options = initialise_filepicker($args);
199 $media_options->context = $ctx;
200 $media_options->client_id = uniqid();
201 $media_options->maxbytes = $this->_options['maxbytes'];
202 $media_options->env = 'editor';
203 $media_options->itemid = $draftitemid;
206 $args->accepted_types = '*';
207 $link_options = initialise_filepicker($args);
208 $link_options->context = $ctx;
209 $link_options->client_id = uniqid();
210 $link_options->maxbytes = $this->_options['maxbytes'];
211 $link_options->env = 'editor';
212 $link_options->itemid = $draftitemid;
214 $fpoptions['image'] = $image_options;
215 $fpoptions['media'] = $media_options;
216 $fpoptions['link'] = $link_options;
219 //If editor is required and tinymce, then set required_tinymce option to initalize tinymce validation.
220 if (($editor instanceof tinymce_texteditor) && !is_null($this->getAttribute('onchange'))) {
221 $this->_options['required'] = true;
224 /// print text area - TODO: add on-the-fly switching, size configuration, etc.
225 $editor->use_editor($id, $this->_options, $fpoptions);
227 $rows = empty($this->_attributes['rows']) ? 15 : $this->_attributes['rows'];
228 $cols = empty($this->_attributes['cols']) ? 80 : $this->_attributes['cols'];
230 //Apply editor validation if required field
232 if (!is_null($this->getAttribute('onblur')) && !is_null($this->getAttribute('onchange'))) {
233 $editorrules = 'onblur="'.htmlspecialchars($this->getAttribute('onblur')).'" onchange="'.htmlspecialchars($this->getAttribute('onchange')).'"';
235 $str .= '<div><textarea id="'.$id.'" name="'.$elname.'[text]" rows="'.$rows.'" cols="'.$cols.'"'.$editorrules.'>';
237 $str .= '</textarea></div>';
240 if (count($formats)>1) {
241 $str.= html_writer::select($formats, $elname.'[format]', $format, false);
243 $str.= html_writer::empty_tag('input',
244 array('name'=>$elname.'[format]', 'type'=> 'hidden', 'value' => array_pop(array_keys($formats))));
248 // during moodle installation, user area doesn't exist
249 // so we need to disable filepicker here.
250 if (!during_initial_install() && empty($CFG->adminsetuppending)) {
251 // 0 means no files, -1 unlimited
252 if ($maxfiles != 0 ) {
253 $str .= '<input type="hidden" name="'.$elname.'[itemid]" value="'.$draftitemid.'" />';
255 // used by non js editor only
256 $editorurl = new moodle_url("$CFG->wwwroot/repository/draftfiles_manager.php", array(
259 'itemid'=>$draftitemid,
261 'maxbytes'=>$maxbytes,
262 'maxfiles'=>$maxfiles,
264 'course'=>$PAGE->course->id,
265 'sesskey'=>sesskey(),
267 $str .= '<noscript>';
268 $str .= "<div><object type='text/html' data='$editorurl' height='160' width='600' style='border:1px solid #000'></object></div>";
269 $str .= '</noscript>';