Skip to content

Commit 851379a

Browse files
committed
MOBILE-2127 data: Sync activity
1 parent 6c6fd78 commit 851379a

File tree

23 files changed

+535
-88
lines changed

23 files changed

+535
-88
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ angular.module('mm.addons.mod_data')
109109
contents: {}
110110
};
111111

112-
return $q.when({entry: entryData});
112+
return {entry: entryData};
113113
}
114114
}
115115
}).then(function(entryData) {
@@ -166,7 +166,7 @@ angular.module('mm.addons.mod_data')
166166

167167
// Create an ID to assign files.
168168
var entryTemp = entryId;
169-
if ((typeof entryId == "undefined" || entryId === false) && offline) {
169+
if (typeof entryId == "undefined" || entryId === false) {
170170
entryTemp = - (new Date().getTime());
171171
}
172172

@@ -175,9 +175,6 @@ angular.module('mm.addons.mod_data')
175175
if (!offline) {
176176
// Cannot submit in online, prepare for offline usage.
177177
offline = true;
178-
if (typeof entryTemp == "undefined") {
179-
entryTemp = - (new Date().getTime());
180-
}
181178

182179
return $mmaModDataHelper.getEditDataFromForm(document.forms['mma-mod_data-edit-form'], $scope.fields, data.id,
183180
entryTemp, $scope.entry.contents, offline);

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ angular.module('mm.addons.mod_data')
2323
*/
2424
.controller('mmaModDataEntryCtrl', function($scope, $stateParams, $mmaModData, mmaModDataComponent, $mmCourse, $q, $mmEvents,
2525
$mmText, $translate, $mmUtil, $mmSite, $mmaModDataHelper, $mmGroups, $ionicScrollDelegate, mmaModDataEventEntryChanged,
26-
$ionicHistory, $mmaModDataOffline) {
26+
$ionicHistory, $mmaModDataOffline, mmaModDataEventAutomSynced) {
2727

2828
var module = $stateParams.module || {},
2929
courseId = $stateParams.courseid,
3030
entryId = $stateParams.entryid || false,
3131
page = $stateParams.page || false,
3232
data,
3333
entryChangedObserver,
34+
syncObserver,
3435
scrollView,
3536
access,
3637
offlineActions = [];
@@ -211,7 +212,21 @@ angular.module('mm.addons.mod_data')
211212
}
212213
});
213214

215+
// Refresh entry on sync.
216+
syncObserver = $mmEvents.on(mmaModDataEventAutomSynced, function(eventData) {
217+
if (eventData.entryid == entryId && data.id == eventData.dataid && $mmSite.getId() == eventData.siteid) {
218+
if (eventData.deleted) {
219+
// If deleted, go back.
220+
$ionicHistory.goBack();
221+
} else {
222+
$scope.databaseLoaded = false;
223+
return fetchEntryData(true);
224+
}
225+
}
226+
});
227+
214228
$scope.$on('$destroy', function() {
215229
entryChangedObserver && entryChangedObserver.off && entryChangedObserver.off();
230+
syncObserver && syncObserver.off && syncObserver.off();
216231
});
217232
});

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

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ angular.module('mm.addons.mod_data')
2323
*/
2424
.controller('mmaModDataIndexCtrl', function($scope, $stateParams, $mmaModData, mmaModDataComponent, $mmCourse, $mmCourseHelper, $q,
2525
$mmText, $translate, $mmEvents, mmCoreEventOnlineStatusChanged, $mmApp, $mmUtil, $mmSite, $mmaModDataHelper, $mmGroups,
26-
mmaModDataEventEntryChanged, $ionicModal, mmaModDataPerPage, $state, $mmComments, $mmaModDataOffline) {
26+
mmaModDataEventEntryChanged, $ionicModal, mmaModDataPerPage, $state, $mmComments, $mmaModDataOffline, $mmaModDataSync,
27+
mmaModDataEventAutomSynced) {
2728

2829
var module = $stateParams.module || {},
2930
courseId = $stateParams.courseid,
3031
siteId = $mmSite.getId(),
3132
data,
3233
entryChangedObserver,
3334
onlineObserver,
35+
syncObserver,
3436
hasComments = false;
3537

3638
$scope.title = module.name;
@@ -65,9 +67,14 @@ angular.module('mm.addons.mod_data')
6567
$scope.data = databaseData;
6668

6769
$scope.database = data;
68-
70+
if (sync) {
71+
// Try to synchronize the database.
72+
return syncDatabase(showErrors).catch(function() {
73+
// Ignore errors.
74+
});
75+
}
76+
}).then(function() {
6977
return $mmaModData.getDatabaseAccessInformation(data.id);
70-
7178
}).then(function(accessData) {
7279
$scope.access = accessData;
7380

@@ -135,7 +142,7 @@ angular.module('mm.addons.mod_data')
135142

136143
$mmUtil.showErrorModalDefault(message, 'mm.course.errorgetmodule', true);
137144
return $q.reject();
138-
}).finally(function(){
145+
}).finally(function() {
139146
$scope.databaseLoaded = true;
140147
});
141148
}
@@ -288,6 +295,22 @@ angular.module('mm.addons.mod_data')
288295
});
289296
}
290297

298+
// Tries to synchronize the database.
299+
function syncDatabase(showErrors) {
300+
return $mmaModDataSync.syncDatabase(data.id).then(function(result) {
301+
if (result.warnings && result.warnings.length) {
302+
$mmUtil.showErrorModal(result.warnings[0]);
303+
}
304+
305+
return result.updated;
306+
}).catch(function(error) {
307+
if (showErrors) {
308+
$mmUtil.showErrorModalDefault(error, 'mm.core.errorsync', true);
309+
}
310+
return $q.reject();
311+
});
312+
}
313+
291314
fetchDatabaseData(false, true).then(function() {
292315
$mmaModData.logView(data.id).then(function() {
293316
$mmCourse.checkModuleCompletion(courseId, module.completionstatus);
@@ -356,7 +379,7 @@ angular.module('mm.addons.mod_data')
356379
return fetchEntriesData().catch(function(message) {
357380
$mmUtil.showErrorModalDefault(message, 'mm.course.errorgetmodule', true);
358381
return $q.reject();
359-
}).finally(function(){
382+
}).finally(function() {
360383
$scope.databaseLoaded = true;
361384
});
362385
};
@@ -399,16 +422,26 @@ angular.module('mm.addons.mod_data')
399422
$scope.isOnline = online;
400423
});
401424

402-
// Refresh entry on change.
425+
// Refresh entries on change.
403426
entryChangedObserver = $mmEvents.on(mmaModDataEventEntryChanged, function(eventData) {
404427
if (data.id == eventData.dataId && siteId == eventData.siteId) {
405428
$scope.databaseLoaded = false;
406429
return fetchDatabaseData(true);
407430
}
408431
});
409432

433+
// Refresh entries on sync.
434+
syncObserver = $mmEvents.on(mmaModDataEventAutomSynced, function(eventData) {
435+
// Update just when all database is synced.
436+
if (data.id == eventData.dataid && siteId == eventData.siteid && typeof eventData.entryid == "undefined") {
437+
$scope.databaseLoaded = false;
438+
return fetchDatabaseData(true);
439+
}
440+
});
441+
410442
$scope.$on('$destroy', function() {
411443
onlineObserver && onlineObserver.off && onlineObserver.off();
412444
entryChangedObserver && entryChangedObserver.off && entryChangedObserver.off();
445+
syncObserver && syncObserver.off && syncObserver.off();
413446
});
414447
});

www/addons/mod/data/directives/action.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ angular.module('mm.addons.mod_data')
1919
*
2020
* @module mm.addons.mod_data
2121
* @ngdoc directive
22-
* @name mmaModDataField
22+
* @name mmaModDataAction
2323
*/
24-
.directive('mmaModDataAction', function($mmSite, $mmUser, $mmaModDataOffline, $mmSite, $mmEvents, mmaModDataEventEntryChanged) {
24+
.directive('mmaModDataAction', function($mmSite, $mmUser, $mmaModDataOffline, $mmEvents, mmaModDataEventEntryChanged) {
2525
return {
2626
restrict: 'E',
2727
priority: 100,
@@ -40,10 +40,10 @@ angular.module('mm.addons.mod_data')
4040
});
4141
}
4242

43-
scope.undoDelete = function(entryId) {
44-
var dataId = scope.database.id,
43+
scope.undoDelete = function() {
44+
var dataId = scope.database.id;
4545
entryId = scope.entry.id;
46-
return $mmaModDataOffline.getEntry(dataId, entryId, 'delete').then(function(entry) {
46+
return $mmaModDataOffline.getEntry(dataId, entryId, 'delete').then(function() {
4747
// Found. Just delete the action.
4848
return $mmaModDataOffline.deleteEntry(dataId, entryId, 'delete');
4949
}).then(function() {

www/addons/mod/data/fields/checkbox/handler.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,6 @@ angular.module('mm.addons.mod_data')
126126
/**
127127
* Override field content data with offline submission.
128128
*
129-
* @module mm.addons.mod_data
130-
* @ngdoc method
131-
* @name $mmaModDataFieldsDelegate#overrideData
132129
* @param {Object} originalContent Original data to be overriden.
133130
* @param {Array} offlineContent Array with all the offline data to override.
134131
* @return {Object} Data overriden

www/addons/mod/data/fields/date/handler.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,13 @@ angular.module('mm.addons.mod_data')
135135
/**
136136
* Override field content data with offline submission.
137137
*
138-
* @module mm.addons.mod_data
139-
* @ngdoc method
140-
* @name $mmaModDataFieldsDelegate#overrideData
141138
* @param {Object} originalContent Original data to be overriden.
142139
* @param {Array} offlineContent Array with all the offline data to override.
143140
* @return {Object} Data overriden
144141
*/
145142
self.overrideData = function(originalContent, offlineContent) {
146-
var date = Date.UTC(offlineContent['year'] || "", offlineContent['month'] ? offlineContent['month'] - 1 : "",
147-
offlineContent['day'] || "");
143+
var date = Date.UTC(offlineContent.year || "", offlineContent.month ? offlineContent.month - 1 : "",
144+
offlineContent.day || "");
148145
date = Math.floor(date / 1000);
149146

150147
originalContent.content = date || "";

www/addons/mod/data/fields/file/handler.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,6 @@ angular.module('mm.addons.mod_data')
107107
/**
108108
* Override field content data with offline submission.
109109
*
110-
* @module mm.addons.mod_data
111-
* @ngdoc method
112-
* @name $mmaModDataFieldsDelegate#overrideData
113110
* @param {Object} originalContent Original data to be overriden.
114111
* @param {Array} offlineContent Array with all the offline data to override.
115112
* @param {Array} offlineFiles Array with all the offline files in the field.

www/addons/mod/data/fields/latlong/handler.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,19 @@ angular.module('mm.addons.mod_data')
9999
* @return {String} String with the notification or false.
100100
*/
101101
self.getFieldsNotifications = function(field, inputData) {
102-
var valuecount = 0;
102+
var valueCount = 0;
103103

104104
// The lat long class has two values that need to be checked.
105105
angular.forEach(inputData, function(value) {
106106
if (typeof value.value != "undefined" && value.value != "") {
107-
valuecount++;
107+
valueCount++;
108108
}
109109
});
110110

111111
// If we get here then only one field has been filled in.
112-
if (valuecount == 1) {
112+
if (valueCount == 1) {
113113
return $translate.instant('mma.mod_data.latlongboth');
114-
} else if (field.required && valuecount == 0) {
114+
} else if (field.required && valueCount == 0) {
115115
return $translate.instant('mma.mod_data.errormustsupplyvalue');
116116
}
117117
return false;
@@ -120,9 +120,6 @@ angular.module('mm.addons.mod_data')
120120
/**
121121
* Override field content data with offline submission.
122122
*
123-
* @module mm.addons.mod_data
124-
* @ngdoc method
125-
* @name $mmaModDataFieldsDelegate#overrideData
126123
* @param {Object} originalContent Original data to be overriden.
127124
* @param {Array} offlineContent Array with all the offline data to override.
128125
* @return {Object} Data overriden

www/addons/mod/data/fields/menu/handler.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,6 @@ angular.module('mm.addons.mod_data')
9595
/**
9696
* Override field content data with offline submission.
9797
*
98-
* @module mm.addons.mod_data
99-
* @ngdoc method
100-
* @name $mmaModDataFieldsDelegate#overrideData
10198
* @param {Object} originalContent Original data to be overriden.
10299
* @param {Array} offlineContent Array with all the offline data to override.
103100
* @return {Object} Data overriden

www/addons/mod/data/fields/multimenu/handler.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,6 @@ angular.module('mm.addons.mod_data')
114114
/**
115115
* Override field content data with offline submission.
116116
*
117-
* @module mm.addons.mod_data
118-
* @ngdoc method
119-
* @name $mmaModDataFieldsDelegate#overrideData
120117
* @param {Object} originalContent Original data to be overriden.
121118
* @param {Array} offlineContent Array with all the offline data to override.
122119
* @return {Object} Data overriden

0 commit comments

Comments
 (0)