Skip to content

Commit

Permalink
MOBILE-4323 enrol: Support enrolment action on siteplugins
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyserver committed Jul 25, 2023
1 parent e8cb47a commit 197cd0e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
39 changes: 39 additions & 0 deletions src/core/features/siteplugins/classes/handlers/enrol-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// (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 { CoreLogger } from '@singletons/logger';
import { CoreSitePluginsBaseHandler } from './base-handler';
import { CoreEnrolAction, CoreEnrolHandler } from '@features/enrol/services/enrol-delegate';
import { CoreSitePluginsContent, CoreSitePluginsEnrolHandlerData } from '@features/siteplugins/services/siteplugins';

/**
* Handler to support a enrol using a site plugin.
*/
export class CoreSitePluginsEnrolHandler extends CoreSitePluginsBaseHandler implements CoreEnrolHandler {

protected logger: CoreLogger;

constructor(
name: string,
public type: string,
public enrolmentAction: CoreEnrolAction,
protected handlerSchema: CoreSitePluginsEnrolHandlerData,
protected initResult: CoreSitePluginsContent | null,
) {
super(name);

this.logger = CoreLogger.getInstance('CoreSitePluginsEnrolHandler');
}

}
33 changes: 33 additions & 0 deletions src/core/features/siteplugins/services/siteplugins-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import {
CoreSitePluginsHandlerCommonData,
CoreSitePluginsInitHandlerData,
CoreSitePluginsMainMenuHomeHandlerData,
CoreSitePluginsEnrolHandlerData,
} from './siteplugins';
import { makeSingleton } from '@singletons';
import { CoreMainMenuHomeDelegate } from '@features/mainmenu/services/home-delegate';
Expand All @@ -86,6 +87,8 @@ import { CoreContentLinksModuleListHandler } from '@features/contentlinks/classe
import { CoreObject } from '@singletons/object';
import { CoreUrlUtils } from '@services/utils/url';
import { CorePath } from '@singletons/path';
import { CoreEnrolAction, CoreEnrolDelegate } from '@features/enrol/services/enrol-delegate';
import { CoreSitePluginsEnrolHandler } from '../classes/handlers/enrol-handler';

const HANDLER_DISABLED = 'core_site_plugins_helper_handler_disabled';

Expand Down Expand Up @@ -561,6 +564,10 @@ export class CoreSitePluginsHelperProvider {
uniqueName = this.registerMainMenuHomeHandler(plugin, handlerName, handlerSchema, initResult);
break;

case 'CoreEnrolDelegate':
uniqueName = this.registerEnrolHandler(plugin, handlerName, handlerSchema, initResult);
break;

default:
// Nothing to do.
}
Expand Down Expand Up @@ -800,6 +807,32 @@ export class CoreSitePluginsHelperProvider {
return uniqueName;
}

/**
* Given a handler in a plugin, register it in the enrol delegate.
*
* @param plugin Data of the plugin.
* @param handlerName Name of the handler in the plugin.
* @param handlerSchema Data about the handler.
* @param initResult Result of init function.
* @returns A string to identify the handler.
*/
protected registerEnrolHandler(
plugin: CoreSitePluginsPlugin,
handlerName: string,
handlerSchema: CoreSitePluginsEnrolHandlerData,
initResult: CoreSitePluginsContent | null,
): string | undefined {
const uniqueName = CoreSitePlugins.getHandlerUniqueName(plugin, handlerName);
const type = (handlerSchema.moodlecomponent || plugin.component).replace('enrol_', '');
const action = handlerSchema.enrolmentAction ?? CoreEnrolAction.BROWSER;

CoreEnrolDelegate.registerHandler(
new CoreSitePluginsEnrolHandler(uniqueName, type, action, handlerSchema, initResult),
);

return uniqueName;
}

/**
* Given a handler in a plugin, register it in the main menu delegate.
*
Expand Down
10 changes: 9 additions & 1 deletion src/core/features/siteplugins/services/siteplugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { CoreLogger } from '@singletons/logger';
import { CoreSitePluginsModuleHandler } from '../classes/handlers/module-handler';
import { CorePromisedValue } from '@classes/promised-value';
import { CorePlatform } from '@services/platform';
import { CoreEnrolAction } from '@features/enrol/services/enrol-delegate';

const ROOT_CACHE_KEY = 'CoreSitePlugins:';

Expand Down Expand Up @@ -825,7 +826,7 @@ export type CoreSitePluginsPlugin = CoreSitePluginsWSPlugin & {
export type CoreSitePluginsHandlerData = CoreSitePluginsInitHandlerData | CoreSitePluginsCourseOptionHandlerData |
CoreSitePluginsMainMenuHandlerData | CoreSitePluginsCourseModuleHandlerData | CoreSitePluginsCourseFormatHandlerData |
CoreSitePluginsUserHandlerData | CoreSitePluginsSettingsHandlerData | CoreSitePluginsMessageOutputHandlerData |
CoreSitePluginsBlockHandlerData | CoreSitePluginsMainMenuHomeHandlerData;
CoreSitePluginsBlockHandlerData | CoreSitePluginsMainMenuHomeHandlerData | CoreSitePluginsEnrolHandlerData;

/**
* Plugin handler data common to all delegates.
Expand Down Expand Up @@ -960,6 +961,13 @@ export type CoreSitePluginsBlockHandlerData = CoreSitePluginsHandlerCommonData &
fallback?: string;
};

/**
* Enrol handler specific data.
*/
export type CoreSitePluginsEnrolHandlerData = CoreSitePluginsHandlerCommonData & {
enrolmentAction?: CoreEnrolAction;
};

/**
* Common handler data with some data from the init method.
*/
Expand Down

0 comments on commit 197cd0e

Please sign in to comment.