MOBILE-3200 database: Fix update access data on group change

main
Pau Ferrer Ocaña 2020-09-29 14:21:47 +02:00
parent 8d95636690
commit 1652e4d8f2
1 changed files with 69 additions and 68 deletions

View File

@ -180,69 +180,67 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp
* @param showErrors If show errors to the user of hide them. * @param showErrors If show errors to the user of hide them.
* @return Promise resolved when done. * @return Promise resolved when done.
*/ */
protected fetchContent(refresh: boolean = false, sync: boolean = false, showErrors: boolean = false): Promise<any> { protected async fetchContent(refresh: boolean = false, sync: boolean = false, showErrors: boolean = false): Promise<any> {
let canAdd = false, let canAdd = false,
canSearch = false; canSearch = false;
return this.dataProvider.getDatabase(this.courseId, this.module.id).then((data) => { this.data = await this.dataProvider.getDatabase(this.courseId, this.module.id);
this.data = data; this.hasComments = this.data.comments;
this.hasComments = data.comments;
this.description = data.intro || data.description; this.description = this.data.intro || this.data.description;
this.dataRetrieved.emit(data); this.dataRetrieved.emit(this.data);
if (sync) { if (sync) {
try {
// Try to synchronize the data. // Try to synchronize the data.
return this.syncActivity(showErrors).catch(() => { await this.syncActivity(showErrors);
// Ignore errors. } catch (error) {
}); // Ignore errors.
} }
}).then(() => { }
return this.dataProvider.getDatabaseAccessInformation(this.data.id, {cmId: this.module.id});
}).then((accessData) => {
this.access = accessData;
if (!accessData.timeavailable) { this.groupInfo = await this.groupsProvider.getActivityGroupInfo(this.data.coursemodule);
const time = this.timeUtils.timestamp(); this.selectedGroup = this.groupsProvider.validateGroupId(this.selectedGroup, this.groupInfo);
this.timeAvailableFrom = this.data.timeavailablefrom && time < this.data.timeavailablefrom ? this.access = await this.dataProvider.getDatabaseAccessInformation(this.data.id, {
parseInt(this.data.timeavailablefrom, 10) * 1000 : false; cmId: this.module.id,
this.timeAvailableFromReadable = this.timeAvailableFrom ? this.timeUtils.userDate(this.timeAvailableFrom) : false; groupId: this.selectedGroup || undefined
this.timeAvailableTo = this.data.timeavailableto && time > this.data.timeavailableto ? });
parseInt(this.data.timeavailableto, 10) * 1000 : false;
this.timeAvailableToReadable = this.timeAvailableTo ? this.timeUtils.userDate(this.timeAvailableTo) : false;
this.isEmpty = true; if (!this.access.timeavailable) {
this.groupInfo = null; const time = this.timeUtils.timestamp();
return; this.timeAvailableFrom = this.data.timeavailablefrom && time < this.data.timeavailablefrom ?
} parseInt(this.data.timeavailablefrom, 10) * 1000 : false;
this.timeAvailableFromReadable = this.timeAvailableFrom ? this.timeUtils.userDate(this.timeAvailableFrom) : false;
this.timeAvailableTo = this.data.timeavailableto && time > this.data.timeavailableto ?
parseInt(this.data.timeavailableto, 10) * 1000 : false;
this.timeAvailableToReadable = this.timeAvailableTo ? this.timeUtils.userDate(this.timeAvailableTo) : false;
this.isEmpty = true;
this.groupInfo = null;
} else {
canSearch = true; canSearch = true;
canAdd = accessData.canaddentry; canAdd = this.access.canaddentry;
}
return this.groupsProvider.getActivityGroupInfo(this.data.coursemodule).then((groupInfo) => { const fields = await this.dataProvider.getFields(this.data.id, {cmId: this.module.id});
this.groupInfo = groupInfo; this.search.advanced = [];
this.selectedGroup = this.groupsProvider.validateGroupId(this.selectedGroup, groupInfo);
});
}).then(() => {
return this.dataProvider.getFields(this.data.id, {cmId: this.module.id}).then((fields) => {
if (fields.length == 0) {
canSearch = false;
canAdd = false;
}
this.search.advanced = [];
this.fields = this.utils.arrayToObject(fields, 'id'); this.fields = this.utils.arrayToObject(fields, 'id');
this.fieldsArray = this.utils.objectToArray(this.fields); this.fieldsArray = this.utils.objectToArray(this.fields);
if (this.fieldsArray.length == 0) {
canSearch = false;
canAdd = false;
}
return this.fetchEntriesData(); try {
}); await this.fetchEntriesData();
}).finally(() => { } finally {
this.canAdd = canAdd; this.canAdd = canAdd;
this.canSearch = canSearch; this.canSearch = canSearch;
this.fillContextMenu(refresh); this.fillContextMenu(refresh);
}); }
} }
/** /**
@ -252,24 +250,16 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp
*/ */
protected fetchEntriesData(): Promise<any> { protected fetchEntriesData(): Promise<any> {
return this.dataProvider.getDatabaseAccessInformation(this.data.id, { const search = this.search.searching && !this.search.searchingAdvanced ? this.search.text : undefined;
const advSearch = this.search.searching && this.search.searchingAdvanced ? this.search.advanced : undefined;
return this.dataHelper.fetchEntries(this.data, this.fieldsArray, {
groupId: this.selectedGroup, groupId: this.selectedGroup,
cmId: this.module.id, search,
}).then((accessData) => { advSearch,
// Update values for current group. sort: Number(this.search.sortBy),
this.access.canaddentry = accessData.canaddentry; order: this.search.sortDirection,
page: this.search.page,
const search = this.search.searching && !this.search.searchingAdvanced ? this.search.text : undefined;
const advSearch = this.search.searching && this.search.searchingAdvanced ? this.search.advanced : undefined;
return this.dataHelper.fetchEntries(this.data, this.fieldsArray, {
groupId: this.selectedGroup,
search,
advSearch,
sort: Number(this.search.sortBy),
order: this.search.sortDirection,
page: this.search.page,
});
}).then((entries) => { }).then((entries) => {
const numEntries = entries.entries.length; const numEntries = entries.entries.length;
const numOfflineEntries = entries.offlineEntries.length; const numOfflineEntries = entries.offlineEntries.length;
@ -390,18 +380,29 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp
* @param groupId Group ID. * @param groupId Group ID.
* @return Resolved when new group is selected or rejected if not. * @return Resolved when new group is selected or rejected if not.
*/ */
setGroup(groupId: number): Promise<any> { async setGroup(groupId: number): Promise<void> {
this.selectedGroup = groupId; this.selectedGroup = groupId;
this.search.page = 0; this.search.page = 0;
return this.fetchEntriesData().then(() => { // Only update canAdd if there's any field, otheerwise, canAdd will remain false.
if (this.fieldsArray.length > 0) {
// Update values for current group.
this.access = await this.dataProvider.getDatabaseAccessInformation(this.data.id, {
groupId: this.selectedGroup,
cmId: this.module.id,
});
this.canAdd = this.access.canaddentry;
}
try {
await this.fetchEntriesData();
// Log activity view for coherence with Moodle web. // Log activity view for coherence with Moodle web.
return this.logView(); return this.logView();
}).catch((message) => { } catch (error) {
this.domUtils.showErrorModalDefault(message, 'core.course.errorgetmodule', true); this.domUtils.showErrorModalDefault(error, 'core.course.errorgetmodule', true);
}
return Promise.reject(null);
});
} }
/** /**