Skip to content

Commit 1f29b3a

Browse files
authored
Merge pull request #887 from dpalou/MOBILE-1632
Mobile 1632
2 parents 1437ed8 + 8113e9b commit 1f29b3a

File tree

19 files changed

+1145
-383
lines changed

19 files changed

+1145
-383
lines changed

upgrade.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ information provided here is intended especially for developers.
99
* Now mmUser#formatRoleList is returning a String instead of a Promise.
1010
* The event mmCoreEventSessionExpired now receives an object instead of a string. Also, mmLoginLaunchSiteURL and mmLoginLaunchPassport have been deprecated, please use mmLoginLaunchData instead.
1111
* Handlers registered in $mmFileUploaderDelegate now must implement a getData function instead of getController. This is because now file picker uses action sheet instead of a new view.
12+
* The function $mmaModForumOffline#convertOfflineReplyToOnline is now in $mmaModForumHelper.
1213
* New Phonegap plugin installed. Please run:
1314
ionic plugin add cordova-universal-clipboard
1415
or

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

Lines changed: 5 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ angular.module('mm.addons.mod_assign')
2121
* @ngdoc service
2222
* @name $mmaModAssignHelper
2323
*/
24-
.factory('$mmaModAssignHelper', function($mmUtil, $mmaModAssignSubmissionDelegate, $q, $mmSite, $mmFS, $mmFilepool, $mmaModAssign,
24+
.factory('$mmaModAssignHelper', function($mmUtil, $mmaModAssignSubmissionDelegate, $q, $mmSite, $mmFS, $mmaModAssign,
2525
$mmFileUploader, mmaModAssignComponent, $mmaModAssignOffline, $mmaModAssignFeedbackDelegate) {
2626

2727
var self = {};
@@ -374,47 +374,9 @@ angular.module('mm.addons.mod_assign')
374374
self.storeSubmissionFiles = function(assignId, pluginName, files, userId, siteId) {
375375
siteId = siteId || $mmSite.getId();
376376

377-
var result = {
378-
online: [],
379-
offline: 0
380-
};
381-
382-
if (!files || !files.length) {
383-
return $q.when(result);
384-
}
385-
377+
// Get the folder where to store the files.
386378
return $mmaModAssignOffline.getSubmissionPluginFolder(assignId, pluginName, userId, siteId).then(function(folderPath) {
387-
// Remove unused files from previous submissions.
388-
return $mmFS.removeUnusedFiles(folderPath, files).then(function() {
389-
var promises = [];
390-
391-
angular.forEach(files, function(file) {
392-
if (file.filename && !file.name) {
393-
// It's an online file, add it to the result and ignore it.
394-
result.online.push({
395-
filename: file.filename,
396-
fileurl: file.fileurl
397-
});
398-
return;
399-
} else if (!file.name) {
400-
// Error.
401-
promises.push($q.reject());
402-
} else if (file.fullPath && file.fullPath.indexOf(folderPath) != -1) {
403-
// File already in the submission folder.
404-
result.offline++;
405-
} else {
406-
// Local file, copy it. Use copy instead of move to prevent having a unstable state if
407-
// some copies succeed and others don't.
408-
var destFile = $mmFS.concatenatePaths(folderPath, file.name);
409-
promises.push($mmFS.copyFile(file.toURL(), destFile));
410-
result.offline++;
411-
}
412-
});
413-
414-
return $q.all(promises).then(function() {
415-
return result;
416-
});
417-
});
379+
return $mmFileUploader.storeFilesToUpload(folderPath, files);
418380
});
419381
};
420382

@@ -431,30 +393,7 @@ angular.module('mm.addons.mod_assign')
431393
* @return {Promise} Promise resolved with the itemId.
432394
*/
433395
self.uploadFile = function(assignId, file, itemId, siteId) {
434-
siteId = siteId || $mmSite.getId();
435-
436-
var promise,
437-
fileName;
438-
439-
if (file.filename && !file.name) {
440-
// It's an online file. We need to download it and re-upload it.
441-
fileName = file.filename;
442-
promise = $mmFilepool.downloadUrl(siteId, file.fileurl, false, mmaModAssignComponent, assignId).then(function(path) {
443-
return $mmFS.getExternalFile(path);
444-
});
445-
} else {
446-
// Local file, we already have the file entry.
447-
fileName = file.name;
448-
promise = $q.when(file);
449-
}
450-
451-
return promise.then(function(fileEntry) {
452-
// Now upload the file.
453-
return $mmFileUploader.uploadGenericFile(fileEntry.toURL(), fileName, fileEntry.type, true, 'draft', itemId, siteId)
454-
.then(function(result) {
455-
return result.itemid;
456-
});
457-
});
396+
return $mmFileUploader.uploadOrReuploadFile(file, itemId, mmaModAssignComponent, assignId, siteId);
458397
};
459398

460399
/**
@@ -471,36 +410,7 @@ angular.module('mm.addons.mod_assign')
471410
* @return {Promise} Promise resolved with the itemId.
472411
*/
473412
self.uploadFiles = function(assignId, files, siteId) {
474-
siteId = siteId || $mmSite.getId();
475-
476-
if (!files || !files.length) {
477-
// Return fake draft ID.
478-
return $q.when(1);
479-
}
480-
481-
// Upload only the first file first to get a draft id.
482-
return self.uploadFile(assignId, files[0]).then(function(itemId) {
483-
var promises = [],
484-
error;
485-
486-
angular.forEach(files, function(file, index) {
487-
if (index === 0) {
488-
// First file has already been uploaded.
489-
return;
490-
}
491-
492-
promises.push(self.uploadFile(assignId, file, itemId, siteId).catch(function(message) {
493-
error = message;
494-
return $q.reject();
495-
}));
496-
});
497-
498-
return $q.all(promises).then(function() {
499-
return itemId;
500-
}).catch(function() {
501-
return $q.reject(error);
502-
});
503-
});
413+
return $mmFileUploader.uploadOrReuploadFiles(files, mmaModAssignComponent, assignId, siteId);
504414
};
505415

506416
/**

www/addons/mod/forum/controllers/discussion.js

Lines changed: 68 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,34 @@ angular.module('mm.addons.mod_forum')
2323
*/
2424
.controller('mmaModForumDiscussionCtrl', function($q, $scope, $stateParams, $mmaModForum, $mmSite, $mmUtil, $translate, $mmEvents,
2525
$ionicScrollDelegate, mmaModForumComponent, mmaModForumReplyDiscussionEvent, $mmaModForumOffline, $mmaModForumSync,
26-
mmaModForumAutomSyncedEvent, mmaModForumManualSyncedEvent, $mmApp, $ionicPlatform, mmCoreEventOnlineStatusChanged) {
26+
mmaModForumAutomSyncedEvent, mmaModForumManualSyncedEvent, $mmApp, $ionicPlatform, mmCoreEventOnlineStatusChanged,
27+
$mmaModForumHelper) {
2728

2829
var discussionId = $stateParams.discussionid,
29-
courseid = $stateParams.cid,
30+
courseId = $stateParams.cid,
3031
forumId = $stateParams.forumid,
3132
cmid = $stateParams.cmid,
3233
scrollView,
3334
syncObserver, syncManualObserver, onlineObserver;
3435

36+
// Block leaving the view, we want to show a confirm to the user if there's unsaved data.
37+
$mmUtil.blockLeaveView($scope, leaveView);
38+
3539
$scope.discussionId = discussionId;
3640
$scope.trackPosts = $stateParams.trackposts;
3741
$scope.component = mmaModForumComponent;
3842
$scope.discussionStr = $translate.instant('discussion');
3943
$scope.componentId = cmid;
40-
$scope.courseid = courseid;
44+
$scope.courseId = courseId;
4145
$scope.refreshPostsIcon = 'spinner';
4246
$scope.syncIcon = 'spinner';
43-
$scope.newpost = {
47+
$scope.newPost = {
4448
replyingto: undefined,
4549
editing: undefined,
4650
subject: '',
4751
text: '',
48-
isEditing: false
52+
isEditing: false,
53+
files: []
4954
};
5055
$scope.sort = {
5156
icon: 'ion-arrow-up-c',
@@ -60,12 +65,14 @@ angular.module('mm.addons.mod_forum')
6065
// Receive locked as param since it's returned by getDiscussions. This means that PullToRefresh won't update this value.
6166
$scope.locked = !!$stateParams.locked;
6267

68+
$scope.originalData = {};
69+
6370
// Convenience function to get the forum.
6471
function fetchForum() {
65-
if (courseid && cmid) {
66-
return $mmaModForum.getForum(courseid, cmid);
67-
} else if (courseid && forumId) {
68-
return $mmaModForum.getForumById(courseid, forumId);
72+
if (courseId && cmid) {
73+
return $mmaModForum.getForum(courseId, cmid);
74+
} else if (courseId && forumId) {
75+
return $mmaModForum.getForumById(courseId, forumId);
6976
} else {
7077
// Cannot get the forum.
7178
return $q.reject();
@@ -95,44 +102,39 @@ angular.module('mm.addons.mod_forum')
95102

96103
}).then(function() {
97104
// Check if there are responses stored in offline.
98-
return $mmaModForumOffline.hasDiscussionReplies(discussionId).then(function(hasOffline) {
99-
$scope.postHasOffline = hasOffline;
100-
101-
if (hasOffline) {
102-
return $mmaModForumOffline.getDiscussionReplies(discussionId).then(function(replies) {
103-
var convertPromises = [];
104-
105-
// Index posts to allow quick access.
106-
var posts = {};
107-
angular.forEach(onlinePosts, function(post) {
108-
posts[post.id] = post;
109-
});
110-
111-
angular.forEach(replies, function(offlineReply) {
112-
// If we don't have forumId and courseId, get it from the post.
113-
if (!forumId) {
114-
forumId = offlineReply.forumid;
115-
}
116-
if (!courseid) {
117-
courseid = offlineReply.courseid;
118-
$scope.courseid = courseid;
119-
}
120-
121-
convertPromises.push($mmaModForumOffline.convertOfflineReplyToOnline(offlineReply)
122-
.then(function(reply) {
123-
offlineReplies.push(reply);
124-
125-
// Disable reply of the parent. Reply in offline to the same post is not allowed, edit instead.
126-
posts[reply.parent].canreply = false;
127-
}));
128-
});
129-
130-
return $q.all(convertPromises).then(function() {
131-
// Convert back to array.
132-
onlinePosts = Object.keys(posts).map(function (key) {return posts[key];});
133-
});
134-
});
135-
}
105+
return $mmaModForumOffline.getDiscussionReplies(discussionId).then(function(replies) {
106+
$scope.postHasOffline = !!replies.length;
107+
108+
var convertPromises = [];
109+
110+
// Index posts to allow quick access.
111+
var posts = {};
112+
angular.forEach(onlinePosts, function(post) {
113+
posts[post.id] = post;
114+
});
115+
116+
angular.forEach(replies, function(offlineReply) {
117+
// If we don't have forumId and courseId, get it from the post.
118+
if (!forumId) {
119+
forumId = offlineReply.forumid;
120+
}
121+
if (!courseId) {
122+
courseId = offlineReply.courseid;
123+
$scope.courseId = courseId;
124+
}
125+
126+
convertPromises.push($mmaModForumHelper.convertOfflineReplyToOnline(offlineReply).then(function(reply) {
127+
offlineReplies.push(reply);
128+
129+
// Disable reply of the parent. Reply in offline to the same post is not allowed, edit instead.
130+
posts[reply.parent].canreply = false;
131+
}));
132+
});
133+
134+
return $q.all(convertPromises).then(function() {
135+
// Convert back to array.
136+
onlinePosts = Object.keys(posts).map(function (key) {return posts[key];});
137+
});
136138
});
137139
});
138140
}).then(function() {
@@ -149,7 +151,7 @@ angular.module('mm.addons.mod_forum')
149151
$scope.posts = $mmaModForum.sortDiscussionPosts(posts, $scope.sort.direction);
150152
}
151153
$scope.defaultSubject = $translate.instant('mma.mod_forum.re') + ' ' + $scope.discussion.subject;
152-
$scope.newpost.subject = $scope.defaultSubject;
154+
$scope.newPost.subject = $scope.defaultSubject;
153155

154156
// Now try to get the forum.
155157
return fetchForum().then(function(forum) {
@@ -161,6 +163,7 @@ angular.module('mm.addons.mod_forum')
161163
forumId = forum.id;
162164
cmid = forum.cmid;
163165
$scope.componentId = cmid;
166+
$scope.forum = forum;
164167
}).catch(function() {
165168
// Ignore errors.
166169
});
@@ -303,6 +306,23 @@ angular.module('mm.addons.mod_forum')
303306
}
304307
});
305308

309+
// Ask to confirm if there are changes.
310+
function leaveView() {
311+
var promise;
312+
313+
if (!$mmaModForumHelper.hasPostDataChanged($scope.newPost, $scope.originalData)) {
314+
promise = $q.when();
315+
} else {
316+
// Show confirmation if some data has been modified.
317+
promise = $mmUtil.showConfirm($translate('mm.core.confirmcanceledit'));
318+
}
319+
320+
return promise.then(function() {
321+
// Delete the local files from the tmp folder.
322+
$mmaModForumHelper.clearTmpFiles($scope.newPost.files);
323+
});
324+
}
325+
306326
function scrollTop() {
307327
if (!scrollView) {
308328
scrollView = $ionicScrollDelegate.$getByHandle('mmaModForumPostsScroll');
@@ -312,12 +332,6 @@ angular.module('mm.addons.mod_forum')
312332

313333
// New post added.
314334
$scope.postListChanged = function() {
315-
$scope.newpost.replyingto = undefined;
316-
$scope.newpost.editing = undefined;
317-
$scope.newpost.subject = $scope.defaultSubject;
318-
$scope.newpost.text = '';
319-
$scope.newpost.isEditing = false;
320-
321335
notifyPostListChanged();
322336

323337
$scope.discussionLoaded = false;

0 commit comments

Comments
 (0)