Skip to content

Commit

Permalink
Merge pull request #586 from dpalou/MOBILE-1701
Browse files Browse the repository at this point in the history
Mobile 1701
  • Loading branch information
jleyva committed Jul 21, 2016
2 parents 5b38dfa + 862b8ef commit ac5e333
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 11 deletions.
22 changes: 17 additions & 5 deletions www/addons/mod/assign/services/helper.js
Expand Up @@ -212,15 +212,21 @@ angular.module('mm.addons.mod_assign')
*/
self.prepareSubmissionPluginData = function(assign, submission, inputData) {
var pluginData = {},
promises = [];
promises = [],
error;

angular.forEach(submission.plugins, function(plugin) {
promises.push($mmaModAssignSubmissionDelegate.preparePluginSubmissionData(
assign, submission, plugin, inputData, pluginData));
assign, submission, plugin, inputData, pluginData).catch(function(message) {
error = message;
return $q.reject();
}));
});

return $mmUtil.allPromises(promises).then(function() {
return pluginData;
}).catch(function() {
return $q.reject(error);
});
};

Expand Down Expand Up @@ -256,7 +262,7 @@ angular.module('mm.addons.mod_assign')

return promise.then(function(fileEntry) {
// Now upload the file.
return $mmFileUploader.uploadGenericFile(fileEntry.toURL(), fileName, fileEntry.type, true, itemId, siteId)
return $mmFileUploader.uploadGenericFile(fileEntry.toURL(), fileName, fileEntry.type, true, 'draft', itemId, siteId)
.then(function(result) {
return result.itemid;
});
Expand Down Expand Up @@ -286,19 +292,25 @@ angular.module('mm.addons.mod_assign')

// Upload only the first file first to get a draft id.
return self.uploadFile(assignId, files[0]).then(function(itemId) {
var promises = [];
var promises = [],
error;

angular.forEach(files, function(file, index) {
if (index === 0) {
// First file has already been uploaded.
return;
}

promises.push(self.uploadFile(assignId, file, itemId, siteId));
promises.push(self.uploadFile(assignId, file, itemId, siteId).catch(function(message) {
error = message;
return $q.reject();
}));
});

return $q.all(promises).then(function() {
return itemId;
}).catch(function() {
return $q.reject(error);
});
});
};
Expand Down
9 changes: 7 additions & 2 deletions www/addons/mod/assign/submission/file/handlers.js
Expand Up @@ -22,7 +22,7 @@ angular.module('mm.addons.mod_assign')
* @name $mmaModAssignSubmissionFileHandler
*/
.factory('$mmaModAssignSubmissionFileHandler', function($mmaModAssignSubmissionFileSession, $mmaModAssign, $mmSite, $q,
$mmaModAssignHelper, $mmWS, $mmFS, $mmFilepool) {
$mmaModAssignHelper, $mmWS, $mmFS, $mmFilepool, $mmUtil) {

var self = {};

Expand Down Expand Up @@ -224,7 +224,12 @@ angular.module('mm.addons.mod_assign')

if (self.hasDataChanged(assign, submission, plugin, inputData)) {
// Data has changed, we need to upload new files and re-upload all the existing files.
var currentFiles = $mmaModAssignSubmissionFileSession.getFiles(assign.id);
var currentFiles = $mmaModAssignSubmissionFileSession.getFiles(assign.id),
error = $mmUtil.hasRepeatedFilenames(currentFiles);

if (error) {
return $q.reject(error);
}

return $mmaModAssignHelper.uploadFiles(assign.id, currentFiles, siteId).then(function(itemId) {
pluginData.files_filemanager = itemId;
Expand Down
5 changes: 4 additions & 1 deletion www/core/components/fileuploader/services/fileuploader.js
Expand Up @@ -127,17 +127,20 @@ angular.module('mm.core.fileuploader')
* @param {String} name File name.
* @param {String} type File type.
* @param {Boolean} deleteAfterUpload Whether the file should be deleted after upload.
* @param {String} [fileArea] File area to upload the file to.
* In Moodle 3.1 or higher defaults to 'draft', in previous versions defaults to 'private'.
* @param {Number} [itemId] Draft ID to upload the file to, 0 to create new. Only for draft files.
* @param {String} [siteId] Id of the site to upload the file to. If not defined, use current site.
* @return {Promise} Promise resolved when the file is uploaded.
*/
self.uploadGenericFile = function(uri, name, type, deleteAfterUpload, itemId, siteId) {
self.uploadGenericFile = function(uri, name, type, deleteAfterUpload, fileArea, itemId, siteId) {
var options = {};
options.fileKey = null;
options.fileName = name;
options.mimeType = type;
options.deleteAfterUpload = deleteAfterUpload;
options.itemId = itemId;
options.fileArea = fileArea;

return self.uploadFile(uri, options, siteId);
};
Expand Down
2 changes: 1 addition & 1 deletion www/core/components/fileuploader/services/helper.js
Expand Up @@ -253,7 +253,7 @@ angular.module('mm.core.fileuploader')
// We won't check size so there's no need to pass maxSize. Functions calling
// uploadGenericFile should check the size before calling this function.
return uploadFile(deleteAfterUpload, uri, -1, false,
$mmFileUploader.uploadGenericFile, uri, name, type, deleteAfterUpload, undefined, siteId);
$mmFileUploader.uploadGenericFile, uri, name, type, deleteAfterUpload, undefined, undefined, siteId);
};

/**
Expand Down
1 change: 1 addition & 0 deletions www/core/lang/en.json
Expand Up @@ -51,6 +51,7 @@
"erroropenpopup": "This activity is trying to open a popup. This is not supported in this app.",
"errorrenamefile": "Error renaming the file. Please try again.",
"filename": "Filename",
"filenameexist": "File name already exists: {{$a}}",
"folder": "Folder",
"fullnameandsitename": "{{fullname}} ({{sitename}})",
"help" : "Help",
Expand Down
29 changes: 29 additions & 0 deletions www/core/lib/util.js
Expand Up @@ -1656,6 +1656,35 @@ angular.module('mm.core')
return $q.when(false);
};

/**
* Given a list of files, check if there are repeated names.
*
* @module mm.core
* @ngdoc method
* @name $mmUtil#hasRepeatedFilenames
* @param {Object[]} files List of files.
* @return {Mixed} String with error message if repeated, false if no repeated.
*/
self.hasRepeatedFilenames = function(files) {
if (!files || !files.length) {
return false;
}

var names = [];

// Check if there are 2 files with the same name.
for (var i = 0; i < files.length; i++) {
var name = files[i].filename || files[i].name;
if (names.indexOf(name) > -1) {
return $translate.instant('mm.core.filenameexist', {$a: name});
} else {
names.push(name);
}
}

return false;
};

return self;
};
});
4 changes: 2 additions & 2 deletions www/core/lib/ws.js
Expand Up @@ -319,8 +319,8 @@ angular.module('mm.core')

if (typeof data.exception !== 'undefined') {
return $q.reject(data.message);
} else if (typeof data.error !== 'undefined') {
return $q.reject(data.error);
} else if (data[0] && typeof data[0].error !== 'undefined') {
return $q.reject(data[0].error);
}

// We uploaded only 1 file, so we only return the first file returned.
Expand Down

0 comments on commit ac5e333

Please sign in to comment.