diff --git a/files/files_ajax.php b/files/files_ajax.php index a8f131e1d8e..0cc00a72003 100755 --- a/files/files_ajax.php +++ b/files/files_ajax.php @@ -155,18 +155,18 @@ case 'downloaddir': $zipper = new zip_packer(); $fs = get_file_storage(); - $file = $fs->get_file($user_context->id, 'user_draft', $itemid, $filepath, '.'); - if ($file->get_parent_directory()) { - $parent_path = $file->get_parent_directory()->get_filepath(); - $filename = trim($filepath, '/').'.zip'; - } else { + $stored_file = $fs->get_file($user_context->id, 'user_draft', $itemid, $filepath, '.'); + if ($filepath === '/') { $parent_path = '/'; $filename = get_string('files').'.zip'; + } else { + $parent_path = $stored_file->get_parent_directory()->get_filepath(); + $filename = trim($filepath, '/').'.zip'; } // archive compressed file to an unused draft area $newdraftitemid = file_get_unused_draft_itemid(); - if ($newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user_draft', $newdraftitemid, '/', $filename, $USER->id)) { + if ($newfile = $zipper->archive_to_storage(array($stored_file), $user_context->id, 'user_draft', $newdraftitemid, '/', $filename, $USER->id)) { $return = new stdclass; $return->fileurl = $CFG->wwwroot . '/draftfile.php/' . $user_context->id .'/user_draft/'.$newdraftitemid.'/'.$filename; $return->filepath = $parent_path; diff --git a/lib/form/filemanager.js b/lib/form/filemanager.js index 6560c8f6d83..92aeedb1e6a 100644 --- a/lib/form/filemanager.js +++ b/lib/form/filemanager.js @@ -23,14 +23,17 @@ * this.mkdir_dialog * this.rename_dialog * this.client_id + * this.filecount, how many files in this filemanager + * this.maxfiles + * this.maxbytes * * FileManager options: * ===== * this.options.currentpath * this.options.itemid */ -var filemanagers = {}; -var fm_filepickers = {}; +M.filemanagers = M.filemanagers||{}; +M.fm_filepickers = M.fm_filepickers || {}; M.core_filemanager = (function(){ function core_filemanager (args) { @@ -49,9 +52,16 @@ M.core_filemanager = (function(){ this.currentpath = '/'; this.filepicker_options = args.filepicker; this.client_id = ''; + this.maxfiles = args.maxfiles; + this.maxbytes = args.maxbytes; if (args.client_id) { this.client_id = args.client_id; } + if (args.filecount) { + this.filecount = args.filecount; + } else { + this.filecount = 0; + } this.setup_buttons(); this.render(); }, @@ -98,7 +108,11 @@ M.core_filemanager = (function(){ Y.io(api, cfg); }, filepicker_callback: function(obj) { - //console.warn(obj); + var button_addfile = Y.one("#btnadd-"+this.client_id); + this.filecount++; + if (this.filecount >= this.maxfiles && this.maxfiles!=-1) { + button_addfile.setStyle('display', 'none'); + } this.refresh(this.currentpath); }, refresh: function(filepath) { @@ -123,34 +137,29 @@ M.core_filemanager = (function(){ var button_download = Y.one("#btndwn-"+client_id); var button_create = Y.one("#btncrt-"+client_id); var button_addfile = Y.one("#btnadd-"+client_id); - button_addfile.on('click', function(e) { - var options = this.filepicker_options; - var scope = this; - var client_id = this.client_id; - Y.use('core_filepicker', function (Y) { - options.formcallback = scope.filepicker_callback; - // XXX: magic here, to let filepicker use filemanager scope - options.magicscope = scope; - options.savepath = scope.currentpath; - if (!fm_filepickers[client_id]) { - fm_filepickers[client_id] = new M.core_filepicker(options); - } - fm_filepickers[client_id].show(); - }); - }, this); // setup 'add file' button // if maxfiles == -1, the no limit - //if (fm_cfg[client_id].filecount >= fm_cfg[client_id].maxfiles - //&& fm_cfg[client_id].maxfiles!=-1) { - //button_addfile.style.display = 'none'; - //} else { - //button_addfile.onclick = function(e) { - //this.options.savepath = this.options.currentpath; - //fm_launch_filepicker(this.options.target, this.options); - //} - //button_addfile.options = fm_cfg[client_id]; - //} + if (this.filecount >= this.maxfiles + && this.maxfiles!=-1) { + button_addfile.setStyle('display', 'none'); + } else { + button_addfile.on('click', function(e) { + var options = this.filepicker_options; + var scope = this; + var client_id = this.client_id; + Y.use('core_filepicker', function (Y) { + options.formcallback = scope.filepicker_callback; + // XXX: magic here, to let filepicker use filemanager scope + options.magicscope = scope; + options.savepath = scope.currentpath; + if (!M.fm_filepickers[client_id]) { + M.fm_filepickers[client_id] = new M.core_filepicker(options); + } + M.fm_filepickers[client_id].show(); + }); + }, this); + } // setup 'make a folder' button if (this.options.subdirs) { @@ -470,7 +479,12 @@ M.core_filemanager = (function(){ scope: scope, params: params, callback: function(id, obj, args) { + scope.filecount--; scope.refresh(obj.filepath); + if (scope.filecount < scope.maxfiles && scope.maxfiles!=-1) { + var button_addfile = Y.one("#btnadd-"+scope.client_id); + button_addfile.setStyle('display', 'inline'); + } } }); } diff --git a/lib/form/filemanager.php b/lib/form/filemanager.php index 28217c2d67a..79e8f26f59e 100644 --- a/lib/form/filemanager.php +++ b/lib/form/filemanager.php @@ -216,9 +216,9 @@ FMHTML;