Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/form/editor.php
Go to the documentation of this file.
00001 <?php
00002 
00003 global $CFG;
00004 
00005 require_once('HTML/QuickForm/element.php');
00006 require_once($CFG->dirroot.'/lib/filelib.php');
00007 require_once($CFG->dirroot.'/repository/lib.php');
00008 
00009 //TODO:
00010 //  * locking
00011 //  * freezing
00012 //  * ajax format conversion
00013 
00014 class MoodleQuickForm_editor extends HTML_QuickForm_element {
00015     public $_helpbutton = '';
00016 
00017     public $_type       = 'editor';
00018     protected $_options    = array('subdirs'=>0, 'maxbytes'=>0, 'maxfiles'=>0, 'changeformat'=>0,
00019                                    'context'=>null, 'noclean'=>0, 'trusttext'=>0);
00020     protected $_values     = array('text'=>null, 'format'=>null, 'itemid'=>null);
00021 
00022     function MoodleQuickForm_editor($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
00023         global $CFG, $PAGE;
00024 
00025         $options = (array)$options;
00026         foreach ($options as $name=>$value) {
00027             if (array_key_exists($name, $this->_options)) {
00028                 $this->_options[$name] = $value;
00029             }
00030         }
00031         if (!empty($options['maxbytes'])) {
00032             $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']);
00033         }
00034         if (!$this->_options['context']) {
00035             $this->_options['context'] = get_context_instance(CONTEXT_SYSTEM);
00036         }
00037         $this->_options['trusted'] = trusttext_trusted($this->_options['context']);
00038         parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
00039 
00040         editors_head_setup();
00041     }
00042 
00043     function setName($name) {
00044         $this->updateAttributes(array('name'=>$name));
00045     }
00046 
00047     function getName() {
00048         return $this->getAttribute('name');
00049     }
00050 
00051     function setValue($values) {
00052         $values = (array)$values;
00053         foreach ($values as $name=>$value) {
00054             if (array_key_exists($name, $this->_values)) {
00055                 $this->_values[$name] = $value;
00056             }
00057         }
00058     }
00059 
00060     function getValue() {
00061         return $this->_values;
00062     }
00063 
00064     function getMaxbytes() {
00065         return $this->_options['maxbytes'];
00066     }
00067 
00068     function setMaxbytes($maxbytes) {
00069         global $CFG;
00070         $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $maxbytes);
00071     }
00072 
00073     function getMaxfiles() {
00074         return $this->_options['maxfiles'];
00075     }
00076 
00077     function setMaxfiles($num) {
00078         $this->_options['maxfiles'] = $num;
00079     }
00080 
00081     function getSubdirs() {
00082         return $this->_options['subdirs'];
00083     }
00084 
00085     function setSubdirs($allow) {
00086         $this->_options['subdirs'] = $allow;
00087     }
00088 
00094     function getFormat() {
00095         return $this->_values['format'];
00096     }
00097 
00103     function isRequired() {
00104         return (isset($this->_options['required']) && $this->_options['required']);
00105     }
00106 
00107     function setHelpButton($_helpbuttonargs, $function='_helpbutton') {
00108         if (!is_array($_helpbuttonargs)) {
00109             $_helpbuttonargs = array($_helpbuttonargs);
00110         } else {
00111             $_helpbuttonargs = $_helpbuttonargs;
00112         }
00113         //we do this to to return html instead of printing it
00114         //without having to specify it in every call to make a button.
00115         if ('_helpbutton' == $function){
00116             $defaultargs = array('', '', 'moodle', true, false, '', true);
00117             $_helpbuttonargs = $_helpbuttonargs + $defaultargs ;
00118         }
00119         $this->_helpbutton=call_user_func_array($function, $_helpbuttonargs);
00120     }
00121 
00122     function getHelpButton() {
00123         return $this->_helpbutton;
00124     }
00125 
00126     function getElementTemplateType() {
00127         if ($this->_flagFrozen){
00128             return 'nodisplay';
00129         } else {
00130             return 'default';
00131         }
00132     }
00133 
00134     function toHtml() {
00135         global $CFG, $PAGE;
00136         require_once($CFG->dirroot.'/repository/lib.php');
00137 
00138         if ($this->_flagFrozen) {
00139             return $this->getFrozenHtml();
00140         }
00141 
00142         $ctx = $this->_options['context'];
00143 
00144         $id           = $this->_attributes['id'];
00145         $elname       = $this->_attributes['name'];
00146 
00147         $subdirs      = $this->_options['subdirs'];
00148         $maxbytes     = $this->_options['maxbytes'];
00149         $maxfiles     = $this->_options['maxfiles'];
00150         $changeformat = $this->_options['changeformat']; // TO DO: implement as ajax calls
00151 
00152         $text         = $this->_values['text'];
00153         $format       = $this->_values['format'];
00154         $draftitemid  = $this->_values['itemid'];
00155 
00156         // security - never ever allow guest/not logged in user to upload anything
00157         if (isguestuser() or !isloggedin()) {
00158             $maxfiles = 0;
00159         }
00160 
00161         $str = $this->_getTabs();
00162         $str .= '<div>';
00163 
00164         $editor = editors_get_preferred_editor($format);
00165         $strformats = format_text_menu();
00166         $formats =  $editor->get_supported_formats();
00167         foreach ($formats as $fid) {
00168             $formats[$fid] = $strformats[$fid];
00169         }
00170 
00171         // get filepicker info
00172         //
00173         $fpoptions = array();
00174         if ($maxfiles != 0 ) {
00175             if (empty($draftitemid)) {
00176                 // no existing area info provided - let's use fresh new draft area
00177                 require_once("$CFG->libdir/filelib.php");
00178                 $this->setValue(array('itemid'=>file_get_unused_draft_itemid()));
00179                 $draftitemid = $this->_values['itemid'];
00180             }
00181 
00182             $args = new stdClass();
00183             // need these three to filter repositories list
00184             $args->accepted_types = array('image');
00185             $args->return_types = (FILE_INTERNAL | FILE_EXTERNAL);
00186             $args->context = $ctx;
00187             $args->env = 'filepicker';
00188             // advimage plugin
00189             $image_options = initialise_filepicker($args);
00190             $image_options->context = $ctx;
00191             $image_options->client_id = uniqid();
00192             $image_options->maxbytes = $this->_options['maxbytes'];
00193             $image_options->env = 'editor';
00194             $image_options->itemid = $draftitemid;
00195 
00196             // moodlemedia plugin
00197             $args->accepted_types = array('video', 'audio');
00198             $media_options = initialise_filepicker($args);
00199             $media_options->context = $ctx;
00200             $media_options->client_id = uniqid();
00201             $media_options->maxbytes  = $this->_options['maxbytes'];
00202             $media_options->env = 'editor';
00203             $media_options->itemid = $draftitemid;
00204 
00205             // advlink plugin
00206             $args->accepted_types = '*';
00207             $link_options = initialise_filepicker($args);
00208             $link_options->context = $ctx;
00209             $link_options->client_id = uniqid();
00210             $link_options->maxbytes  = $this->_options['maxbytes'];
00211             $link_options->env = 'editor';
00212             $link_options->itemid = $draftitemid;
00213 
00214             $fpoptions['image'] = $image_options;
00215             $fpoptions['media'] = $media_options;
00216             $fpoptions['link'] = $link_options;
00217         }
00218 
00219         //If editor is required and tinymce, then set required_tinymce option to initalize tinymce validation.
00220         if (($editor instanceof tinymce_texteditor)  && !is_null($this->getAttribute('onchange'))) {
00221             $this->_options['required'] = true;
00222         }
00223 
00225         $editor->use_editor($id, $this->_options, $fpoptions);
00226 
00227         $rows = empty($this->_attributes['rows']) ? 15 : $this->_attributes['rows'];
00228         $cols = empty($this->_attributes['cols']) ? 80 : $this->_attributes['cols'];
00229 
00230         //Apply editor validation if required field
00231         $editorrules = '';
00232         if (!is_null($this->getAttribute('onblur')) && !is_null($this->getAttribute('onchange'))) {
00233             $editorrules = ' onblur="'.htmlspecialchars($this->getAttribute('onblur')).'" onchange="'.htmlspecialchars($this->getAttribute('onchange')).'"';
00234         }
00235         $str .= '<div><textarea id="'.$id.'" name="'.$elname.'[text]" rows="'.$rows.'" cols="'.$cols.'"'.$editorrules.'>';
00236         $str .= s($text);
00237         $str .= '</textarea></div>';
00238 
00239         $str .= '<div>';
00240         if (count($formats)>1) {
00241             $str.= html_writer::select($formats, $elname.'[format]', $format, false);
00242         } else {
00243             $str.= html_writer::empty_tag('input',
00244                     array('name'=>$elname.'[format]', 'type'=> 'hidden', 'value' => array_pop(array_keys($formats))));
00245         }
00246         $str .= '</div>';
00247 
00248         // during moodle installation, user area doesn't exist
00249         // so we need to disable filepicker here.
00250         if (!during_initial_install() && empty($CFG->adminsetuppending)) {
00251             // 0 means no files, -1 unlimited
00252             if ($maxfiles != 0 ) {
00253                 $str .= '<input type="hidden" name="'.$elname.'[itemid]" value="'.$draftitemid.'" />';
00254 
00255                 // used by non js editor only
00256                 $editorurl = new moodle_url("$CFG->wwwroot/repository/draftfiles_manager.php", array(
00257                     'action'=>'browse',
00258                     'env'=>'editor',
00259                     'itemid'=>$draftitemid,
00260                     'subdirs'=>$subdirs,
00261                     'maxbytes'=>$maxbytes,
00262                     'maxfiles'=>$maxfiles,
00263                     'ctx_id'=>$ctx->id,
00264                     'course'=>$PAGE->course->id,
00265                     'sesskey'=>sesskey(),
00266                     ));
00267                 $str .= '<noscript>';
00268                 $str .= "<div><object type='text/html' data='$editorurl' height='160' width='600' style='border:1px solid #000'></object></div>";
00269                 $str .= '</noscript>';
00270             }
00271         }
00272 
00273 
00274         $str .= '</div>';
00275 
00276         return $str;
00277     }
00278 
00284     function getFrozenHtml() {
00285 
00286         return '';
00287     }
00288 }
 All Data Structures Namespaces Files Functions Variables Enumerations