Skip to content

Commit fb915fd

Browse files
authored
Merge pull request #931 from crazyserver/MOBILE-1966-2
Mobile 1966 2
2 parents 88e95e8 + d8d01ce commit fb915fd

File tree

20 files changed

+425
-249
lines changed

20 files changed

+425
-249
lines changed

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

Lines changed: 4 additions & 8 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) {
25+
$mmaModAssignOffline, mmaModAssignSubmissionFileName, $mmFileUploaderHelper) {
2626
return {
2727
restrict: 'A',
2828
priority: 100,
@@ -40,13 +40,9 @@ angular.module('mm.addons.mod_assign')
4040
offlineData.plugindata.files_filemanager.offline) {
4141
// Has offline files.
4242
return $mmaModAssignHelper.getStoredSubmissionFiles(scope.assign.id, mmaModAssignSubmissionFileName)
43-
.then(function(result) {
44-
// Mark the files as pending offline.
45-
angular.forEach(result, function(file) {
46-
file.offline = true;
47-
file.filename = file.name;
48-
});
49-
scope.files = scope.files.concat(result);
43+
.then(function(files) {
44+
files = $mmFileUploaderHelper.markOfflineFiles(files);
45+
scope.files = scope.files.concat(files);
5046
});
5147
}
5248
}).finally(function() {

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ angular.module('mm.addons.mod_assign')
2424
* @name $mmaModAssignSubmissionFileHandler
2525
*/
2626
.factory('$mmaModAssignSubmissionFileHandler', function($mmaModAssignSubmissionFileSession, $mmaModAssign, $mmSite, $q,
27-
$mmaModAssignHelper, $mmWS, $mmFS, $mmFilepool, $mmUtil, $mmaModAssignOffline, mmaModAssignSubmissionFileName) {
27+
$mmaModAssignHelper, $mmWS, $mmFS, $mmFilepool, $mmUtil, $mmaModAssignOffline, mmaModAssignSubmissionFileName,
28+
$mmFileUploaderHelper) {
2829

2930
var self = {};
3031

@@ -42,13 +43,8 @@ angular.module('mm.addons.mod_assign')
4243

4344
// Clear the files in session for this assign.
4445
$mmaModAssignSubmissionFileSession.clearFiles(assign.id);
45-
4646
// Now delete the local files from the tmp folder.
47-
files.forEach(function(file) {
48-
if (!file.offline && file.remove) {
49-
file.remove();
50-
}
51-
});
47+
$mmFileUploaderHelper.clearTmpFiles(files);
5248
};
5349

5450
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ angular.module('mm.addons.mod_forum')
2424
.controller('mmaModForumDiscussionCtrl', function($q, $scope, $stateParams, $mmaModForum, $mmSite, $mmUtil, $translate, $mmEvents,
2525
$ionicScrollDelegate, mmaModForumComponent, mmaModForumReplyDiscussionEvent, $mmaModForumOffline, $mmaModForumSync,
2626
mmaModForumAutomSyncedEvent, mmaModForumManualSyncedEvent, $mmApp, $ionicPlatform, mmCoreEventOnlineStatusChanged,
27-
$mmaModForumHelper) {
27+
$mmaModForumHelper, $mmFileUploaderHelper) {
2828

2929
var discussionId = $stateParams.discussionid,
3030
courseId = $stateParams.cid,
@@ -285,7 +285,7 @@ angular.module('mm.addons.mod_forum')
285285

286286
return promise.then(function() {
287287
// Delete the local files from the tmp folder.
288-
$mmaModForumHelper.clearTmpFiles($scope.newPost.files);
288+
$mmFileUploaderHelper.clearTmpFiles($scope.newPost.files);
289289
});
290290
}
291291

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ angular.module('mm.addons.mod_forum')
2323
*/
2424
.controller('mmaModForumNewDiscussionCtrl', function($scope, $stateParams, $mmGroups, $q, $mmaModForum, $mmEvents, $ionicPlatform,
2525
$mmUtil, $ionicHistory, $translate, mmaModForumNewDiscussionEvent, $mmaModForumOffline, $mmSite, mmaModForumComponent,
26-
mmaModForumAutomSyncedEvent, $mmSyncBlock, $mmaModForumSync, $mmText, $mmaModForumHelper) {
26+
mmaModForumAutomSyncedEvent, $mmSyncBlock, $mmaModForumSync, $mmText, $mmaModForumHelper, $mmFileUploaderHelper) {
2727

2828
var courseId = $stateParams.cid,
2929
forumId = $stateParams.forumid,
@@ -250,7 +250,7 @@ angular.module('mm.addons.mod_forum')
250250
$mmEvents.trigger(mmaModForumNewDiscussionEvent, data);
251251

252252
// Delete the local files from the tmp folder.
253-
$mmaModForumHelper.clearTmpFiles($scope.newDiscussion.files);
253+
$mmFileUploaderHelper.clearTmpFiles($scope.newDiscussion.files);
254254

255255
if ($ionicPlatform.isTablet()) {
256256
// Empty form.
@@ -278,7 +278,7 @@ angular.module('mm.addons.mod_forum')
278278

279279
return promise.then(function() {
280280
// Delete the local files from the tmp folder.
281-
$mmaModForumHelper.clearTmpFiles($scope.newDiscussion.files);
281+
$mmFileUploaderHelper.clearTmpFiles($scope.newDiscussion.files);
282282
});
283283
}
284284

www/addons/mod/forum/directives/discussionpost.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ angular.module('mm.addons.mod_forum')
4141
* @param {Object} [originalData] Original newpost data. Used to detect if data has changed.
4242
*/
4343
.directive('mmaModForumDiscussionPost', function($mmaModForum, $mmUtil, $translate, $q, $mmaModForumOffline, $mmSyncBlock,
44-
mmaModForumComponent, $mmaModForumSync, $mmText, $mmaModForumHelper, $ionicScrollDelegate) {
44+
mmaModForumComponent, $mmaModForumSync, $mmText, $mmaModForumHelper, $ionicScrollDelegate, $mmFileUploaderHelper) {
4545

4646
// Confirm discard changes if any.
4747
function confirmDiscard(scope) {
@@ -56,7 +56,7 @@ angular.module('mm.addons.mod_forum')
5656
// Set data to new post, clearing tmp files and updating original data.
5757
function setPostData(scope, scrollView, replyingTo, editing, isEditing, subject, text, files) {
5858
// Delete the local files from the tmp folder if any.
59-
$mmaModForumHelper.clearTmpFiles(scope.newpost.files);
59+
$mmFileUploaderHelper.clearTmpFiles(scope.newpost.files);
6060

6161
scope.newpost.replyingto = replyingTo;
6262
scope.newpost.editing = editing;

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

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,11 @@ angular.module('mm.addons.mod_forum')
2121
* @ngdoc service
2222
* @name $mmaModForumHelper
2323
*/
24-
.factory('$mmaModForumHelper', function($mmaModForumOffline, $mmSite, $mmFileUploader, $mmFS, mmaModForumComponent, $mmUser, $q) {
24+
.factory('$mmaModForumHelper', function($mmaModForumOffline, $mmSite, $mmFileUploader, $mmFS, mmaModForumComponent, $mmUser, $q,
25+
$mmFileUploaderHelper) {
2526

2627
var self = {};
2728

28-
/**
29-
* Clear temporary attachments because a new discussion or post was cancelled.
30-
* Attachments already saved in an offline discussion or post will NOT be deleted.
31-
*
32-
* @module mm.addons.mod_forum
33-
* @ngdoc method
34-
* @name $mmaModForumHelper#clearTmpFiles
35-
* @param {Object[]} files List of current files.
36-
* @return {Void}
37-
*/
38-
self.clearTmpFiles = function(files) {
39-
// Delete the local files from the tmp folder.
40-
files.forEach(function(file) {
41-
if (!file.offline && file.remove) {
42-
file.remove();
43-
}
44-
});
45-
};
46-
4729
/**
4830
* Convert offline reply to online format in order to be compatible with them.
4931
*
@@ -150,7 +132,7 @@ angular.module('mm.addons.mod_forum')
150132
*/
151133
self.getNewDiscussionStoredFiles = function(forumId, timecreated, siteId) {
152134
return $mmaModForumOffline.getNewDiscussionFolder(forumId, timecreated, siteId).then(function(folderPath) {
153-
return getStoredFiles(folderPath);
135+
return $mmFileUploaderHelper.getStoredFiles(folderPath);
154136
});
155137
};
156138

@@ -168,27 +150,10 @@ angular.module('mm.addons.mod_forum')
168150
*/
169151
self.getReplyStoredFiles = function(forumId, postId, siteId, userId) {
170152
return $mmaModForumOffline.getReplyFolder(forumId, postId, siteId, userId).then(function(folderPath) {
171-
return getStoredFiles(folderPath);
153+
return $mmFileUploaderHelper.getStoredFiles(folderPath);
172154
});
173155
};
174156

175-
/**
176-
* Get the files stored in a folder, marking them as offline.
177-
*
178-
* @param {String} folderPath Folder where to get the files.
179-
* @return {Promise} Promise resolved with the list of files.
180-
*/
181-
function getStoredFiles(folderPath) {
182-
return $mmFS.getDirectoryContents(folderPath).then(function(files) {
183-
// Mark the files as pending offline.
184-
angular.forEach(files, function(file) {
185-
file.offline = true;
186-
file.filename = file.name;
187-
});
188-
return files;
189-
});
190-
}
191-
192157
/**
193158
* Check if the data of a post/discussion has changed.
194159
*
@@ -205,20 +170,11 @@ angular.module('mm.addons.mod_forum')
205170
return false;
206171
}
207172

208-
var postFiles = post.files || [],
209-
originalFiles = original.files || [];
210-
211-
if (original.subject != post.subject || original.text != post.text || postFiles.length != originalFiles.length) {
173+
if (original.subject != post.subject || original.text != post.text) {
212174
return true;
213175
}
214176

215-
for (var i = 0; i < postFiles.length; i++) {
216-
if (postFiles[i].name != originalFiles[i].name) {
217-
return true;
218-
}
219-
}
220-
221-
return false;
177+
return $mmFileUploaderHelper.areFileListDifferent(post.files, original.files);
222178
};
223179

224180
/**

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

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ angular.module('mm.addons.mod_glossary')
2222
* @name mmaModGlossaryEditCtrl
2323
*/
2424
.controller('mmaModGlossaryEditCtrl', function($stateParams, $scope, mmaModGlossaryComponent, $mmUtil, $q, $mmaModGlossary, $mmText,
25-
$translate, $ionicHistory, $mmEvents, mmaModGlossaryAddEntryEvent, $mmaModGlossaryOffline, $mmaModGlossaryHelper) {
25+
$translate, $ionicHistory, $mmEvents, mmaModGlossaryAddEntryEvent, $mmaModGlossaryOffline, $mmaModGlossaryHelper, $mmLang,
26+
$mmFileUploaderHelper) {
2627

2728
var module = $stateParams.module,
2829
courseId = $stateParams.courseid,
2930
cmid = $stateParams.cmid,
3031
glossaryId = $stateParams.glossaryid,
3132
glossary = $stateParams.glossary || {},
32-
entry = $stateParams.entry || false;
33+
originalData = null,
34+
entry = $stateParams.entry || false,
35+
allowDuplicateEntries = !!glossary.allowduplicatedentries;
3336

3437
$scope.entry = {
3538
concept: '',
@@ -38,11 +41,11 @@ angular.module('mm.addons.mod_glossary')
3841
$scope.title = module.name;
3942
$scope.component = mmaModGlossaryComponent;
4043
$scope.componentId = module.id;
41-
$scope.autolinking = glossary.usedynalink;
44+
$scope.autolinking = !!glossary.usedynalink;
4245
$scope.options = {
4346
categories: null,
4447
aliases: "",
45-
usedynalink: glossary.usedynalink,
48+
usedynalink: false,
4649
casesensitive: false,
4750
fullmatch: false
4851
};
@@ -51,18 +54,27 @@ angular.module('mm.addons.mod_glossary')
5154
if (entry) {
5255
$scope.entry.concept = entry.concept || '';
5356
$scope.entry.text = entry.definition || '';
57+
58+
originalData = {};
59+
originalData.text = $scope.entry.text;
60+
originalData.concept = $scope.entry.concept;
61+
originalData.files = [];
62+
5463
if (entry.options) {
5564
$scope.options.categories = entry.options.categories || null;
5665
$scope.options.aliases = entry.options.aliases || "";
57-
$scope.options.usedynalink = !!entry.options.usedynalink || glossary.usedynalink;
58-
$scope.options.casesensitive = !!entry.options.casesensitive;
59-
$scope.options.fullmatch = !!entry.options.fullmatch;
66+
$scope.options.usedynalink = !!entry.options.usedynalink;
67+
if ($scope.options.usedynalink) {
68+
$scope.options.casesensitive = !!entry.options.casesensitive;
69+
$scope.options.fullmatch = !!entry.options.fullmatch;
70+
}
6071
}
6172

6273
// Treat offline attachments if any.
6374
if (entry.attachments && entry.attachments.offline) {
64-
$mmaModGlossaryHelper.getStoredFiles(glossaryId, entry.concept).then(function(files) {
75+
$mmaModGlossaryHelper.getStoredFiles(glossaryId, entry.concept, entry.timecreated).then(function(files) {
6576
$scope.attachments = files;
77+
originalData.files = angular.copy(files);
6678
});
6779
}
6880
}
@@ -92,7 +104,7 @@ angular.module('mm.addons.mod_glossary')
92104
function cancel() {
93105
var promise;
94106

95-
if (!$mmaModGlossaryHelper.hasEntryDataChanged($scope.entry, $scope.attachments)) {
107+
if (!$mmaModGlossaryHelper.hasEntryDataChanged($scope.entry, $scope.attachments, originalData)) {
96108
promise = $q.when();
97109
} else {
98110
// Show confirmation if some data has been modified.
@@ -101,7 +113,7 @@ angular.module('mm.addons.mod_glossary')
101113

102114
return promise.then(function() {
103115
// Delete the local files from the tmp folder.
104-
$mmaModGlossaryHelper.clearTmpFiles($scope.attachments);
116+
$mmFileUploaderHelper.clearTmpFiles($scope.attachments);
105117
});
106118
}
107119

@@ -110,6 +122,7 @@ angular.module('mm.addons.mod_glossary')
110122
definition = $scope.entry.text,
111123
modal,
112124
attachments,
125+
timecreated = entry && entry.timecreated || Date.now(),
113126
saveOffline = false;
114127

115128
if (!concept || !definition) {
@@ -126,22 +139,15 @@ angular.module('mm.addons.mod_glossary')
126139
definition = $mmText.formatHtmlLines(definition);
127140
}
128141

129-
// If editing an offline entry and concept is different, delete previous first.
130-
if (entry.concept && entry.concept != concept) {
131-
return $mmaModGlossaryOffline.deleteAddEntry(glossaryId, entry.concept);
132-
}
133-
return $q.when();
134-
135-
}).then(function() {
136142
attachments = $scope.attachments;
137143

138144
// Upload attachments first if any.
139145
if (attachments.length) {
140-
return $mmaModGlossaryHelper.uploadOrStoreFiles(glossaryId, concept, attachments, false)
146+
return $mmaModGlossaryHelper.uploadOrStoreFiles(glossaryId, concept, timecreated, attachments, false)
141147
.catch(function() {
142148
// Cannot upload them in online, save them in offline.
143149
saveOffline = true;
144-
return $mmaModGlossaryHelper.uploadOrStoreFiles(glossaryId, concept, attachments, true);
150+
return $mmaModGlossaryHelper.uploadOrStoreFiles(glossaryId, concept, timecreated, attachments, true);
145151
});
146152
}
147153
}).then(function(attach) {
@@ -165,21 +171,37 @@ angular.module('mm.addons.mod_glossary')
165171
}
166172

167173
if (saveOffline) {
168-
// Save entry in offline.
169-
return $mmaModGlossaryOffline.saveAddEntry(glossaryId, concept, definition, courseId, options, attach).then(function() {
170-
// Don't return anything.
174+
var promise;
175+
if (entry && !allowDuplicateEntries) {
176+
// Check if the entry is duplicated in online or offline mode.
177+
promise = $mmaModGlossary.isConceptUsed(glossaryId, concept).then(function() {
178+
// There's a entry with same name, reject with error message.
179+
return $mmLang.translateAndReject('mma.mod_glossary.errconceptalreadyexists');
180+
}, function() {
181+
// Not found, entry can be sent.
182+
});
183+
} else {
184+
promise = $q.when();
185+
}
186+
187+
return promise.then(function() {
188+
// Save entry in offline.
189+
return $mmaModGlossaryOffline.saveAddEntry(glossaryId, concept, definition, courseId, options, attach,
190+
undefined, undefined, entry).then(function() {
191+
// Don't return anything.
192+
});
171193
});
172194
} else {
173195
// Try to send it to server.
174196
// Don't allow offline if there are attachments since they were uploaded fine.
175197
return $mmaModGlossary.addEntry(glossaryId, concept, definition, courseId, options, attach, undefined,
176-
!attachments.length);
198+
entry, !attachments.length);
177199
}
178200
}).then(function(entryId) {
179201
if (entryId) {
180202
$scope.entry.id = entryId;
181203
// Data sent to server, delete stored files (if any).
182-
$mmaModGlossaryHelper.deleteStoredFiles(glossaryId, concept);
204+
$mmaModGlossaryHelper.deleteStoredFiles(glossaryId, concept, timecreated);
183205
}
184206
$scope.entry.glossaryid = glossaryId;
185207
$scope.entry.definition = definition;
@@ -198,7 +220,7 @@ angular.module('mm.addons.mod_glossary')
198220
entry: $scope.entry
199221
};
200222

201-
$mmaModGlossaryHelper.clearTmpFiles($scope.attachments);
223+
$mmFileUploaderHelper.clearTmpFiles($scope.attachments);
202224

203225
$mmEvents.trigger(mmaModGlossaryAddEntryEvent, data);
204226

www/addons/mod/glossary/controllers/entry.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,6 @@ angular.module('mm.addons.mod_glossary')
8585
// Log that the entry was viewed.
8686
$mmaModGlossary.logEntryView(entry.id);
8787
}).finally(function() {
88-
$scope.loaded = true;
88+
$scope.entryLoaded = true;
8989
});
9090
});

0 commit comments

Comments
 (0)