Skip to content

Commit

Permalink
MOBILE-4323 enrol: Handle methods without get_enrol_info
Browse files Browse the repository at this point in the history
  • Loading branch information
dpalou committed Sep 13, 2023
1 parent ceb4b40 commit bbe1e7d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
protected async getEnrolmentInfo(enrolmentMethods?: string[]): Promise<void> {
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;
Expand Down
17 changes: 15 additions & 2 deletions src/core/features/enrol/services/enrol-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CoreEnrolmentsByType> {
const enrolmentMethods = await CoreEnrol.getSupportedCourseEnrolmentMethods(courseId);
async getEnrolmentsByType(courseId: number, allMethodTypes?: string[]): Promise<CoreEnrolmentsByType> {
// 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[] = [];
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion src/core/features/enrol/services/enrol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CoreEnrolEnrolmentMethod[]> {
async getCourseEnrolmentMethods(courseId: number, siteId?: string): Promise<CoreEnrolEnrolmentMethod[]> {
const site = await CoreSites.getSite(siteId);

const params: CoreEnrolGetCourseEnrolmentMethodsWSParams = {
Expand All @@ -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.
Expand Down

0 comments on commit bbe1e7d

Please sign in to comment.