Skip to content

Commit

Permalink
MDL-26388, pop up a dialog when attaching a file already being used i…
Browse files Browse the repository at this point in the history
…n draft area.
  • Loading branch information
Dongsheng Cai committed May 2, 2011
1 parent 7daba27 commit 9fb4a63
Show file tree
Hide file tree
Showing 11 changed files with 415 additions and 183 deletions.
5 changes: 5 additions & 0 deletions lang/en/repository.php
Expand Up @@ -94,6 +94,9 @@
$string['existingrepository'] = 'This repository already exists';
$string['federatedsearch'] = 'Federated search';
$string['fileexists'] = 'File name already being used, please use another name';
$string['fileexistsdialog_editor'] = 'A file with that name has already been attached to the text you are editing.';
$string['fileexistsdialog_filemanager'] = 'A file with that name has already been attached';
$string['fileexistsdialogheader'] = 'File exists';
$string['filename'] = 'Filename';
$string['filenotnull'] = 'You must select a file to upload.';
$string['filesaved'] = 'The file has been saved';
Expand Down Expand Up @@ -138,6 +141,7 @@
$string['openpicker'] = 'Choose a file...';
$string['operation'] = 'Operation';
$string['on'] = 'Enabled and visible';
$string['overwrite'] = 'Overwrite';
$string['personalrepositories'] = 'Available repository instances';
$string['plugin'] = 'Repository plug-ins';
$string['pluginerror'] = 'Errors in repository plugin.';
Expand All @@ -148,6 +152,7 @@
$string['refresh'] = 'Refresh';
$string['refreshnonjsfilepicker'] = 'Please close this window and refresh non-javascript file picker';
$string['removed'] = 'Repository removed';
$string['renameto'] = 'Rename to';
$string['repositories'] = 'Repositories';
$string['repository'] = 'Repository';
$string['repositorycourse'] = 'Course repositories';
Expand Down
6 changes: 5 additions & 1 deletion lib/form/filemanager.js
Expand Up @@ -595,7 +595,11 @@ M.form_filemanager.init = function(Y, options) {
scope: scope,
params: params,
callback: function(id, obj, args) {
scope.refresh(obj.filepath);
if (obj == false) {
alert(M.str.repository.fileexists);
} else {
scope.refresh(obj.filepath);
}
Y.one('#fm-rename-input').set('value', '');
scope.rename_dialog.hide();
}
Expand Down
5 changes: 4 additions & 1 deletion lib/outputrequirementslib.php
Expand Up @@ -415,7 +415,10 @@ protected function find_module($component) {
array('cancel'), array('chooselicense', 'repository'), array('author', 'repository'),
array('ok', 'moodle'), array('error', 'moodle'), array('info', 'moodle'), array('norepositoriesavailable', 'repository'), array('norepositoriesexternalavailable', 'repository'),
array('nofilesattached', 'repository'), array('filepicker', 'repository'),
array('nofilesavailable', 'repository')
array('nofilesavailable', 'repository'), array('overwrite', 'repository'),
array('renameto', 'repository'), array('fileexists', 'repository'),
array('fileexistsdialogheader', 'repository'), array('fileexistsdialog_editor', 'repository'),
array('fileexistsdialog_filemanager', 'repository')
));
break;
case 'core_comment':
Expand Down
53 changes: 9 additions & 44 deletions repository/coursefiles/lib.php
Expand Up @@ -123,50 +123,6 @@ public function get_listing($encodedpath = '') {
return $ret;
}

/**
* Copy a file to file area
*
* @global object $USER
* @global object $DB
* @param string $encoded The metainfo of file, it is base64 encoded php serialized data
* @param string $draftitemid itemid
* @param string $new_filename The intended name of file
* @param string $new_filepath the new path in draft area
* @return array The information of file
*/
public function copy_to_area($encoded, $draftitemid, $new_filepath, $new_filename) {
global $USER, $DB;
$info = array();

$browser = get_file_browser();
$fs = get_file_storage();
$user_context = get_context_instance(CONTEXT_USER, $USER->id);

// the final file
$params = unserialize(base64_decode($encoded));
$contextid = clean_param($params['contextid'], PARAM_INT);
$fileitemid = clean_param($params['itemid'], PARAM_INT);
$filename = clean_param($params['filename'], PARAM_FILE);
$filepath = clean_param($params['filepath'], PARAM_PATH);;
$filearea = clean_param($params['filearea'], PARAM_ALPHAEXT);
$component = clean_param($params['component'], PARAM_ALPHAEXT);
$context = get_context_instance_by_id($contextid);

if ($existingfile = $fs->get_file($user_context->id, 'user', 'draft', $draftitemid, $new_filepath, $new_filename)) {
throw new moodle_exception('fileexists');
}

$file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
$file_info->copy_to_storage($user_context->id, 'user', 'draft', $draftitemid, $new_filepath, $new_filename);

$info['itemid'] = $draftitemid;
$info['title'] = $new_filename;
$info['contextid'] = $user_context->id;
$info['filesize'] = $file_info->get_filesize();

return $info;
}

public function get_link($encoded) {
$info = array();

Expand Down Expand Up @@ -216,4 +172,13 @@ public function supported_returntypes() {
public static function get_type_option_names() {
return array();
}

/**
* Does this repository used to browse moodle files?
*
* @return boolean
*/
public function has_moodle_files() {
return true;
}
}
103 changes: 101 additions & 2 deletions repository/filepicker.js
Expand Up @@ -85,7 +85,11 @@ M.core_filepicker.init = function(Y, options) {

request: function(args, redraw) {
var client_id = args.client_id;
var api = this.api + '?action='+args.action;
if (!args.api) {
var api = this.api + '?action='+args.action;
} else {
var api = args.api + '?action='+args.action;
}
var params = {};
var scope = this;
if (args['scope']) {
Expand Down Expand Up @@ -147,6 +151,14 @@ M.core_filepicker.init = function(Y, options) {
scope.print_msg(data.error, 'error');
scope.list();
return;
} else if (data && data.event) {
switch (data.event) {
case 'fileexists':
scope.process_existing_file(data);
break;
default:
break;
}
} else {
if (data.msg) {
scope.print_msg(data.msg, 'info');
Expand All @@ -172,6 +184,94 @@ M.core_filepicker.init = function(Y, options) {
this.wait('load');
}
},
process_existing_file: function(data) {
var scope = this;
var repository_id = scope.active_repo.id;
var client_id = scope.options.client_id;
var handleOverwrite = function() {
// overwrite
var dialog = this;
var params = {}
params['existingfilename'] = data.existingfile.filename;
params['existingfilepath'] = data.existingfile.filepath;
params['newfilename'] = data.newfile.filename;
params['newfilepath'] = data.newfile.filepath;
scope.request({
'params': params,
'scope': scope,
'action':'overwrite',
'path': '',
'client_id': client_id,
'repository_id': repository_id,
'callback': function(id, o, args) {
dialog.cancel();
scope.hide();
// editor needs to update url
// filemanager do nothing
if (scope.options.editor_target&&scope.options.env=='editor') {
scope.options.editor_target.value=data.existingfile.url;
scope.options.editor_target.onchange();
}
}
}, true);
}
var handleRename = function() {
if (scope.options.editor_target&&scope.options.env=='editor') {
scope.options.editor_target.value=data.newfile.url;
scope.options.editor_target.onchange();
}
this.cancel();
scope.hide();
data.client_id = client_id;
var formcallback_scope = null;
if (scope.options.magicscope) {
formcallback_scope = scope.options.magicscope;
} else {
formcallback_scope = scope;
}
scope.options.formcallback.apply(formcallback_scope, [data]);
}
var handleCancel = function() {
// Delete tmp file
var dialog = this;
var params = {};
params['newfilename'] = data.newfile.filename;
params['newfilepath'] = data.newfile.filepath;
scope.request({
'params': params,
'scope': scope,
'action':'deletetmpfile',
'path': '',
'client_id': client_id,
'repository_id': repository_id,
'callback': function(id, o, args) {
scope.hide();
dialog.cancel();
}
}, true);
}
var dialog = new YAHOO.widget.SimpleDialog("dlg", {
width: "50em",
fixedcenter: true,
modal: true,
close: false,
icon: YAHOO.widget.SimpleDialog.ICON_HELP,
visible: true,
draggable: true,
buttons: [{ text: M.str.repository.overwrite, handler: handleOverwrite },
{ text: M.str.repository.renameto + ' "' + data.newfile.filename + '"', handler: handleRename },
{ text: M.str.moodle.cancel, handler: handleCancel, isDefault:true}]
});
dialog.setHeader(M.str.repository.fileexistsdialogheader);
if (scope.options.env=='editor') {
dialog.setBody(M.str.repository.fileexistsdialog_editor);
} else {
dialog.setBody(M.str.repository.fileexistsdialog_filemanager);
}

dialog.render(document.body);
dialog.show();
},
print_msg: function(msg, type) {
var client_id = this.options.client_id;
var dlg_id = 'fp-msg-dlg-'+client_id;
Expand Down Expand Up @@ -1113,7 +1213,6 @@ M.core_filepicker.init = function(Y, options) {
this.print_paging('header');
//}


var toolbar = Y.one('#repo-tb-'+client_id);

if(!r.nosearch) {
Expand Down

0 comments on commit 9fb4a63

Please sign in to comment.