@@ -93,7 +93,7 @@ export class AddonModDataEditPage {
9393 * View loaded.
9494 */
9595 ionViewDidLoad ( ) : void {
96- this . fetchEntryData ( ) ;
96+ this . fetchEntryData ( true ) ;
9797 }
9898
9999 /**
@@ -126,38 +126,78 @@ export class AddonModDataEditPage {
126126 /**
127127 * Fetch the entry data.
128128 *
129+ * @param [refresh] To refresh all downloaded data.
129130 * @return Resolved when done.
130131 */
131- protected fetchEntryData ( ) : Promise < any > {
132- return this . dataProvider . getDatabase ( this . courseId , this . module . id ) . then ( ( data ) => {
133- this . title = data . name || this . title ;
134- this . data = data ;
135- this . cssClass = 'addon-data-entries-' + data . id ;
136-
137- return this . dataProvider . getDatabaseAccessInformation ( data . id , { cmId : this . module . id } ) ;
138- } ) . then ( ( accessData ) => {
139- if ( this . entryId ) {
140- return this . groupsProvider . getActivityGroupInfo ( this . data . coursemodule ) . then ( ( groupInfo ) => {
141- this . groupInfo = groupInfo ;
142- this . selectedGroup = this . groupsProvider . validateGroupId ( this . selectedGroup , groupInfo ) ;
143- } ) ;
144- }
145- } ) . then ( ( ) => {
146- return this . dataProvider . getFields ( this . data . id , { cmId : this . module . id } ) ;
147- } ) . then ( ( fieldsData ) => {
148- this . fieldsArray = fieldsData ;
149- this . fields = this . utils . arrayToObject ( fieldsData , 'id' ) ;
150-
151- return this . dataHelper . fetchEntry ( this . data , fieldsData , this . entryId ) ;
152- } ) . then ( ( entry ) => {
132+ protected async fetchEntryData ( refresh : boolean = false ) : Promise < void > {
133+ try {
134+ this . data = await this . dataProvider . getDatabase ( this . courseId , this . module . id ) ;
135+ this . title = this . data . name || this . title ;
136+ this . cssClass = 'addon-data-entries-' + this . data . id ;
137+
138+ this . fieldsArray = await this . dataProvider . getFields ( this . data . id , { cmId : this . module . id } ) ;
139+ this . fields = this . utils . arrayToObject ( this . fieldsArray , 'id' ) ;
140+
141+ const entry = await this . dataHelper . fetchEntry ( this . data , this . fieldsArray , this . entryId ) ;
142+
153143 this . entry = entry . entry ;
154144
145+ // Load correct group.
146+ this . selectedGroup = this . selectedGroup == null ? this . entry . groupid : this . selectedGroup ;
147+
148+ // Check permissions when adding a new entry or offline entry.
149+ if ( ! this . isEditing ) {
150+ let haveAccess = false ;
151+
152+ if ( refresh ) {
153+ this . groupInfo = await this . groupsProvider . getActivityGroupInfo ( this . data . coursemodule ) ;
154+ this . selectedGroup = this . groupsProvider . validateGroupId ( this . selectedGroup , this . groupInfo ) ;
155+ this . initialSelectedGroup = this . selectedGroup ;
156+ }
157+
158+ if ( this . groupInfo . groups . length > 0 ) {
159+ if ( refresh ) {
160+ const canAddGroup = { } ;
161+
162+ await Promise . all ( this . groupInfo . groups . map ( async ( group ) => {
163+ const accessData = await this . dataProvider . getDatabaseAccessInformation ( this . data . id , {
164+ cmId : this . module . id , groupId : group . id } ) ;
165+
166+ canAddGroup [ group . id ] = accessData . canaddentry ;
167+ } ) ) ;
168+
169+ this . groupInfo . groups = this . groupInfo . groups . filter ( ( group ) => {
170+ return ! ! canAddGroup [ group . id ] ;
171+ } ) ;
172+
173+ haveAccess = canAddGroup [ this . selectedGroup ] ;
174+ } else {
175+ // Groups already filtered, so it have access.
176+ haveAccess = true ;
177+ }
178+ } else {
179+ const accessData = await this . dataProvider . getDatabaseAccessInformation ( this . data . id , { cmId : this . module . id } ) ;
180+ haveAccess = accessData . canaddentry ;
181+ }
182+
183+ if ( ! haveAccess ) {
184+ // You shall not pass, go back.
185+ this . domUtils . showErrorModal ( 'addon.mod_data.noaccess' , true ) ;
186+
187+ // Go back to entry list.
188+ this . forceLeave = true ;
189+ this . navCtrl . pop ( ) ;
190+
191+ return ;
192+ }
193+ }
194+
155195 this . editFormRender = this . displayEditFields ( ) ;
156- } ) . catch ( ( message ) => {
196+ } catch ( message ) {
157197 this . domUtils . showErrorModalDefault ( message , 'core.course.errorgetmodule' , true ) ;
158- } ) . finally ( ( ) => {
159- this . loaded = true ;
160- } ) ;
198+ }
199+
200+ this . loaded = true ;
161201 }
162202
163203 /**
@@ -166,7 +206,7 @@ export class AddonModDataEditPage {
166206 * @param e Event.
167207 * @return Resolved when done.
168208 */
169- save ( e : Event ) : Promise < any > {
209+ save ( e : Event ) : Promise < void > {
170210 e . preventDefault ( ) ;
171211 e . stopPropagation ( ) ;
172212
0 commit comments