Skip to content

Commit 7f34948

Browse files
committed
MOBILE-2178 database: Fix sync errors and autom sync detection
1 parent c698bd6 commit 7f34948

File tree

4 files changed

+46
-18
lines changed

4 files changed

+46
-18
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,15 @@ angular.module('mm.addons.mod_data')
214214

215215
// Refresh entry on sync.
216216
syncObserver = $mmEvents.on(mmaModDataEventAutomSynced, function(eventData) {
217-
if (eventData.entryid == entryId && data.id == eventData.dataid && $mmSite.getId() == eventData.siteid) {
217+
if ((eventData.entryid == entryId || eventData.offlineentryid == entryId) && data.id == eventData.dataid &&
218+
$mmSite.getId() == eventData.siteid) {
218219
if (eventData.deleted) {
219220
// If deleted, go back.
220221
$ionicHistory.goBack();
221222
} else {
223+
entryId = eventData.entryid;
222224
$scope.databaseLoaded = false;
223-
return fetchEntryData(true);
225+
fetchEntryData(true);
224226
}
225227
}
226228
});

www/addons/mod/data/controllers/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ angular.module('mm.addons.mod_data')
439439
// Update just when all database is synced.
440440
if (data.id == eventData.dataid && siteId == eventData.siteid && typeof eventData.entryid == "undefined") {
441441
$scope.databaseLoaded = false;
442-
return fetchDatabaseData(true);
442+
fetchDatabaseData(true);
443443
}
444444
});
445445

www/addons/mod/data/services/data_offline.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,27 @@ angular.module('mm.addons.mod_data')
6262

6363
var self = {};
6464

65+
/**
66+
* Delete all the actions of an entry.
67+
*
68+
* @module mm.addons.mod_data
69+
* @ngdoc method
70+
* @name $mmaModDataOffline#deleteAllEntryActions
71+
* @param {Number} dataId Database ID.
72+
* @param {Number} entryId Database entry ID.
73+
* @param {String} [siteId] Site ID. If not defined, current site.
74+
* @return {Promise} Promise resolved if deleted, rejected if failure.
75+
*/
76+
self.deleteAllEntryActions = function(dataId, entryId, siteId) {
77+
return self.getEntryActions(dataId, entryId, siteId).then(function(actions) {
78+
var promises = [];
79+
angular.forEach(actions, function(action) {
80+
promises.push(self.deleteEntry(dataId, entryId, action.action, siteId));
81+
});
82+
return $q.all(promises);
83+
});
84+
};
85+
6586
/**
6687
* Delete an stored entry.
6788
*
@@ -122,7 +143,7 @@ angular.module('mm.addons.mod_data')
122143
* @param {Number} dataId Database ID.
123144
* @param {String} entryId Database entry Id.
124145
* @param {String} [siteId] Site ID. If not defined, current site.
125-
* @return {Promise} Promise resolved with entry.
146+
* @return {Promise} Promise resolved with entry actions.
126147
*/
127148
self.getEntryActions = function(dataId, entryId, siteId) {
128149
return $mmSitesManager.getSite(siteId).then(function(site) {

www/addons/mod/data/services/data_sync.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ angular.module('mm.addons.mod_data')
188188
});
189189

190190
angular.forEach(offlineEntries, function(entryActions) {
191-
promises.push(syncEntry(data, entryActions, result.warnings, siteId));
191+
promises.push(syncEntry(data, entryActions, result, siteId));
192192
});
193193

194194
return $q.all(promises);
@@ -216,16 +216,17 @@ angular.module('mm.addons.mod_data')
216216
/**
217217
* Synchronize an entry.
218218
*
219-
* @param {Object} data Database.
220-
* @param {Object} entryActions Entry actions.
221-
* @param {Object[]} warnings List of warnings.
222-
* @param {String} [siteId] Site ID. If not defined, current site.
223-
* @return {Promise} Promise resolved if success, rejected otherwise.
219+
* @param {Object} data Database.
220+
* @param {Object} entryActions Entry actions.
221+
* @param {Object} result Object with the result of the sync.
222+
* @param {String} [siteId] Site ID. If not defined, current site.
223+
* @return {Promise} Promise resolved if success, rejected otherwise.
224224
*/
225-
function syncEntry(data, entryActions, warnings, siteId) {
225+
function syncEntry(data, entryActions, result, siteId) {
226226
var discardError,
227227
timePromise,
228228
entryId = 0,
229+
offlineId,
229230
deleted = false,
230231
promises = [];
231232

@@ -237,20 +238,22 @@ angular.module('mm.addons.mod_data')
237238
entryId = entryActions[0].entryid;
238239

239240
if (entryId > 0) {
240-
timePromise = $mmaModData.getEntry(data.id, entryActions[0].entryid, siteId).then(function(entry) {
241+
timePromise = $mmaModData.getEntry(data.id, entryId, siteId).then(function(entry) {
241242
return entry.entry.timemodified;
242243
}).catch(function() {
243244
return -1;
244245
});
245246
} else {
247+
offlineId = entryId;
246248
timePromise = $q.when(0);
247249
}
248250

249251
return timePromise.then(function(timemodified) {
250252
if (timemodified < 0 || timemodified >= entryActions[0].timemodified) {
251253
// The entry was not found in Moodle or the entry has been modified, discard the action.
254+
result.updated = true;
252255
discardError = $translate.instant('mma.mod_data.warningsubmissionmodified');
253-
return;
256+
return $mmaModDataOffline.deleteAllEntryActions(data.id, entryId, siteId);
254257
}
255258

256259
angular.forEach(entryActions, function(action) {
@@ -310,6 +313,7 @@ angular.module('mm.addons.mod_data')
310313
}
311314
}).then(function() {
312315
// Delete the offline data.
316+
result.updated = true;
313317
return $mmaModDataOffline.deleteEntry(action.dataid, action.entryid, action.action, siteId);
314318
}));
315319
});
@@ -323,17 +327,18 @@ angular.module('mm.addons.mod_data')
323327
error: discardError
324328
});
325329

326-
if (warnings.indexOf(message) == -1) {
327-
warnings.push(message);
330+
if (result.warnings.indexOf(message) == -1) {
331+
result.warnings.push(message);
328332
}
329333
}
330334

331335
// Sync done. Send event.
332336
$mmEvents.trigger(mmaModDataEventAutomSynced, {
333337
siteid: siteId,
334-
dataid: action.dataid,
335-
entryid: action.entryid,
336-
warnings: warnings,
338+
dataid: data.id,
339+
entryid: entryId,
340+
offlineentryid: offlineId,
341+
warnings: result.warnings,
337342
deleted: deleted
338343
});
339344
});

0 commit comments

Comments
 (0)