Skip to content

Commit

Permalink
Merge pull request #948 from dpalou/MOBILE-1987
Browse files Browse the repository at this point in the history
Mobile 1987
  • Loading branch information
jleyva authored Feb 28, 2017
2 parents 7e891ce + 31055e9 commit 9901e76
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 67 deletions.
12 changes: 6 additions & 6 deletions www/addons/mod/assign/controllers/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ angular.module('mm.addons.mod_assign')
// Cannot connect. Get cached data.
return $mmaModAssign.getSubmissionStatus(assign.id, userId, isBlind).then(function(response) {
var userSubmission = $mmaModAssign.getSubmissionObjectFromAttempt(assign, response.lastattempt);
if (userSubmission && (userSubmission.status == 'new' || userSubmission.status == 'reopened')) {
// It's a new submission, allow creating it in offline.
if ($mmaModAssignHelper.canEditSubmissionOffline(assign, userSubmission)) {
return response;
} else {
// User is editing a submission, we don't allow it in offline for now so reject.
return $q.reject(error);
}

// Submission cannot be edited in offline, reject.
$scope.allowOffline = false;
return $q.reject(error);
});
}).then(function(response) {
if (!response.lastattempt.canedit) {
Expand All @@ -80,7 +80,7 @@ angular.module('mm.addons.mod_assign')
}

$scope.userSubmission = $mmaModAssign.getSubmissionObjectFromAttempt(assign, response.lastattempt);
$scope.allowOffline = $scope.userSubmission.status == 'new' || $scope.userSubmission.status == 'reopened';
$scope.allowOffline = true; // If offline isn't allowed we shouldn't have reached this point.

// Only show submission statement if we are editing our own submission.
if (assign.requiresubmissionstatement && !assign.submissiondrafts && userId == $mmSite.getUserId()) {
Expand Down
30 changes: 30 additions & 0 deletions www/addons/mod/assign/services/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,36 @@ angular.module('mm.addons.mod_assign')

var self = {};

/**
* Check if a submission can be edited in offline.
*
* @module mm.addons.mod_assign
* @ngdoc method
* @name $mmaModAssignHelper#canEditSubmissionOffline
* @param {Object} assign Assignment.
* @param {Object} submission Submission.
* @return {Boolean} True if can edit offline, false otherwise.
*/
self.canEditSubmissionOffline = function(assign, submission) {
if (!submission) {
return false;
}

if (submission.status == 'new' || submission.status == 'reopened') {
// It's a new submission, allow creating it in offline.
return true;
}

for (var i = 0; i < submission.plugins.length; i++) {
var plugin = submission.plugins[i];
if (!$mmaModAssignSubmissionDelegate.canPluginEditOffline(assign, submission, plugin)) {
return false;
}
}

return true;
};

/**
* Clear plugins temporary data because a submission was cancelled.
*
Expand Down
67 changes: 46 additions & 21 deletions www/addons/mod/assign/services/submissiondelegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,27 @@ angular.module('mm.addons.mod_assign')
updatePromises = {},
lastUpdateHandlersStart;

/**
* Check if the plugin can be edited in offline for existing submissions.
* In general, this should return false if the plugin uses Moodle filters. The reason is that the app only prefetches
* filtered data, and the user should edit unfiltered data.
*
* @module mm.addons.mod_assign
* @ngdoc method
* @name $mmaModAssignSubmissionDelegate#canPluginEditOffline
* @param {Object} assign Assignment.
* @param {Object} submission Submission.
* @param {Object} plugin Plugin.
* @return {Boolean} Whether the plugin can be edited in offline for existing submissions.
*/
self.canPluginEditOffline = function(assign, submission, plugin) {
var handler = self.getPluginHandler(plugin.type);
if (handler && handler.canEditOffline) {
return handler.canEditOffline(assign, submission, plugin);
}
return false;
};

/**
* Clear some temporary data because a submission was cancelled.
*
Expand Down Expand Up @@ -381,40 +402,44 @@ angular.module('mm.addons.mod_assign')
* @param {String|Object|Function} handler Must be resolved to an object defining the following properties. Or to a function
* returning an object defining these properties. See {@link $mmUtil#resolveObject}.
* - isEnabled (Boolean|Promise) Whether or not the handler is enabled on a site level.
* When using a promise, it should return a boolean.
* When using a promise, it should return a boolean.
* - isEnabledForEdit (Boolean|Promise) Whether or not the handler is enabled for edit on a site
* level. When using a promise, it should return a boolean.
* This should return true if the plugin has no submission component.
* level. When using a promise, it should return a boolean.
* This should return true if the plugin has no submission component.
* - getDirectiveName(plugin, edit) (String) Optional. Returns the name of the directive to render
* the plugin.
* the plugin.
* - prepareSubmissionData(assign, submission, plugin, inputData, pluginData, offline, userId, siteId).
* Should prepare and add to pluginData the data to send to server
* based in the input. Return promise if async.
* Should prepare and add to pluginData the data to send to server based in the
* input. Return promise if async.
* - copySubmissionData(assign, plugin, pluginData). Function meant to copy a submission. Should
* add to pluginData the data to send to server based in the data
* in plugin (previous attempt).
* add to pluginData the data to send to server based in the data in plugin
* (previous attempt).
* - hasDataChanged(assign, submission, plugin, inputData) (Promise|Boolean) Check if the
* submission data has changed for this plugin.
* submission data has changed for this plugin.
* - clearTmpData(assign, submission, plugin, inputData). Optional. Should clear temporary data
* for a cancelled submission.
* for a cancelled submission.
* - getSizeForCopy(assign, plugin). Optional. Get the size of data (in bytes) this plugin will
* send to copy a previous attempt.
* send to copy a previous attempt.
* - getSizeForEdit(assign, submission, plugin, inputData). Optional. Get the size of data (in
* bytes) this plugin will send to add or edit a submission.
* bytes) this plugin will send to add or edit a submission.
* - getPluginFiles(assign, submission, plugin, siteId). Optional. Get files used by this plugin.
* The files returned by this function will be prefetched when the
* user prefetches the assign.
* The files returned by this function will be prefetched when the user
* prefetches the assign.
* - prefetch(assign, submission, plugin, siteId). Optional. Prefetch any required data for the
* plugin. This should NOT prefetch files. Files to be prefetched
* should be returned by the getPluginFiles function.
* plugin. This should NOT prefetch files. Files to be prefetched should be
* returned by the getPluginFiles function.
* - deleteOfflineData(assign, submission, plugin, offlineData, siteId). Optional. Delete any
* stored data for the plugin and submission.
* stored data for the plugin and submission.
* - prepareSyncData(assign, submission, plugin, offlineData, pluginData, siteId). Optional. Should
* prepare and add to pluginData the data to send to server based in
* the offline data stored. This is to perform a synchronziation.
* prepare and add to pluginData the data to send to server based in the offline
* data stored. This is to perform a synchronziation.
* - getPluginName(plugin). Optional. Should return a human readable String. If not present, default
* translation will be applied, if translation not found, optional
* name will be used.
* translation will be applied. If translation not found, optional name will
* be used.
* - canEditOffline(assign, submission, plugin). Optional. True if the plugin can be edited in
* offline for existing submissions. In general, this should return false if the
* plugin uses Moodle filters. The reason is that the app only prefetches filtered
* data, and the user should edit unfiltered data. Defaults to false.
*/
self.registerHandler = function(addon, pluginType, handler) {
if (typeof handlers[pluginType] !== 'undefined') {
Expand Down
32 changes: 31 additions & 1 deletion www/addons/mod/assign/submission/comments/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,30 @@ angular.module('mm.addons.mod_assign')

var self = {};

/**
* Check if the plugin can be edited in offline for existing submissions.
* In general, this should return false if the plugin uses Moodle filters. The reason is that the app only prefetches
* filtered data, and the user should edit unfiltered data.
*
* @module mm.addons.mod_assign
* @ngdoc method
* @name $mmaModAssignSubmissionCommentsHandler#canEditOffline
* @param {Object} assign Assignment.
* @param {Object} submission Submission.
* @param {Object} plugin Plugin.
* @return {Boolean} Whether the plugin can be edited in offline for existing submissions.
*/
self.canEditOffline = function(assign, submission, plugin) {
// This plugin is read only, but return true to prevent blocking the edition.
return true;
};

/**
* Whether or not the rule is enabled for the site.
*
* @module mm.addons.mod_assign
* @ngdoc method
* @name $mmaModAssignSubmissionCommentsHandler#isEnabled
* @return {Promise} Promise resolved with true if enabled, rejected or resolved with false otherwise.
*/
self.isEnabled = function() {
Expand All @@ -39,7 +60,10 @@ angular.module('mm.addons.mod_assign')
* This should return true if the plugin has no submission component (allow_submissions=false),
* otherwise the user won't be able to edit submissions at all.
*
* @return {Boolean}
* @module mm.addons.mod_assign
* @ngdoc method
* @name $mmaModAssignSubmissionCommentsHandler#isEnabledForEdit
* @return {Boolean} Whether the plugin is enabled.
*/
self.isEnabledForEdit = function() {
return true;
Expand All @@ -48,6 +72,9 @@ angular.module('mm.addons.mod_assign')
/**
* Get the name of the directive to render this plugin.
*
* @module mm.addons.mod_assign
* @ngdoc method
* @name $mmaModAssignSubmissionCommentsHandler#getDirectiveName
* @param {Object} plugin Plugin to get the directive for.
* @param {Boolean} edit True if editing a submission, false if read only.
* @return {String} Directive name.
Expand All @@ -59,6 +86,9 @@ angular.module('mm.addons.mod_assign')
/**
* Prefetch submission data.
*
* @module mm.addons.mod_assign
* @ngdoc method
* @name $mmaModAssignSubmissionCommentsHandler#prefetch
* @param {Object} assign Assignment.
* @param {Object} submission Submission to check data.
* @param {Object} plugin Plugin.
Expand Down
31 changes: 20 additions & 11 deletions www/addons/mod/assign/submission/file/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ angular.module('mm.addons.mod_assign')
* @name mmaModAssignSubmissionFile
*/
.directive('mmaModAssignSubmissionFile', function($mmaModAssign, $mmaModAssignSubmissionFileSession, $mmaModAssignHelper,
$mmaModAssignOffline, mmaModAssignSubmissionFileName, $mmFileUploaderHelper) {
$mmaModAssignOffline, mmaModAssignSubmissionFileName, $mmFileUploaderHelper, $q) {
return {
restrict: 'A',
priority: 100,
Expand All @@ -32,18 +32,27 @@ angular.module('mm.addons.mod_assign')
return;
}

scope.files = $mmaModAssign.getSubmissionPluginAttachments(scope.plugin);

// Get the offline data.
$mmaModAssignOffline.getSubmission(scope.assign.id).then(function(offlineData) {
if (offlineData && offlineData.plugindata && offlineData.plugindata.files_filemanager &&
offlineData.plugindata.files_filemanager.offline) {
// Has offline files.
return $mmaModAssignHelper.getStoredSubmissionFiles(scope.assign.id, mmaModAssignSubmissionFileName)
.then(function(files) {
files = $mmFileUploaderHelper.markOfflineFiles(files);
scope.files = scope.files.concat(files);
$mmaModAssignOffline.getSubmission(scope.assign.id).catch(function() {
// Error getting data, assume there's no offline submission.
}).then(function(offlineData) {
if (offlineData && offlineData.plugindata && offlineData.plugindata.files_filemanager) {
// Has offline data.
var promise;
if (offlineData.plugindata.files_filemanager.offline) {
promise = $mmaModAssignHelper.getStoredSubmissionFiles(scope.assign.id, mmaModAssignSubmissionFileName);
} else {
promise = $q.when([]);
}

return promise.then(function(offlineFiles) {
var onlineFiles = offlineData.plugindata.files_filemanager.online || [];
offlineFiles = $mmFileUploaderHelper.markOfflineFiles(offlineFiles);
scope.files = onlineFiles.concat(offlineFiles);
});
} else {
// No offline data, get the online files.
scope.files = $mmaModAssign.getSubmissionPluginAttachments(scope.plugin);
}
}).finally(function() {
$mmaModAssignSubmissionFileSession.setFiles(scope.assign.id, scope.files);
Expand Down
Loading

0 comments on commit 9901e76

Please sign in to comment.