Skip to content

Commit d8d01ce

Browse files
committed
MOBILE-1966 glossary: Discard entries just before saving new one
1 parent d2de7e2 commit d8d01ce

File tree

5 files changed

+38
-29
lines changed

5 files changed

+38
-29
lines changed

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,6 @@ angular.module('mm.addons.mod_glossary')
139139
definition = $mmText.formatHtmlLines(definition);
140140
}
141141

142-
// If editing an offline entry, delete previous first.
143-
if (entry) {
144-
return $mmaModGlossaryOffline.deleteAddEntry(glossaryId, entry.concept, entry.timecreated);
145-
}
146-
return $q.when();
147-
148-
}).then(function() {
149142
attachments = $scope.attachments;
150143

151144
// Upload attachments first if any.
@@ -178,25 +171,31 @@ angular.module('mm.addons.mod_glossary')
178171
}
179172

180173
if (saveOffline) {
174+
var promise;
181175
if (entry && !allowDuplicateEntries) {
182176
// Check if the entry is duplicated in online or offline mode.
183177
promise = $mmaModGlossary.isConceptUsed(glossaryId, concept).then(function() {
184-
// There's a page with same name, reject with error message.
178+
// There's a entry with same name, reject with error message.
185179
return $mmLang.translateAndReject('mma.mod_glossary.errconceptalreadyexists');
186180
}, function() {
187-
// Not found, page can be sent.
181+
// Not found, entry can be sent.
188182
});
183+
} else {
184+
promise = $q.when();
189185
}
190186

191-
// Save entry in offline.
192-
return $mmaModGlossaryOffline.saveAddEntry(glossaryId, concept, definition, courseId, options, attach).then(function() {
193-
// Don't return anything.
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+
});
194193
});
195194
} else {
196195
// Try to send it to server.
197196
// Don't allow offline if there are attachments since they were uploaded fine.
198197
return $mmaModGlossary.addEntry(glossaryId, concept, definition, courseId, options, attach, undefined,
199-
entry.timecreated, !attachments.length);
198+
entry, !attachments.length);
200199
}
201200
}).then(function(entryId) {
202201
if (entryId) {

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -765,11 +765,11 @@ angular.module('mm.addons.mod_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.
767767
* @param {String} [siteId] Site ID. If not defined, current site.
768-
* @param {Number} [timecreated] The time the entry was created. Only used when editing entries.
769-
* @param {Boolean} allowOffline True if it can be stored in offline, false otherwise.
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.
770770
* @return {Promise} Promise resolved with entry ID if entry was created in server, false if stored in device.
771771
*/
772-
self.addEntry = function(glossaryId, concept, definition, courseId, options, attach, siteId, timecreated, allowOffline) {
772+
self.addEntry = function(glossaryId, concept, definition, courseId, options, attach, siteId, discardEntry, allowOffline) {
773773
siteId = siteId || $mmSite.getId();
774774

775775
if (!$mmApp.isOnline() && allowOffline) {
@@ -778,8 +778,8 @@ angular.module('mm.addons.mod_glossary')
778778
}
779779

780780
// If we are editing an offline entry, discard previous first.
781-
var discardPromise = timecreated ?
782-
$mmaModGlossaryOffline.deleteAddEntry(glossaryId, concept, timecreated, siteId) : $q.when();
781+
var discardPromise = discardEntry ?
782+
$mmaModGlossaryOffline.deleteAddEntry(glossaryId, discardEntry.concept, discardEntry.timecreated, siteId) : $q.when();
783783

784784
return discardPromise.then(function() {
785785
// Try to add it in online.
@@ -796,7 +796,7 @@ angular.module('mm.addons.mod_glossary')
796796
});
797797
});
798798

799-
// Convenience function to store a new page to be synchronized later.
799+
// Convenience function to store a new entry to be synchronized later.
800800
function storeOffline() {
801801
// Check if the entry is duplicated in online or offline mode.
802802
return self.isConceptUsed(glossaryId, concept, siteId).then(function(used) {
@@ -805,7 +805,7 @@ angular.module('mm.addons.mod_glossary')
805805
}
806806

807807
return $mmaModGlossaryOffline.saveAddEntry(glossaryId, concept, definition, courseId, options, attach,
808-
siteId).then(function() {
808+
siteId, undefined, discardEntry).then(function() {
809809
return false;
810810
});
811811
});
@@ -879,6 +879,9 @@ angular.module('mm.addons.mod_glossary')
879879
/**
880880
* Check if a entry concept is already used.
881881
*
882+
* @module mm.addons.mod_glossary
883+
* @ngdoc method
884+
* @name $mmaModGlossary#isConceptUsed
882885
* @param {Number} glossaryId Glossary ID.
883886
* @param {String} concept Concept to check.
884887
* @param {String} [siteId] Site ID. If not defined, current site.

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

Lines changed: 13 additions & 6 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) {
55+
.factory('$mmaModGlossaryOffline', function($mmSitesManager, $log, mmaModGlossaryAddEntryStore, $mmFS, $q) {
5656
$log = $log.getInstance('$mmaModGlossaryOffline');
5757

5858
var self = {};
@@ -82,7 +82,7 @@ angular.module('mm.addons.mod_glossary')
8282
* @ngdoc method
8383
* @name $mmaModGlossaryOffline#getAllAddEntries
8484
* @param {String} [siteId] Site ID. If not defined, current site.
85-
* @return {Promise} Promise resolved with pages.
85+
* @return {Promise} Promise resolved with entries.
8686
*/
8787
self.getAllAddEntries = function(siteId) {
8888
return $mmSitesManager.getSite(siteId).then(function(site) {
@@ -100,7 +100,7 @@ angular.module('mm.addons.mod_glossary')
100100
* @param {String} concept Glossary entry concept.
101101
* @param {Number} timecreated Time to allow duplicated entries.
102102
* @param {String} [siteId] Site ID. If not defined, current site.
103-
* @return {Promise} Promise resolved with page.
103+
* @return {Promise} Promise resolved with entry.
104104
*/
105105
self.getAddEntry = function(glossaryId, concept, timecreated, siteId) {
106106
return $mmSitesManager.getSite(siteId).then(function(site) {
@@ -117,7 +117,7 @@ angular.module('mm.addons.mod_glossary')
117117
* @param {Number} glossaryId Glossary ID.
118118
* @param {String} [siteId] Site ID. If not defined, current site.
119119
* @param {Number} [userId] User the entries belong to. If not defined, current user in site.
120-
* @return {Promise} Promise resolved with pages.
120+
* @return {Promise} Promise resolved with entries.
121121
*/
122122
self.getGlossaryAddEntries = function(glossaryId, siteId, userId) {
123123
return $mmSitesManager.getSite(siteId).then(function(site) {
@@ -164,9 +164,10 @@ angular.module('mm.addons.mod_glossary')
164164
* @param {Object} [attach] Result of $mmFileUploader#storeFilesToUpload for attachments.
165165
* @param {String} [siteId] Site ID. If not defined, current site.
166166
* @param {Number} [userId] User the entry belong to. If not defined, current user in site.
167+
* @param {Object} [discardEntry] The entry provided will be discarded if found.
167168
* @return {Promise} Promise resolved if stored, rejected if failure.
168169
*/
169-
self.saveAddEntry = function(glossaryId, concept, definition, courseId, options, attach, siteId, userId) {
170+
self.saveAddEntry = function(glossaryId, concept, definition, courseId, options, attach, siteId, userId, discardEntry) {
170171
return $mmSitesManager.getSite(siteId).then(function(site) {
171172
userId = userId || site.getUserId();
172173

@@ -185,7 +186,13 @@ angular.module('mm.addons.mod_glossary')
185186
entry.attachments = attach;
186187
}
187188

188-
return site.getDb().insert(mmaModGlossaryAddEntryStore, entry);
189+
// If editing an offline entry, delete previous first.
190+
var discardPromise = discardEntry ?
191+
self.deleteAddEntry(glossaryId, discardEntry.concept, discardEntry.timecreated, site.getId()) : $q.when();
192+
193+
return discardPromise.then(function() {
194+
return site.getDb().insert(mmaModGlossaryAddEntryStore, entry);
195+
});
189196
});
190197
};
191198

www/addons/mod/glossary/templates/edit.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
<ion-nav-buttons side="secondary">
44
<a class="button button-clear" ng-click="save()"> {{ 'mm.core.save' | translate }}</a>
55
</ion-nav-buttons>
6-
<ion-content mm-state-class delegate-handle="mmaModGlossaryEditPage">
6+
<ion-content mm-state-class delegate-handle="mmaModGlossaryEditEntry">
77
<mm-loading hide-until="glossaryLoaded">
88
<form>
99
<div class="item-title">
1010
<input type="text" placeholder="{{ 'mma.mod_glossary.concept' | translate }}" ng-model="entry.concept">
1111
</div>
1212
<div class="item item-noborder mm-item-has-rich-text-editor">
13-
<mm-rich-text-editor model="entry" placeholder="{{ 'mma.mod_glossary.definition' | translate }}" name="glossary_entry_definition" scroll-handle="mmaModGlossaryEditPage" component="{{component}}" component-id="{{componentId}}"></mm-rich-text-editor>
13+
<mm-rich-text-editor model="entry" placeholder="{{ 'mma.mod_glossary.definition' | translate }}" name="glossary_entry_definition" scroll-handle="mmaModGlossaryEditEntry" component="{{component}}" component-id="{{componentId}}"></mm-rich-text-editor>
1414
</div>
1515
<mm-multiple-select ng-if="categories.length > 0" title="{{ 'mma.mod_glossary.categories' | translate }}" options="categories" key-property="id" value-property="name" selected-property="selected">
1616
</mm-multiple-select>

www/core/lib/sitesfactory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ angular.module('mm.core')
817817
};
818818

819819
/**
820-
* Invalidates all the cache entries with for an array of keys.
820+
* Invalidates all the cache entries in an array of keys.
821821
*
822822
* @param {Array} keys Keys to search.
823823
* @return {Promise} Promise resolved when the cache entries are invalidated.

0 commit comments

Comments
 (0)