diff --git a/src/addon/mod/forum/pages/discussion/discussion.ts b/src/addon/mod/forum/pages/discussion/discussion.ts index bc5b23206..c8d18d494 100644 --- a/src/addon/mod/forum/pages/discussion/discussion.ts +++ b/src/addon/mod/forum/pages/discussion/discussion.ts @@ -331,6 +331,8 @@ export class AddonModForumDiscussionPage implements OnDestroy { return this.forumProvider.getDiscussionPosts(this.discussionId, this.cmId).then((response) => { onlinePosts = response.posts; ratingInfo = response.ratinginfo; + this.courseId = response.courseid; + this.forumId = response.forumid; }).then(() => { // Check if there are responses stored in offline. return this.forumOffline.getDiscussionReplies(this.discussionId).then((replies) => { @@ -371,17 +373,13 @@ export class AddonModForumDiscussionPage implements OnDestroy { let posts = offlineReplies.concat(onlinePosts); const startingPost = this.forumProvider.extractStartingPost(posts); - if (startingPost) { - // Update discussion data from first post. - this.discussion = Object.assign(this.discussion || {}, startingPost); - } // If sort type is nested, normal sorting is disabled and nested posts will be displayed. if (this.sort == 'nested') { // Sort first by creation date to make format tree work. this.forumProvider.sortDiscussionPosts(posts, 'ASC'); - posts = this.utils.formatTree(posts, 'parent', 'id', this.discussion.id); + posts = this.utils.formatTree(posts, 'parent', 'id', this.discussion ? this.discussion.id : startingPost.id); } else { // Set default reply subject. const direction = this.sort == 'flat-newest' ? 'DESC' : 'ASC'; @@ -424,6 +422,10 @@ export class AddonModForumDiscussionPage implements OnDestroy { }).catch(() => { // Ignore errors. }).then(() => { + if (startingPost) { + // Update discussion data from first post. + this.discussion = Object.assign(this.discussion || {}, startingPost); + } if (!this.discussion) { // The discussion object was not passed as parameter and there is no starting post. Should not happen. diff --git a/src/addon/mod/forum/providers/forum.ts b/src/addon/mod/forum/providers/forum.ts index fa6cebdfc..ff91b9935 100644 --- a/src/addon/mod/forum/providers/forum.ts +++ b/src/addon/mod/forum/providers/forum.ts @@ -496,7 +496,8 @@ export class AddonModForumProvider { * @param siteId Site ID. If not defined, current site. * @return Promise resolved with forum posts and rating info. */ - getDiscussionPosts(discussionId: number, cmId: number, siteId?: string): Promise<{posts: any[], ratinginfo?: CoreRatingInfo}> { + getDiscussionPosts(discussionId: number, cmId: number, siteId?: string): Promise<{posts: any[], courseid?: number, + forumid?: number, ratinginfo?: CoreRatingInfo}> { const params = { discussionid: discussionId };