MOBILE-3068 database: Fix comments not updated on PTR

main
Dani Palou 2019-08-28 10:38:44 +02:00
parent 160b544d8e
commit 5533c39288
5 changed files with 73 additions and 12 deletions

View File

@ -85,7 +85,6 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp
private prefetchHandler: AddonModDataPrefetchHandler, private prefetchHandler: AddonModDataPrefetchHandler,
private timeUtils: CoreTimeUtilsProvider, private timeUtils: CoreTimeUtilsProvider,
private groupsProvider: CoreGroupsProvider, private groupsProvider: CoreGroupsProvider,
private commentsProvider: CoreCommentsProvider,
private modalCtrl: ModalController, private modalCtrl: ModalController,
private utils: CoreUtilsProvider, private utils: CoreUtilsProvider,
protected navCtrl: NavController) { protected navCtrl: NavController) {
@ -152,8 +151,12 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp
promises.push(this.dataProvider.invalidateDatabaseAccessInformationData(this.data.id)); promises.push(this.dataProvider.invalidateDatabaseAccessInformationData(this.data.id));
promises.push(this.groupsProvider.invalidateActivityGroupInfo(this.data.coursemodule)); promises.push(this.groupsProvider.invalidateActivityGroupInfo(this.data.coursemodule));
promises.push(this.dataProvider.invalidateEntriesData(this.data.id)); promises.push(this.dataProvider.invalidateEntriesData(this.data.id));
if (this.hasComments) { if (this.hasComments) {
promises.push(this.commentsProvider.invalidateCommentsByInstance('module', this.data.coursemodule)); this.eventsProvider.trigger(CoreCommentsProvider.REFRESH_COMMENTS_EVENT, {
contextLevel: 'module',
instanceId: this.data.coursemodule
}, this.sitesProvider.getCurrentSiteId());
} }
} }
@ -192,6 +195,7 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp
return this.dataProvider.getDatabase(this.courseId, this.module.id).then((data) => { return this.dataProvider.getDatabase(this.courseId, this.module.id).then((data) => {
this.data = data; this.data = data;
this.hasComments = data.comments;
this.description = data.intro || data.description; this.description = data.intro || data.description;
this.dataRetrieved.emit(data); this.dataRetrieved.emit(data);
@ -258,7 +262,6 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp
* @return {Promise<any>} Resolved then done. * @return {Promise<any>} Resolved then done.
*/ */
protected fetchEntriesData(): Promise<any> { protected fetchEntriesData(): Promise<any> {
this.hasComments = false;
return this.dataProvider.getDatabaseAccessInformation(this.data.id, this.selectedGroup).then((accessData) => { return this.dataProvider.getDatabaseAccessInformation(this.data.id, this.selectedGroup).then((accessData) => {
// Update values for current group. // Update values for current group.

View File

@ -27,6 +27,7 @@ import { AddonModDataSyncProvider } from '../../providers/sync';
import { AddonModDataFieldsDelegate } from '../../providers/fields-delegate'; import { AddonModDataFieldsDelegate } from '../../providers/fields-delegate';
import { AddonModDataComponentsModule } from '../../components/components.module'; import { AddonModDataComponentsModule } from '../../components/components.module';
import { CoreCommentsProvider } from '@core/comments/providers/comments'; import { CoreCommentsProvider } from '@core/comments/providers/comments';
import { CoreCommentsCommentsComponent } from '@core/comments/components/comments/comments';
/** /**
* Page that displays the view entry page. * Page that displays the view entry page.
@ -38,6 +39,7 @@ import { CoreCommentsProvider } from '@core/comments/providers/comments';
}) })
export class AddonModDataEntryPage implements OnDestroy { export class AddonModDataEntryPage implements OnDestroy {
@ViewChild(Content) content: Content; @ViewChild(Content) content: Content;
@ViewChild(CoreCommentsCommentsComponent) comments: CoreCommentsCommentsComponent;
protected module: any; protected module: any;
protected entryId: number; protected entryId: number;
@ -211,13 +213,16 @@ export class AddonModDataEntryPage implements OnDestroy {
promises.push(this.dataProvider.invalidateDatabaseData(this.courseId)); promises.push(this.dataProvider.invalidateDatabaseData(this.courseId));
if (this.data) { if (this.data) {
if (this.data.comments && this.entry && this.entry.id > 0 && this.commentsEnabled) {
promises.push(this.commentsProvider.invalidateCommentsData('module', this.data.coursemodule, 'mod_data',
this.entry.id, 'database_entry'));
}
promises.push(this.dataProvider.invalidateEntryData(this.data.id, this.entryId)); promises.push(this.dataProvider.invalidateEntryData(this.data.id, this.entryId));
promises.push(this.groupsProvider.invalidateActivityGroupInfo(this.data.coursemodule)); promises.push(this.groupsProvider.invalidateActivityGroupInfo(this.data.coursemodule));
promises.push(this.dataProvider.invalidateEntriesData(this.data.id)); promises.push(this.dataProvider.invalidateEntriesData(this.data.id));
if (this.data.comments && this.entry && this.entry.id > 0 && this.commentsEnabled && this.comments) {
// Refresh comments. Don't add it to promises because we don't want the comments fetch to block the entry fetch.
this.comments.doRefresh().catch(() => {
// Ignore errors.
});
}
} }
return Promise.all(promises).finally(() => { return Promise.all(promises).finally(() => {

View File

@ -158,11 +158,12 @@ export class AddonModDataHelperProvider {
* @param {any} entry Entry. * @param {any} entry Entry.
* @param {number} offset Entry offset. * @param {number} offset Entry offset.
* @param {string} mode Mode list or show. * @param {string} mode Mode list or show.
* @param {AddonModDataOfflineAction[]} actions Actions that can be performed to the record. * @param {{[name: string]: boolean}} actions Actions that can be performed to the record.
* @return {string} Generated HTML. * @return {string} Generated HTML.
*/ */
displayShowFields(template: string, fields: any[], entry: any, offset: number, mode: string, displayShowFields(template: string, fields: any[], entry: any, offset: number, mode: string,
actions: AddonModDataOfflineAction[]): string { actions: {[name: string]: boolean}): string {
if (!template) { if (!template) {
return ''; return '';
} }
@ -357,9 +358,9 @@ export class AddonModDataHelperProvider {
* @param {any} database Database activity. * @param {any} database Database activity.
* @param {any} accessInfo Access info to the activity. * @param {any} accessInfo Access info to the activity.
* @param {any} record Entry or record where the actions will be performed. * @param {any} record Entry or record where the actions will be performed.
* @return {any} Keyed with the action names and boolean to evalute if it can or cannot be done. * @return {{[name: string]: boolean}} Keyed with the action names and boolean to evalute if it can or cannot be done.
*/ */
getActions(database: any, accessInfo: any, record: any): any { getActions(database: any, accessInfo: any, record: any): {[name: string]: boolean} {
return { return {
more: true, more: true,
moreurl: true, moreurl: true,

View File

@ -41,6 +41,7 @@ export class CoreCommentsCommentsComponent implements OnChanges, OnDestroy {
disabled = false; disabled = false;
protected updateSiteObserver; protected updateSiteObserver;
protected refreshCommentsObserver;
constructor(private navCtrl: NavController, private commentsProvider: CoreCommentsProvider, constructor(private navCtrl: NavController, private commentsProvider: CoreCommentsProvider,
sitesProvider: CoreSitesProvider, eventsProvider: CoreEventsProvider) { sitesProvider: CoreSitesProvider, eventsProvider: CoreEventsProvider) {
@ -58,6 +59,19 @@ export class CoreCommentsCommentsComponent implements OnChanges, OnDestroy {
this.fetchData(); this.fetchData();
} }
}, sitesProvider.getCurrentSiteId()); }, sitesProvider.getCurrentSiteId());
// Refresh comments if event received.
this.refreshCommentsObserver = eventsProvider.on(CoreCommentsProvider.REFRESH_COMMENTS_EVENT, (data) => {
// Verify these comments need to be updated.
if (this.undefinedOrEqual(data, 'contextLevel') && this.undefinedOrEqual(data, 'instanceId') &&
this.undefinedOrEqual(data, 'component') && this.undefinedOrEqual(data, 'itemId') &&
this.undefinedOrEqual(data, 'area')) {
this.doRefresh().catch(() => {
// Ignore errors.
});
}
}, sitesProvider.getCurrentSiteId());
} }
/** /**
@ -77,7 +91,10 @@ export class CoreCommentsCommentsComponent implements OnChanges, OnDestroy {
} }
} }
protected fetchData(): void { /**
* Fetch comments data.
*/
fetchData(): void {
if (this.disabled) { if (this.disabled) {
return; return;
} }
@ -94,6 +111,27 @@ export class CoreCommentsCommentsComponent implements OnChanges, OnDestroy {
}); });
} }
/**
* Refresh comments.
*
* @return {Promise<any>} Promise resolved when done.
*/
doRefresh(): Promise<any> {
return this.invalidateComments().then(() => {
return this.fetchData();
});
}
/**
* Invalidate comments data.
*
* @return {Promise<any>} Promise resolved when done.
*/
invalidateComments(): Promise<any> {
return this.commentsProvider.invalidateCommentsData(this.contextLevel, this.instanceId, this.component, this.itemId,
this.area);
}
/** /**
* Opens the comments page. * Opens the comments page.
*/ */
@ -116,5 +154,17 @@ export class CoreCommentsCommentsComponent implements OnChanges, OnDestroy {
*/ */
ngOnDestroy(): void { ngOnDestroy(): void {
this.updateSiteObserver && this.updateSiteObserver.off(); this.updateSiteObserver && this.updateSiteObserver.off();
this.refreshCommentsObserver && this.refreshCommentsObserver.off();
}
/**
* Check if a certain value in data is undefined or equal to this instance value.
*
* @param {any} data Data object.
* @param {string} name Name of the property to check.
* @return {boolean} Whether it's undefined or equal.
*/
protected undefinedOrEqual(data: any, name: string): boolean {
return typeof data[name] == 'undefined' || data[name] == this[name];
} }
} }

View File

@ -25,6 +25,8 @@ import { CoreCommentsOfflineProvider } from './offline';
@Injectable() @Injectable()
export class CoreCommentsProvider { export class CoreCommentsProvider {
static REFRESH_COMMENTS_EVENT = 'core_comments_refresh_comments';
protected ROOT_CACHE_KEY = 'mmComments:'; protected ROOT_CACHE_KEY = 'mmComments:';
static pageSize = null; static pageSize = null;
static pageSizeOK = false; // If true, the pageSize is definitive. If not, it's a temporal value to reduce WS calls. static pageSizeOK = false; // If true, the pageSize is definitive. If not, it's a temporal value to reduce WS calls.