Skip to content

Commit 9901e76

Browse files
authored
Merge pull request #948 from dpalou/MOBILE-1987
Mobile 1987
2 parents 7e891ce + 31055e9 commit 9901e76

File tree

9 files changed

+298
-67
lines changed

9 files changed

+298
-67
lines changed

www/addons/mod/assign/controllers/edit.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ angular.module('mm.addons.mod_assign')
6565
// Cannot connect. Get cached data.
6666
return $mmaModAssign.getSubmissionStatus(assign.id, userId, isBlind).then(function(response) {
6767
var userSubmission = $mmaModAssign.getSubmissionObjectFromAttempt(assign, response.lastattempt);
68-
if (userSubmission && (userSubmission.status == 'new' || userSubmission.status == 'reopened')) {
69-
// It's a new submission, allow creating it in offline.
68+
if ($mmaModAssignHelper.canEditSubmissionOffline(assign, userSubmission)) {
7069
return response;
71-
} else {
72-
// User is editing a submission, we don't allow it in offline for now so reject.
73-
return $q.reject(error);
7470
}
71+
72+
// Submission cannot be edited in offline, reject.
73+
$scope.allowOffline = false;
74+
return $q.reject(error);
7575
});
7676
}).then(function(response) {
7777
if (!response.lastattempt.canedit) {
@@ -80,7 +80,7 @@ angular.module('mm.addons.mod_assign')
8080
}
8181

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

8585
// Only show submission statement if we are editing our own submission.
8686
if (assign.requiresubmissionstatement && !assign.submissiondrafts && userId == $mmSite.getUserId()) {

www/addons/mod/assign/services/helper.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,36 @@ angular.module('mm.addons.mod_assign')
2626

2727
var self = {};
2828

29+
/**
30+
* Check if a submission can be edited in offline.
31+
*
32+
* @module mm.addons.mod_assign
33+
* @ngdoc method
34+
* @name $mmaModAssignHelper#canEditSubmissionOffline
35+
* @param {Object} assign Assignment.
36+
* @param {Object} submission Submission.
37+
* @return {Boolean} True if can edit offline, false otherwise.
38+
*/
39+
self.canEditSubmissionOffline = function(assign, submission) {
40+
if (!submission) {
41+
return false;
42+
}
43+
44+
if (submission.status == 'new' || submission.status == 'reopened') {
45+
// It's a new submission, allow creating it in offline.
46+
return true;
47+
}
48+
49+
for (var i = 0; i < submission.plugins.length; i++) {
50+
var plugin = submission.plugins[i];
51+
if (!$mmaModAssignSubmissionDelegate.canPluginEditOffline(assign, submission, plugin)) {
52+
return false;
53+
}
54+
}
55+
56+
return true;
57+
};
58+
2959
/**
3060
* Clear plugins temporary data because a submission was cancelled.
3161
*

www/addons/mod/assign/services/submissiondelegate.js

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,27 @@ angular.module('mm.addons.mod_assign')
5353
updatePromises = {},
5454
lastUpdateHandlersStart;
5555

56+
/**
57+
* Check if the plugin can be edited in offline for existing submissions.
58+
* In general, this should return false if the plugin uses Moodle filters. The reason is that the app only prefetches
59+
* filtered data, and the user should edit unfiltered data.
60+
*
61+
* @module mm.addons.mod_assign
62+
* @ngdoc method
63+
* @name $mmaModAssignSubmissionDelegate#canPluginEditOffline
64+
* @param {Object} assign Assignment.
65+
* @param {Object} submission Submission.
66+
* @param {Object} plugin Plugin.
67+
* @return {Boolean} Whether the plugin can be edited in offline for existing submissions.
68+
*/
69+
self.canPluginEditOffline = function(assign, submission, plugin) {
70+
var handler = self.getPluginHandler(plugin.type);
71+
if (handler && handler.canEditOffline) {
72+
return handler.canEditOffline(assign, submission, plugin);
73+
}
74+
return false;
75+
};
76+
5677
/**
5778
* Clear some temporary data because a submission was cancelled.
5879
*
@@ -381,40 +402,44 @@ angular.module('mm.addons.mod_assign')
381402
* @param {String|Object|Function} handler Must be resolved to an object defining the following properties. Or to a function
382403
* returning an object defining these properties. See {@link $mmUtil#resolveObject}.
383404
* - isEnabled (Boolean|Promise) Whether or not the handler is enabled on a site level.
384-
* When using a promise, it should return a boolean.
405+
* When using a promise, it should return a boolean.
385406
* - isEnabledForEdit (Boolean|Promise) Whether or not the handler is enabled for edit on a site
386-
* level. When using a promise, it should return a boolean.
387-
* This should return true if the plugin has no submission component.
407+
* level. When using a promise, it should return a boolean.
408+
* This should return true if the plugin has no submission component.
388409
* - getDirectiveName(plugin, edit) (String) Optional. Returns the name of the directive to render
389-
* the plugin.
410+
* the plugin.
390411
* - prepareSubmissionData(assign, submission, plugin, inputData, pluginData, offline, userId, siteId).
391-
* Should prepare and add to pluginData the data to send to server
392-
* based in the input. Return promise if async.
412+
* Should prepare and add to pluginData the data to send to server based in the
413+
* input. Return promise if async.
393414
* - copySubmissionData(assign, plugin, pluginData). Function meant to copy a submission. Should
394-
* add to pluginData the data to send to server based in the data
395-
* in plugin (previous attempt).
415+
* add to pluginData the data to send to server based in the data in plugin
416+
* (previous attempt).
396417
* - hasDataChanged(assign, submission, plugin, inputData) (Promise|Boolean) Check if the
397-
* submission data has changed for this plugin.
418+
* submission data has changed for this plugin.
398419
* - clearTmpData(assign, submission, plugin, inputData). Optional. Should clear temporary data
399-
* for a cancelled submission.
420+
* for a cancelled submission.
400421
* - getSizeForCopy(assign, plugin). Optional. Get the size of data (in bytes) this plugin will
401-
* send to copy a previous attempt.
422+
* send to copy a previous attempt.
402423
* - getSizeForEdit(assign, submission, plugin, inputData). Optional. Get the size of data (in
403-
* bytes) this plugin will send to add or edit a submission.
424+
* bytes) this plugin will send to add or edit a submission.
404425
* - getPluginFiles(assign, submission, plugin, siteId). Optional. Get files used by this plugin.
405-
* The files returned by this function will be prefetched when the
406-
* user prefetches the assign.
426+
* The files returned by this function will be prefetched when the user
427+
* prefetches the assign.
407428
* - prefetch(assign, submission, plugin, siteId). Optional. Prefetch any required data for the
408-
* plugin. This should NOT prefetch files. Files to be prefetched
409-
* should be returned by the getPluginFiles function.
429+
* plugin. This should NOT prefetch files. Files to be prefetched should be
430+
* returned by the getPluginFiles function.
410431
* - deleteOfflineData(assign, submission, plugin, offlineData, siteId). Optional. Delete any
411-
* stored data for the plugin and submission.
432+
* stored data for the plugin and submission.
412433
* - prepareSyncData(assign, submission, plugin, offlineData, pluginData, siteId). Optional. Should
413-
* prepare and add to pluginData the data to send to server based in
414-
* the offline data stored. This is to perform a synchronziation.
434+
* prepare and add to pluginData the data to send to server based in the offline
435+
* data stored. This is to perform a synchronziation.
415436
* - getPluginName(plugin). Optional. Should return a human readable String. If not present, default
416-
* translation will be applied, if translation not found, optional
417-
* name will be used.
437+
* translation will be applied. If translation not found, optional name will
438+
* be used.
439+
* - canEditOffline(assign, submission, plugin). Optional. True if the plugin can be edited in
440+
* offline for existing submissions. In general, this should return false if the
441+
* plugin uses Moodle filters. The reason is that the app only prefetches filtered
442+
* data, and the user should edit unfiltered data. Defaults to false.
418443
*/
419444
self.registerHandler = function(addon, pluginType, handler) {
420445
if (typeof handlers[pluginType] !== 'undefined') {

www/addons/mod/assign/submission/comments/handlers.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,30 @@ angular.module('mm.addons.mod_assign')
2525

2626
var self = {};
2727

28+
/**
29+
* Check if the plugin can be edited in offline for existing submissions.
30+
* In general, this should return false if the plugin uses Moodle filters. The reason is that the app only prefetches
31+
* filtered data, and the user should edit unfiltered data.
32+
*
33+
* @module mm.addons.mod_assign
34+
* @ngdoc method
35+
* @name $mmaModAssignSubmissionCommentsHandler#canEditOffline
36+
* @param {Object} assign Assignment.
37+
* @param {Object} submission Submission.
38+
* @param {Object} plugin Plugin.
39+
* @return {Boolean} Whether the plugin can be edited in offline for existing submissions.
40+
*/
41+
self.canEditOffline = function(assign, submission, plugin) {
42+
// This plugin is read only, but return true to prevent blocking the edition.
43+
return true;
44+
};
45+
2846
/**
2947
* Whether or not the rule is enabled for the site.
3048
*
49+
* @module mm.addons.mod_assign
50+
* @ngdoc method
51+
* @name $mmaModAssignSubmissionCommentsHandler#isEnabled
3152
* @return {Promise} Promise resolved with true if enabled, rejected or resolved with false otherwise.
3253
*/
3354
self.isEnabled = function() {
@@ -39,7 +60,10 @@ angular.module('mm.addons.mod_assign')
3960
* This should return true if the plugin has no submission component (allow_submissions=false),
4061
* otherwise the user won't be able to edit submissions at all.
4162
*
42-
* @return {Boolean}
63+
* @module mm.addons.mod_assign
64+
* @ngdoc method
65+
* @name $mmaModAssignSubmissionCommentsHandler#isEnabledForEdit
66+
* @return {Boolean} Whether the plugin is enabled.
4367
*/
4468
self.isEnabledForEdit = function() {
4569
return true;
@@ -48,6 +72,9 @@ angular.module('mm.addons.mod_assign')
4872
/**
4973
* Get the name of the directive to render this plugin.
5074
*
75+
* @module mm.addons.mod_assign
76+
* @ngdoc method
77+
* @name $mmaModAssignSubmissionCommentsHandler#getDirectiveName
5178
* @param {Object} plugin Plugin to get the directive for.
5279
* @param {Boolean} edit True if editing a submission, false if read only.
5380
* @return {String} Directive name.
@@ -59,6 +86,9 @@ angular.module('mm.addons.mod_assign')
5986
/**
6087
* Prefetch submission data.
6188
*
89+
* @module mm.addons.mod_assign
90+
* @ngdoc method
91+
* @name $mmaModAssignSubmissionCommentsHandler#prefetch
6292
* @param {Object} assign Assignment.
6393
* @param {Object} submission Submission to check data.
6494
* @param {Object} plugin Plugin.

www/addons/mod/assign/submission/file/directive.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ angular.module('mm.addons.mod_assign')
2222
* @name mmaModAssignSubmissionFile
2323
*/
2424
.directive('mmaModAssignSubmissionFile', function($mmaModAssign, $mmaModAssignSubmissionFileSession, $mmaModAssignHelper,
25-
$mmaModAssignOffline, mmaModAssignSubmissionFileName, $mmFileUploaderHelper) {
25+
$mmaModAssignOffline, mmaModAssignSubmissionFileName, $mmFileUploaderHelper, $q) {
2626
return {
2727
restrict: 'A',
2828
priority: 100,
@@ -32,18 +32,27 @@ angular.module('mm.addons.mod_assign')
3232
return;
3333
}
3434

35-
scope.files = $mmaModAssign.getSubmissionPluginAttachments(scope.plugin);
36-
3735
// Get the offline data.
38-
$mmaModAssignOffline.getSubmission(scope.assign.id).then(function(offlineData) {
39-
if (offlineData && offlineData.plugindata && offlineData.plugindata.files_filemanager &&
40-
offlineData.plugindata.files_filemanager.offline) {
41-
// Has offline files.
42-
return $mmaModAssignHelper.getStoredSubmissionFiles(scope.assign.id, mmaModAssignSubmissionFileName)
43-
.then(function(files) {
44-
files = $mmFileUploaderHelper.markOfflineFiles(files);
45-
scope.files = scope.files.concat(files);
36+
$mmaModAssignOffline.getSubmission(scope.assign.id).catch(function() {
37+
// Error getting data, assume there's no offline submission.
38+
}).then(function(offlineData) {
39+
if (offlineData && offlineData.plugindata && offlineData.plugindata.files_filemanager) {
40+
// Has offline data.
41+
var promise;
42+
if (offlineData.plugindata.files_filemanager.offline) {
43+
promise = $mmaModAssignHelper.getStoredSubmissionFiles(scope.assign.id, mmaModAssignSubmissionFileName);
44+
} else {
45+
promise = $q.when([]);
46+
}
47+
48+
return promise.then(function(offlineFiles) {
49+
var onlineFiles = offlineData.plugindata.files_filemanager.online || [];
50+
offlineFiles = $mmFileUploaderHelper.markOfflineFiles(offlineFiles);
51+
scope.files = onlineFiles.concat(offlineFiles);
4652
});
53+
} else {
54+
// No offline data, get the online files.
55+
scope.files = $mmaModAssign.getSubmissionPluginAttachments(scope.plugin);
4756
}
4857
}).finally(function() {
4958
$mmaModAssignSubmissionFileSession.setFiles(scope.assign.id, scope.files);

0 commit comments

Comments
 (0)