|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00026 global $CFG; 00027 00028 require_once('HTML/QuickForm/element.php'); 00029 require_once($CFG->dirroot.'/lib/filelib.php'); 00030 require_once($CFG->dirroot.'/repository/lib.php'); 00031 00032 class MoodleQuickForm_filemanager extends HTML_QuickForm_element { 00033 public $_helpbutton = ''; 00034 protected $_options = array('mainfile'=>'', 'subdirs'=>1, 'maxbytes'=>-1, 'maxfiles'=>-1, 'accepted_types'=>'*', 'return_types'=>FILE_INTERNAL); 00035 00036 function MoodleQuickForm_filemanager($elementName=null, $elementLabel=null, $attributes=null, $options=null) { 00037 global $CFG, $PAGE; 00038 00039 $options = (array)$options; 00040 foreach ($options as $name=>$value) { 00041 if (array_key_exists($name, $this->_options)) { 00042 $this->_options[$name] = $value; 00043 } 00044 } 00045 if (!empty($options['maxbytes'])) { 00046 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']); 00047 } 00048 $this->_type = 'filemanager'; 00049 parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes); 00050 } 00051 00052 function setName($name) { 00053 $this->updateAttributes(array('name'=>$name)); 00054 } 00055 00056 function getName() { 00057 return $this->getAttribute('name'); 00058 } 00059 00060 function setValue($value) { 00061 $this->updateAttributes(array('value'=>$value)); 00062 } 00063 00064 function getValue() { 00065 return $this->getAttribute('value'); 00066 } 00067 00068 function getMaxbytes() { 00069 return $this->_options['maxbytes']; 00070 } 00071 00072 function setMaxbytes($maxbytes) { 00073 global $CFG; 00074 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $maxbytes); 00075 } 00076 00077 function getSubdirs() { 00078 return $this->_options['subdirs']; 00079 } 00080 00081 function setSubdirs($allow) { 00082 $this->_options['subdirs'] = $allow; 00083 } 00084 00085 function getMaxfiles() { 00086 return $this->_options['maxfiles']; 00087 } 00088 00089 function setMaxfiles($num) { 00090 $this->_options['maxfiles'] = $num; 00091 } 00092 00093 function setHelpButton($helpbuttonargs, $function='helpbutton'){ 00094 debugging('component setHelpButton() is not used any more, please use $mform->setHelpButton() instead'); 00095 } 00096 00097 function getHelpButton() { 00098 return $this->_helpbutton; 00099 } 00100 00101 function getElementTemplateType() { 00102 if ($this->_flagFrozen){ 00103 return 'nodisplay'; 00104 } else { 00105 return 'default'; 00106 } 00107 } 00108 00109 function toHtml() { 00110 global $CFG, $USER, $COURSE, $PAGE, $OUTPUT; 00111 require_once("$CFG->dirroot/repository/lib.php"); 00112 00113 // security - never ever allow guest/not logged in user to upload anything or use this element! 00114 if (isguestuser() or !isloggedin()) { 00115 print_error('noguest'); 00116 } 00117 00118 if ($this->_flagFrozen) { 00119 return $this->getFrozenHtml(); 00120 } 00121 00122 $id = $this->_attributes['id']; 00123 $elname = $this->_attributes['name']; 00124 $subdirs = $this->_options['subdirs']; 00125 $maxbytes = $this->_options['maxbytes']; 00126 $draftitemid = $this->getValue(); 00127 $accepted_types = $this->_options['accepted_types']; 00128 00129 if (empty($draftitemid)) { 00130 // no existing area info provided - let's use fresh new draft area 00131 require_once("$CFG->libdir/filelib.php"); 00132 $this->setValue(file_get_unused_draft_itemid()); 00133 $draftitemid = $this->getValue(); 00134 } 00135 00136 $client_id = uniqid(); 00137 00138 // filemanager options 00139 $options = new stdClass(); 00140 $options->mainfile = $this->_options['mainfile']; 00141 $options->maxbytes = $this->_options['maxbytes']; 00142 $options->maxfiles = $this->getMaxfiles(); 00143 $options->client_id = $client_id; 00144 $options->itemid = $draftitemid; 00145 $options->subdirs = $this->_options['subdirs']; 00146 $options->target = $id; 00147 $options->accepted_types = $accepted_types; 00148 $options->return_types = FILE_INTERNAL; 00149 $options->context = $PAGE->context; 00150 00151 $html = $this->_getTabs(); 00152 $html .= form_filemanager_render($options); 00153 00154 $html .= '<input value="'.$draftitemid.'" name="'.$elname.'" type="hidden" />'; 00155 // label element needs 'for' attribute work 00156 $html .= '<input value="" id="id_'.$elname.'" type="hidden" />'; 00157 00158 return $html; 00159 } 00160 } 00161 00162 00163 00171 class form_filemanaer_x { 00172 //TODO: do not use this abstraction (skodak) 00173 00174 public $options; 00175 public function __construct(stdClass $options) { 00176 global $CFG, $USER, $PAGE; 00177 require_once($CFG->dirroot. '/repository/lib.php'); 00178 $defaults = array( 00179 'maxbytes'=>-1, 00180 'maxfiles'=>-1, 00181 'itemid'=>0, 00182 'subdirs'=>0, 00183 'client_id'=>uniqid(), 00184 'accepted_types'=>'*', 00185 'return_types'=>FILE_INTERNAL, 00186 'context'=>$PAGE->context 00187 ); 00188 foreach ($defaults as $key=>$value) { 00189 if (empty($options->$key)) { 00190 $options->$key = $value; 00191 } 00192 } 00193 00194 $fs = get_file_storage(); 00195 00196 // initilise options, getting files in root path 00197 $this->options = file_get_drafarea_files($options->itemid, '/'); 00198 00199 // calculate file count 00200 $usercontext = get_context_instance(CONTEXT_USER, $USER->id); 00201 $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id', false); 00202 $filecount = count($files); 00203 $this->options->filecount = $filecount; 00204 00205 // copying other options 00206 foreach ($options as $name=>$value) { 00207 $this->options->$name = $value; 00208 } 00209 00210 // building file picker options 00211 $params = new stdClass(); 00212 $params->accepted_types = $options->accepted_types; 00213 $params->return_types = $options->return_types; 00214 $params->context = $options->context; 00215 $params->env = 'filemanager'; 00216 $params->disable_types = !empty($options->disable_types)?$options->disable_types:array(); 00217 $filepicker_options = initialise_filepicker($params); 00218 $this->options->filepicker = $filepicker_options; 00219 } 00220 } 00221 00241 function form_filemanager_render($options) { 00242 global $CFG, $OUTPUT, $PAGE; 00243 00244 $fm = new form_filemanaer_x($options); //TODO: this is unnecessary here, the nested options are getting too complex 00245 00246 static $filemanagertemplateloaded; 00247 00248 $html = ''; 00249 $options = $fm->options; 00250 $straddfile = get_string('addfile', 'repository'); 00251 $strmakedir = get_string('makeafolder', 'moodle'); 00252 $strdownload = get_string('downloadfolder', 'repository'); 00253 $strloading = get_string('loading', 'repository'); 00254 00255 $icon_progress = $OUTPUT->pix_icon('i/loading_small', $strloading).''; 00256 00257 $client_id = $options->client_id; 00258 $itemid = $options->itemid; 00259 list($context, $course, $cm) = get_context_info_array($options->context->id); 00260 if (is_object($course)) { 00261 $course_maxbytes = $course->maxbytes; 00262 } else { 00263 $course_maxbytes = $CFG->maxbytes; 00264 } 00265 00266 if ($options->maxbytes == -1 || empty($options->maxbytes)) { 00267 $options->maxbytes = $CFG->maxbytes; 00268 } 00269 00270 if (empty($options->filecount)) { 00271 $extra = ' style="display:none"'; 00272 } else { 00273 $extra = ''; 00274 } 00275 00276 $maxsize = get_string('maxfilesize', 'moodle', display_size(get_max_upload_file_size($CFG->maxbytes, $course_maxbytes, $options->maxbytes))); 00277 $html .= <<<FMHTML 00278 <div class="filemanager-loading mdl-align" id='filemanager-loading-{$client_id}'> 00279 $icon_progress 00280 </div> 00281 <div id="filemanager-wrapper-{$client_id}" style="display:none"> 00282 <div class="fm-breadcrumb" id="fm-path-{$client_id}"></div> 00283 <div class="filemanager-toolbar"> 00284 <input type="button" class="fm-btn-add" id="btnadd-{$client_id}" onclick="return false" value="{$straddfile}" /> 00285 <input type="button" class="fm-btn-mkdir" id="btncrt-{$client_id}" onclick="return false" value="{$strmakedir}" /> 00286 <input type="button" class="fm-btn-download" id="btndwn-{$client_id}" onclick="return false" {$extra} value="{$strdownload}" /> 00287 <span> $maxsize </span> 00288 </div> 00289 <div class="filemanager-container" id="filemanager-{$client_id}"> 00290 <ul id="draftfiles-{$client_id}" class="fm-filelist"> 00291 <li>Loading...</li> 00292 </ul> 00293 </div> 00294 </div> 00295 <div class='clearer'></div> 00296 FMHTML; 00297 if (empty($filemanagertemplateloaded)) { 00298 $filemanagertemplateloaded = true; 00299 $html .= <<<FMHTML 00300 <div id="fm-template" style="display:none">___fullname___ ___action___</div> 00301 FMHTML; 00302 } 00303 00304 $module = array( 00305 'name'=>'form_filemanager', 00306 'fullpath'=>'/lib/form/filemanager.js', 00307 'requires' => array('core_filepicker', 'base', 'io-base', 'node', 'json', 'yui2-button', 'yui2-container', 'yui2-layout', 'yui2-menu', 'yui2-treeview'), 00308 'strings' => array(array('loading', 'repository'), array('nomorefiles', 'repository'), array('confirmdeletefile', 'repository'), 00309 array('add', 'repository'), array('accessiblefilepicker', 'repository'), array('move', 'moodle'), 00310 array('cancel', 'moodle'), array('download', 'moodle'), array('ok', 'moodle'), 00311 array('emptylist', 'repository'), array('nofilesattached', 'repository'), array('entername', 'repository'), array('enternewname', 'repository'), 00312 array('zip', 'editor'), array('unzip', 'moodle'), array('rename', 'moodle'), array('delete', 'moodle'), 00313 array('cannotdeletefile', 'error'), array('confirmdeletefile', 'repository'), 00314 array('nopathselected', 'repository'), array('popupblockeddownload', 'repository'), 00315 array('draftareanofiles', 'repository'), array('path', 'moodle'), array('setmainfile', 'repository'), 00316 array('moving', 'repository'), array('files', 'moodle') 00317 ) 00318 ); 00319 $PAGE->requires->js_module($module); 00320 $PAGE->requires->js_init_call('M.form_filemanager.init', array($options), true, $module); 00321 00322 // non javascript file manager 00323 $filemanagerurl = new moodle_url('/repository/draftfiles_manager.php', array( 00324 'env'=>'filemanager', 00325 'action'=>'browse', 00326 'itemid'=>$itemid, 00327 'subdirs'=>$options->subdirs, 00328 'maxbytes'=>$options->maxbytes, 00329 'maxfiles'=>$options->maxfiles, 00330 'ctx_id'=>$PAGE->context->id, 00331 'course'=>$PAGE->course->id, 00332 'sesskey'=>sesskey(), 00333 )); 00334 00335 $html .= '<noscript>'; 00336 $html .= "<div><object type='text/html' data='$filemanagerurl' height='160' width='600' style='border:1px solid #000'></object></div>"; 00337 $html .= '</noscript>'; 00338 00339 00340 return $html; 00341 }