Skip to content

Commit a30dacd

Browse files
committed
MOBILE-1987 glossary: Check the duplicate is not the one we are editing
1 parent 9430c06 commit a30dacd

File tree

4 files changed

+74
-27
lines changed

4 files changed

+74
-27
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ angular.module('mm.addons.mod_glossary')
142142
attachments = $scope.attachments;
143143

144144
// Upload attachments first if any.
145-
if (attachments.length) {
145+
if (!!attachments.length) {
146146
return $mmaModGlossaryHelper.uploadOrStoreFiles(glossaryId, concept, timecreated, attachments, false)
147147
.catch(function() {
148148
// Cannot upload them in online, save them in offline.
@@ -174,7 +174,7 @@ angular.module('mm.addons.mod_glossary')
174174
var promise;
175175
if (entry && !allowDuplicateEntries) {
176176
// Check if the entry is duplicated in online or offline mode.
177-
promise = $mmaModGlossary.isConceptUsed(glossaryId, concept).then(function() {
177+
promise = $mmaModGlossary.isConceptUsed(glossaryId, concept, entry.timecreated).then(function() {
178178
// There's a entry with same name, reject with error message.
179179
return $mmLang.translateAndReject('mma.mod_glossary.errconceptalreadyexists');
180180
}, function() {
@@ -195,7 +195,7 @@ angular.module('mm.addons.mod_glossary')
195195
// Try to send it to server.
196196
// Don't allow offline if there are attachments since they were uploaded fine.
197197
return $mmaModGlossary.addEntry(glossaryId, concept, definition, courseId, options, attach, undefined,
198-
entry, !attachments.length);
198+
entry, !attachments.length, !allowDuplicateEntries);
199199
}
200200
}).then(function(entryId) {
201201
if (entryId) {

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

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -758,18 +758,20 @@ angular.module('mm.addons.mod_glossary')
758758
* @module mm.addons.mod_glossary
759759
* @ngdoc method
760760
* @name $mmaModGlossary#addEntry
761-
* @param {Number} glossaryId Glossary ID.
762-
* @param {String} concept Glossary entry concept.
763-
* @param {String} definition Glossary entry concept definition.
764-
* @param {Number} courseId Course ID of the glossary.
765-
* @param {Array} [options] Array of options for the entry.
766-
* @param {Mixed} [attach] Attachments ID if sending online, result of $mmFileUploader#storeFilesToUpload otherwise.
767-
* @param {String} [siteId] Site ID. If not defined, current site.
768-
* @param {Object} [discardEntry] The entry provided will be discarded if found.
769-
* @param {Boolean} allowOffline True if it can be stored in offline, false otherwise.
761+
* @param {Number} glossaryId Glossary ID.
762+
* @param {String} concept Glossary entry concept.
763+
* @param {String} definition Glossary entry concept definition.
764+
* @param {Number} courseId Course ID of the glossary.
765+
* @param {Array} [options] Array of options for the entry.
766+
* @param {Mixed} [attach] Attachments ID if sending online, result of $mmFileUploader#storeFilesToUpload otherwise.
767+
* @param {String} [siteId] Site ID. If not defined, current site.
768+
* @param {Object} [discardEntry] The entry provided will be discarded if found.
769+
* @param {Boolean} allowOffline True if it can be stored in offline, false otherwise.
770+
* @param {Boolean} [checkDuplicates] Check for duplicates before storing offline. Only used if allowOffline is true.
770771
* @return {Promise} Promise resolved with entry ID if entry was created in server, false if stored in device.
771772
*/
772-
self.addEntry = function(glossaryId, concept, definition, courseId, options, attach, siteId, discardEntry, allowOffline) {
773+
self.addEntry = function(glossaryId, concept, definition, courseId, options, attach, siteId, discardEntry, allowOffline,
774+
checkDuplicates) {
773775
siteId = siteId || $mmSite.getId();
774776

775777
if (!$mmApp.isOnline() && allowOffline) {
@@ -798,8 +800,10 @@ angular.module('mm.addons.mod_glossary')
798800

799801
// Convenience function to store a new entry to be synchronized later.
800802
function storeOffline() {
803+
var timecreated = discardEntry && discardEntry.timecreated,
804+
duplicatesPromise = checkDuplicates ? self.isConceptUsed(glossaryId, concept, timecreated, siteId) : $q.when(false);
801805
// Check if the entry is duplicated in online or offline mode.
802-
return self.isConceptUsed(glossaryId, concept, siteId).then(function(used) {
806+
return duplicatesPromise.then(function(used) {
803807
if (used) {
804808
return $mmLang.translateAndReject('mma.mod_glossary.errconceptalreadyexists');
805809
}
@@ -882,14 +886,15 @@ angular.module('mm.addons.mod_glossary')
882886
* @module mm.addons.mod_glossary
883887
* @ngdoc method
884888
* @name $mmaModGlossary#isConceptUsed
885-
* @param {Number} glossaryId Glossary ID.
886-
* @param {String} concept Concept to check.
887-
* @param {String} [siteId] Site ID. If not defined, current site.
888-
* @return {Promise} Promise resolved with true if used, resolved with false if not used or error.
889+
* @param {Number} glossaryId Glossary ID.
890+
* @param {String} concept Concept to check.
891+
* @param {Number} [timecreated] Timecreated to check that is not the timecreated we are editing.
892+
* @param {String} [siteId] Site ID. If not defined, current site.
893+
* @return {Promise} Promise resolved with true if used, resolved with false if not used or error.
889894
*/
890-
self.isConceptUsed = function(glossaryId, concept, siteId) {
895+
self.isConceptUsed = function(glossaryId, concept, timecreated, siteId) {
891896
// Check offline first.
892-
return $mmaModGlossaryOffline.isConceptUsed(glossaryId, concept, siteId).then(function(exists) {
897+
return $mmaModGlossaryOffline.isConceptUsed(glossaryId, concept, timecreated, siteId).then(function(exists) {
893898
if (exists) {
894899
return true;
895900
}

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ angular.module('mm.addons.mod_glossary')
5252
* @ngdoc service
5353
* @name $mmaModGlossaryOffline
5454
*/
55-
.factory('$mmaModGlossaryOffline', function($mmSitesManager, $log, mmaModGlossaryAddEntryStore, $mmFS, $q) {
55+
.factory('$mmaModGlossaryOffline', function($mmSitesManager, $log, mmaModGlossaryAddEntryStore, $mmFS, $q, $mmUtil) {
5656
$log = $log.getInstance('$mmaModGlossaryOffline');
5757

5858
var self = {};
@@ -132,16 +132,24 @@ angular.module('mm.addons.mod_glossary')
132132
* @module mm.addons.mod_glossary
133133
* @ngdoc method
134134
* @name $mmaModGlossaryOffline#isConceptUsed
135-
* @param {Number} glossaryId Glossary ID.
136-
* @param {String} concept Concept to check.
137-
* @param {String} [siteId] Site ID. If not defined, current site.
138-
* @return {Promise} Promise resolved with true if concept is found, false otherwise.
135+
* @param {Number} glossaryId Glossary ID.
136+
* @param {String} concept Concept to check.
137+
* @param {Number} [timecreated] Timecreated to check that is not the timecreated we are editing.
138+
* @param {String} [siteId] Site ID. If not defined, current site.
139+
* @return {Promise} Promise resolved with true if concept is found, false otherwise.
139140
*/
140-
self.isConceptUsed = function(glossaryId, concept, siteId) {
141+
self.isConceptUsed = function(glossaryId, concept, timecreated, siteId) {
141142
return $mmSitesManager.getSite(siteId).then(function(site) {
142143
return site.getDb().whereEqual(mmaModGlossaryAddEntryStore, 'glossaryAndConcept', [glossaryId, concept])
143144
.then(function(entries) {
144-
return !!entries.length;
145+
if (!!entries.length) {
146+
if (entries.length > 1 || !timecreated) {
147+
return true;
148+
}
149+
// If there's only one entry, check that is not the one we are editing.
150+
return $mmUtil.promiseFails(self.getAddEntry(glossaryId, concept, timecreated, siteId));
151+
}
152+
return false;
145153
});
146154
}).catch(function() {
147155
// No offline data found, return false.

www/core/lib/util.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,40 @@ angular.module('mm.core')
12031203
return deferred.promise;
12041204
};
12051205

1206+
/**
1207+
* Check if a promise works and returns true if resolves or false if rejects.
1208+
*
1209+
* @module mm.core
1210+
* @ngdoc method
1211+
* @name $mmUtil#promiseWorks
1212+
* @param {Promise} promise Promise to check
1213+
* @return {Promise} Promise resolved with true if the promises resolves and false if rejects.
1214+
*/
1215+
self.promiseWorks = function(promise) {
1216+
return promise.then(function() {
1217+
return true;
1218+
}).catch(function() {
1219+
return false;
1220+
});
1221+
};
1222+
1223+
/**
1224+
* Check if a promise works and returns true if rejects or false if resolves.
1225+
*
1226+
* @module mm.core
1227+
* @ngdoc method
1228+
* @name $mmUtil#promiseFails
1229+
* @param {Promise} promise Promise to check
1230+
* @return {Promise} Promise resolved with true if the promises rejects and false if resolves.
1231+
*/
1232+
self.promiseFails = function(promise) {
1233+
return promise.then(function() {
1234+
return false;
1235+
}).catch(function() {
1236+
return true;
1237+
});
1238+
};
1239+
12061240
/**
12071241
* Compare two objects. This function won't compare functions and proto properties, it's a basic compare.
12081242
* Also, this will only check if itemA's properties are in itemB with same value. This function will still

0 commit comments

Comments
 (0)