MOBILE-1633 forum: Post ratings

main
Albert Gasset 2019-02-28 17:34:45 +01:00
parent beaab9d2f6
commit 0f70d83682
10 changed files with 155 additions and 39 deletions

View File

@ -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: [
],

View File

@ -21,7 +21,7 @@
<core-course-module-description [description]="description" [component]="component" [componentId]="componentId" [note]="descriptionNote"></core-course-module-description>
<!-- Forum discussions found to be synchronized -->
<ion-card class="core-warning-card" icon-start *ngIf="hasOffline">
<ion-card class="core-warning-card" icon-start *ngIf="hasOffline || hasOfflineRatings">
<ion-icon name="warning"></ion-icon> {{ 'core.hasdatatosync' | translate:{$a: moduleName} }}
</ion-card>

View File

@ -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<boolean> {
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();
}
}

View File

@ -23,6 +23,8 @@
</ng-container>
</div>
</ion-card-content>
<core-rating-rate *ngIf="forum && ratingInfo" [ratingInfo]="ratingInfo" contextLevel="module" [instanceId]="componentId" [itemId]="post.id" [itemSetId]="discussionId" [courseId]="courseId" [aggregateMethod]="forum.assessed" [scaleId]="forum.scale" [userId]="post.userid"></core-rating-rate>
<core-rating-aggregate *ngIf="forum && ratingInfo" [ratingInfo]="ratingInfo" contextLevel="module" [instanceId]="componentId" [itemId]="post.id" [courseId]="courseId" [aggregateMethod]="forum.assessed" [scaleId]="forum.scale"></core-rating-aggregate>
<ion-item no-padding text-end *ngIf="post.id && post.canreply" class="addon-forum-reply-button">
<button ion-button icon-left clear small (click)="showReply()" [attr.aria-controls]="'addon-forum-reply-edit-form-' + uniqueId" [attr.aria-expanded]="replyData.replyingTo === post.id">
<ion-icon name="undo"></ion-icon> {{ 'addon.mod_forum.reply' | translate }}

View File

@ -25,6 +25,7 @@ import { AddonModForumProvider } from '../../providers/forum';
import { AddonModForumHelperProvider } from '../../providers/helper';
import { AddonModForumOfflineProvider } from '../../providers/offline';
import { AddonModForumSyncProvider } from '../../providers/sync';
import { CoreRatingInfo } from '@core/rating/providers/rating';
/**
* Components that shows a discussion post, its attachments and the action buttons allowed (reply, etc.).
@ -44,6 +45,7 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy {
@Input() trackPosts: boolean; // True if post is being tracked.
@Input() forum: any; // The forum the post belongs to. Required for attachments and offline posts.
@Input() defaultSubject: string; // Default subject to set to new posts.
@Input() ratingInfo?: CoreRatingInfo; // Rating info item.
@Output() onPostChange: EventEmitter<void>; // Event emitted when a reply is posted or modified.
messageControl = new FormControl();

View File

@ -22,7 +22,7 @@
<core-loading [hideUntil]="discussionLoaded">
<!-- Discussion replies found to be synchronized -->
<ion-card class="core-warning-card" *ngIf="postHasOffline">
<ion-card class="core-warning-card" *ngIf="postHasOffline || hasOfflineRatings">
<ion-icon name="warning"></ion-icon> {{ 'core.hasdatatosync' | translate:{$a: discussionStr} }}
</ion-card>
@ -31,13 +31,13 @@
</ion-card>
<ion-card *ngIf="discussion" margin-bottom class="highlight">
<addon-mod-forum-post [post]="discussion" [courseId]="courseId" [discussionId]="discussionId" [component]="component" [componentId]="cmId" [replyData]="replyData" [originalData]="originalData" [defaultSubject]="defaultSubject" [forum]="forum" [trackPosts]="trackPosts" (onPostChange)="postListChanged()"></addon-mod-forum-post>
<addon-mod-forum-post [post]="discussion" [courseId]="courseId" [discussionId]="discussionId" [component]="component" [componentId]="cmId" [replyData]="replyData" [originalData]="originalData" [defaultSubject]="defaultSubject" [forum]="forum" [trackPosts]="trackPosts" [ratingInfo]="ratingInfo" (onPostChange)="postListChanged()"></addon-mod-forum-post>
</ion-card>
<ion-card *ngIf="sort != 'nested'">
<ng-container *ngFor="let post of posts; first as first">
<ion-item-divider *ngIf="!first"></ion-item-divider>
<addon-mod-forum-post [post]="post" [courseId]="courseId" [discussionId]="discussionId" [component]="component" [componentId]="cmId" [replyData]="replyData" [originalData]="originalData" [defaultSubject]="defaultSubject" [forum]="forum" [trackPosts]="trackPosts" (onPostChange)="postListChanged()"></addon-mod-forum-post>
<addon-mod-forum-post [post]="post" [courseId]="courseId" [discussionId]="discussionId" [component]="component" [componentId]="cmId" [replyData]="replyData" [originalData]="originalData" [defaultSubject]="defaultSubject" [forum]="forum" [trackPosts]="trackPosts" [ratingInfo]="ratingInfo" (onPostChange)="postListChanged()"></addon-mod-forum-post>
</ng-container>
</ion-card>
@ -49,7 +49,7 @@
<ng-template #nestedPosts let-post="post">
<ion-card>
<addon-mod-forum-post [post]="post" [courseId]="courseId" [discussionId]="discussionId" [component]="component" [componentId]="cmId" [replyData]="replyData" [originalData]="originalData" [defaultSubject]="defaultSubject" [forum]="forum" [trackPosts]="trackPosts" (onPostChange)="postListChanged()"></addon-mod-forum-post>
<addon-mod-forum-post [post]="post" [courseId]="courseId" [discussionId]="discussionId" [component]="component" [componentId]="cmId" [replyData]="replyData" [originalData]="originalData" [defaultSubject]="defaultSubject" [forum]="forum" [trackPosts]="trackPosts" [ratingInfo]="ratingInfo" (onPostChange)="postListChanged()"></addon-mod-forum-post>
</ion-card>
<div padding-left *ngIf="post.children.length && post.children[0].subject">
<ng-container *ngFor="let child of post.children">

View File

@ -23,6 +23,9 @@ import { CoreDomUtilsProvider } from '@providers/utils/dom';
import { CoreUtilsProvider } from '@providers/utils/utils';
import { CoreFileUploaderProvider } from '@core/fileuploader/providers/fileuploader';
import { CoreSplitViewComponent } from '@components/split-view/split-view';
import { CoreRatingProvider, CoreRatingInfo } 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 { AddonModForumOfflineProvider } from '../../providers/offline';
import { AddonModForumHelperProvider } from '../../providers/helper';
@ -77,6 +80,11 @@ export class AddonModForumDiscussionPage implements OnDestroy {
protected syncObserver: any;
protected syncManualObserver: any;
ratingInfo?: CoreRatingInfo;
hasOfflineRatings: boolean;
protected ratingOfflineObserver: any;
protected ratingSyncObserver: any;
constructor(navParams: NavParams,
network: Network,
zone: NgZone,
@ -91,6 +99,7 @@ export class AddonModForumDiscussionPage implements OnDestroy {
private forumOffline: AddonModForumOfflineProvider,
private forumHelper: AddonModForumHelperProvider,
private forumSync: AddonModForumSyncProvider,
private ratingOffline: CoreRatingOfflineProvider,
@Optional() private svComponent: CoreSplitViewComponent) {
this.courseId = navParams.get('courseId');
this.cmId = navParams.get('cmId');
@ -147,6 +156,20 @@ export class AddonModForumDiscussionPage implements OnDestroy {
forumId: this.forumId,
discussion: this.discussionId
}, this.sitesProvider.getCurrentSiteId());
// Listen for offline ratings saved and synced.
this.ratingOfflineObserver = this.eventsProvider.on(CoreRatingProvider.RATING_SAVED_EVENT, (data) => {
if (data.component == 'mod_forum' && data.ratingArea == 'post' && data.contextLevel == 'module' &&
data.instanceId == this.cmId && data.itemSetId == this.discussionId) {
this.hasOfflineRatings = true;
}
});
this.ratingSyncObserver = this.eventsProvider.on(CoreRatingSyncProvider.SYNCED_EVENT, (data) => {
if (data.component == 'mod_forum' && data.ratingArea == 'post' && data.contextLevel == 'module' &&
data.instanceId == this.cmId && data.itemSetId == this.discussionId) {
this.hasOfflineRatings = false;
}
});
}
/**
@ -210,9 +233,9 @@ export class AddonModForumDiscussionPage implements OnDestroy {
let hasUnreadPosts = false;
return syncPromise.then(() => {
return this.forumProvider.getDiscussionPosts(this.discussionId).then((posts) => {
onlinePosts = posts;
return this.forumProvider.getDiscussionPosts(this.discussionId).then((response) => {
onlinePosts = response.posts;
this.ratingInfo = response.ratinginfo;
}).then(() => {
// Check if there are responses stored in offline.
return this.forumOffline.getDiscussionReplies(this.discussionId).then((replies) => {
@ -290,6 +313,10 @@ export class AddonModForumDiscussionPage implements OnDestroy {
// Ignore errors.
this.forum = {};
});
}).then(() => {
return this.ratingOffline.hasRatings('mod_forum', 'post', 'module', this.cmId, this.discussionId).then((hasRatings) => {
this.hasOfflineRatings = hasRatings;
});
}).catch((message) => {
this.domUtils.showErrorModal(message);
}).finally(() => {
@ -319,7 +346,9 @@ export class AddonModForumDiscussionPage implements OnDestroy {
* @return {Promise<any>} Promise resolved when done.
*/
protected syncDiscussion(showErrors: boolean): Promise<any> {
return this.forumSync.syncDiscussionReplies(this.discussionId).then((result) => {
const promises = [];
promises.push(this.forumSync.syncDiscussionReplies(this.forumId, this.discussionId).then((result) => {
if (result.warnings && result.warnings.length) {
this.domUtils.showErrorModal(result.warnings[0]);
}
@ -334,7 +363,15 @@ export class AddonModForumDiscussionPage implements OnDestroy {
}
return result.updated;
}).catch((error) => {
}));
promises.push(this.forumSync.syncRatings(this.cmId, this.discussionId).then((result) => {
if (result.warnings && result.warnings.length) {
this.domUtils.showErrorModal(result.warnings[0]);
}
}));
return Promise.all(promises).catch((error) => {
if (showErrors) {
this.domUtils.showErrorModalDefault(error, 'core.errorsync', true);
}
@ -419,6 +456,8 @@ export class AddonModForumDiscussionPage implements OnDestroy {
ionViewWillLeave(): void {
this.syncObserver && this.syncObserver.off();
this.syncManualObserver && this.syncManualObserver.off();
this.ratingOfflineObserver && this.ratingOfflineObserver.off();
this.ratingSyncObserver && this.ratingSyncObserver.off();
}
/**

View File

@ -22,6 +22,7 @@ import { CoreUserProvider } from '@core/user/providers/user';
import { CoreUtilsProvider } from '@providers/utils/utils';
import { CoreCourseLogHelperProvider } from '@core/course/providers/log-helper';
import { AddonModForumOfflineProvider } from './offline';
import { CoreRatingInfo } from '@core/rating/providers/rating';
/**
* Service that provides some features for forums.
@ -369,9 +370,9 @@ export class AddonModForumProvider {
*
* @param {number} discussionId Discussion ID.
* @param {string} [siteId] Site ID. If not defined, current site.
* @return {Promise<any[]>} Promise resolved with forum posts.
* @return {Promise<{posts: any[], ratinginfo?: CoreRatingInfo}>} Promise resolved with forum posts and rating info.
*/
getDiscussionPosts(discussionId: number, siteId?: string): Promise<any> {
getDiscussionPosts(discussionId: number, siteId?: string): Promise<{posts: any[], ratinginfo?: CoreRatingInfo}> {
const params = {
discussionid: discussionId
};
@ -384,7 +385,7 @@ export class AddonModForumProvider {
if (response) {
this.storeUserData(response.posts);
return response.posts;
return response;
} else {
return Promise.reject(null);
}

View File

@ -24,6 +24,7 @@ import { CoreCourseActivityPrefetchHandlerBase } from '@core/course/classes/acti
import { CoreGroupsProvider } from '@providers/groups';
import { CoreUserProvider } from '@core/user/providers/user';
import { AddonModForumProvider } from './forum';
import { CoreRatingProvider } from '@core/rating/providers/rating';
/**
* Handler to prefetch forums.
@ -44,7 +45,8 @@ export class AddonModForumPrefetchHandler extends CoreCourseActivityPrefetchHand
domUtils: CoreDomUtilsProvider,
private groupsProvider: CoreGroupsProvider,
private userProvider: CoreUserProvider,
private forumProvider: AddonModForumProvider) {
private forumProvider: AddonModForumProvider,
private ratingProvider: CoreRatingProvider) {
super(translate, appProvider, utils, courseProvider, filepoolProvider, sitesProvider, domUtils);
}
@ -62,7 +64,7 @@ export class AddonModForumPrefetchHandler extends CoreCourseActivityPrefetchHand
const files = this.getIntroFilesFromInstance(module, forum);
// Get posts.
return this.getPostsForPrefetch(forum.id).then((posts) => {
return this.getPostsForPrefetch(forum).then((posts) => {
// Add posts attachments and embedded files.
return files.concat(this.getPostsFiles(posts));
});
@ -99,12 +101,12 @@ export class AddonModForumPrefetchHandler extends CoreCourseActivityPrefetchHand
/**
* Get the posts to be prefetched.
*
* @param {number} forumId Forum ID
* @param {any} forum Forum instance.
* @return {Promise<any[]>} Promise resolved with array of posts.
*/
protected getPostsForPrefetch(forumId: number): Promise<any[]> {
protected getPostsForPrefetch(forum: any): Promise<any[]> {
// Get discussions in first 2 pages.
return this.forumProvider.getDiscussionsInPages(forumId, false, 2).then((response) => {
return this.forumProvider.getDiscussionsInPages(forum.id, false, 2).then((response) => {
if (response.error) {
return Promise.reject(null);
}
@ -113,8 +115,11 @@ export class AddonModForumPrefetchHandler extends CoreCourseActivityPrefetchHand
let posts = [];
response.discussions.forEach((discussion) => {
promises.push(this.forumProvider.getDiscussionPosts(discussion.discussion).then((ps) => {
posts = posts.concat(ps);
promises.push(this.forumProvider.getDiscussionPosts(discussion.discussion).then((response) => {
posts = posts.concat(response.posts);
return this.ratingProvider.prefetchRatings('module', forum.cmid, forum.scale, forum.course,
response.ratinginfo);
}));
});
@ -179,7 +184,7 @@ export class AddonModForumPrefetchHandler extends CoreCourseActivityPrefetchHand
// Get the forum data.
return this.forumProvider.getForum(courseId, module.id).then((forum) => {
// Prefetch the posts.
return this.getPostsForPrefetch(forum.id).then((posts) => {
return this.getPostsForPrefetch(forum).then((posts) => {
const promises = [];
// Prefetch user profiles.

View File

@ -29,6 +29,7 @@ import { CoreUtilsProvider } from '@providers/utils/utils';
import { AddonModForumProvider } from './forum';
import { AddonModForumHelperProvider } from './helper';
import { AddonModForumOfflineProvider } from './offline';
import { CoreRatingSyncProvider } from '@core/rating/providers/sync';
/**
* Service to sync forums.
@ -55,7 +56,8 @@ export class AddonModForumSyncProvider extends CoreSyncBaseProvider {
private forumProvider: AddonModForumProvider,
private forumHelper: AddonModForumHelperProvider,
private forumOffline: AddonModForumOfflineProvider,
private logHelper: CoreCourseLogHelperProvider) {
private logHelper: CoreCourseLogHelperProvider,
private ratingSync: CoreRatingSyncProvider) {
super('AddonModForumSyncProvider', loggerProvider, sitesProvider, appProvider, syncProvider, textUtils, translate,
timeUtils);
@ -135,6 +137,8 @@ export class AddonModForumSyncProvider extends CoreSyncBaseProvider {
return Promise.all(this.utils.objectToArray(promises));
}));
sitePromises.push(this.syncRatings(undefined, undefined, siteId));
return Promise.all(sitePromises);
}
@ -273,6 +277,50 @@ export class AddonModForumSyncProvider extends CoreSyncBaseProvider {
return this.addOngoingSync(syncId, syncPromise, siteId);
}
/**
* Synchronize forum offline ratings.
*
* @param {number} [cmId] Course module to be synced. If not defined, sync all forums.
* @param {number} [discussionId] Discussion id to be synced. If not defined, sync all discussions.
* @param {string} [siteId] Site ID. If not defined, current site.
* @return {Promise<any>} Promise resolved if sync is successful, rejected otherwise.
*/
syncRatings(cmId?: number, discussionId?: number, siteId?: string): Promise<any> {
siteId = siteId || this.sitesProvider.getCurrentSiteId();
return this.ratingSync.syncRatings('mod_forum', 'post', 'module', cmId, discussionId, siteId).then((results) => {
let updated = false;
const warnings = [];
const promises = [];
results.forEach((result) => {
if (result.updated) {
updated = true;
// Invalidate discussions of updated ratings.
promises.push(this.forumProvider.invalidateDiscussionPosts(result.itemSet.itemSetId, siteId));
}
if (result.warnings.length) {
// Fetch forum to construct the warning message.
promises.push(this.forumProvider.getForum(result.itemSet.courseId, result.itemSet.instanceId, siteId)
.then((forum) => {
result.warnings.forEach((warning) => {
warnings.push(this.translate.instant('core.warningofflinedatadeleted', {
component: this.componentTranslate,
name: forum.name,
error: warning
}));
});
}));
}
});
return this.utils.allPromises(promises).then(() => {
return { updated, warnings };
});
});
}
/**
* Synchronize all offline discussion replies of a forum.
*