2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
22 * @copyright 1999 onwards Dongsheng Cai <dongsheng@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require_once('HTML/QuickForm/element.php');
29 require_once($CFG->dirroot.'/lib/filelib.php');
30 require_once($CFG->dirroot.'/repository/lib.php');
32 class MoodleQuickForm_filemanager extends HTML_QuickForm_element {
33 public $_helpbutton = '';
34 protected $_options = array('mainfile'=>'', 'subdirs'=>1, 'maxbytes'=>-1, 'maxfiles'=>-1, 'accepted_types'=>'*', 'return_types'=>FILE_INTERNAL);
36 function MoodleQuickForm_filemanager($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
39 $options = (array)$options;
40 foreach ($options as $name=>$value) {
41 if (array_key_exists($name, $this->_options)) {
42 $this->_options[$name] = $value;
45 if (!empty($options['maxbytes'])) {
46 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']);
48 $this->_type = 'filemanager';
49 parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
52 function setName($name) {
53 $this->updateAttributes(array('name'=>$name));
57 return $this->getAttribute('name');
60 function setValue($value) {
61 $this->updateAttributes(array('value'=>$value));
65 return $this->getAttribute('value');
68 function getMaxbytes() {
69 return $this->_options['maxbytes'];
72 function setMaxbytes($maxbytes) {
74 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $maxbytes);
77 function getSubdirs() {
78 return $this->_options['subdirs'];
81 function setSubdirs($allow) {
82 $this->_options['subdirs'] = $allow;
85 function getMaxfiles() {
86 return $this->_options['maxfiles'];
89 function setMaxfiles($num) {
90 $this->_options['maxfiles'] = $num;
93 function setHelpButton($helpbuttonargs, $function='helpbutton'){
94 debugging('component setHelpButton() is not used any more, please use $mform->setHelpButton() instead');
97 function getHelpButton() {
98 return $this->_helpbutton;
101 function getElementTemplateType() {
102 if ($this->_flagFrozen){
110 global $CFG, $USER, $COURSE, $PAGE, $OUTPUT;
111 require_once("$CFG->dirroot/repository/lib.php");
113 // security - never ever allow guest/not logged in user to upload anything or use this element!
114 if (isguestuser() or !isloggedin()) {
115 print_error('noguest');
118 if ($this->_flagFrozen) {
119 return $this->getFrozenHtml();
122 $id = $this->_attributes['id'];
123 $elname = $this->_attributes['name'];
124 $subdirs = $this->_options['subdirs'];
125 $maxbytes = $this->_options['maxbytes'];
126 $draftitemid = $this->getValue();
127 $accepted_types = $this->_options['accepted_types'];
129 if (empty($draftitemid)) {
130 // no existing area info provided - let's use fresh new draft area
131 require_once("$CFG->libdir/filelib.php");
132 $this->setValue(file_get_unused_draft_itemid());
133 $draftitemid = $this->getValue();
136 $client_id = uniqid();
138 // filemanager options
139 $options = new stdClass();
140 $options->mainfile = $this->_options['mainfile'];
141 $options->maxbytes = $this->_options['maxbytes'];
142 $options->maxfiles = $this->getMaxfiles();
143 $options->client_id = $client_id;
144 $options->itemid = $draftitemid;
145 $options->subdirs = $this->_options['subdirs'];
146 $options->target = $id;
147 $options->accepted_types = $accepted_types;
148 $options->return_types = FILE_INTERNAL;
149 $options->context = $PAGE->context;
151 $html = $this->_getTabs();
152 $html .= form_filemanager_render($options);
154 $html .= '<input value="'.$draftitemid.'" name="'.$elname.'" type="hidden" />';
155 // label element needs 'for' attribute work
156 $html .= '<input value="" id="id_'.$elname.'" type="hidden" />';
165 * Data structure representing a file manager.
167 * @copyright 2010 Dongsheng Cai
168 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
171 class form_filemanaer_x {
172 //TODO: do not use this abstraction (skodak)
175 public function __construct(stdClass $options) {
176 global $CFG, $USER, $PAGE;
177 require_once($CFG->dirroot. '/repository/lib.php');
183 'client_id'=>uniqid(),
184 'accepted_types'=>'*',
185 'return_types'=>FILE_INTERNAL,
186 'context'=>$PAGE->context
188 foreach ($defaults as $key=>$value) {
189 if (empty($options->$key)) {
190 $options->$key = $value;
194 $fs = get_file_storage();
196 // initilise options, getting files in root path
197 $this->options = file_get_drafarea_files($options->itemid, '/');
199 // calculate file count
200 $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
201 $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id', false);
202 $filecount = count($files);
203 $this->options->filecount = $filecount;
205 // copying other options
206 foreach ($options as $name=>$value) {
207 $this->options->$name = $value;
210 // building file picker options
211 $params = new stdClass();
212 $params->accepted_types = $options->accepted_types;
213 $params->return_types = $options->return_types;
214 $params->context = $options->context;
215 $params->env = 'filemanager';
216 $params->disable_types = !empty($options->disable_types)?$options->disable_types:array();
217 $filepicker_options = initialise_filepicker($params);
218 $this->options->filepicker = $filepicker_options;
223 * Print the file manager
226 * $OUTPUT->file_manager($options);
229 * @param array $options associative array with file manager options
235 * client_id=>uniqid(),
236 * acepted_types=>'*',
237 * return_types=>FILE_INTERNAL,
238 * context=>$PAGE->context
239 * @return string HTML fragment
241 function form_filemanager_render($options) {
242 global $CFG, $OUTPUT, $PAGE;
244 $fm = new form_filemanaer_x($options); //TODO: this is unnecessary here, the nested options are getting too complex
246 static $filemanagertemplateloaded;
249 $options = $fm->options;
250 $straddfile = get_string('addfile', 'repository');
251 $strmakedir = get_string('makeafolder', 'moodle');
252 $strdownload = get_string('downloadfolder', 'repository');
253 $strloading = get_string('loading', 'repository');
255 $icon_progress = $OUTPUT->pix_icon('i/loading_small', $strloading).'';
257 $client_id = $options->client_id;
258 $itemid = $options->itemid;
259 list($context, $course, $cm) = get_context_info_array($options->context->id);
260 if (is_object($course)) {
261 $course_maxbytes = $course->maxbytes;
263 $course_maxbytes = $CFG->maxbytes;
266 if ($options->maxbytes == -1 || empty($options->maxbytes)) {
267 $options->maxbytes = $CFG->maxbytes;
270 if (empty($options->filecount)) {
271 $extra = ' style="display:none"';
276 $maxsize = get_string('maxfilesize', 'moodle', display_size(get_max_upload_file_size($CFG->maxbytes, $course_maxbytes, $options->maxbytes)));
278 <div class="filemanager-loading mdl-align" id='filemanager-loading-{$client_id}'>
281 <div id="filemanager-wrapper-{$client_id}" style="display:none">
282 <div class="fm-breadcrumb" id="fm-path-{$client_id}"></div>
283 <div class="filemanager-toolbar">
284 <input type="button" class="fm-btn-add" id="btnadd-{$client_id}" onclick="return false" value="{$straddfile}" />
285 <input type="button" class="fm-btn-mkdir" id="btncrt-{$client_id}" onclick="return false" value="{$strmakedir}" />
286 <input type="button" class="fm-btn-download" id="btndwn-{$client_id}" onclick="return false" {$extra} value="{$strdownload}" />
287 <span> $maxsize </span>
289 <div class="filemanager-container" id="filemanager-{$client_id}">
290 <ul id="draftfiles-{$client_id}" class="fm-filelist">
295 <div class='clearer'></div>
297 if (empty($filemanagertemplateloaded)) {
298 $filemanagertemplateloaded = true;
300 <div id="fm-template" style="display:none">___fullname___ ___action___</div>
305 'name'=>'form_filemanager',
306 'fullpath'=>'/lib/form/filemanager.js',
307 'requires' => array('core_filepicker', 'base', 'io-base', 'node', 'json', 'yui2-button', 'yui2-container', 'yui2-layout', 'yui2-menu', 'yui2-treeview'),
308 'strings' => array(array('loading', 'repository'), array('nomorefiles', 'repository'), array('confirmdeletefile', 'repository'),
309 array('add', 'repository'), array('accessiblefilepicker', 'repository'), array('move', 'moodle'),
310 array('cancel', 'moodle'), array('download', 'moodle'), array('ok', 'moodle'),
311 array('emptylist', 'repository'), array('nofilesattached', 'repository'), array('entername', 'repository'), array('enternewname', 'repository'),
312 array('zip', 'editor'), array('unzip', 'moodle'), array('rename', 'moodle'), array('delete', 'moodle'),
313 array('cannotdeletefile', 'error'), array('confirmdeletefile', 'repository'),
314 array('nopathselected', 'repository'), array('popupblockeddownload', 'repository'),
315 array('draftareanofiles', 'repository'), array('path', 'moodle'), array('setmainfile', 'repository'),
316 array('moving', 'repository'), array('files', 'moodle')
319 $PAGE->requires->js_module($module);
320 $PAGE->requires->js_init_call('M.form_filemanager.init', array($options), true, $module);
322 // non javascript file manager
323 $filemanagerurl = new moodle_url('/repository/draftfiles_manager.php', array(
324 'env'=>'filemanager',
327 'subdirs'=>$options->subdirs,
328 'maxbytes'=>$options->maxbytes,
329 'maxfiles'=>$options->maxfiles,
330 'ctx_id'=>$PAGE->context->id,
331 'course'=>$PAGE->course->id,
332 'sesskey'=>sesskey(),
335 $html .= '<noscript>';
336 $html .= "<div><object type='text/html' data='$filemanagerurl' height='160' width='600' style='border:1px solid #000'></object></div>";
337 $html .= '</noscript>';