Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/form/filepicker.php
Go to the documentation of this file.
00001 <?php
00002 
00003 global $CFG;
00004 
00005 require_once("HTML/QuickForm/button.php");
00006 require_once($CFG->dirroot.'/repository/lib.php');
00007 
00016 class MoodleQuickForm_filepicker extends HTML_QuickForm_input {
00017     public $_helpbutton = '';
00018     protected $_options    = array('maxbytes'=>0, 'accepted_types'=>'*', 'return_types'=>FILE_INTERNAL);
00019 
00020     function MoodleQuickForm_filepicker($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
00021         global $CFG;
00022 
00023         $options = (array)$options;
00024         foreach ($options as $name=>$value) {
00025             if (array_key_exists($name, $this->_options)) {
00026                 $this->_options[$name] = $value;
00027             }
00028         }
00029         if (!empty($options['maxbytes'])) {
00030             $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']);
00031         }
00032         $this->_type = 'filepicker';
00033         parent::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
00034     }
00035 
00036     function setHelpButton($helpbuttonargs, $function='helpbutton') {
00037         debugging('component setHelpButton() is not used any more, please use $mform->setHelpButton() instead');
00038     }
00039 
00040     function getHelpButton() {
00041         return $this->_helpbutton;
00042     }
00043 
00044     function getElementTemplateType() {
00045         if ($this->_flagFrozen){
00046             return 'nodisplay';
00047         } else {
00048             return 'default';
00049         }
00050     }
00051 
00052     function toHtml() {
00053         global $CFG, $COURSE, $USER, $PAGE, $OUTPUT;
00054         $id     = $this->_attributes['id'];
00055         $elname = $this->_attributes['name'];
00056 
00057         if ($this->_flagFrozen) {
00058             return $this->getFrozenHtml();
00059         }
00060         if (!$draftitemid = (int)$this->getValue()) {
00061             // no existing area info provided - let's use fresh new draft area
00062             $draftitemid = file_get_unused_draft_itemid();
00063             $this->setValue($draftitemid);
00064         }
00065 
00066         if ($COURSE->id == SITEID) {
00067             $context = get_context_instance(CONTEXT_SYSTEM);
00068         } else {
00069             $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
00070         }
00071 
00072         $client_id = uniqid();
00073 
00074         $args = new stdClass();
00075         // need these three to filter repositories list
00076         $args->accepted_types = $this->_options['accepted_types']?$this->_options['accepted_types']:'*';
00077         $args->return_types = FILE_INTERNAL;
00078         $args->itemid = $draftitemid;
00079         $args->maxbytes = $this->_options['maxbytes'];
00080         $args->context = $PAGE->context;
00081         $args->buttonname = $elname.'choose';
00082         $args->elementname = $elname;
00083 
00084         $html = $this->_getTabs();
00085         $fp = new file_picker($args);
00086         $options = $fp->options;
00087         $options->context = $PAGE->context;
00088         $html .= $OUTPUT->render($fp);
00089         $html .= '<input type="hidden" name="'.$elname.'" id="'.$id.'" value="'.$draftitemid.'" class="filepickerhidden"/>';
00090 
00091         $module = array('name'=>'form_filepicker', 'fullpath'=>'/lib/form/filepicker.js', 'requires'=>array('core_filepicker', 'node', 'node-event-simulate'));
00092         $PAGE->requires->js_init_call('M.form_filepicker.init', array($fp->options), true, $module);
00093 
00094         $nonjsfilepicker = new moodle_url('/repository/draftfiles_manager.php', array(
00095             'env'=>'filepicker',
00096             'action'=>'browse',
00097             'itemid'=>$draftitemid,
00098             'subdirs'=>0,
00099             'maxbytes'=>$options->maxbytes,
00100             'maxfiles'=>1,
00101             'ctx_id'=>$PAGE->context->id,
00102             'course'=>$PAGE->course->id,
00103             'sesskey'=>sesskey(),
00104             ));
00105 
00106         // non js file picker
00107         $html .= '<noscript>';
00108         $html .= "<div><object type='text/html' data='$nonjsfilepicker' height='160' width='600' style='border:1px solid #000'></object></div>";
00109         $html .= '</noscript>';
00110 
00111         return $html;
00112     }
00113 
00114     function exportValue(&$submitValues, $assoc = false) {
00115         global $USER;
00116 
00117         $draftitemid = $this->_findValue($submitValues);
00118         if (null === $draftitemid) {
00119             $draftitemid = $this->getValue();
00120         }
00121 
00122         // make sure max one file is present and it is not too big
00123         if (!is_null($draftitemid)) {
00124             $fs = get_file_storage();
00125             $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
00126             if ($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id DESC', false)) {
00127                 $file = array_shift($files);
00128                 if ($this->_options['maxbytes'] and $file->get_filesize() > $this->_options['maxbytes']) {
00129                     // bad luck, somebody tries to sneak in oversized file
00130                     $file->delete();
00131                 }
00132                 foreach ($files as $file) {
00133                     // only one file expected
00134                     $file->delete();
00135                 }
00136             }
00137         }
00138 
00139         return $this->_prepareValue($draftitemid, true);
00140     }
00141 }
 All Data Structures Namespaces Files Functions Variables Enumerations