Skip to content

Commit

Permalink
Merge pull request #3754 from crazyserver/MOBILE-4323
Browse files Browse the repository at this point in the history
MOBILE-4323 enrol: Create enrol delegates
  • Loading branch information
dpalou authored Aug 24, 2023
2 parents c9bf366 + 77d4f53 commit a6eeacf
Show file tree
Hide file tree
Showing 42 changed files with 1,819 additions and 510 deletions.
15 changes: 8 additions & 7 deletions scripts/langindex.json
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@
"addon.coursecompletion.requirement": "block_completionstatus",
"addon.coursecompletion.status": "moodle",
"addon.coursecompletion.viewcoursereport": "completion",
"addon.enrol_guest.guestaccess_withoutpassword": "enrol_guest",
"addon.enrol_guest.guestaccess_withpassword": "enrol_guest",
"addon.enrol_guest.passwordinvalid": "enrol_guest",
"addon.enrol_self.confirmselfenrol": "local_moodlemobileapp",
"addon.enrol_self.errorselfenrol": "local_moodlemobileapp",
"addon.enrol_self.nopassword": "enrol_self",
"addon.enrol_self.password": "enrol_self",
"addon.enrol_self.pluginname": "enrol_self",
"addon.messageoutput_airnotifier.processorsettingsdesc": "local_moodlemobileapp",
"addon.messageoutput_airnotifier.pushdisabledwarning": "local_moodlemobileapp",
"addon.messages.acceptandaddcontact": "message",
Expand Down Expand Up @@ -1590,9 +1598,6 @@
"core.course.errordownloadingsection": "local_moodlemobileapp",
"core.course.errorgetmodule": "local_moodlemobileapp",
"core.course.failed": "completion",
"core.course.guestaccess": "enrol_guest/pluginname",
"core.course.guestaccess_passwordinvalid": "enrol_guest/passwordinvalid",
"core.course.guestaccess_withpassword": "enrol_guest",
"core.course.hiddenfromstudents": "moodle",
"core.course.hiddenoncoursepage": "moodle",
"core.course.highlighted": "moodle",
Expand Down Expand Up @@ -1623,7 +1628,6 @@
"core.coursedetails": "moodle",
"core.coursenogroups": "local_moodlemobileapp",
"core.courses.addtofavourites": "block_myoverview",
"core.courses.allowguests": "enrol_guest",
"core.courses.aria:coursecategory": "course",
"core.courses.aria:coursename": "course",
"core.courses.aria:courseprogress": "block_myoverview",
Expand All @@ -1633,15 +1637,13 @@
"core.courses.cannotretrievemorecategories": "local_moodlemobileapp",
"core.courses.categories": "moodle",
"core.courses.completeenrolmentbrowser": "local_moodlemobileapp",
"core.courses.confirmselfenrol": "local_moodlemobileapp",
"core.courses.courses": "moodle",
"core.courses.downloadcourses": "local_moodlemobileapp",
"core.courses.enrolme": "local_moodlemobileapp",
"core.courses.errorloadcategories": "local_moodlemobileapp",
"core.courses.errorloadcourses": "local_moodlemobileapp",
"core.courses.errorloadplugins": "local_moodlemobileapp",
"core.courses.errorsearching": "local_moodlemobileapp",
"core.courses.errorselfenrol": "local_moodlemobileapp",
"core.courses.favourite": "course",
"core.courses.filtermycourses": "local_moodlemobileapp",
"core.courses.frontpage": "admin",
Expand All @@ -1663,7 +1665,6 @@
"core.courses.search": "moodle",
"core.courses.searchcourses": "moodle",
"core.courses.searchcoursesadvice": "local_moodlemobileapp",
"core.courses.selfenrolment": "local_moodlemobileapp",
"core.courses.show": "block_myoverview",
"core.courses.showonlyenrolled": "local_moodlemobileapp",
"core.courses.therearecourses": "moodle",
Expand Down
2 changes: 2 additions & 0 deletions src/addons/addons.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { AddonBlogModule } from './blog/blog.module';
import { AddonCalendarModule } from './calendar/calendar.module';
import { AddonCompetencyModule } from './competency/competency.module';
import { AddonCourseCompletionModule } from './coursecompletion/coursecompletion.module';
import { AddonEnrolModule } from './enrol/enrol.module';
import { AddonFilterModule } from './filter/filter.module';
import { AddonMessageOutputModule } from './messageoutput/messageoutput.module';
import { AddonMessagesModule } from './messages/messages.module';
Expand All @@ -42,6 +43,7 @@ import { AddonUserProfileFieldModule } from './userprofilefield/userprofilefield
AddonCalendarModule,
AddonCompetencyModule,
AddonCourseCompletionModule,
AddonEnrolModule,
AddonFilterModule,
AddonMessageOutputModule,
AddonMessagesModule,
Expand Down
4 changes: 2 additions & 2 deletions src/addons/competency/services/handlers/course-option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ export class AddonCompetencyCourseOptionHandlerService implements CoreCourseOpti
accessData: CoreCourseAccess,
navOptions?: CoreCourseUserAdminOrNavOptionIndexed,
): Promise<boolean> {
if (accessData && accessData.type == CoreCourseProvider.ACCESS_GUEST) {
return false; // Not enabled for guests.
if (accessData && accessData.type === CoreCourseProvider.ACCESS_GUEST) {
return false; // Not enabled for guest access.
}

if (navOptions && navOptions.competencies !== undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export class AddonCourseCompletionCourseOptionHandlerService implements CoreCour
* @inheritdoc
*/
async isEnabledForCourse(courseId: number, accessData: CoreCourseAccess): Promise<boolean> {
if (accessData && accessData.type == CoreCourseProvider.ACCESS_GUEST) {
return false; // Not enabled for guests.
if (accessData && accessData.type === CoreCourseProvider.ACCESS_GUEST) {
return false; // Not enabled for guest access.
}

const courseEnabled = await AddonCourseCompletion.isPluginViewEnabledForCourse(courseId);
Expand Down
30 changes: 30 additions & 0 deletions src/addons/enrol/enrol.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { NgModule } from '@angular/core';

import { AddonEnrolFeeModule } from './fee/fee.module';
import { AddonEnrolGuestModule } from './guest/guest.module';
import { AddonEnrolPaypalModule } from './paypal/paypal.module';
import { AddonEnrolSelfModule } from './self/self.module';

@NgModule({
imports: [
AddonEnrolFeeModule,
AddonEnrolGuestModule,
AddonEnrolPaypalModule,
AddonEnrolSelfModule,
],
})
export class AddonEnrolModule {}
30 changes: 30 additions & 0 deletions src/addons/enrol/fee/fee.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { APP_INITIALIZER, NgModule } from '@angular/core';
import { AddonEnrolFeeHandler } from './services/enrol-handler';
import { CoreEnrolDelegate } from '@features/enrol/services/enrol-delegate';

@NgModule({
providers: [
{
provide: APP_INITIALIZER,
multi: true,
useValue: () => {
CoreEnrolDelegate.registerHandler(AddonEnrolFeeHandler.instance);
},
},
],
})
export class AddonEnrolFeeModule {}
38 changes: 38 additions & 0 deletions src/addons/enrol/fee/services/enrol-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { Injectable } from '@angular/core';
import { CoreEnrolAction, CoreEnrolHandler } from '@features/enrol/services/enrol-delegate';
import { makeSingleton } from '@singletons';

/**
* Enrol handler.
*/
@Injectable({ providedIn: 'root' })
export class AddonEnrolFeeHandlerService implements CoreEnrolHandler {

name = 'AddonEnrolFee';
type = 'fee';
enrolmentAction = CoreEnrolAction.BROWSER;

/**
* @inheritdoc
*/
async isEnabled(): Promise<boolean> {
return true;
}

}

export const AddonEnrolFeeHandler = makeSingleton(AddonEnrolFeeHandlerService);
30 changes: 30 additions & 0 deletions src/addons/enrol/guest/guest.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { APP_INITIALIZER, NgModule } from '@angular/core';
import { AddonEnrolGuestHandler } from './services/enrol-handler';
import { CoreEnrolDelegate } from '@features/enrol/services/enrol-delegate';

@NgModule({
providers: [
{
provide: APP_INITIALIZER,
multi: true,
useValue: () => {
CoreEnrolDelegate.registerHandler(AddonEnrolGuestHandler.instance);
},
},
],
})
export class AddonEnrolGuestModule {}
5 changes: 5 additions & 0 deletions src/addons/enrol/guest/lang.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"guestaccess_withpassword": "Guest access requires password",
"guestaccess_withoutpassword": "Guest access",
"passwordinvalid": "Incorrect access password, please try again"
}
150 changes: 150 additions & 0 deletions src/addons/enrol/guest/services/enrol-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { Injectable } from '@angular/core';
import {
CoreEnrolAction,
CoreEnrolCanAccessData,
CoreEnrolGuestHandler,
CoreEnrolInfoIcon,
} from '@features/enrol/services/enrol-delegate';
import { makeSingleton } from '@singletons';
import { AddonEnrolGuest } from './guest';
import { CorePasswordModalResponse } from '@components/password-modal/password-modal';
import { CoreDomUtils } from '@services/utils/dom';
import { CoreWSError } from '@classes/errors/wserror';
import { CoreEnrol, CoreEnrolEnrolmentMethod } from '@features/enrol/services/enrol';

/**
* Enrol handler.
*/
@Injectable({ providedIn: 'root' })
export class AddonEnrolGuestHandlerService implements CoreEnrolGuestHandler {

name = 'AddonEnrolGuest';
type = 'guest';
enrolmentAction = <CoreEnrolAction.GUEST> CoreEnrolAction.GUEST;

/**
* @inheritdoc
*/
async isEnabled(): Promise<boolean> {
return true;
}

/**
* @inheritdoc
*/
async getInfoIcons(courseId: number): Promise<CoreEnrolInfoIcon[]> {
const guestEnrolments = await CoreEnrol.getSupportedCourseEnrolmentMethods(courseId, { type: this.type });

for (const guestEnrolment of guestEnrolments) {
const info = await AddonEnrolGuest.getGuestEnrolmentInfo(guestEnrolment.id);
// Don't allow guest access if it requires a password if not supported.
if (!info.passwordrequired) {
return [{
label: 'addon.enrol_guest.guestaccess_withoutpassword',
icon: 'fas-unlock',
}];
} else {
return [{
label: 'addon.enrol_guest.guestaccess_withpassword',
icon: 'fas-key',
}];
}
}

return [];
}

/**
* @inheritdoc
*/
async canAccess(method: CoreEnrolEnrolmentMethod): Promise<CoreEnrolCanAccessData> {
const info = await AddonEnrolGuest.getGuestEnrolmentInfo(method.id);

return {
canAccess: info.status && (!info.passwordrequired || AddonEnrolGuest.isValidateGuestAccessPasswordAvailable()),
requiresUserInput: info.passwordrequired,
};
}

/**
* @inheritdoc
*/
async validateAccess(method: CoreEnrolEnrolmentMethod): Promise<boolean> {
const info = await AddonEnrolGuest.getGuestEnrolmentInfo(method.id);

if (!info.status) {
return false;
}

if (!info.passwordrequired) {
return true;
}

if (!AddonEnrolGuest.isValidateGuestAccessPasswordAvailable()) {
return false;
}

const validatePassword = async (password: string): Promise<CorePasswordModalResponse> => {
const modal = await CoreDomUtils.showModalLoading('core.loading', true);

try {
const response = await AddonEnrolGuest.validateGuestAccessPassword(method.id, password);

let error = response.hint;
if (!response.validated && !error) {
error = 'addon.enrol_guest.passwordinvalid';
}

return {
password, validated: response.validated, error,
};
} finally {
modal.dismiss();
}
};

try {
const response = await CoreDomUtils.promptPassword<CorePasswordModalResponse>({
title: method.name,
validator: validatePassword,
});

if (!response.validated) {
return false;
}
} catch (error) {
if (error instanceof CoreWSError) {
throw error;
}

// Cancelled, return
return false;
}

return true;
}

/**
* @inheritdoc
*/
async invalidate(method: CoreEnrolEnrolmentMethod): Promise<void> {
return AddonEnrolGuest.invalidateGuestEnrolmentInfo(method.id);
}

}

export const AddonEnrolGuestHandler = makeSingleton(AddonEnrolGuestHandlerService);
Loading

0 comments on commit a6eeacf

Please sign in to comment.