From 1071e9208ec242bcc8f1225bdb40ec35135b76fc Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 16 May 2019 12:59:55 +0200 Subject: [PATCH] MOBILE-3033 core: Fix collisions in site plugin stored data --- src/classes/delegate.ts | 17 +++++++++++++++++ src/core/block/providers/delegate.ts | 14 -------------- .../assign-feedback/assign-feedback.ts | 6 ++++-- .../assign-submission/assign-submission.ts | 6 ++++-- .../components/course-format/course-format.ts | 8 ++++++-- .../components/module-index/module-index.ts | 9 ++++++--- .../question-behaviour/question-behaviour.ts | 6 ++++-- .../siteplugins/components/question/question.ts | 6 ++++-- .../quiz-access-rule/quiz-access-rule.ts | 6 ++++-- .../user-profile-field/user-profile-field.ts | 6 ++++-- .../workshop-assessment-strategy.ts | 6 ++++-- src/core/siteplugins/providers/helper.ts | 8 ++++---- 12 files changed, 61 insertions(+), 37 deletions(-) diff --git a/src/classes/delegate.ts b/src/classes/delegate.ts index db6726933..5710cbdd9 100644 --- a/src/classes/delegate.ts +++ b/src/classes/delegate.ts @@ -163,6 +163,23 @@ export class CoreDelegate { return enabled ? this.enabledHandlers[handlerName] : this.handlers[handlerName]; } + /** + * Gets the handler full name for a given name. This is useful when the handlerNameProperty is different than "name". + * E.g. blocks are indexed by blockName. If you call this function passing the blockName it will return the name. + * + * @param {string} name Name used to indentify the handler. + * @return {string} Full name of corresponding handler. + */ + getHandlerName(name: string): string { + const handler = this.getHandler(name, true); + + if (!handler) { + return ''; + } + + return handler.name; + } + /** * Check if function exists on a handler. * diff --git a/src/core/block/providers/delegate.ts b/src/core/block/providers/delegate.ts index a319d9b06..47b90ecda 100644 --- a/src/core/block/providers/delegate.ts +++ b/src/core/block/providers/delegate.ts @@ -163,20 +163,6 @@ export class CoreBlockDelegate extends CoreDelegate { return this.areBlocksDisabledInSite(site) || super.isFeatureDisabled(handler, site); } - /** - * Gets the handler name for a given block name. - * - * @param {string} name Block name e.g. 'activity_modules' - * @return {string} Full name of corresponding handler - */ - getHandlerName(name: string): string { - if (!this.isBlockSupported(name)) { - return ''; - } - - return this.getHandler(name, true).name; - } - /** * Called when there are new block handlers available. Informs anyone who subscribed to the * observable. diff --git a/src/core/siteplugins/components/assign-feedback/assign-feedback.ts b/src/core/siteplugins/components/assign-feedback/assign-feedback.ts index b8e1d8dcb..4fc0fe2eb 100644 --- a/src/core/siteplugins/components/assign-feedback/assign-feedback.ts +++ b/src/core/siteplugins/components/assign-feedback/assign-feedback.ts @@ -16,6 +16,7 @@ import { Component, OnInit, Input } from '@angular/core'; import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreSitePluginsProvider } from '../../providers/siteplugins'; import { CoreSitePluginsCompileInitComponent } from '../../classes/compile-init-component'; +import { AddonModAssignFeedbackDelegate } from '@addon/mod/assign/providers/feedback-delegate'; /** * Component that displays an assign feedback plugin created using a site plugin. @@ -33,7 +34,8 @@ export class CoreSitePluginsAssignFeedbackComponent extends CoreSitePluginsCompi @Input() canEdit: boolean; // Whether the user can edit. @Input() edit: boolean; // Whether the user is editing. - constructor(sitePluginsProvider: CoreSitePluginsProvider, utils: CoreUtilsProvider) { + constructor(sitePluginsProvider: CoreSitePluginsProvider, utils: CoreUtilsProvider, + protected assignFeedbackDelegate: AddonModAssignFeedbackDelegate) { super(sitePluginsProvider, utils); } @@ -51,7 +53,7 @@ export class CoreSitePluginsAssignFeedbackComponent extends CoreSitePluginsCompi this.jsData.canEdit = this.canEdit; if (this.plugin) { - this.getHandlerData('assignfeedback_' + this.plugin.type); + this.getHandlerData(this.assignFeedbackDelegate.getHandlerName(this.plugin.type)); } } diff --git a/src/core/siteplugins/components/assign-submission/assign-submission.ts b/src/core/siteplugins/components/assign-submission/assign-submission.ts index cd7af884e..f034caf41 100644 --- a/src/core/siteplugins/components/assign-submission/assign-submission.ts +++ b/src/core/siteplugins/components/assign-submission/assign-submission.ts @@ -16,6 +16,7 @@ import { Component, OnInit, Input } from '@angular/core'; import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreSitePluginsProvider } from '../../providers/siteplugins'; import { CoreSitePluginsCompileInitComponent } from '../../classes/compile-init-component'; +import { AddonModAssignSubmissionDelegate } from '@addon/mod/assign/providers/submission-delegate'; /** * Component that displays an assign submission plugin created using a site plugin. @@ -32,7 +33,8 @@ export class CoreSitePluginsAssignSubmissionComponent extends CoreSitePluginsCom @Input() edit: boolean; // Whether the user is editing. @Input() allowOffline: boolean; // Whether to allow offline. - constructor(sitePluginsProvider: CoreSitePluginsProvider, utils: CoreUtilsProvider) { + constructor(sitePluginsProvider: CoreSitePluginsProvider, utils: CoreUtilsProvider, + protected assignSubmissionDelegate: AddonModAssignSubmissionDelegate) { super(sitePluginsProvider, utils); } @@ -49,7 +51,7 @@ export class CoreSitePluginsAssignSubmissionComponent extends CoreSitePluginsCom this.jsData.allowOffline = this.allowOffline; if (this.plugin) { - this.getHandlerData('assignsubmission_' + this.plugin.type); + this.getHandlerData(this.assignSubmissionDelegate.getHandlerName(this.plugin.type)); } } diff --git a/src/core/siteplugins/components/course-format/course-format.ts b/src/core/siteplugins/components/course-format/course-format.ts index 9673a7a26..3f4407c6e 100644 --- a/src/core/siteplugins/components/course-format/course-format.ts +++ b/src/core/siteplugins/components/course-format/course-format.ts @@ -16,6 +16,7 @@ import { Component, OnChanges, Input, ViewChild, Output, EventEmitter } from '@a import { CoreSitePluginsProvider } from '../../providers/siteplugins'; import { CoreSitePluginsPluginContentComponent } from '../plugin-content/plugin-content'; import { CoreCourseFormatComponent } from '@core/course/components/format/format'; +import { CoreCourseFormatDelegate } from '@core/course/providers/format-delegate'; /** * Component that displays the index of a course format site plugin. @@ -46,7 +47,8 @@ export class CoreSitePluginsCourseFormatComponent implements OnChanges { initResult: any; data: any; - constructor(protected sitePluginsProvider: CoreSitePluginsProvider) { } + constructor(protected sitePluginsProvider: CoreSitePluginsProvider, + protected courseFormatDelegate: CoreCourseFormatDelegate) { } /** * Detect changes on input properties. @@ -55,7 +57,9 @@ export class CoreSitePluginsCourseFormatComponent implements OnChanges { if (this.course && this.course.format) { if (!this.component) { // Initialize the data. - const handler = this.sitePluginsProvider.getSitePluginHandler(this.course.format); + const handlerName = this.courseFormatDelegate.getHandlerName(this.course.format), + handler = this.sitePluginsProvider.getSitePluginHandler(handlerName); + if (handler) { this.component = handler.plugin.component; this.method = handler.handlerSchema.method; diff --git a/src/core/siteplugins/components/module-index/module-index.ts b/src/core/siteplugins/components/module-index/module-index.ts index f24cc0685..3e231ecf4 100644 --- a/src/core/siteplugins/components/module-index/module-index.ts +++ b/src/core/siteplugins/components/module-index/module-index.ts @@ -17,7 +17,7 @@ import { TranslateService } from '@ngx-translate/core'; import { CoreTextUtilsProvider } from '@providers/utils/text'; import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreSitePluginsProvider } from '../../providers/siteplugins'; -import { CoreCourseModuleMainComponent } from '@core/course/providers/module-delegate'; +import { CoreCourseModuleDelegate, CoreCourseModuleMainComponent } from '@core/course/providers/module-delegate'; import { CoreCourseModulePrefetchDelegate } from '@core/course/providers/module-prefetch-delegate'; import { CoreCourseHelperProvider } from '@core/course/providers/helper'; import { CoreSitePluginsPluginContentComponent } from '../plugin-content/plugin-content'; @@ -60,7 +60,8 @@ export class CoreSitePluginsModuleIndexComponent implements OnInit, OnDestroy, C constructor(protected sitePluginsProvider: CoreSitePluginsProvider, protected courseHelper: CoreCourseHelperProvider, protected prefetchDelegate: CoreCourseModulePrefetchDelegate, protected textUtils: CoreTextUtilsProvider, - protected translate: TranslateService, protected utils: CoreUtilsProvider) { } + protected translate: TranslateService, protected utils: CoreUtilsProvider, + protected moduleDelegate: CoreCourseModuleDelegate) { } /** * Component being initialized. @@ -69,7 +70,9 @@ export class CoreSitePluginsModuleIndexComponent implements OnInit, OnDestroy, C this.refreshIcon = 'spinner'; if (this.module) { - const handler = this.sitePluginsProvider.getSitePluginHandler(this.module.modname); + const handlerName = this.moduleDelegate.getHandlerName(this.module.modname), + handler = this.sitePluginsProvider.getSitePluginHandler(handlerName); + if (handler) { this.component = handler.plugin.component; this.method = handler.handlerSchema.method; diff --git a/src/core/siteplugins/components/question-behaviour/question-behaviour.ts b/src/core/siteplugins/components/question-behaviour/question-behaviour.ts index 0f0bcee48..17970be85 100644 --- a/src/core/siteplugins/components/question-behaviour/question-behaviour.ts +++ b/src/core/siteplugins/components/question-behaviour/question-behaviour.ts @@ -16,6 +16,7 @@ import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core'; import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreSitePluginsProvider } from '../../providers/siteplugins'; import { CoreSitePluginsCompileInitComponent } from '../../classes/compile-init-component'; +import { CoreQuestionBehaviourDelegate } from '@core/question/providers/behaviour-delegate'; /** * Component that displays a question behaviour created using a site plugin. @@ -33,7 +34,8 @@ export class CoreSitePluginsQuestionBehaviourComponent extends CoreSitePluginsCo @Output() buttonClicked: EventEmitter; // Should emit an event when a behaviour button is clicked. @Output() onAbort: EventEmitter; // Should emit an event if the question should be aborted. - constructor(sitePluginsProvider: CoreSitePluginsProvider, utils: CoreUtilsProvider) { + constructor(sitePluginsProvider: CoreSitePluginsProvider, utils: CoreUtilsProvider, + protected questionBehaviourDelegate: CoreQuestionBehaviourDelegate) { super(sitePluginsProvider, utils); } @@ -51,7 +53,7 @@ export class CoreSitePluginsQuestionBehaviourComponent extends CoreSitePluginsCo this.jsData.onAbort = this.onAbort; if (this.question) { - this.getHandlerData('qbehaviour_' + this.question.preferredBehaviour); + this.getHandlerData(this.questionBehaviourDelegate.getHandlerName(this.question.preferredBehaviour)); } } } diff --git a/src/core/siteplugins/components/question/question.ts b/src/core/siteplugins/components/question/question.ts index 012d23af9..2765ea707 100644 --- a/src/core/siteplugins/components/question/question.ts +++ b/src/core/siteplugins/components/question/question.ts @@ -16,6 +16,7 @@ import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core'; import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreSitePluginsProvider } from '../../providers/siteplugins'; import { CoreSitePluginsCompileInitComponent } from '../../classes/compile-init-component'; +import { CoreQuestionDelegate } from '@core/question/providers/delegate'; /** * Component that displays a question created using a site plugin. @@ -33,7 +34,8 @@ export class CoreSitePluginsQuestionComponent extends CoreSitePluginsCompileInit @Output() buttonClicked: EventEmitter; // Should emit an event when a behaviour button is clicked. @Output() onAbort: EventEmitter; // Should emit an event if the question should be aborted. - constructor(sitePluginsProvider: CoreSitePluginsProvider, utils: CoreUtilsProvider) { + constructor(sitePluginsProvider: CoreSitePluginsProvider, utils: CoreUtilsProvider, + protected questionDelegate: CoreQuestionDelegate) { super(sitePluginsProvider, utils); } @@ -51,7 +53,7 @@ export class CoreSitePluginsQuestionComponent extends CoreSitePluginsCompileInit this.jsData.onAbort = this.onAbort; if (this.question) { - this.getHandlerData('qtype_' + this.question.type); + this.getHandlerData(this.questionDelegate.getHandlerName('qtype_' + this.question.type)); } } } diff --git a/src/core/siteplugins/components/quiz-access-rule/quiz-access-rule.ts b/src/core/siteplugins/components/quiz-access-rule/quiz-access-rule.ts index caa3f382c..b80f62c76 100644 --- a/src/core/siteplugins/components/quiz-access-rule/quiz-access-rule.ts +++ b/src/core/siteplugins/components/quiz-access-rule/quiz-access-rule.ts @@ -17,6 +17,7 @@ import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreSitePluginsProvider } from '../../providers/siteplugins'; import { CoreSitePluginsCompileInitComponent } from '../../classes/compile-init-component'; import { FormGroup } from '@angular/forms'; +import { AddonModQuizAccessRuleDelegate } from '@addon/mod/quiz/providers/access-rules-delegate'; /** * Component that displays a quiz access rule created using a site plugin. @@ -33,7 +34,8 @@ export class CoreSitePluginsQuizAccessRuleComponent extends CoreSitePluginsCompi @Input() siteId: string; // Site ID. @Input() form: FormGroup; // Form where to add the form control. - constructor(sitePluginsProvider: CoreSitePluginsProvider, utils: CoreUtilsProvider) { + constructor(sitePluginsProvider: CoreSitePluginsProvider, utils: CoreUtilsProvider, + protected accessRulesDelegate: AddonModQuizAccessRuleDelegate) { super(sitePluginsProvider, utils); } @@ -50,7 +52,7 @@ export class CoreSitePluginsQuizAccessRuleComponent extends CoreSitePluginsCompi this.jsData.form = this.form; if (this.rule) { - this.getHandlerData(this.rule); + this.getHandlerData(this.accessRulesDelegate.getHandlerName(this.rule)); } } } diff --git a/src/core/siteplugins/components/user-profile-field/user-profile-field.ts b/src/core/siteplugins/components/user-profile-field/user-profile-field.ts index 5e460eb5a..201b95930 100644 --- a/src/core/siteplugins/components/user-profile-field/user-profile-field.ts +++ b/src/core/siteplugins/components/user-profile-field/user-profile-field.ts @@ -17,6 +17,7 @@ import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreSitePluginsProvider } from '../../providers/siteplugins'; import { CoreSitePluginsCompileInitComponent } from '../../classes/compile-init-component'; import { FormGroup } from '@angular/forms'; +import { CoreUserProfileFieldDelegate } from '@core/user/providers/user-profile-field-delegate'; /** * Component that displays a user profile field created using a site plugin. @@ -33,7 +34,8 @@ export class CoreSitePluginsUserProfileFieldComponent extends CoreSitePluginsCom @Input() signup = false; // True if editing the field in signup. Defaults to false. @Input() registerAuth?: string; // Register auth method. E.g. 'email'. - constructor(sitePluginsProvider: CoreSitePluginsProvider, utils: CoreUtilsProvider) { + constructor(sitePluginsProvider: CoreSitePluginsProvider, utils: CoreUtilsProvider, + protected profileFieldDelegate: CoreUserProfileFieldDelegate) { super(sitePluginsProvider, utils); } @@ -51,7 +53,7 @@ export class CoreSitePluginsUserProfileFieldComponent extends CoreSitePluginsCom this.jsData.registerAuth = this.registerAuth; if (this.field) { - this.getHandlerData('profilefield_' + (this.field.type || this.field.datatype)); + this.getHandlerData(this.profileFieldDelegate.getHandlerName(this.field.type || this.field.datatype)); } } } diff --git a/src/core/siteplugins/components/workshop-assessment-strategy/workshop-assessment-strategy.ts b/src/core/siteplugins/components/workshop-assessment-strategy/workshop-assessment-strategy.ts index 3da761007..5c5b8c53f 100644 --- a/src/core/siteplugins/components/workshop-assessment-strategy/workshop-assessment-strategy.ts +++ b/src/core/siteplugins/components/workshop-assessment-strategy/workshop-assessment-strategy.ts @@ -16,6 +16,7 @@ import { Component, OnInit, Input } from '@angular/core'; import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreSitePluginsProvider } from '../../providers/siteplugins'; import { CoreSitePluginsCompileInitComponent } from '../../classes/compile-init-component'; +import { AddonWorkshopAssessmentStrategyDelegate } from '@addon/mod/workshop/providers/assessment-strategy-delegate'; /** * Component that displays a workshop assessment strategy plugin created using a site plugin. @@ -32,7 +33,8 @@ export class CoreSitePluginsWorkshopAssessmentStrategyComponent extends CoreSite @Input() fieldErrors: any; @Input() strategy: string; - constructor(sitePluginsProvider: CoreSitePluginsProvider, utils: CoreUtilsProvider) { + constructor(sitePluginsProvider: CoreSitePluginsProvider, utils: CoreUtilsProvider, + private workshopAssessmentStrategyDelegate: AddonWorkshopAssessmentStrategyDelegate) { super(sitePluginsProvider, utils); } @@ -48,6 +50,6 @@ export class CoreSitePluginsWorkshopAssessmentStrategyComponent extends CoreSite this.jsData.fieldErrors = this.fieldErrors; this.jsData.strategy = this.strategy; - this.getHandlerData('workshopform_' + this.strategy); + this.getHandlerData(this.workshopAssessmentStrategyDelegate.getHandlerName(this.strategy)); } } diff --git a/src/core/siteplugins/providers/helper.ts b/src/core/siteplugins/providers/helper.ts index ef7b93a60..b55378728 100644 --- a/src/core/siteplugins/providers/helper.ts +++ b/src/core/siteplugins/providers/helper.ts @@ -548,7 +548,7 @@ export class CoreSitePluginsHelperProvider { this.logger.debug('Register site plugin', plugin, handlerSchema); // Execute the main method and its JS. The template returned will be used in the right component. - return this.executeMethodAndJS(plugin, handlerSchema.method).then((result) => { + return this.executeMethodAndJS(plugin, handlerSchema.method).then((result): any => { // Create and register the handler. const uniqueName = this.sitePluginsProvider.getHandlerUniqueName(plugin, handlerName), @@ -571,7 +571,7 @@ export class CoreSitePluginsHelperProvider { delegate.registerHandler(handler); - return handlerSchema.moodlecomponent || plugin.component; + return uniqueName; }).catch((err) => { this.logger.error('Error executing main method', plugin.component, handlerSchema.method, err); }); @@ -654,7 +654,7 @@ export class CoreSitePluginsHelperProvider { formatName = (handlerSchema.moodlecomponent || plugin.component).replace('format_', ''); this.courseFormatDelegate.registerHandler(new CoreSitePluginsCourseFormatHandler(uniqueName, formatName, handlerSchema)); - return formatName; + return uniqueName; } /** @@ -777,7 +777,7 @@ export class CoreSitePluginsHelperProvider { this.sitePluginsProvider, plugin.component, uniqueName, modName, handlerSchema)); } - return modName; + return uniqueName; } /**