Skip to content

Commit e60c54f

Browse files
committed
MOBILE-1987 glossary: Check timecreated when getting offline files
1 parent 363c2d7 commit e60c54f

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,15 @@ angular.module('mm.addons.mod_glossary')
151151
});
152152
}
153153
}).then(function(attach) {
154-
var cats = $scope.categories.filter(function(category) {
155-
return category.selected;
156-
}).map(function(category) {
157-
return category.id;
158-
});
154+
var cats = [];
155+
156+
if ($scope.categories) {
157+
cats = $scope.categories.filter(function(category) {
158+
return category.selected;
159+
}).map(function(category) {
160+
return category.id;
161+
});
162+
}
159163

160164
var options = {
161165
aliases: $scope.options.aliases || "",
@@ -174,11 +178,11 @@ angular.module('mm.addons.mod_glossary')
174178
var promise;
175179
if (entry && !allowDuplicateEntries) {
176180
// Check if the entry is duplicated in online or offline mode.
177-
promise = $mmaModGlossary.isConceptUsed(glossaryId, concept, entry.timecreated).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.
181+
promise = $mmaModGlossary.isConceptUsed(glossaryId, concept, entry.timecreated).then(function(used) {
182+
if (used) {
183+
// There's a entry with same name, reject with error message.
184+
return $mmLang.translateAndReject('mma.mod_glossary.errconceptalreadyexists');
185+
}
182186
});
183187
} else {
184188
promise = $q.when();
@@ -187,14 +191,14 @@ angular.module('mm.addons.mod_glossary')
187191
return promise.then(function() {
188192
// Save entry in offline.
189193
return $mmaModGlossaryOffline.saveAddEntry(glossaryId, concept, definition, courseId, options, attach,
190-
undefined, undefined, entry).then(function() {
194+
timecreated, undefined, undefined, entry).then(function() {
191195
// Don't return anything.
192196
});
193197
});
194198
} else {
195199
// Try to send it to server.
196200
// Don't allow offline if there are attachments since they were uploaded fine.
197-
return $mmaModGlossary.addEntry(glossaryId, concept, definition, courseId, options, attach, undefined,
201+
return $mmaModGlossary.addEntry(glossaryId, concept, definition, courseId, options, attach, timecreated, undefined,
198202
entry, !attachments.length, !allowDuplicateEntries);
199203
}
200204
}).then(function(entryId) {

www/addons/mod/glossary/services/glossary.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -764,14 +764,15 @@ angular.module('mm.addons.mod_glossary')
764764
* @param {Number} courseId Course ID of the glossary.
765765
* @param {Array} [options] Array of options for the entry.
766766
* @param {Mixed} [attach] Attachments ID if sending online, result of $mmFileUploader#storeFilesToUpload otherwise.
767+
* @param {Number} [timecreated] The time the entry was created. If not defined, current time.
767768
* @param {String} [siteId] Site ID. If not defined, current site.
768769
* @param {Object} [discardEntry] The entry provided will be discarded if found.
769770
* @param {Boolean} allowOffline True if it can be stored in offline, false otherwise.
770771
* @param {Boolean} [checkDuplicates] Check for duplicates before storing offline. Only used if allowOffline is true.
771772
* @return {Promise} Promise resolved with entry ID if entry was created in server, false if stored in device.
772773
*/
773-
self.addEntry = function(glossaryId, concept, definition, courseId, options, attach, siteId, discardEntry, allowOffline,
774-
checkDuplicates) {
774+
self.addEntry = function(glossaryId, concept, definition, courseId, options, attach, timecreated, siteId, discardEntry,
775+
allowOffline, checkDuplicates) {
775776
siteId = siteId || $mmSite.getId();
776777

777778
if (!$mmApp.isOnline() && allowOffline) {
@@ -800,15 +801,15 @@ angular.module('mm.addons.mod_glossary')
800801

801802
// Convenience function to store a new entry to be synchronized later.
802803
function storeOffline() {
803-
var timecreated = discardEntry && discardEntry.timecreated,
804-
duplicatesPromise = checkDuplicates ? self.isConceptUsed(glossaryId, concept, timecreated, siteId) : $q.when(false);
804+
var discardTime = discardEntry && discardEntry.timecreated,
805+
duplicatesPromise = checkDuplicates ? self.isConceptUsed(glossaryId, concept, discardTime, siteId) : $q.when(false);
805806
// Check if the entry is duplicated in online or offline mode.
806807
return duplicatesPromise.then(function(used) {
807808
if (used) {
808809
return $mmLang.translateAndReject('mma.mod_glossary.errconceptalreadyexists');
809810
}
810811

811-
return $mmaModGlossaryOffline.saveAddEntry(glossaryId, concept, definition, courseId, options, attach,
812+
return $mmaModGlossaryOffline.saveAddEntry(glossaryId, concept, definition, courseId, options, attach, timecreated,
812813
siteId, undefined, discardEntry).then(function() {
813814
return false;
814815
});
@@ -909,7 +910,6 @@ angular.module('mm.addons.mod_glossary')
909910
return true;
910911
}
911912
}
912-
913913
return false;
914914
});
915915
}).catch(function() {

www/addons/mod/glossary/services/glossary_offline.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,14 @@ angular.module('mm.addons.mod_glossary')
170170
* @param {Number} courseId Course ID of the glossary.
171171
* @param {Array} [options] Array of options for the entry.
172172
* @param {Object} [attach] Result of $mmFileUploader#storeFilesToUpload for attachments.
173+
* @param {Number} [timecreated] The time the entry was created. If not defined, current time.
173174
* @param {String} [siteId] Site ID. If not defined, current site.
174175
* @param {Number} [userId] User the entry belong to. If not defined, current user in site.
175176
* @param {Object} [discardEntry] The entry provided will be discarded if found.
176177
* @return {Promise} Promise resolved if stored, rejected if failure.
177178
*/
178-
self.saveAddEntry = function(glossaryId, concept, definition, courseId, options, attach, siteId, userId, discardEntry) {
179+
self.saveAddEntry = function(glossaryId, concept, definition, courseId, options, attach, timecreated, siteId, userId,
180+
discardEntry) {
179181
return $mmSitesManager.getSite(siteId).then(function(site) {
180182
userId = userId || site.getUserId();
181183

@@ -187,7 +189,7 @@ angular.module('mm.addons.mod_glossary')
187189
definitionformat: 'html',
188190
options: options,
189191
userid: userId,
190-
timecreated: new Date().getTime()
192+
timecreated: timecreated || new Date().getTime()
191193
};
192194

193195
if (attach) {

0 commit comments

Comments
 (0)