From 1cc25e158e16d11732bca7e721bc89febe68e03c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Mon, 15 Jul 2024 09:35:29 +0200 Subject: [PATCH] MOBILE-4632 siteplugins: Minor changes on declarations --- src/core/features/siteplugins/constants.ts | 4 + ...eplugins-helper.ts => siteplugins-init.ts} | 399 +++++++++++------- .../siteplugins/services/siteplugins.ts | 20 +- .../siteplugins/siteplugins.module.ts | 13 +- 4 files changed, 281 insertions(+), 155 deletions(-) rename src/core/features/siteplugins/services/{siteplugins-helper.ts => siteplugins-init.ts} (74%) diff --git a/src/core/features/siteplugins/constants.ts b/src/core/features/siteplugins/constants.ts index c971aa1e88e..a215c83e0c0 100644 --- a/src/core/features/siteplugins/constants.ts +++ b/src/core/features/siteplugins/constants.ts @@ -13,3 +13,7 @@ // limitations under the License. export const CORE_SITE_PLUGINS_UPDATE_COURSE_CONTENT = 'siteplugins_update_course_content'; + +export const CORE_SITE_PLUGINS_COMPONENT = 'CoreSitePlugins'; + +export const CORE_SITE_PLUGINS_PATH = 'siteplugins'; diff --git a/src/core/features/siteplugins/services/siteplugins-helper.ts b/src/core/features/siteplugins/services/siteplugins-init.ts similarity index 74% rename from src/core/features/siteplugins/services/siteplugins-helper.ts rename to src/core/features/siteplugins/services/siteplugins-init.ts index 3417db1e33f..e3a974de189 100644 --- a/src/core/features/siteplugins/services/siteplugins-helper.ts +++ b/src/core/features/siteplugins/services/siteplugins-init.ts @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Injectable } from '@angular/core'; - import { AddonMessageOutputDelegate } from '@addons/messageoutput/services/messageoutput-delegate'; import { AddonModAssignFeedbackDelegate } from '@addons/mod/assign/services/feedback-delegate'; import { AddonModAssignSubmissionDelegate } from '@addons/mod/assign/services/submission-delegate'; @@ -62,7 +60,6 @@ import { CoreSitePluginsContent, CoreSitePluginsPlugin, CoreSitePluginsHandlerData, - CoreSitePluginsProvider, CoreSitePluginsCourseOptionHandlerData, CoreSitePluginsMainMenuHandlerData, CoreSitePluginsCourseModuleHandlerData, @@ -76,7 +73,6 @@ import { CoreSitePluginsMainMenuHomeHandlerData, CoreSitePluginsEnrolHandlerData, } from './siteplugins'; -import { makeSingleton } from '@singletons'; import { CoreMainMenuHomeDelegate } from '@features/mainmenu/services/home-delegate'; import { CoreSitePluginsMainMenuHomeHandler } from '../classes/handlers/main-menu-home-handler'; import { AddonWorkshopAssessmentStrategyDelegate } from '@addons/mod/workshop/services/assessment-strategy-delegate'; @@ -89,8 +85,7 @@ 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'; +import { CORE_SITE_PLUGINS_UPDATE_COURSE_CONTENT } from '../constants'; /** * Helper service to provide functionalities regarding site plugins. It basically has the features to load and register site @@ -101,43 +96,40 @@ const HANDLER_DISABLED = 'core_site_plugins_helper_handler_disabled'; * @todo Support ViewChild and similar in site plugins. Possible solution: make components and directives inject the instance * inside the host DOM element? */ -@Injectable({ providedIn: 'root' }) -export class CoreSitePluginsHelperProvider { +export class CoreSitePluginsInitializer { - protected logger: CoreLogger; - protected courseRestrictHandlers: Record = {}; - constructor() { - this.logger = CoreLogger.getInstance('CoreSitePluginsHelperProvider'); - } + protected static readonly HANDLER_DISABLED = 'core_site_plugins_helper_handler_disabled'; + protected static logger = CoreLogger.getInstance('CoreSitePluginsHelper'); /** * Initialize. */ - initialize(): void { + static init(): void { // Fetch the plugins on login. CoreEvents.on(CoreEvents.LOGIN, async (data) => { try { const plugins = await CoreUtils.ignoreErrors(CoreSitePlugins.getPlugins(data.siteId)); // Plugins fetched, check that site hasn't changed. - if (data.siteId != CoreSites.getCurrentSiteId() || !plugins?.length) { + if (data.siteId !== CoreSites.getCurrentSiteId() || !plugins?.length) { return; } // Site is still the same. Load the plugins and trigger the event. try { - await this.loadSitePlugins(plugins); + await CoreSitePluginsInitializer.loadSitePlugins(plugins); } finally { CoreEvents.trigger(CoreEvents.SITE_PLUGINS_LOADED, {}, data.siteId); } } catch (error) { - this.logger.error(error); + CoreSitePluginsInitializer.logger.error(error); } finally { CoreSitePlugins.setPluginsFetched(); } @@ -145,8 +137,8 @@ export class CoreSitePluginsHelperProvider { // Re-load plugins restricted for courses when the list of user courses changes. CoreEvents.on(CoreCoursesProvider.EVENT_MY_COURSES_CHANGED, (data) => { - if (data && data.siteId && data.siteId == CoreSites.getCurrentSiteId() && data.added && data.added.length) { - this.reloadCourseRestrictHandlers(); + if (data?.siteId === CoreSites.getCurrentSiteId() && data?.added.length) { + CoreSitePluginsInitializer.reloadCourseRestrictHandlers(); } }); } @@ -160,7 +152,7 @@ export class CoreSitePluginsHelperProvider { * @param siteId Site ID. If not provided, current site. * @returns Promise resolved with the CSS code. */ - async downloadStyles( + protected static async downloadStyles( plugin: CoreSitePluginsPlugin, handlerName: string, handlerSchema: CoreSitePluginsHandlerData, @@ -182,9 +174,9 @@ export class CoreSitePluginsHelperProvider { const uniqueName = CoreSitePlugins.getHandlerUniqueName(plugin, handlerName); const componentId = uniqueName + '#main'; - // Remove the CSS files for this handler that aren't used anymore. Don't block the call for this. + // Remove the CSS files for this handler that aren't used anymore. Don't block the call for CoreSitePluginsInitializer. const files = await CoreUtils.ignoreErrors( - CoreFilepool.getFilesByComponent(site.getId(), CoreSitePluginsProvider.COMPONENT, componentId), + CoreFilepool.getFilesByComponent(site.getId(), CORE_SITE_PLUGINS_UPDATE_COURSE_CONTENT, componentId), ); files?.forEach((file) => { @@ -209,7 +201,7 @@ export class CoreSitePluginsHelperProvider { site.getId(), url, false, - CoreSitePluginsProvider.COMPONENT, + CORE_SITE_PLUGINS_UPDATE_COURSE_CONTENT, componentId, 0, undefined, @@ -230,7 +222,7 @@ export class CoreSitePluginsHelperProvider { * @returns Promise resolved when done. It returns the results of the getContent call and the data returned by * the init JS (if any). */ - protected async executeHandlerInit( + protected static async executeHandlerInit( plugin: CoreSitePluginsPlugin, handlerSchema: CoreSitePluginsHandlerData, ): Promise { @@ -238,7 +230,7 @@ export class CoreSitePluginsHelperProvider { return null; } - return this.executeMethodAndJS(plugin, handlerSchema.init, true); + return CoreSitePluginsInitializer.executeMethodAndJS(plugin, handlerSchema.init, true); } /** @@ -249,7 +241,7 @@ export class CoreSitePluginsHelperProvider { * @param isInit Whether it's the init method. * @returns Promise resolved with the results of the getContent call and the data returned by the JS (if any). */ - protected async executeMethodAndJS( + protected static async executeMethodAndJS( plugin: CoreSitePluginsPlugin, method: string, isInit?: boolean, @@ -270,7 +262,7 @@ export class CoreSitePluginsHelperProvider { // Create a "fake" instance to hold all the libraries. const instance = { // eslint-disable-next-line @typescript-eslint/naming-convention - HANDLER_DISABLED: HANDLER_DISABLED, + HANDLER_DISABLED: CoreSitePluginsInitializer.HANDLER_DISABLED, }; await CoreCompile.loadLibraries(); @@ -285,7 +277,7 @@ export class CoreSitePluginsHelperProvider { // Now execute the javascript using this instance. result.jsResult = CoreCompile.executeJavascript(instance, result.javascript); - if (result.jsResult == HANDLER_DISABLED) { + if (result.jsResult == CoreSitePluginsInitializer.HANDLER_DISABLED) { // The "disabled" field was added in 3.8, this is a workaround for previous versions. result.disabled = true; } @@ -299,7 +291,7 @@ export class CoreSitePluginsHelperProvider { * @param addon Name of the addon (plugin.addon). * @returns Prefix. */ - protected getPrefixForStrings(addon: string): string { + protected static getPrefixForStrings(addon: string): string { if (addon) { return 'plugin.' + addon + '.'; } @@ -311,11 +303,11 @@ export class CoreSitePluginsHelperProvider { * Given an addon name and the key of a string, return the full string key (prefixed). * * @param addon Name of the addon (plugin.addon). - * @param key The key of the string. + * @param key The key of the string. Defaults to pluginname. * @returns Full string key. */ - protected getPrefixedString(addon: string, key: string): string { - return this.getPrefixForStrings(addon) + key; + protected static getPrefixedString(addon: string, key = 'pluginname'): string { + return CoreSitePluginsInitializer.getPrefixForStrings(addon) + key; } /** @@ -323,13 +315,13 @@ export class CoreSitePluginsHelperProvider { * * @param plugin Data of the plugin. */ - loadLangStrings(plugin: CoreSitePluginsPlugin): void { + protected static loadLangStrings(plugin: CoreSitePluginsPlugin): void { if (!plugin.parsedLang) { return; } for (const lang in plugin.parsedLang) { - const prefix = this.getPrefixForStrings(plugin.addon); + const prefix = CoreSitePluginsInitializer.getPrefixForStrings(plugin.addon); CoreLang.addSitePluginsStrings(lang, plugin.parsedLang[lang], prefix); } @@ -339,16 +331,15 @@ export class CoreSitePluginsHelperProvider { * Load a site plugin. * * @param plugin Data of the plugin. - * @returns Promise resolved when loaded. */ - async loadSitePlugin(plugin: CoreSitePluginsPlugin): Promise { - this.logger.debug('Load site plugin:', plugin); + protected static async loadSitePlugin(plugin: CoreSitePluginsPlugin): Promise { + CoreSitePluginsInitializer.logger.debug('Load site plugin:', plugin); if (!plugin.parsedHandlers && plugin.handlers) { plugin.parsedHandlers = CoreTextUtils.parseJSON( plugin.handlers, null, - error => this.logger.error('Error parsing site plugin handlers', error), + error => CoreSitePluginsInitializer.logger.error('Error parsing site plugin handlers', error), ); } @@ -356,20 +347,20 @@ export class CoreSitePluginsHelperProvider { plugin.parsedLang = CoreTextUtils.parseJSON( plugin.lang, null, - error => this.logger.error('Error parsing site plugin lang', error), + error => CoreSitePluginsInitializer.logger.error('Error parsing site plugin lang', error), ); } CoreSitePlugins.setPluginsLoaded(true); // Register lang strings. - this.loadLangStrings(plugin); + CoreSitePluginsInitializer.loadLangStrings(plugin); if (plugin.parsedHandlers) { // Register all the handlers. const parsedHandlers = plugin.parsedHandlers; await CoreUtils.allPromises(Object.keys(parsedHandlers).map(async (name) => { - await this.registerHandler(plugin, name, parsedHandlers[name]); + await CoreSitePluginsInitializer.registerHandler(plugin, name, parsedHandlers[name]); })); } } @@ -378,13 +369,12 @@ export class CoreSitePluginsHelperProvider { * Load site plugins. * * @param plugins The plugins to load. - * @returns Promise resolved when loaded. */ - async loadSitePlugins(plugins: CoreSitePluginsPlugin[]): Promise { - this.courseRestrictHandlers = {}; + protected static async loadSitePlugins(plugins: CoreSitePluginsPlugin[]): Promise { + CoreSitePluginsInitializer.courseRestrictHandlers = {}; await CoreUtils.allPromises(plugins.map(async (plugin) => { - const pluginPromise = this.loadSitePlugin(plugin); + const pluginPromise = CoreSitePluginsInitializer.loadSitePlugin(plugin); CoreSitePlugins.registerSitePluginPromise(plugin.component, pluginPromise); await pluginPromise; @@ -401,7 +391,7 @@ export class CoreSitePluginsHelperProvider { * @param version Styles version. * @param siteId Site ID. If not provided, current site. */ - loadStyles( + protected static loadStyles( plugin: CoreSitePluginsPlugin, handlerName: string, fileUrl: string, @@ -436,7 +426,7 @@ export class CoreSitePluginsHelperProvider { // Styles have been loaded, now treat the CSS. CoreUtils.ignoreErrors( - CoreFilepool.treatCSSCode(siteId, fileUrl, cssCode, CoreSitePluginsProvider.COMPONENT, uniqueName, version), + CoreFilepool.treatCSSCode(siteId, fileUrl, cssCode, CORE_SITE_PLUGINS_UPDATE_COURSE_CONTENT, uniqueName, version), ); } @@ -446,9 +436,8 @@ export class CoreSitePluginsHelperProvider { * @param plugin Data of the plugin. * @param handlerName Name of the handler in the plugin. * @param handlerSchema Data about the handler. - * @returns Promise resolved when done. */ - async registerHandler( + protected static async registerHandler( plugin: CoreSitePluginsPlugin, handlerName: string, handlerSchema: CoreSitePluginsHandlerData, @@ -458,93 +447,146 @@ export class CoreSitePluginsHelperProvider { try { const [initResult, cssCode] = await Promise.all([ - this.executeHandlerInit(plugin, handlerSchema), - this.downloadStyles(plugin, handlerName, handlerSchema, siteId).catch((error) => { - this.logger.error('Error getting styles for plugin', handlerName, handlerSchema, error); + CoreSitePluginsInitializer.executeHandlerInit(plugin, handlerSchema), + CoreSitePluginsInitializer.downloadStyles(plugin, handlerName, handlerSchema, siteId).catch((error) => { + CoreSitePluginsInitializer.logger.error( + 'Error getting styles for plugin', + handlerName, + handlerSchema, + error, + ); }), ]); if (initResult?.disabled) { // This handler is disabled for the current user, stop. - this.logger.warn('Handler disabled by init function', plugin, handlerSchema); + CoreSitePluginsInitializer.logger.warn('Handler disabled by init function', plugin, handlerSchema); return; } if (cssCode && handlerSchema.styles?.url) { // Load the styles. - this.loadStyles(plugin, handlerName, handlerSchema.styles.url, cssCode, handlerSchema.styles.version, siteId); + CoreSitePluginsInitializer.loadStyles( + plugin, + handlerName, + handlerSchema.styles.url, + cssCode, + handlerSchema.styles.version, + siteId, + ); } let uniqueName: string | undefined; switch (handlerSchema.delegate) { case 'CoreMainMenuDelegate': - uniqueName = this.registerMainMenuHandler(plugin, handlerName, handlerSchema, initResult); + uniqueName = CoreSitePluginsInitializer.registerMainMenuHandler(plugin, handlerName, handlerSchema, initResult); break; case 'CoreCourseModuleDelegate': - uniqueName = this.registerModuleHandler(plugin, handlerName, handlerSchema, initResult); + uniqueName = CoreSitePluginsInitializer.registerModuleHandler(plugin, handlerName, handlerSchema, initResult); break; case 'CoreUserDelegate': - uniqueName = this.registerUserProfileHandler(plugin, handlerName, handlerSchema, initResult); + uniqueName = CoreSitePluginsInitializer.registerUserProfileHandler( + plugin, + handlerName, + handlerSchema, + initResult, + ); break; case 'CoreCourseOptionsDelegate': - uniqueName = this.registerCourseOptionHandler(plugin, handlerName, handlerSchema, initResult); + uniqueName = CoreSitePluginsInitializer.registerCourseOptionHandler( + plugin, + handlerName, + handlerSchema, + initResult, + ); break; case 'CoreCourseFormatDelegate': - uniqueName = this.registerCourseFormatHandler(plugin, handlerName, handlerSchema); + uniqueName = CoreSitePluginsInitializer.registerCourseFormatHandler(plugin, handlerName, handlerSchema); break; case 'CoreUserProfileFieldDelegate': - uniqueName = await this.registerUserProfileFieldHandler(plugin, handlerName, handlerSchema); + uniqueName = await CoreSitePluginsInitializer.registerUserProfileFieldHandler( + plugin, + handlerName, + handlerSchema, + ); break; case 'CoreSettingsDelegate': - uniqueName = this.registerSettingsHandler(plugin, handlerName, handlerSchema, initResult); + uniqueName = CoreSitePluginsInitializer.registerSettingsHandler(plugin, handlerName, handlerSchema, initResult); break; case 'CoreQuestionDelegate': - uniqueName = await this.registerQuestionHandler(plugin, handlerName, handlerSchema); + uniqueName = await CoreSitePluginsInitializer.registerQuestionHandler(plugin, handlerName, handlerSchema); break; case 'CoreQuestionBehaviourDelegate': - uniqueName = await this.registerQuestionBehaviourHandler(plugin, handlerName, handlerSchema); + uniqueName = await CoreSitePluginsInitializer.registerQuestionBehaviourHandler( + plugin, + handlerName, + handlerSchema, + ); break; case 'CoreBlockDelegate': - uniqueName = this.registerBlockHandler(plugin, handlerName, handlerSchema, initResult); + uniqueName = CoreSitePluginsInitializer.registerBlockHandler(plugin, handlerName, handlerSchema, initResult); break; case 'AddonMessageOutputDelegate': - uniqueName = this.registerMessageOutputHandler(plugin, handlerName, handlerSchema, initResult); + uniqueName = CoreSitePluginsInitializer.registerMessageOutputHandler( +plugin, + handlerName, + handlerSchema, + initResult, +); break; case 'AddonModQuizAccessRuleDelegate': - uniqueName = await this.registerQuizAccessRuleHandler(plugin, handlerName, handlerSchema); + uniqueName = await CoreSitePluginsInitializer.registerQuizAccessRuleHandler(plugin, handlerName, handlerSchema); break; case 'AddonModAssignFeedbackDelegate': - uniqueName = await this.registerAssignFeedbackHandler(plugin, handlerName, handlerSchema); + uniqueName = await CoreSitePluginsInitializer.registerAssignFeedbackHandler(plugin, handlerName, handlerSchema); break; case 'AddonModAssignSubmissionDelegate': - uniqueName = await this.registerAssignSubmissionHandler(plugin, handlerName, handlerSchema); + uniqueName = await CoreSitePluginsInitializer.registerAssignSubmissionHandler( + plugin, + handlerName, + handlerSchema, + ); break; case 'AddonWorkshopAssessmentStrategyDelegate': - uniqueName = await this.registerWorkshopAssessmentStrategyHandler(plugin, handlerName, handlerSchema); + uniqueName = await CoreSitePluginsInitializer.registerWorkshopAssessmentStrategyHandler( + plugin, + handlerName, + handlerSchema, + ); break; case 'CoreMainMenuHomeDelegate': - uniqueName = this.registerMainMenuHomeHandler(plugin, handlerName, handlerSchema, initResult); + uniqueName = CoreSitePluginsInitializer.registerMainMenuHomeHandler( + plugin, + handlerName, + handlerSchema, + initResult, + ); break; case 'CoreEnrolDelegate': - uniqueName = await this.registerEnrolHandler(plugin, handlerName, handlerSchema, initResult); + uniqueName = await CoreSitePluginsInitializer.registerEnrolHandler( + plugin, + handlerName, + handlerSchema, + initResult, + ); break; default: @@ -575,7 +617,7 @@ export class CoreSitePluginsHelperProvider { * @param handlerSchema Data about the handler. * @returns A promise resolved with a string to identify the handler. */ - protected async registerComponentInitHandler( + protected static async registerComponentInitHandler( plugin: CoreSitePluginsPlugin, handlerName: string, handlerSchema: CoreSitePluginsInitHandlerData, @@ -585,16 +627,20 @@ export class CoreSitePluginsHelperProvider { if (!handlerSchema.method) { // Required data not provided, stop. - this.logger.warn('Ignore site plugin because it doesn\'t provide method', plugin, handlerSchema); + CoreSitePluginsInitializer.logger.warn( + 'Ignore site plugin because it doesn\'t provide method', + plugin, + handlerSchema, + ); return; } - this.logger.debug('Register site plugin', plugin, handlerSchema); + CoreSitePluginsInitializer.logger.debug('Register site plugin', plugin, handlerSchema); try { // Execute the main method and its JS. The template returned will be used in the right component. - const result = await this.executeMethodAndJS(plugin, handlerSchema.method); + const result = await CoreSitePluginsInitializer.executeMethodAndJS(plugin, handlerSchema.method); // Create and register the handler. const uniqueName = CoreSitePlugins.getHandlerUniqueName(plugin, handlerName); @@ -622,7 +668,12 @@ export class CoreSitePluginsHelperProvider { return uniqueName; } catch (error) { - this.logger.error('Error executing main method', plugin.component, handlerSchema.method, error); + CoreSitePluginsInitializer.logger.error( + 'Error executing main method', + plugin.component, + handlerSchema.method, + error, + ); } } @@ -634,20 +685,20 @@ export class CoreSitePluginsHelperProvider { * @param handlerSchema Data about the handler. * @returns Promise resolved with a string to identify the handler. */ - protected registerAssignFeedbackHandler( + protected static registerAssignFeedbackHandler( plugin: CoreSitePluginsPlugin, handlerName: string, handlerSchema: CoreSitePluginsHandlerCommonData, ): Promise { - return this.registerComponentInitHandler( + return CoreSitePluginsInitializer.registerComponentInitHandler( plugin, handlerName, handlerSchema, AddonModAssignFeedbackDelegate.instance, (uniqueName) => { const type = (handlerSchema.moodlecomponent || plugin.component).replace('assignfeedback_', ''); - const prefix = this.getPrefixForStrings(plugin.addon); + const prefix = CoreSitePluginsInitializer.getPrefixForStrings(plugin.addon); return new CoreSitePluginsAssignFeedbackHandler(uniqueName, type, prefix); }, @@ -662,20 +713,20 @@ export class CoreSitePluginsHelperProvider { * @param handlerSchema Data about the handler. * @returns Promise resolved with a string to identify the handler. */ - protected registerAssignSubmissionHandler( + protected static registerAssignSubmissionHandler( plugin: CoreSitePluginsPlugin, handlerName: string, handlerSchema: CoreSitePluginsHandlerCommonData, ): Promise { - return this.registerComponentInitHandler( + return CoreSitePluginsInitializer.registerComponentInitHandler( plugin, handlerName, handlerSchema, AddonModAssignSubmissionDelegate.instance, (uniqueName) => { const type = (handlerSchema.moodlecomponent || plugin.component).replace('assignsubmission_', ''); - const prefix = this.getPrefixForStrings(plugin.addon); + const prefix = CoreSitePluginsInitializer.getPrefixForStrings(plugin.addon); return new CoreSitePluginsAssignSubmissionHandler(uniqueName, type, prefix); }, @@ -691,7 +742,7 @@ export class CoreSitePluginsHelperProvider { * @param initResult Result of init function. * @returns A string to identify the handler. */ - protected registerBlockHandler( + protected static registerBlockHandler( plugin: CoreSitePluginsPlugin, handlerName: string, handlerSchema: CoreSitePluginsBlockHandlerData, @@ -700,8 +751,8 @@ export class CoreSitePluginsHelperProvider { const uniqueName = CoreSitePlugins.getHandlerUniqueName(plugin, handlerName); const blockName = (handlerSchema.moodlecomponent || plugin.component).replace('block_', ''); - const titleString = handlerSchema.displaydata?.title ?? 'pluginname'; - const prefixedTitle = this.getPrefixedString(plugin.addon, titleString); + const titleString = handlerSchema.displaydata?.title; + const prefixedTitle = CoreSitePluginsInitializer.getPrefixedString(plugin.addon, titleString); CoreBlockDelegate.registerHandler( new CoreSitePluginsBlockHandler(uniqueName, prefixedTitle, blockName, handlerSchema, initResult), @@ -718,12 +769,12 @@ export class CoreSitePluginsHelperProvider { * @param handlerSchema Data about the handler. * @returns A string to identify the handler. */ - protected registerCourseFormatHandler( + protected static registerCourseFormatHandler( plugin: CoreSitePluginsPlugin, handlerName: string, handlerSchema: CoreSitePluginsCourseFormatHandlerData, ): string { - this.logger.debug('Register site plugin in course format delegate:', plugin, handlerSchema); + CoreSitePluginsInitializer.logger.debug('Register site plugin in course format delegate:', plugin, handlerSchema); // Create and register the handler. const uniqueName = CoreSitePlugins.getHandlerUniqueName(plugin, handlerName); @@ -744,7 +795,7 @@ export class CoreSitePluginsHelperProvider { * @param initResult Result of the init WS call. * @returns A string to identify the handler. */ - protected registerCourseOptionHandler( + protected static registerCourseOptionHandler( plugin: CoreSitePluginsPlugin, handlerName: string, handlerSchema: CoreSitePluginsCourseOptionHandlerData, @@ -752,16 +803,28 @@ export class CoreSitePluginsHelperProvider { ): string | undefined { if (!handlerSchema.displaydata) { // Required data not provided, stop. - this.logger.warn('Ignore site plugin because it doesn\'t provide displaydata', plugin, handlerSchema); + CoreSitePluginsInitializer.logger.warn( + 'Ignore site plugin because it doesn\'t provide displaydata', + plugin, + handlerSchema, + ); return; } - this.logger.debug('Register site plugin in course option delegate:', plugin, handlerSchema, initResult); + CoreSitePluginsInitializer.logger.debug( + 'Register site plugin in course option delegate:', + plugin, + handlerSchema, + initResult, + ); // Create and register the handler. const uniqueName = CoreSitePlugins.getHandlerUniqueName(plugin, handlerName); - const prefixedTitle = this.getPrefixedString(plugin.addon, handlerSchema.displaydata.title || 'pluginname'); + const prefixedTitle = CoreSitePluginsInitializer.getPrefixedString( + plugin.addon, + handlerSchema.displaydata.title, + ); const handler = new CoreSitePluginsCourseOptionHandler( uniqueName, prefixedTitle, @@ -774,7 +837,7 @@ export class CoreSitePluginsHelperProvider { if (initResult?.restrict?.courses) { // This handler is restricted to certan courses, store it in the list. - this.courseRestrictHandlers[uniqueName] = { + CoreSitePluginsInitializer.courseRestrictHandlers[uniqueName] = { plugin, handlerName, handlerSchema, @@ -794,7 +857,7 @@ export class CoreSitePluginsHelperProvider { * @param initResult Result of init function. * @returns A string to identify the handler. */ - protected async registerEnrolHandler( + protected static async registerEnrolHandler( plugin: CoreSitePluginsPlugin, handlerName: string, handlerSchema: CoreSitePluginsEnrolHandlerData, @@ -806,24 +869,30 @@ export class CoreSitePluginsHelperProvider { const handler = new CoreSitePluginsEnrolHandler(uniqueName, type, action, handlerSchema, initResult); if (!handlerSchema.method && (action === CoreEnrolAction.SELF || action === CoreEnrolAction.GUEST)) { - this.logger.error('"self" or "guest" enrol plugins must implement a method to override the required JS functions.'); + CoreSitePluginsInitializer.logger.error( + '"self" or "guest" enrol plugins must implement a method to override the required JS functions.', + ); return; } if (handlerSchema.method) { // Execute the main method and its JS to allow implementing the handler functions. - const result = await this.executeMethodAndJS(plugin, handlerSchema.method); + const result = await CoreSitePluginsInitializer.executeMethodAndJS(plugin, handlerSchema.method); if (action === CoreEnrolAction.SELF && !result.jsResult?.enrol) { - this.logger.error('"self" enrol plugins must implement an "enrol" function in the JS returned by the method.'); + CoreSitePluginsInitializer.logger.error( + '"self" enrol plugins must implement an "enrol" function in the JS returned by the method.', + ); return; } if (action === CoreEnrolAction.GUEST && (!result.jsResult?.canAccess || !result.jsResult?.validateAccess)) { - this.logger.error('"guest" enrol plugins must implement "canAccess" and "validateAccess" functions in the JS ' + - 'returned by the method.'); + CoreSitePluginsInitializer.logger.error( + '"guest" enrol plugins must implement "canAccess" and "validateAccess" functions in the JS ' + + 'returned by the method.', +); return; } @@ -856,7 +925,7 @@ export class CoreSitePluginsHelperProvider { * @param initResult Result of the init WS call. * @returns A string to identify the handler. */ - protected registerMainMenuHandler( + protected static registerMainMenuHandler( plugin: CoreSitePluginsPlugin, handlerName: string, handlerSchema: CoreSitePluginsMainMenuHandlerData, @@ -864,16 +933,28 @@ export class CoreSitePluginsHelperProvider { ): string | undefined { if (!handlerSchema.displaydata) { // Required data not provided, stop. - this.logger.warn('Ignore site plugin because it doesn\'t provide displaydata', plugin, handlerSchema); + CoreSitePluginsInitializer.logger.warn( + 'Ignore site plugin because it doesn\'t provide displaydata', + plugin, + handlerSchema, + ); return; } - this.logger.debug('Register site plugin in main menu delegate:', plugin, handlerSchema, initResult); + CoreSitePluginsInitializer.logger.debug( + 'Register site plugin in main menu delegate:', + plugin, + handlerSchema, + initResult, + ); // Create and register the handler. const uniqueName = CoreSitePlugins.getHandlerUniqueName(plugin, handlerName); - const prefixedTitle = this.getPrefixedString(plugin.addon, handlerSchema.displaydata.title || 'pluginname'); + const prefixedTitle = CoreSitePluginsInitializer.getPrefixedString( + plugin.addon, + handlerSchema.displaydata.title, + ); CoreMainMenuDelegate.registerHandler( new CoreSitePluginsMainMenuHandler(uniqueName, prefixedTitle, plugin, handlerSchema, initResult), @@ -891,7 +972,7 @@ export class CoreSitePluginsHelperProvider { * @param initResult Result of the init WS call. * @returns A string to identify the handler. */ - protected registerMessageOutputHandler( + protected static registerMessageOutputHandler( plugin: CoreSitePluginsPlugin, handlerName: string, handlerSchema: CoreSitePluginsMessageOutputHandlerData, @@ -899,16 +980,28 @@ export class CoreSitePluginsHelperProvider { ): string | undefined { if (!handlerSchema.displaydata) { // Required data not provided, stop. - this.logger.warn('Ignore site plugin because it doesn\'t provide displaydata', plugin, handlerSchema); + CoreSitePluginsInitializer.logger.warn( + 'Ignore site plugin because it doesn\'t provide displaydata', + plugin, + handlerSchema, + ); return; } - this.logger.debug('Register site plugin in message output delegate:', plugin, handlerSchema, initResult); + CoreSitePluginsInitializer.logger.debug( + 'Register site plugin in message output delegate:', + plugin, + handlerSchema, + initResult, + ); // Create and register the handler. const uniqueName = CoreSitePlugins.getHandlerUniqueName(plugin, handlerName); - const prefixedTitle = this.getPrefixedString(plugin.addon, handlerSchema.displaydata.title || 'pluginname'); + const prefixedTitle = CoreSitePluginsInitializer.getPrefixedString( + plugin.addon, + handlerSchema.displaydata.title, + ); const processorName = (handlerSchema.moodlecomponent || plugin.component).replace('message_', ''); AddonMessageOutputDelegate.registerHandler( @@ -927,7 +1020,7 @@ export class CoreSitePluginsHelperProvider { * @param initResult Result of the init WS call. * @returns A string to identify the handler. */ - protected registerModuleHandler( + protected static registerModuleHandler( plugin: CoreSitePluginsPlugin, handlerName: string, handlerSchema: CoreSitePluginsCourseModuleHandlerData, @@ -935,12 +1028,16 @@ export class CoreSitePluginsHelperProvider { ): string | undefined { if (!handlerSchema.displaydata) { // Required data not provided, stop. - this.logger.warn('Ignore site plugin because it doesn\'t provide displaydata', plugin, handlerSchema); + CoreSitePluginsInitializer.logger.warn( + 'Ignore site plugin because it doesn\'t provide displaydata', + plugin, + handlerSchema, + ); return; } - this.logger.debug('Register site plugin in module delegate:', plugin, handlerSchema, initResult); + CoreSitePluginsInitializer.logger.debug('Register site plugin in module delegate:', plugin, handlerSchema, initResult); // Create and register the handler. const uniqueName = CoreSitePlugins.getHandlerUniqueName(plugin, handlerName); @@ -981,14 +1078,14 @@ export class CoreSitePluginsHelperProvider { * @param handlerSchema Data about the handler. * @returns Promise resolved with a string to identify the handler. */ - protected registerQuestionHandler( + protected static registerQuestionHandler( plugin: CoreSitePluginsPlugin, handlerName: string, handlerSchema: CoreSitePluginsHandlerCommonData, ): Promise { const component = handlerSchema.moodlecomponent || plugin.component; - return this.registerComponentInitHandler( + return CoreSitePluginsInitializer.registerComponentInitHandler( plugin, handlerName, handlerSchema, @@ -1005,13 +1102,13 @@ export class CoreSitePluginsHelperProvider { * @param handlerSchema Data about the handler. * @returns Promise resolved with a string to identify the handler. */ - protected registerQuestionBehaviourHandler( + protected static registerQuestionBehaviourHandler( plugin: CoreSitePluginsPlugin, handlerName: string, handlerSchema: CoreSitePluginsHandlerCommonData, ): Promise { - return this.registerComponentInitHandler( + return CoreSitePluginsInitializer.registerComponentInitHandler( plugin, handlerName, handlerSchema, @@ -1032,14 +1129,14 @@ export class CoreSitePluginsHelperProvider { * @param handlerSchema Data about the handler. * @returns Promise resolved with a string to identify the handler. */ - protected registerQuizAccessRuleHandler( + protected static registerQuizAccessRuleHandler( plugin: CoreSitePluginsPlugin, handlerName: string, handlerSchema: CoreSitePluginsHandlerCommonData, ): Promise { const component = handlerSchema.moodlecomponent || plugin.component; - return this.registerComponentInitHandler( + return CoreSitePluginsInitializer.registerComponentInitHandler( plugin, handlerName, handlerSchema, @@ -1057,7 +1154,7 @@ export class CoreSitePluginsHelperProvider { * @param initResult Result of the init WS call. * @returns A string to identify the handler. */ - protected registerSettingsHandler( + protected static registerSettingsHandler( plugin: CoreSitePluginsPlugin, handlerName: string, handlerSchema: CoreSitePluginsSettingsHandlerData, @@ -1065,16 +1162,20 @@ export class CoreSitePluginsHelperProvider { ): string | undefined { if (!handlerSchema.displaydata) { // Required data not provided, stop. - this.logger.warn('Ignore site plugin because it doesn\'t provide displaydata', plugin, handlerSchema); + CoreSitePluginsInitializer.logger.warn( + 'Ignore site plugin because it doesn\'t provide displaydata', + plugin, + handlerSchema, + ); return; } - this.logger.debug('Register site plugin in settings delegate:', plugin, handlerSchema, initResult); + CoreSitePluginsInitializer.logger.debug('Register site plugin in settings delegate:', plugin, handlerSchema, initResult); // Create and register the handler. const uniqueName = CoreSitePlugins.getHandlerUniqueName(plugin, handlerName); - const prefixedTitle = this.getPrefixedString(plugin.addon, handlerSchema.displaydata.title || 'pluginname'); + const prefixedTitle = CoreSitePluginsInitializer.getPrefixedString(plugin.addon, handlerSchema.displaydata.title); CoreSettingsDelegate.registerHandler( new CoreSitePluginsSettingsHandler(uniqueName, prefixedTitle, plugin, handlerSchema, initResult), @@ -1092,7 +1193,7 @@ export class CoreSitePluginsHelperProvider { * @param initResult Result of the init WS call. * @returns A string to identify the handler. */ - protected registerUserProfileHandler( + protected static registerUserProfileHandler( plugin: CoreSitePluginsPlugin, handlerName: string, handlerSchema: CoreSitePluginsUserHandlerData, @@ -1100,23 +1201,32 @@ export class CoreSitePluginsHelperProvider { ): string | undefined { if (!handlerSchema.displaydata) { // Required data not provided, stop. - this.logger.warn('Ignore site plugin because it doesn\'t provide displaydata', plugin, handlerSchema); + CoreSitePluginsInitializer.logger.warn( + 'Ignore site plugin because it doesn\'t provide displaydata', + plugin, + handlerSchema, + ); return; } - this.logger.debug('Register site plugin in user profile delegate:', plugin, handlerSchema, initResult); + CoreSitePluginsInitializer.logger.debug( + 'Register site plugin in user profile delegate:', + plugin, + handlerSchema, + initResult, + ); // Create and register the handler. const uniqueName = CoreSitePlugins.getHandlerUniqueName(plugin, handlerName); - const prefixedTitle = this.getPrefixedString(plugin.addon, handlerSchema.displaydata.title || 'pluginname'); + const prefixedTitle = CoreSitePluginsInitializer.getPrefixedString(plugin.addon, handlerSchema.displaydata.title); const handler = new CoreSitePluginsUserProfileHandler(uniqueName, prefixedTitle, plugin, handlerSchema, initResult); CoreUserDelegate.registerHandler(handler); if (initResult && initResult.restrict && initResult.restrict.courses) { // This handler is restricted to certan courses, store it in the list. - this.courseRestrictHandlers[uniqueName] = { + CoreSitePluginsInitializer.courseRestrictHandlers[uniqueName] = { plugin, handlerName, handlerSchema, @@ -1135,13 +1245,13 @@ export class CoreSitePluginsHelperProvider { * @param handlerSchema Data about the handler. * @returns Promise resolved with a string to identify the handler. */ - protected registerUserProfileFieldHandler( + protected static registerUserProfileFieldHandler( plugin: CoreSitePluginsPlugin, handlerName: string, handlerSchema: CoreSitePluginsHandlerCommonData, ): Promise { - return this.registerComponentInitHandler( + return CoreSitePluginsInitializer.registerComponentInitHandler( plugin, handlerName, handlerSchema, @@ -1162,12 +1272,12 @@ export class CoreSitePluginsHelperProvider { * @param handlerSchema Data about the handler. * @returns Promise resolved with a string to identify the handler. */ - protected registerWorkshopAssessmentStrategyHandler( + protected static registerWorkshopAssessmentStrategyHandler( plugin: CoreSitePluginsPlugin, handlerName: string, handlerSchema: CoreSitePluginsHandlerCommonData, ): Promise { - return this.registerComponentInitHandler( + return CoreSitePluginsInitializer.registerComponentInitHandler( plugin, handlerName, handlerSchema, @@ -1182,17 +1292,15 @@ export class CoreSitePluginsHelperProvider { /** * Reload the handlers that are restricted to certain courses. - * - * @returns Promise resolved when done. */ - protected async reloadCourseRestrictHandlers(): Promise { - if (!Object.keys(this.courseRestrictHandlers).length) { + protected static async reloadCourseRestrictHandlers(): Promise { + if (!Object.keys(CoreSitePluginsInitializer.courseRestrictHandlers).length) { // No course restrict handlers, nothing to do. return; } - await Promise.all(Object.keys(this.courseRestrictHandlers).map(async (name) => { - const data = this.courseRestrictHandlers[name]; + await Promise.all(Object.keys(CoreSitePluginsInitializer.courseRestrictHandlers).map(async (name) => { + const data = CoreSitePluginsInitializer.courseRestrictHandlers[name]; if (!data.handler || !data.handler.setInitResult) { // No handler or it doesn't implement a required function, ignore it. @@ -1203,11 +1311,11 @@ export class CoreSitePluginsHelperProvider { data.handler.updatingInit && data.handler.updatingInit(); try { - const initResult = await this.executeHandlerInit(data.plugin, data.handlerSchema); + const initResult = await CoreSitePluginsInitializer.executeHandlerInit(data.plugin, data.handlerSchema); data.handler.setInitResult(initResult); } catch (error) { - this.logger.error('Error reloading course restrict handler', error, data.plugin); + CoreSitePluginsInitializer.logger.error('Error reloading course restrict handler', error, data.plugin); } })); @@ -1223,7 +1331,7 @@ export class CoreSitePluginsHelperProvider { * @param initResult Result of the init WS call. * @returns A string to identify the handler. */ - protected registerMainMenuHomeHandler( + protected static registerMainMenuHomeHandler( plugin: CoreSitePluginsPlugin, handlerName: string, handlerSchema: CoreSitePluginsMainMenuHomeHandlerData, @@ -1231,16 +1339,25 @@ export class CoreSitePluginsHelperProvider { ): string | undefined { if (!handlerSchema.displaydata) { // Required data not provided, stop. - this.logger.warn('Ignore site plugin because it doesn\'t provide displaydata', plugin, handlerSchema); + CoreSitePluginsInitializer.logger.warn( + 'Ignore site plugin because it doesn\'t provide displaydata', + plugin, + handlerSchema, + ); return; } - this.logger.debug('Register site plugin in main menu home delegate:', plugin, handlerSchema, initResult); + CoreSitePluginsInitializer.logger.debug( + 'Register site plugin in main menu home delegate:', + plugin, + handlerSchema, + initResult, + ); // Create and register the handler. const uniqueName = CoreSitePlugins.getHandlerUniqueName(plugin, handlerName); - const prefixedTitle = this.getPrefixedString(plugin.addon, handlerSchema.displaydata.title || 'pluginname'); + const prefixedTitle = CoreSitePluginsInitializer.getPrefixedString(plugin.addon, handlerSchema.displaydata.title); CoreMainMenuHomeDelegate.registerHandler( new CoreSitePluginsMainMenuHomeHandler(uniqueName, prefixedTitle, plugin, handlerSchema, initResult), @@ -1250,5 +1367,3 @@ export class CoreSitePluginsHelperProvider { } } - -export const CoreSitePluginsHelper = makeSingleton(CoreSitePluginsHelperProvider); diff --git a/src/core/features/siteplugins/services/siteplugins.ts b/src/core/features/siteplugins/services/siteplugins.ts index 014e0faa8c9..30deff990c6 100644 --- a/src/core/features/siteplugins/services/siteplugins.ts +++ b/src/core/features/siteplugins/services/siteplugins.ts @@ -34,9 +34,7 @@ import { CorePlatform } from '@services/platform'; import { CoreEnrolAction, CoreEnrolInfoIcon } from '@features/enrol/services/enrol-delegate'; import { CoreSiteWSPreSets } from '@classes/sites/authenticated-site'; import { CoreUserProfileHandlerType } from '@features/user/services/user-delegate'; -import { CORE_SITE_PLUGINS_UPDATE_COURSE_CONTENT } from '../constants'; - -const ROOT_CACHE_KEY = 'CoreSitePlugins:'; +import { CORE_SITE_PLUGINS_COMPONENT, CORE_SITE_PLUGINS_UPDATE_COURSE_CONTENT } from '../constants'; /** * Service to provide functionalities regarding site plugins. @@ -44,7 +42,14 @@ const ROOT_CACHE_KEY = 'CoreSitePlugins:'; @Injectable({ providedIn: 'root' }) export class CoreSitePluginsProvider { - static readonly COMPONENT = 'CoreSitePlugins'; + protected static readonly ROOT_CACHE_KEY = 'CoreSitePlugins:'; + /** + * @deprecated since 4.5.0. Use CORE_SITE_PLUGINS_COMPONENT instead. + */ + static readonly COMPONENT = CORE_SITE_PLUGINS_COMPONENT; + /** + * @deprecated since 4.5.0. Use CORE_SITE_PLUGINS_UPDATE_COURSE_CONTENT instead. + */ static readonly UPDATE_COURSE_CONTENT = CORE_SITE_PLUGINS_UPDATE_COURSE_CONTENT; protected logger: CoreLogger; @@ -183,7 +188,7 @@ export class CoreSitePluginsProvider { * @returns Cache key. */ protected getCallWSCommonCacheKey(method: string): string { - return ROOT_CACHE_KEY + 'ws:' + method; + return CoreSitePluginsProvider.ROOT_CACHE_KEY + 'ws:' + method; } /** @@ -250,7 +255,8 @@ export class CoreSitePluginsProvider { * @returns Cache key. */ protected getContentCacheKey(component: string, method: string, args: Record): string { - return ROOT_CACHE_KEY + 'content:' + component + ':' + method + ':' + CoreUtils.sortAndStringify(args); + return CoreSitePluginsProvider.ROOT_CACHE_KEY + 'content:' + component + ':' + method + + ':' + CoreUtils.sortAndStringify(args); } /** @@ -322,7 +328,7 @@ export class CoreSitePluginsProvider { * @returns Cache key. */ protected getPluginsCacheKey(): string { - return ROOT_CACHE_KEY + 'plugins'; + return CoreSitePluginsProvider.ROOT_CACHE_KEY + 'plugins'; } /** diff --git a/src/core/features/siteplugins/siteplugins.module.ts b/src/core/features/siteplugins/siteplugins.module.ts index 8a58ea4bbb0..2646ba42846 100644 --- a/src/core/features/siteplugins/siteplugins.module.ts +++ b/src/core/features/siteplugins/siteplugins.module.ts @@ -20,12 +20,13 @@ import { CoreMainMenuTabRoutingModule } from '@features/mainmenu/mainmenu-tab-ro import { CoreMainMenuHomeRoutingModule } from '@features/mainmenu/mainmenu-home-routing.module'; import { CoreSitePreferencesRoutingModule } from '@features/settings/settings-site-routing.module'; import { CoreSitePluginsComponentsModule } from './components/components.module'; -import { CoreSitePluginsHelper } from './services/siteplugins-helper'; +import { CoreSitePluginsInitializer } from './services/siteplugins-init'; import { CoreSharedModule } from '@/core/shared.module'; import { CoreSitePluginsPluginPage } from '@features/siteplugins/pages/plugin/plugin'; import { canLeaveGuard } from '@guards/can-leave'; import { CoreSitePluginsCourseOptionPage } from '@features/siteplugins/pages/course-option/course-option'; import { CoreSitePluginsModuleIndexPage } from '@features/siteplugins/pages/module-index/module-index'; +import { CORE_SITE_PLUGINS_PATH } from './constants'; /** * Get site plugins exported objects. @@ -65,7 +66,7 @@ export async function getSitePluginsExportedObjects(): Promise { - CoreSitePluginsHelper.initialize(); + CoreSitePluginsInitializer.init(); }, }, ],