From bbe1e7d65efcb43ef6bf2678a4b59edbab46a002 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Wed, 13 Sep 2023 09:44:49 +0200 Subject: [PATCH] MOBILE-4323 enrol: Handle methods without get_enrol_info --- .../pages/course-summary/course-summary.page.ts | 10 ++++++---- .../features/enrol/services/enrol-helper.ts | 17 +++++++++++++++-- src/core/features/enrol/services/enrol.ts | 6 +++++- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/core/features/course/pages/course-summary/course-summary.page.ts b/src/core/features/course/pages/course-summary/course-summary.page.ts index d3c5700dca9..dbe4d467c5a 100644 --- a/src/core/features/course/pages/course-summary/course-summary.page.ts +++ b/src/core/features/course/pages/course-summary/course-summary.page.ts @@ -211,18 +211,20 @@ export class CoreCourseSummaryPage implements OnInit, OnDestroy { } } - await this.getEnrolmentInfo(); + await this.getEnrolmentInfo(courseByField?.enrollmentmethods); } /** - * Get course enrolment methods. + * Get course enrolment info. + * + * @param enrolmentMethods Enrolment methods. */ - protected async getEnrolmentInfo(): Promise { + protected async getEnrolmentInfo(enrolmentMethods?: string[]): Promise { if (this.isEnrolled) { return; } - const enrolByType = await CoreEnrolHelper.getEnrolmentsByType(this.courseId); + const enrolByType = await CoreEnrolHelper.getEnrolmentsByType(this.courseId, enrolmentMethods); this.hasBrowserEnrolments = enrolByType.hasBrowser; this.selfEnrolInstances = enrolByType.self; diff --git a/src/core/features/enrol/services/enrol-helper.ts b/src/core/features/enrol/services/enrol-helper.ts index 1954254fddc..54d3dd03b77 100644 --- a/src/core/features/enrol/services/enrol-helper.ts +++ b/src/core/features/enrol/services/enrol-helper.ts @@ -75,10 +75,12 @@ export class CoreEnrolHelperService { * Get enrolment methods divided by type. * * @param courseId Course Id. + * @param allMethodTypes List of enrolment methods returned by getCourseByField. * @returns Enrolment info divided by types. */ - async getEnrolmentsByType(courseId: number): Promise { - const enrolmentMethods = await CoreEnrol.getSupportedCourseEnrolmentMethods(courseId); + async getEnrolmentsByType(courseId: number, allMethodTypes?: string[]): Promise { + // Don't use getSupportedCourseEnrolmentMethods to treat unsupported methods and methods with disabled status. + const enrolmentMethods = await CoreEnrol.getCourseEnrolmentMethods(courseId); const self: CoreEnrolEnrolmentMethod[] = []; const guest: CoreEnrolEnrolmentMethod[] = []; @@ -108,6 +110,17 @@ export class CoreEnrolHelperService { } }); + // Now treat the methods returned by getCourseByField but not by getCourseEnrolmentMethods. + allMethodTypes?.forEach(type => { + if (enrolmentMethods.some(method => method.type === type)) { + return; // Already treated. + } + + const action = CoreEnrolDelegate.getEnrolmentAction(type); + hasBrowser = hasBrowser || action === CoreEnrolAction.BROWSER; + hasNotSupported = hasNotSupported || action === CoreEnrolAction.NOT_SUPPORTED; + }); + return { self, guest, diff --git a/src/core/features/enrol/services/enrol.ts b/src/core/features/enrol/services/enrol.ts index b18a3a2e24b..28fd198ed6b 100644 --- a/src/core/features/enrol/services/enrol.ts +++ b/src/core/features/enrol/services/enrol.ts @@ -29,12 +29,14 @@ export class CoreEnrolService { /** * Get the enrolment methods from a course. + * Please notice that this function will only return methods that implement get_enrol_info, it won't return all + * enrolment methods in a course. * * @param courseId ID of the course. * @param siteId Site ID. If not defined, use current site. * @returns Promise resolved with the methods. */ - protected async getCourseEnrolmentMethods(courseId: number, siteId?: string): Promise { + async getCourseEnrolmentMethods(courseId: number, siteId?: string): Promise { const site = await CoreSites.getSite(siteId); const params: CoreEnrolGetCourseEnrolmentMethodsWSParams = { @@ -50,6 +52,8 @@ export class CoreEnrolService { /** * Get the enrolment methods from a course that are enabled and supported by the app. + * Please notice that this function will only return methods that implement get_enrol_info, it won't return all + * enrolment methods in a course. * * @param courseId ID of the course. * @param options Options.