Skip to content

Commit 1652e4d

Browse files
committed
MOBILE-3200 database: Fix update access data on group change
1 parent 8d95636 commit 1652e4d

File tree

1 file changed

+69
-68
lines changed
  • src/addon/mod/data/components/index

1 file changed

+69
-68
lines changed

src/addon/mod/data/components/index/index.ts

Lines changed: 69 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -180,69 +180,67 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp
180180
* @param showErrors If show errors to the user of hide them.
181181
* @return Promise resolved when done.
182182
*/
183-
protected fetchContent(refresh: boolean = false, sync: boolean = false, showErrors: boolean = false): Promise<any> {
183+
protected async fetchContent(refresh: boolean = false, sync: boolean = false, showErrors: boolean = false): Promise<any> {
184184
let canAdd = false,
185185
canSearch = false;
186186

187-
return this.dataProvider.getDatabase(this.courseId, this.module.id).then((data) => {
188-
this.data = data;
189-
this.hasComments = data.comments;
187+
this.data = await this.dataProvider.getDatabase(this.courseId, this.module.id);
188+
this.hasComments = this.data.comments;
190189

191-
this.description = data.intro || data.description;
192-
this.dataRetrieved.emit(data);
190+
this.description = this.data.intro || this.data.description;
191+
this.dataRetrieved.emit(this.data);
193192

194-
if (sync) {
193+
if (sync) {
194+
try {
195195
// Try to synchronize the data.
196-
return this.syncActivity(showErrors).catch(() => {
197-
// Ignore errors.
198-
});
196+
await this.syncActivity(showErrors);
197+
} catch (error) {
198+
// Ignore errors.
199199
}
200-
}).then(() => {
201-
return this.dataProvider.getDatabaseAccessInformation(this.data.id, {cmId: this.module.id});
202-
}).then((accessData) => {
203-
this.access = accessData;
200+
}
204201

205-
if (!accessData.timeavailable) {
206-
const time = this.timeUtils.timestamp();
202+
this.groupInfo = await this.groupsProvider.getActivityGroupInfo(this.data.coursemodule);
203+
this.selectedGroup = this.groupsProvider.validateGroupId(this.selectedGroup, this.groupInfo);
207204

208-
this.timeAvailableFrom = this.data.timeavailablefrom && time < this.data.timeavailablefrom ?
209-
parseInt(this.data.timeavailablefrom, 10) * 1000 : false;
210-
this.timeAvailableFromReadable = this.timeAvailableFrom ? this.timeUtils.userDate(this.timeAvailableFrom) : false;
211-
this.timeAvailableTo = this.data.timeavailableto && time > this.data.timeavailableto ?
212-
parseInt(this.data.timeavailableto, 10) * 1000 : false;
213-
this.timeAvailableToReadable = this.timeAvailableTo ? this.timeUtils.userDate(this.timeAvailableTo) : false;
205+
this.access = await this.dataProvider.getDatabaseAccessInformation(this.data.id, {
206+
cmId: this.module.id,
207+
groupId: this.selectedGroup || undefined
208+
});
214209

215-
this.isEmpty = true;
216-
this.groupInfo = null;
210+
if (!this.access.timeavailable) {
211+
const time = this.timeUtils.timestamp();
217212

218-
return;
219-
}
213+
this.timeAvailableFrom = this.data.timeavailablefrom && time < this.data.timeavailablefrom ?
214+
parseInt(this.data.timeavailablefrom, 10) * 1000 : false;
215+
this.timeAvailableFromReadable = this.timeAvailableFrom ? this.timeUtils.userDate(this.timeAvailableFrom) : false;
216+
this.timeAvailableTo = this.data.timeavailableto && time > this.data.timeavailableto ?
217+
parseInt(this.data.timeavailableto, 10) * 1000 : false;
218+
this.timeAvailableToReadable = this.timeAvailableTo ? this.timeUtils.userDate(this.timeAvailableTo) : false;
220219

220+
this.isEmpty = true;
221+
this.groupInfo = null;
222+
} else {
221223
canSearch = true;
222-
canAdd = accessData.canaddentry;
224+
canAdd = this.access.canaddentry;
225+
}
223226

224-
return this.groupsProvider.getActivityGroupInfo(this.data.coursemodule).then((groupInfo) => {
225-
this.groupInfo = groupInfo;
226-
this.selectedGroup = this.groupsProvider.validateGroupId(this.selectedGroup, groupInfo);
227-
});
228-
}).then(() => {
229-
return this.dataProvider.getFields(this.data.id, {cmId: this.module.id}).then((fields) => {
230-
if (fields.length == 0) {
231-
canSearch = false;
232-
canAdd = false;
233-
}
234-
this.search.advanced = [];
227+
const fields = await this.dataProvider.getFields(this.data.id, {cmId: this.module.id});
228+
this.search.advanced = [];
235229

236-
this.fields = this.utils.arrayToObject(fields, 'id');
237-
this.fieldsArray = this.utils.objectToArray(this.fields);
230+
this.fields = this.utils.arrayToObject(fields, 'id');
231+
this.fieldsArray = this.utils.objectToArray(this.fields);
232+
if (this.fieldsArray.length == 0) {
233+
canSearch = false;
234+
canAdd = false;
235+
}
238236

239-
return this.fetchEntriesData();
240-
});
241-
}).finally(() => {
237+
try {
238+
await this.fetchEntriesData();
239+
} finally {
242240
this.canAdd = canAdd;
243241
this.canSearch = canSearch;
244242
this.fillContextMenu(refresh);
245-
});
243+
}
246244
}
247245

248246
/**
@@ -252,24 +250,16 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp
252250
*/
253251
protected fetchEntriesData(): Promise<any> {
254252

255-
return this.dataProvider.getDatabaseAccessInformation(this.data.id, {
256-
groupId: this.selectedGroup,
257-
cmId: this.module.id,
258-
}).then((accessData) => {
259-
// Update values for current group.
260-
this.access.canaddentry = accessData.canaddentry;
253+
const search = this.search.searching && !this.search.searchingAdvanced ? this.search.text : undefined;
254+
const advSearch = this.search.searching && this.search.searchingAdvanced ? this.search.advanced : undefined;
261255

262-
const search = this.search.searching && !this.search.searchingAdvanced ? this.search.text : undefined;
263-
const advSearch = this.search.searching && this.search.searchingAdvanced ? this.search.advanced : undefined;
264-
265-
return this.dataHelper.fetchEntries(this.data, this.fieldsArray, {
266-
groupId: this.selectedGroup,
267-
search,
268-
advSearch,
269-
sort: Number(this.search.sortBy),
270-
order: this.search.sortDirection,
271-
page: this.search.page,
272-
});
256+
return this.dataHelper.fetchEntries(this.data, this.fieldsArray, {
257+
groupId: this.selectedGroup,
258+
search,
259+
advSearch,
260+
sort: Number(this.search.sortBy),
261+
order: this.search.sortDirection,
262+
page: this.search.page,
273263
}).then((entries) => {
274264
const numEntries = entries.entries.length;
275265
const numOfflineEntries = entries.offlineEntries.length;
@@ -390,18 +380,29 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp
390380
* @param groupId Group ID.
391381
* @return Resolved when new group is selected or rejected if not.
392382
*/
393-
setGroup(groupId: number): Promise<any> {
383+
async setGroup(groupId: number): Promise<void> {
394384
this.selectedGroup = groupId;
395385
this.search.page = 0;
396386

397-
return this.fetchEntriesData().then(() => {
387+
// Only update canAdd if there's any field, otheerwise, canAdd will remain false.
388+
if (this.fieldsArray.length > 0) {
389+
// Update values for current group.
390+
this.access = await this.dataProvider.getDatabaseAccessInformation(this.data.id, {
391+
groupId: this.selectedGroup,
392+
cmId: this.module.id,
393+
});
394+
395+
this.canAdd = this.access.canaddentry;
396+
}
397+
398+
try {
399+
await this.fetchEntriesData();
400+
398401
// Log activity view for coherence with Moodle web.
399402
return this.logView();
400-
}).catch((message) => {
401-
this.domUtils.showErrorModalDefault(message, 'core.course.errorgetmodule', true);
402-
403-
return Promise.reject(null);
404-
});
403+
} catch (error) {
404+
this.domUtils.showErrorModalDefault(error, 'core.course.errorgetmodule', true);
405+
}
405406
}
406407

407408
/**

0 commit comments

Comments
 (0)