From e84a2ee78cc7b44b7d81d9f2cde2e6b2120d4c95 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 25 Mar 2024 10:32:58 +0100 Subject: [PATCH] MOBILE-4522 forum: Don't calculate unread number if tracking disabled --- src/addons/mod/forum/services/forum.ts | 9 +++++++ .../mod/forum/services/handlers/module.ts | 27 ++++++++++++++++++- src/core/features/user/services/user.ts | 1 + 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/addons/mod/forum/services/forum.ts b/src/addons/mod/forum/services/forum.ts index 5c1184ab5..676b21b5b 100644 --- a/src/addons/mod/forum/services/forum.ts +++ b/src/addons/mod/forum/services/forum.ts @@ -2134,3 +2134,12 @@ export type AddonModForumMarkReadData = { courseId: number; moduleId: number; }; + +/** + * Tracking options. + */ +export const enum AddonModForumTracking { + OFF = 0, + OPTIONAL = 1, + FORCED = 2, +} diff --git a/src/addons/mod/forum/services/handlers/module.ts b/src/addons/mod/forum/services/handlers/module.ts index 005ccce63..e6244665c 100644 --- a/src/addons/mod/forum/services/handlers/module.ts +++ b/src/addons/mod/forum/services/handlers/module.ts @@ -13,7 +13,7 @@ // limitations under the License. import { Injectable, Type } from '@angular/core'; -import { AddonModForum, AddonModForumProvider } from '../forum'; +import { AddonModForum, AddonModForumProvider, AddonModForumTracking } from '../forum'; import { makeSingleton, Translate } from '@singletons'; import { CoreEvents } from '@singletons/events'; import { CoreSites, CoreSitesReadingStrategy } from '@services/sites'; @@ -21,6 +21,8 @@ import { CoreCourseModuleHandler, CoreCourseModuleHandlerData } from '@features/ import { CoreConstants, ModPurpose } from '@/core/constants'; import { CoreModuleHandlerBase } from '@features/course/classes/module-base-handler'; import { CoreCourseModuleData } from '@features/course/services/course-helper'; +import { CoreTextUtils } from '@services/utils/text'; +import { CoreUser } from '@features/user/services/user'; /** * Handler to support forum modules. @@ -55,6 +57,29 @@ export class AddonModForumModuleHandlerService extends CoreModuleHandlerBase imp async getData(module: CoreCourseModuleData, courseId: number): Promise { const data = await super.getData(module, courseId); + const customData = module.customdata ? + CoreTextUtils.parseJSON<{ trackingtype?: string | number } | ''>(module.customdata, {}) : {}; + const trackingType = typeof customData !== 'string' && customData.trackingtype !== undefined ? + Number(customData.trackingtype) : undefined; + + if (trackingType === AddonModForumTracking.OFF) { + // Tracking is disabled in forum. + data.extraBadge = ''; + + return data; + } + + if (trackingType === AddonModForumTracking.OPTIONAL) { + // Forum has tracking optional, check if user has tracking enabled. + const user = await CoreUser.getProfile(CoreSites.getCurrentSiteUserId()); + + if (user.trackforums === 0) { + data.extraBadge = ''; + + return data; + } + } + if ('afterlink' in module && !!module.afterlink) { const match = />(\d+)[^<]+/.exec(module.afterlink); data.extraBadge = match ? Translate.instant('addon.mod_forum.unreadpostsnumber', { $a : match[1] }) : ''; diff --git a/src/core/features/user/services/user.ts b/src/core/features/user/services/user.ts index d603f6433..e4734b160 100644 --- a/src/core/features/user/services/user.ts +++ b/src/core/features/user/services/user.ts @@ -966,6 +966,7 @@ export type CoreUserData = { theme?: string; // Theme name such as "standard", must exist on server. timezone?: string; // Timezone code such as Australia/Perth, or 99 for default. mailformat?: number; // Mail format code is 0 for plain text, 1 for HTML etc. + trackforums?: number; // @since 4.4. Whether the user is tracking forums. description?: string; // User profile description. descriptionformat?: number; // Int format (1 = HTML, 0 = MOODLE, 2 = PLAIN or 4 = MARKDOWN). city?: string; // Home city of the user.