From d7ce9677464ad3a883f7236b4e9638f38843d403 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 13 Feb 2024 13:09:03 +0100 Subject: [PATCH] MOBILE-2768 policy: Implement new WebService calls --- src/core/features/policy/services/policy.ts | 147 +++++++++++++++++++- 1 file changed, 144 insertions(+), 3 deletions(-) diff --git a/src/core/features/policy/services/policy.ts b/src/core/features/policy/services/policy.ts index b77363700..d67b71aca 100644 --- a/src/core/features/policy/services/policy.ts +++ b/src/core/features/policy/services/policy.ts @@ -17,10 +17,11 @@ import { CoreError } from '@classes/errors/error'; import { CoreWSError } from '@classes/errors/wserror'; import { CoreLoginHelper } from '@features/login/services/login-helper'; import { CoreNavigator } from '@services/navigator'; -import { CoreSites } from '@services/sites'; +import { CoreSites, CoreSitesCommonWSOptions } from '@services/sites'; import { CoreWSExternalWarning } from '@services/ws'; import { makeSingleton } from '@singletons'; import { POLICY_PAGE_NAME, SITE_POLICY_PAGE_NAME } from '../constants'; +import { CoreSite } from '@classes/sites/site'; /** * Service that provides some common features regarding policies. @@ -28,6 +29,8 @@ import { POLICY_PAGE_NAME, SITE_POLICY_PAGE_NAME } from '../constants'; @Injectable({ providedIn: 'root' }) export class CorePolicyService { + protected static readonly ROOT_CACHE_KEY = 'CorePolicy:'; + /** * Accept all mandatory site policies. * @@ -37,7 +40,7 @@ export class CorePolicyService { async acceptMandatorySitePolicies(siteId?: string): Promise { const site = await CoreSites.getSite(siteId); - const result = await site.write('core_user_agree_site_policy', {}); + const result = await site.write('core_user_agree_site_policy', {}); if (result.status) { return; @@ -86,6 +89,42 @@ export class CorePolicyService { return sitePolicy; } + /** + * Get user acceptances. + * + * @param options Options + * @returns List of policies with their acceptances. + */ + async getUserAcceptances(options: CoreSitesCommonWSOptions = {}): Promise { + const site = await CoreSites.getSite(options.siteId); + + const data: CorePolicyGetUserAcceptancesWSParams = { + userid: site.getUserId(), + }; + const preSets = { + cacheKey: this.getUserAcceptancesCacheKey(site.getUserId()), + updateFrequency: CoreSite.FREQUENCY_RARELY, + ...CoreSites.getReadingStrategyPreSets(options.readingStrategy), + }; + + const response = await site.read('tool_policy_get_user_acceptances', data, preSets); + if (response.warnings?.length) { + throw new CoreWSError(response.warnings[0]); + } + + return response.policies; + } + + /** + * Get the cache key for the get user acceptances call. + * + * @param userId ID of the user to get the badges from. + * @returns Cache key. + */ + protected getUserAcceptancesCacheKey(userId: number): string { + return CorePolicyService.ROOT_CACHE_KEY + 'userAcceptances:' + userId; + } + /** * Open page to accept site policies. * @@ -108,6 +147,44 @@ export class CorePolicyService { CoreNavigator.navigate(routePath, { params: { siteId }, reset: true }); } + /** + * Check whether a site allows getting and setting acceptances. + * + * @param siteId Site Id. + * @returns Whether the site allows getting and setting acceptances. + */ + async isManageAcceptancesAvailable(siteId?: string): Promise { + const site = await CoreSites.getSite(siteId); + + return site.wsAvailable('tool_policy_get_user_acceptances') && site.wsAvailable('tool_policy_set_acceptances_status'); + } + + /** + * Set user acceptances. + * + * @param policies Policies to accept or decline. Keys are policy version id, value is whether to accept or decline. + * @param siteId Site ID. If not defined, current site. + * @returns New value for policyagreed. + */ + async setUserAcceptances(policies: Record, siteId?: string): Promise { + const site = await CoreSites.getSite(siteId); + + const data: CorePolicySetAcceptancesWSParams = { + userid: site.getUserId(), + policies: Object.keys(policies).map((versionId) => ({ + versionid: Number(versionId), + status: policies[versionId], + })), + }; + + const response = await site.write('tool_policy_get_user_acceptances', data); + if (response.warnings?.length) { + throw new CoreWSError(response.warnings[0]); + } + + return response.policyagreed; + } + } export const CorePolicy = makeSingleton(CorePolicyService); @@ -115,7 +192,71 @@ export const CorePolicy = makeSingleton(CorePolicyService); /** * Result of WS core_user_agree_site_policy. */ -type AgreeSitePolicyResult = { +type CorePolicyAgreeSitePolicyResult = { status: boolean; // Status: true only if we set the policyagreed to 1 for the user. warnings?: CoreWSExternalWarning[]; }; + +/** + * Params of tool_policy_get_user_acceptances WS. + */ +type CorePolicyGetUserAcceptancesWSParams = { + userid?: number; // The user id we want to retrieve the acceptances. +}; + +/** + * Data returned by tool_policy_get_user_acceptances WS. + */ +type CorePolicyGetUserAcceptancesWSResponse = { + policies: CorePolicySitePolicy[]; + warnings?: CoreWSExternalWarning[]; +}; + +/** + * Policy data returned by tool_policy_get_user_acceptances WS. + */ +export type CorePolicySitePolicy = { + policyid: number; // The policy id. + versionid: number; // The policy version id. + agreementstyle: number; // The policy agreement style. 0: consent page, 1: own page. + optional: number; // Whether the policy is optional. 0: compulsory, 1: optional. + revision: string; // The policy revision. + status: number; // The policy status. 0: draft, 1: active, 2: archived. + name: string; // The policy name. + summary?: string; // The policy summary. + summaryformat: number; // Summary format (1 = HTML, 0 = MOODLE, 2 = PLAIN, or 4 = MARKDOWN). + content?: string; // The policy content. + contentformat: number; // Content format (1 = HTML, 0 = MOODLE, 2 = PLAIN, or 4 = MARKDOWN). + acceptance?: CorePolicySitePolicyAcceptance; // Acceptance status for the given user. +}; + +/** + * Policy acceptance data returned by tool_policy_get_user_acceptances WS. + */ +export type CorePolicySitePolicyAcceptance = { + status: number; // The acceptance status. 0: declined, 1: accepted. + lang: string; // The policy lang. + timemodified: number; // The time the acceptance was set. + usermodified: number; // The user who accepted. + note?: string; // The policy note/remarks. + modfullname?: string; // The fullname who accepted on behalf. +}; + +/** + * Params of tool_policy_set_acceptances_status WS. + */ +type CorePolicySetAcceptancesWSParams = { + policies: { + versionid: number; // The policy version id. + status: number; // The policy acceptance status. 0: decline, 1: accept. + }[]; // Policies acceptances for the given user. + userid?: number; // The user id we want to set the acceptances. Default is the current user. +}; + +/** + * Data returned by tool_policy_set_acceptances_status WS. + */ +type CorePolicySetAcceptancesWSResponse = { + policyagreed: number; // Whether the user has provided acceptance to all current site policies. 1 if yes, 0 if not. + warnings?: CoreWSExternalWarning[]; +};