@@ -23,20 +23,22 @@ angular.module('mm.addons.mod_data')
2323 */
2424. controller ( 'mmaModDataEditCtrl' , function ( $scope , $stateParams , $mmaModData , mmaModDataComponent , $q , $mmUtil , $mmaModDataHelper ,
2525 $mmGroups , $ionicHistory , $mmEvents , mmaModDataEventEntryChanged , $mmSite , $translate , $mmFileUploaderHelper , $timeout ,
26- $ionicScrollDelegate ) {
26+ $ionicScrollDelegate , $mmApp , $mmaModDataOffline ) {
2727
2828 var module = $stateParams . module || { } ,
2929 courseId = $stateParams . courseid ,
3030 data ,
3131 scrollView ,
32+ offlineActions ,
3233 siteId = $mmSite . getId ( ) ,
33- entryId = $stateParams . entryid || false ;
34+ offline = ! $mmApp . isOnline ( ) ,
35+ entryId = $stateParams . entryid || false ,
36+ entry ;
3437
3538 $scope . title = module . name ;
3639 $scope . component = mmaModDataComponent ;
3740 $scope . databaseLoaded = false ;
3841 $scope . selectedGroup = $stateParams . group || 0 ;
39- $scope . entryContents = { } ;
4042
4143 // Block leaving the view, we want to show a confirm to the user if there's unsaved data.
4244 $mmUtil . blockLeaveView ( $scope , cancel ) ;
@@ -53,16 +55,8 @@ angular.module('mm.addons.mod_data')
5355 } ) . then ( function ( accessData ) {
5456 $scope . cssTemplate = $mmaModDataHelper . prefixCSS ( data . csstemplate , '.mma-data-entries-' + data . id ) ;
5557
56- if ( entryId ) {
57- // Editing, group is set.
58- return $mmaModData . getEntry ( data . id , entryId ) . then ( function ( entryData ) {
59- $scope . entryContents = { } ;
60- angular . forEach ( entryData . entry . contents , function ( field ) {
61- $scope . entryContents [ field . fieldid ] = field ;
62- } ) ;
63- } ) ;
64- } else {
65- // Adding, get groups.
58+ if ( entryId !== false ) {
59+ // Adding, get groups because it's not set.
6660 return $mmGroups . getActivityGroupInfo ( data . coursemodule , accessData . canmanageentries ) . then ( function ( groupInfo ) {
6761 $scope . groupInfo = groupInfo ;
6862
@@ -82,14 +76,62 @@ angular.module('mm.addons.mod_data')
8276 } ) ;
8377 }
8478 } ) . then ( function ( ) {
79+ return $mmaModDataOffline . getEntryActions ( data . id , entryId ) ;
80+ } ) . then ( function ( actions ) {
81+ offlineActions = actions ;
82+
8583 return $mmaModData . getFields ( data . id ) ;
86- } ) . then ( function ( fields ) {
84+ } ) . then ( function ( fieldsData ) {
8785 $scope . fields = { } ;
88- angular . forEach ( fields , function ( field ) {
86+ angular . forEach ( fieldsData , function ( field ) {
8987 $scope . fields [ field . id ] = field ;
9088 } ) ;
9189
92- $scope . editForm = $mmaModDataHelper . displayEditFields ( data . addtemplate , $scope . fields , $scope . entryContents ) ;
90+ if ( entryId > 0 ) {
91+ return $mmaModData . getEntry ( data . id , entryId ) ;
92+ }
93+
94+ for ( var x in offlineActions ) {
95+ if ( offlineActions [ x ] . action == 'add' ) {
96+ offlineEntry = offlineActions [ x ] ;
97+
98+ var siteInfo = $mmSite . getInfo ( ) ,
99+ entryData = {
100+ id : offlineEntry . entryid ,
101+ canmanageentry : true ,
102+ approved : ! data . approval || data . manageapproved ,
103+ dataid : offlineEntry . dataid ,
104+ groupid : offlineEntry . groupid ,
105+ timecreated : - offlineEntry . entryid ,
106+ timemodified : - offlineEntry . entryid ,
107+ userid : siteInfo . userid ,
108+ fullname : siteInfo . fullname ,
109+ contents : { }
110+ } ;
111+
112+ return { entry : entryData } ;
113+ }
114+ }
115+ } ) . then ( function ( entryData ) {
116+ if ( entryData ) {
117+ entry = entryData . entry ;
118+
119+ // Index contents by fieldid.
120+ var contents = { } ;
121+ angular . forEach ( entry . contents , function ( field ) {
122+ contents [ field . fieldid ] = field ;
123+ } ) ;
124+ entry . contents = contents ;
125+ } else {
126+ entry = { } ;
127+ entry . contents = { } ;
128+ }
129+
130+ return $mmaModDataHelper . applyOfflineActions ( entry , offlineActions , $scope . fields ) ;
131+ } ) . then ( function ( entryData ) {
132+ $scope . entry = entryData ;
133+
134+ $scope . editForm = $mmaModDataHelper . displayEditFields ( data . addtemplate , $scope . fields , entry . contents ) ;
93135 } ) . catch ( function ( message ) {
94136 $mmUtil . showErrorModalDefault ( message , 'mm.course.errorgetmodule' , true ) ;
95137 return $q . reject ( ) ;
@@ -109,7 +151,7 @@ angular.module('mm.addons.mod_data')
109151 // Saves data.
110152 $scope . save = function ( ) {
111153 return $mmaModDataHelper . hasEditDataChanged ( document . forms [ 'mma-mod_data-edit-form' ] , $scope . fields , data . id ,
112- $scope . entryContents ) . then ( function ( changed ) {
154+ $scope . entry . contents ) . then ( function ( changed ) {
113155
114156 if ( ! changed ) {
115157 if ( entryId ) {
@@ -122,14 +164,31 @@ angular.module('mm.addons.mod_data')
122164
123165 var modal = $mmUtil . showModalLoading ( 'mm.core.sending' , true ) ;
124166
167+ // Create an ID to assign files.
168+ var entryTemp = entryId ;
169+ if ( typeof entryId == "undefined" || entryId === false ) {
170+ entryTemp = - ( new Date ( ) . getTime ( ) ) ;
171+ }
172+
125173 return $mmaModDataHelper . getEditDataFromForm ( document . forms [ 'mma-mod_data-edit-form' ] , $scope . fields , data . id ,
126- $scope . entryContents ) . then ( function ( editData ) {
174+ entryTemp , $scope . entry . contents , offline ) . catch ( function ( e ) {
175+ if ( ! offline ) {
176+ // Cannot submit in online, prepare for offline usage.
177+ offline = true ;
178+
179+ return $mmaModDataHelper . getEditDataFromForm ( document . forms [ 'mma-mod_data-edit-form' ] , $scope . fields , data . id ,
180+ entryTemp , $scope . entry . contents , offline ) ;
181+ }
182+
183+ return $q . reject ( e ) ;
184+ } ) . then ( function ( editData ) {
127185
128186 if ( editData . length > 0 ) {
129- if ( entryId ) {
130- return $mmaModData . editEntry ( entryId , editData ) ;
187+ if ( entryId !== false ) {
188+ return $mmaModData . editEntry ( data . id , entryId , courseId , editData , $scope . fields , undefined , offline ) ;
131189 }
132- return $mmaModData . addEntry ( data . id , editData , $scope . selectedGroup ) ;
190+ return $mmaModData . addEntry ( data . id , entryTemp , courseId , editData , $scope . selectedGroup , $scope . fields , undefined ,
191+ offline ) ;
133192 }
134193 } ) . then ( function ( result ) {
135194 if ( ! result ) {
@@ -138,7 +197,7 @@ angular.module('mm.addons.mod_data')
138197 }
139198
140199 // This is done if entry is updated when editing or creating if not.
141- if ( ( entryId && result . updated ) || ( ! entryId && result . newentryid ) ) {
200+ if ( ( entryId !== false && result . updated ) || ( ! entryId && result . newentryid ) ) {
142201 var promises = [ ] ;
143202
144203 entryId = entryId || result . newentryid ;
@@ -186,7 +245,7 @@ angular.module('mm.addons.mod_data')
186245
187246 function returnToEntryList ( ) {
188247 return $mmaModDataHelper . getEditTmpFiles ( document . forms [ 'mma-mod_data-edit-form' ] , $scope . fields , data . id ,
189- $scope . entryContents ) . then ( function ( files ) {
248+ $scope . entry . contents ) . then ( function ( files ) {
190249 $mmFileUploaderHelper . clearTmpFiles ( files ) ;
191250 } ) . finally ( function ( ) {
192251 // Go back to discussions list.
@@ -197,7 +256,7 @@ angular.module('mm.addons.mod_data')
197256 // Just ask to confirm the lost of data.
198257 function cancel ( ) {
199258 return $mmaModDataHelper . hasEditDataChanged ( document . forms [ 'mma-mod_data-edit-form' ] , $scope . fields , data . id ,
200- $scope . entryContents ) . then ( function ( changed ) {
259+ $scope . entry . contents ) . then ( function ( changed ) {
201260 if ( ! changed ) {
202261 return $q . when ( ) ;
203262 }
@@ -206,7 +265,7 @@ angular.module('mm.addons.mod_data')
206265 } ) . then ( function ( ) {
207266 // Delete the local files from the tmp folder.
208267 return $mmaModDataHelper . getEditTmpFiles ( document . forms [ 'mma-mod_data-edit-form' ] , $scope . fields , data . id ,
209- $scope . entryContents ) . then ( function ( files ) {
268+ $scope . entry . contents ) . then ( function ( files ) {
210269 $mmFileUploaderHelper . clearTmpFiles ( files ) ;
211270 } ) ;
212271 } ) ;
0 commit comments