@@ -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} ) ;
0 commit comments