From 0f70d83682601468920bb012b67867d7a017a877 Mon Sep 17 00:00:00 2001 From: Albert Gasset Date: Thu, 28 Feb 2019 17:34:45 +0100 Subject: [PATCH] MOBILE-1633 forum: Post ratings --- .../mod/forum/components/components.module.ts | 4 +- .../index/addon-mod-forum-index.html | 2 +- src/addon/mod/forum/components/index/index.ts | 49 ++++++++++++------ .../components/post/addon-mod-forum-post.html | 2 + src/addon/mod/forum/components/post/post.ts | 2 + .../forum/pages/discussion/discussion.html | 8 +-- .../mod/forum/pages/discussion/discussion.ts | 49 ++++++++++++++++-- src/addon/mod/forum/providers/forum.ts | 7 +-- .../mod/forum/providers/prefetch-handler.ts | 21 +++++--- src/addon/mod/forum/providers/sync.ts | 50 ++++++++++++++++++- 10 files changed, 155 insertions(+), 39 deletions(-) diff --git a/src/addon/mod/forum/components/components.module.ts b/src/addon/mod/forum/components/components.module.ts index e9b656899..0f3bf1b10 100644 --- a/src/addon/mod/forum/components/components.module.ts +++ b/src/addon/mod/forum/components/components.module.ts @@ -20,6 +20,7 @@ import { CoreComponentsModule } from '@components/components.module'; import { CoreDirectivesModule } from '@directives/directives.module'; import { CorePipesModule } from '@pipes/pipes.module'; import { CoreCourseComponentsModule } from '@core/course/components/components.module'; +import { CoreRatingComponentsModule } from '@core/rating/components/components.module'; import { AddonModForumIndexComponent } from './index/index'; import { AddonModForumPostComponent } from './post/post'; @@ -35,7 +36,8 @@ import { AddonModForumPostComponent } from './post/post'; CoreComponentsModule, CoreDirectivesModule, CorePipesModule, - CoreCourseComponentsModule + CoreCourseComponentsModule, + CoreRatingComponentsModule ], providers: [ ], diff --git a/src/addon/mod/forum/components/index/addon-mod-forum-index.html b/src/addon/mod/forum/components/index/addon-mod-forum-index.html index e4d9b273d..9eab4d526 100644 --- a/src/addon/mod/forum/components/index/addon-mod-forum-index.html +++ b/src/addon/mod/forum/components/index/addon-mod-forum-index.html @@ -21,7 +21,7 @@ - + {{ 'core.hasdatatosync' | translate:{$a: moduleName} }} diff --git a/src/addon/mod/forum/components/index/index.ts b/src/addon/mod/forum/components/index/index.ts index 774c8ae4d..036697410 100644 --- a/src/addon/mod/forum/components/index/index.ts +++ b/src/addon/mod/forum/components/index/index.ts @@ -19,6 +19,9 @@ import { CoreCourseModuleMainActivityComponent } from '@core/course/classes/main import { CoreCourseModulePrefetchDelegate } from '@core/course/providers/module-prefetch-delegate'; import { CoreUserProvider } from '@core/user/providers/user'; import { CoreGroupsProvider } from '@providers/groups'; +import { CoreRatingProvider } from '@core/rating/providers/rating'; +import { CoreRatingOfflineProvider } from '@core/rating/providers/offline'; +import { CoreRatingSyncProvider } from '@core/rating/providers/sync'; import { AddonModForumProvider } from '../../providers/forum'; import { AddonModForumHelperProvider } from '../../providers/helper'; import { AddonModForumOfflineProvider } from '../../providers/offline'; @@ -56,6 +59,10 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom protected newDiscObserver: any; protected viewDiscObserver: any; + hasOfflineRatings: boolean; + protected ratingOfflineObserver: any; + protected ratingSyncObserver: any; + constructor(injector: Injector, @Optional() protected content: Content, protected navCtrl: NavController, @@ -66,7 +73,8 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom protected forumOffline: AddonModForumOfflineProvider, protected forumSync: AddonModForumSyncProvider, protected prefetchDelegate: CoreCourseModulePrefetchDelegate, - protected prefetchHandler: AddonModForumPrefetchHandler) { + protected prefetchHandler: AddonModForumPrefetchHandler, + protected ratingOffline: CoreRatingOfflineProvider) { super(injector); } @@ -100,6 +108,22 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom } }, this.sitesProvider.getCurrentSiteId()); + // Listen for offline ratings saved and synced. + this.ratingOfflineObserver = this.eventsProvider.on(CoreRatingProvider.RATING_SAVED_EVENT, (data) => { + if (this.forum && data.component == 'mod_forum' && data.ratingArea == 'post' && + data.contextLevel == 'module' && data.instanceId == this.forum.cmid) { + this.hasOfflineRatings = true; + } + }); + this.ratingSyncObserver = this.eventsProvider.on(CoreRatingSyncProvider.SYNCED_EVENT, (data) => { + if (this.forum && data.component == 'mod_forum' && data.ratingArea == 'post' && + data.contextLevel == 'module' && data.instanceId == this.forum.cmid) { + this.ratingOffline.hasRatings('mod_forum', 'post', 'module', this.forum.cmid).then((hasRatings) => { + this.hasOfflineRatings = hasRatings; + }); + } + }); + this.loadContent(false, true).then(() => { if (!this.forum) { return; @@ -178,6 +202,9 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom return Promise.all([ this.fetchOfflineDiscussion(), this.fetchDiscussions(refresh), + this.ratingOffline.hasRatings('mod_forum', 'post', 'module', this.forum.cmid).then((hasRatings) => { + this.hasOfflineRatings = hasRatings; + }) ]); }).catch((message) => { if (!refresh) { @@ -351,21 +378,9 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom protected sync(): Promise { const promises = []; - promises.push(this.forumSync.syncForumDiscussions(this.forum.id).then((result) => { - if (result.warnings && result.warnings.length) { - this.domUtils.showErrorModal(result.warnings[0]); - } - - return result; - })); - - promises.push(this.forumSync.syncForumReplies(this.forum.id).then((result) => { - if (result.warnings && result.warnings.length) { - this.domUtils.showErrorModal(result.warnings[0]); - } - - return result; - })); + promises.push(this.forumSync.syncForumDiscussions(this.forum.id)); + promises.push(this.forumSync.syncForumReplies(this.forum.id)); + promises.push(this.forumSync.syncRatings(this.forum.cmid)); return Promise.all(promises).then((results) => { return results.reduce((a, b) => ({ @@ -476,5 +491,7 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom this.newDiscObserver && this.newDiscObserver.off(); this.replyObserver && this.replyObserver.off(); this.viewDiscObserver && this.viewDiscObserver.off(); + this.ratingOfflineObserver && this.ratingOfflineObserver.off(); + this.ratingSyncObserver && this.ratingSyncObserver.off(); } } diff --git a/src/addon/mod/forum/components/post/addon-mod-forum-post.html b/src/addon/mod/forum/components/post/addon-mod-forum-post.html index cad199ead..253e7208a 100644 --- a/src/addon/mod/forum/components/post/addon-mod-forum-post.html +++ b/src/addon/mod/forum/components/post/addon-mod-forum-post.html @@ -23,6 +23,8 @@ + +