From 7126bdf4742875e1aa3c8018581a61e6fc01f5f5 Mon Sep 17 00:00:00 2001 From: Albert Gasset Date: Wed, 16 May 2018 16:10:14 +0200 Subject: [PATCH] MOBILE-2341 forum: PR fixes --- .../mod/forum/components/index/index.html | 8 +-- src/addon/mod/forum/components/index/index.ts | 39 ++++++++--- src/addon/mod/forum/components/post/post.ts | 8 ++- src/addon/mod/forum/lang/en.json | 4 +- .../mod/forum/pages/discussion/discussion.ts | 9 +-- src/addon/mod/forum/providers/forum.ts | 69 ++++++++++--------- src/addon/mod/forum/providers/offline.ts | 14 ++-- .../mod/forum/providers/prefetch-handler.ts | 24 ------- .../singleactivity/providers/handler.ts | 2 +- 9 files changed, 93 insertions(+), 84 deletions(-) diff --git a/src/addon/mod/forum/components/index/index.html b/src/addon/mod/forum/components/index/index.html index 33b90c5d6..f7eb42ee1 100644 --- a/src/addon/mod/forum/components/index/index.html +++ b/src/addon/mod/forum/components/index/index.html @@ -27,7 +27,7 @@
@@ -51,7 +51,7 @@ - +

{{discussion.subject}}

@@ -87,13 +87,13 @@

-
- + diff --git a/src/addon/mod/forum/components/index/index.ts b/src/addon/mod/forum/components/index/index.ts index 6ff264e86..caae5c8d7 100644 --- a/src/addon/mod/forum/components/index/index.ts +++ b/src/addon/mod/forum/components/index/index.ts @@ -40,16 +40,16 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom descriptionNote: string; forum: any; - trackPosts = false; - usesGroups = false; canLoadMore = false; discussions = []; offlineDiscussions = []; - count = 0; selectedDiscussion = 0; // Disucssion ID or negative timecreated if it's an offline discussion. + addDiscussionText = this.translate.instant('addon.mod_forum.addanewdiscussion'); protected syncEventName = AddonModForumSyncProvider.AUTO_SYNCED; protected page = 0; + protected trackPosts = false; + protected usesGroups = false; protected syncManualObserver: any; // It will observe the sync manual event. protected replyObserver: any; protected newDiscObserver: any; @@ -133,6 +133,18 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom this.dataRetrieved.emit(forum); + switch (forum.type) { + case 'news': + case 'blog': + this.addDiscussionText = this.translate.instant('addon.mod_forum.addanewtopic'); + break; + case 'qanda': + this.addDiscussionText = this.translate.instant('addon.mod_forum.addanewquestion'); + break; + default: + this.addDiscussionText = this.translate.instant('addon.mod_forum.addanewdiscussion'); + } + if (sync) { // Try to synchronize the forum. return this.syncActivity(showErrors).then((updated) => { @@ -141,6 +153,7 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom this.eventsProvider.trigger(AddonModForumSyncProvider.MANUAL_SYNCED, { forumId: forum.id, userId: this.sitesProvider.getCurrentSiteUserId(), + source: 'index', }, this.sitesProvider.getCurrentSiteId()); } }); @@ -164,8 +177,6 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom this.domUtils.showErrorModalDefault(message, 'addon.mod_forum.errorgetforum', true); this.canLoadMore = false; // Set to false to prevent infinite calls with infinite-loading. - - return Promise.reject(null); }); } @@ -282,11 +293,19 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom return Promise.all(offlinePromises); }); }); - }).catch((message) => { - this.domUtils.showErrorModal(message); - this.canLoadMore = false; // Set to false to prevent infinite calls with infinite-loading. + }); + } - return Promise.reject(null); + /** + * Convenience function to load more forum discussions. + * + * @return {Promise} Promise resolved when done. + */ + protected fetchMoreDiscussions(): Promise { + return this.fetchDiscussions(false).catch((message) => { + this.domUtils.showErrorModalDefault(message, 'addon.mod_forum.errorgetforum', true); + + this.canLoadMore = false; // Set to false to prevent infinite calls with infinite-loading. }); } @@ -357,7 +376,7 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom * @return {boolean} True if refresh is needed, false otherwise. */ protected isRefreshSyncNeeded(syncEventData: any): boolean { - return this.forum && syncEventData.forumId == this.forum.id && + return this.forum && syncEventData.source != 'index' && syncEventData.forumId == this.forum.id && syncEventData.userId == this.sitesProvider.getCurrentSiteUserId(); } diff --git a/src/addon/mod/forum/components/post/post.ts b/src/addon/mod/forum/components/post/post.ts index 04dda2b06..528b10974 100644 --- a/src/addon/mod/forum/components/post/post.ts +++ b/src/addon/mod/forum/components/post/post.ts @@ -87,7 +87,13 @@ export class AddonModForumPostComponent implements OnInit, OnDestroy { } /** - * Set data to new post, clearing tmp files and updating original data. + * Set data to new post, clearing temporary files and updating original data. + * + * @param {number} [replyingTo] Id of post beeing replied. + * @param {boolean} [isEditing] True it's an offline reply beeing edited, false otherwise. + * @param {string} [subject] Subject of the reply. + * @param {string} [message] Message of the reply. + * @param {any[]} [files] Reply attachments. */ protected setReplyData(replyingTo?: number, isEditing?: boolean, subject?: string, message?: string, files?: any[]): void { // Delete the local files from the tmp folder if any. diff --git a/src/addon/mod/forum/lang/en.json b/src/addon/mod/forum/lang/en.json index 0b3ffb881..5a461dcfd 100644 --- a/src/addon/mod/forum/lang/en.json +++ b/src/addon/mod/forum/lang/en.json @@ -1,5 +1,7 @@ { "addanewdiscussion": "Add a new discussion topic", + "addanewquestion": "Add a new question", + "addanewtopic": "Add a new topic", "cannotadddiscussion": "Adding discussions to this forum requires group membership.", "cannotadddiscussionall": "You do not have permission to add a new discussion topic for all participants.", "cannotcreatediscussion": "Could not create new discussion", @@ -29,4 +31,4 @@ "subject": "Subject", "unread": "Unread", "unreadpostsnumber": "{{$a}} unread posts" -} \ No newline at end of file +} diff --git a/src/addon/mod/forum/pages/discussion/discussion.ts b/src/addon/mod/forum/pages/discussion/discussion.ts index 95f37419d..ae1dab47b 100644 --- a/src/addon/mod/forum/pages/discussion/discussion.ts +++ b/src/addon/mod/forum/pages/discussion/discussion.ts @@ -42,8 +42,6 @@ export class AddonModForumDiscussionPage implements OnDestroy { @ViewChild(Content) content: Content; courseId: number; - cmId: number; - forumId: number; discussionId: number; forum: any; discussion: any; @@ -71,6 +69,8 @@ export class AddonModForumDiscussionPage implements OnDestroy { refreshIcon = 'spinner'; syncIcon = 'spinner'; + protected cmId: number; + protected forumId: number; protected onlineObserver: any; protected syncObserver: any; protected syncManualObserver: any; @@ -126,7 +126,8 @@ export class AddonModForumDiscussionPage implements OnDestroy { // Refresh data if this forum discussion is synchronized from discussions list. this.syncManualObserver = this.eventsProvider.on(AddonModForumSyncProvider.MANUAL_SYNCED, (data) => { - if (data.forumId == this.forumId && data.userId == this.sitesProvider.getCurrentSiteUserId()) { + if (data.source != 'discussion' && data.forumId == this.forumId && + data.userId == this.sitesProvider.getCurrentSiteUserId()) { // Refresh the data. this.discussionLoaded = false; this.refreshPosts(); @@ -310,7 +311,7 @@ export class AddonModForumDiscussionPage implements OnDestroy { this.eventsProvider.trigger(AddonModForumSyncProvider.MANUAL_SYNCED, { forumId: this.forumId, userId: this.sitesProvider.getCurrentSiteUserId(), - warnings: result.warnings + source: 'discussion' }, this.sitesProvider.getCurrentSiteId()); } diff --git a/src/addon/mod/forum/providers/forum.ts b/src/addon/mod/forum/providers/forum.ts index 4b41b2b21..37f36b139 100644 --- a/src/addon/mod/forum/providers/forum.ts +++ b/src/addon/mod/forum/providers/forum.ts @@ -190,14 +190,15 @@ export class AddonModForumProvider { /** * Check if a user can post to a certain group. * - * @param {number} forumId Forum ID. - * @param {number} groupId Group ID. - * @return {Promise} Promise resolved with an object with the following properties: - * - status (boolean) - * - canpindiscussions (boolean) - * - cancreateattachment (boolean) + * @param {number} forumId Forum ID. + * @param {number} groupId Group ID. + * @param {string} [siteId] Site ID. If not defined, current site. + * @return {Promise} Promise resolved with an object with the following properties: + * - status (boolean) + * - canpindiscussions (boolean) + * - cancreateattachment (boolean) */ - canAddDiscussion(forumId: number, groupId: number): Promise { + canAddDiscussion(forumId: number, groupId: number, siteId?: string): Promise { const params = { forumid: forumId, groupid: groupId @@ -206,21 +207,23 @@ export class AddonModForumProvider { cacheKey: this.getCanAddDiscussionCacheKey(forumId, groupId) }; - return this.sitesProvider.getCurrentSite().read('mod_forum_can_add_discussion', params, preSets).then((result) => { - if (result) { - if (typeof result.canpindiscussions == 'undefined') { - // WS doesn't support it yet, default it to false to prevent students from seing the option. - result.canpindiscussions = false; - } - if (typeof result.cancreateattachment == 'undefined') { - // WS doesn't support it yet, default it to true since usually the users will be able to create them. - result.cancreateattachment = true; + return this.sitesProvider.getSite(siteId).then((site) => { + return site.read('mod_forum_can_add_discussion', params, preSets).then((result) => { + if (result) { + if (typeof result.canpindiscussions == 'undefined') { + // WS doesn't support it yet, default it to false to prevent students from seing the option. + result.canpindiscussions = false; + } + if (typeof result.cancreateattachment == 'undefined') { + // WS doesn't support it yet, default it to true since usually the users will be able to create them. + result.cancreateattachment = true; + } + + return result; } - return result; - } - - return Promise.reject(null); + return Promise.reject(null); + }); }); } @@ -361,26 +364,28 @@ export class AddonModForumProvider { /** * Get forum discussion posts. * - * @param {number} discussionid Discussion ID. + * @param {number} discussionId Discussion ID. + * @param {string} [siteId] Site ID. If not defined, current site. * @return {Promise} Promise resolved with forum posts. */ - getDiscussionPosts(discussionid: number): Promise { - const site = this.sitesProvider.getCurrentSite(); + getDiscussionPosts(discussionId: number, siteId?: string): Promise { const params = { - discussionid: discussionid + discussionid: discussionId }; const preSets = { - cacheKey: this.getDiscussionPostsCacheKey(discussionid) + cacheKey: this.getDiscussionPostsCacheKey(discussionId) }; - return site.read('mod_forum_get_forum_discussion_posts', params, preSets).then((response) => { - if (response) { - this.storeUserData(response.posts); + return this.sitesProvider.getSite(siteId).then((site) => { + return site.read('mod_forum_get_forum_discussion_posts', params, preSets).then((response) => { + if (response) { + this.storeUserData(response.posts); - return response.posts; - } else { - return Promise.reject(null); - } + return response.posts; + } else { + return Promise.reject(null); + } + }); }); } diff --git a/src/addon/mod/forum/providers/offline.ts b/src/addon/mod/forum/providers/offline.ts index 5c024bd42..ac3a44261 100644 --- a/src/addon/mod/forum/providers/offline.ts +++ b/src/addon/mod/forum/providers/offline.ts @@ -163,7 +163,7 @@ export class AddonModForumOfflineProvider { }; return site.getDb().getRecord(this.DISCUSSIONS_TABLE, conditions).then((record) => { - record.options = JSON.parse(record.options); + record.options = this.textUtils.parseJSON(record.options); return record; }); @@ -178,7 +178,7 @@ export class AddonModForumOfflineProvider { */ getAllNewDiscussions(siteId?: string): Promise { return this.sitesProvider.getSite(siteId).then((site) => { - return site.getDb().getRecords(this.DISCUSSIONS_TABLE).then(this.parseRecordOptions); + return site.getDb().getRecords(this.DISCUSSIONS_TABLE).then(this.parseRecordOptions.bind(this)); }); } @@ -214,7 +214,7 @@ export class AddonModForumOfflineProvider { userid: userId || site.getUserId(), }; - return site.getDb().getRecords(this.DISCUSSIONS_TABLE, conditions).then(this.parseRecordOptions); + return site.getDb().getRecords(this.DISCUSSIONS_TABLE, conditions).then(this.parseRecordOptions.bind(this)); }); } @@ -279,7 +279,7 @@ export class AddonModForumOfflineProvider { */ getAllReplies(siteId?: string): Promise { return this.sitesProvider.getSite(siteId).then((site) => { - return site.getDb().getRecords(this.REPLIES_TABLE).then(this.parseRecordOptions); + return site.getDb().getRecords(this.REPLIES_TABLE).then(this.parseRecordOptions.bind(this)); }); } @@ -315,7 +315,7 @@ export class AddonModForumOfflineProvider { userid: userId || site.getUserId(), }; - return site.getDb().getRecords(this.REPLIES_TABLE, conditions).then(this.parseRecordOptions); + return site.getDb().getRecords(this.REPLIES_TABLE, conditions).then(this.parseRecordOptions.bind(this)); }); } @@ -351,7 +351,7 @@ export class AddonModForumOfflineProvider { userid: userId || site.getUserId(), }; - return site.getDb().getRecords(this.REPLIES_TABLE, conditions).then(this.parseRecordOptions); + return site.getDb().getRecords(this.REPLIES_TABLE, conditions).then(this.parseRecordOptions.bind(this)); }); } @@ -446,7 +446,7 @@ export class AddonModForumOfflineProvider { */ protected parseRecordOptions(records: any[]): any[] { records.forEach((record) => { - record.options = JSON.parse(record.options); + record.options = this.textUtils.parseJSON(record.options); }); return records; diff --git a/src/addon/mod/forum/providers/prefetch-handler.ts b/src/addon/mod/forum/providers/prefetch-handler.ts index 5ac82167b..89da20694 100644 --- a/src/addon/mod/forum/providers/prefetch-handler.ts +++ b/src/addon/mod/forum/providers/prefetch-handler.ts @@ -13,7 +13,6 @@ // limitations under the License. import { Injectable, Injector } from '@angular/core'; -import { CoreCourseModulePrefetchDelegate } from '@core/course/providers/module-prefetch-delegate'; import { CoreCourseModulePrefetchHandlerBase } from '@core/course/classes/module-prefetch-handler'; import { CoreGroupsProvider } from '@providers/groups'; import { CoreUserProvider } from '@core/user/providers/user'; @@ -32,7 +31,6 @@ export class AddonModForumPrefetchHandler extends CoreCourseModulePrefetchHandle constructor(injector: Injector, private groupsProvider: CoreGroupsProvider, private userProvider: CoreUserProvider, - private prefetchDelegate: CoreCourseModulePrefetchDelegate, private forumProvider: AddonModForumProvider) { super(injector); } @@ -133,28 +131,6 @@ export class AddonModForumPrefetchHandler extends CoreCourseModulePrefetchHandle return this.forumProvider.invalidateContent(moduleId, courseId); } - /** - * Invalidate WS calls needed to determine module status. - * - * @param {any} module Module. - * @param {number} courseId Course ID the module belongs to. - * @return {Promise} Promise resolved when invalidated. - */ - invalidateModule(module: any, courseId: number): Promise { - if (this.prefetchDelegate.canCheckUpdates()) { - // If can check updates only get forum by course is needed. - return this.forumProvider.invalidateForumData(courseId); - } - - // Get the forum since we need its ID. - return this.forumProvider.getForum(courseId, module.id).then((forum) => { - return Promise.all([ - this.forumProvider.invalidateForumData(courseId), - this.forumProvider.invalidateDiscussionsList(forum.id), - ]); - }); - } - /** * Prefetch a module. * diff --git a/src/core/course/formats/singleactivity/providers/handler.ts b/src/core/course/formats/singleactivity/providers/handler.ts index 31a967606..977ada030 100644 --- a/src/core/course/formats/singleactivity/providers/handler.ts +++ b/src/core/course/formats/singleactivity/providers/handler.ts @@ -93,7 +93,7 @@ export class CoreCourseFormatSingleActivityHandler implements CoreCourseFormatHa * @return {boolean} Whether the refresher should be displayed. */ displayRefresher(course: any, sections: any[]): boolean { - if (sections && sections[0] && sections[0].modules) { + if (sections && sections[0] && sections[0].modules && sections[0].modules[0]) { return this.moduleDelegate.displayRefresherInSingleActivity(sections[0].modules[0].modname); } else { return true;