diff --git a/src/addons/mod/workshop/assessment/accumulative/accumulative.module.ts b/src/addons/mod/workshop/assessment/accumulative/accumulative.module.ts index be3c10dc1..ae072947f 100644 --- a/src/addons/mod/workshop/assessment/accumulative/accumulative.module.ts +++ b/src/addons/mod/workshop/assessment/accumulative/accumulative.module.ts @@ -13,15 +13,11 @@ // limitations under the License. import { APP_INITIALIZER, NgModule } from '@angular/core'; -import { AddonModWorkshopAssessmentStrategyAccumulativeComponent } from './component/accumulative'; -import { AddonModWorkshopAssessmentStrategyAccumulativeHandler } from './services/handler'; import { AddonWorkshopAssessmentStrategyDelegate } from '../../services/assessment-strategy-delegate'; import { CoreSharedModule } from '@/core/shared.module'; +import { getAssessmentStrategyHandlerInstance } from '@addons/mod/workshop/assessment/accumulative/services/handler'; @NgModule({ - declarations: [ - AddonModWorkshopAssessmentStrategyAccumulativeComponent, - ], imports: [ CoreSharedModule, ], @@ -30,14 +26,9 @@ import { CoreSharedModule } from '@/core/shared.module'; provide: APP_INITIALIZER, multi: true, useValue: () => { - AddonWorkshopAssessmentStrategyDelegate.registerHandler( - AddonModWorkshopAssessmentStrategyAccumulativeHandler.instance, - ); + AddonWorkshopAssessmentStrategyDelegate.registerHandler(getAssessmentStrategyHandlerInstance()); }, }, ], - exports: [ - AddonModWorkshopAssessmentStrategyAccumulativeComponent, - ], }) export class AddonModWorkshopAssessmentStrategyAccumulativeModule {} diff --git a/src/addons/mod/workshop/assessment/accumulative/services/handler-lazy.ts b/src/addons/mod/workshop/assessment/accumulative/services/handler-lazy.ts new file mode 100644 index 000000000..c9f8bcd8e --- /dev/null +++ b/src/addons/mod/workshop/assessment/accumulative/services/handler-lazy.ts @@ -0,0 +1,149 @@ +// (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 { + AddonModWorkshopAssessmentStrategyFieldErrors, +} from '@addons/mod/workshop/components/assessment-strategy/assessment-strategy'; +import { + AddonModWorkshopGetAssessmentFormDefinitionData, + AddonModWorkshopGetAssessmentFormFieldsParsedData, +} from '@addons/mod/workshop/services/workshop'; +import { Injectable, Type } from '@angular/core'; +import { CoreGradesHelper } from '@features/grades/services/grades-helper'; +import { makeSingleton, Translate } from '@singletons'; +import { CoreFormFields } from '@singletons/form'; +import { AddonWorkshopAssessmentStrategyHandler } from '../../../services/assessment-strategy-delegate'; +import { AddonModWorkshopAssessmentStrategyAccumulativeComponent } from '../component/accumulative'; +import { AddonModWorkshopAssessmentStrategyAccumulativeHandlerService } from './handler'; + +/** + * Handler for accumulative assessment strategy plugin. + */ +@Injectable({ providedIn: 'root' }) +export class AddonModWorkshopAssessmentStrategyAccumulativeHandlerLazyService + extends AddonModWorkshopAssessmentStrategyAccumulativeHandlerService + implements AddonWorkshopAssessmentStrategyHandler { + + /** + * @inheritdoc + */ + async isEnabled(): Promise { + return true; + } + + /** + * @inheritdoc + */ + getComponent(): Type { + return AddonModWorkshopAssessmentStrategyAccumulativeComponent; + } + + /** + * @inheritdoc + */ + async getOriginalValues( + form: AddonModWorkshopGetAssessmentFormDefinitionData, + ): Promise { + const defaultGrade = Translate.instant('core.choosedots'); + const originalValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[] = []; + const promises: Promise[] = []; + + form.fields.forEach((field, n) => { + field.dimtitle = Translate.instant('addon.mod_workshop_assessment_accumulative.dimensionnumber', { $a: field.number }); + + if (!form.current[n]) { + form.current[n] = {}; + } + + originalValues[n] = {}; + originalValues[n].peercomment = form.current[n].peercomment || ''; + originalValues[n].number = field.number; // eslint-disable-line id-blacklist + + form.current[n].grade = form.current[n].grade ? parseInt(String(form.current[n].grade), 10) : -1; + + const gradingType = parseInt(String(field.grade), 10); + const dimension = form.dimensionsinfo.find((dimension) => dimension.id == parseInt(field.dimensionid, 10)); + const scale = dimension && gradingType < 0 ? dimension.scale : undefined; + + promises.push(CoreGradesHelper.makeGradesMenu(gradingType, undefined, defaultGrade, -1, scale).then((grades) => { + field.grades = grades; + originalValues[n].grade = form.current[n].grade; + + return; + })); + }); + + await Promise.all(promises); + + return originalValues; + } + + /** + * @inheritdoc + */ + hasDataChanged( + originalValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], + currentValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], + ): boolean { + for (const x in originalValues) { + if (originalValues[x].grade != currentValues[x].grade) { + return true; + } + if (originalValues[x].peercomment != currentValues[x].peercomment) { + return true; + } + } + + return false; + } + + /** + * @inheritdoc + */ + async prepareAssessmentData( + currentValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], + form: AddonModWorkshopGetAssessmentFormDefinitionData, + ): Promise { + const data: CoreFormFields = {}; + const errors: AddonModWorkshopAssessmentStrategyFieldErrors = {}; + let hasErrors = false; + + form.fields.forEach((field, idx) => { + if (idx < form.dimenssionscount) { + const grade = parseInt(String(currentValues[idx].grade), 10); + if (!isNaN(grade) && grade >= 0) { + data['grade__idx_' + idx] = grade; + } else { + errors['grade_' + idx] = Translate.instant('addon.mod_workshop_assessment_accumulative.mustchoosegrade'); + hasErrors = true; + } + + data['peercomment__idx_' + idx] = currentValues[idx].peercomment ?? ''; + + data['gradeid__idx_' + idx] = parseInt(form.current[idx].gradeid, 10) || 0; + data['dimensionid__idx_' + idx] = parseInt(field.dimensionid, 10); + data['weight__idx_' + idx] = parseInt(field.weight, 10) || 0; + } + }); + + if (hasErrors) { + throw errors; + } + + return data; + } + +} +export const AddonModWorkshopAssessmentStrategyAccumulativeHandler = + makeSingleton(AddonModWorkshopAssessmentStrategyAccumulativeHandlerLazyService); diff --git a/src/addons/mod/workshop/assessment/accumulative/services/handler.ts b/src/addons/mod/workshop/assessment/accumulative/services/handler.ts index 563be19b8..869652eee 100644 --- a/src/addons/mod/workshop/assessment/accumulative/services/handler.ts +++ b/src/addons/mod/workshop/assessment/accumulative/services/handler.ts @@ -12,138 +12,33 @@ // See the License for the specific language governing permissions and // limitations under the License. +import { asyncInstance } from '@/core/utils/async-instance'; +import { AddonWorkshopAssessmentStrategyHandler } from '@addons/mod/workshop/services/assessment-strategy-delegate'; import { - AddonModWorkshopAssessmentStrategyFieldErrors, -} from '@addons/mod/workshop/components/assessment-strategy/assessment-strategy'; -import { - AddonModWorkshopGetAssessmentFormDefinitionData, - AddonModWorkshopGetAssessmentFormFieldsParsedData, -} from '@addons/mod/workshop/services/workshop'; -import { Injectable, Type } from '@angular/core'; -import { CoreGradesHelper } from '@features/grades/services/grades-helper'; -import { makeSingleton, Translate } from '@singletons'; -import { CoreFormFields } from '@singletons/form'; -import { AddonWorkshopAssessmentStrategyHandler } from '../../../services/assessment-strategy-delegate'; -import { AddonModWorkshopAssessmentStrategyAccumulativeComponent } from '../component/accumulative'; + ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_ACCUMULATIVE_NAME, + ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_ACCUMULATIVE_STRATEGY_NAME, +} from '@addons/mod/workshop/assessment/constants'; -/** - * Handler for accumulative assessment strategy plugin. - */ -@Injectable({ providedIn: 'root' }) -export class AddonModWorkshopAssessmentStrategyAccumulativeHandlerService implements AddonWorkshopAssessmentStrategyHandler { +export class AddonModWorkshopAssessmentStrategyAccumulativeHandlerService { - name = 'AddonModWorkshopAssessmentStrategyAccumulative'; - strategyName = 'accumulative'; - - /** - * @inheritdoc - */ - async isEnabled(): Promise { - return true; - } - - /** - * @inheritdoc - */ - getComponent(): Type { - return AddonModWorkshopAssessmentStrategyAccumulativeComponent; - } - - /** - * @inheritdoc - */ - async getOriginalValues( - form: AddonModWorkshopGetAssessmentFormDefinitionData, - ): Promise { - const defaultGrade = Translate.instant('core.choosedots'); - const originalValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[] = []; - const promises: Promise[] = []; - - form.fields.forEach((field, n) => { - field.dimtitle = Translate.instant('addon.mod_workshop_assessment_accumulative.dimensionnumber', { $a: field.number }); - - if (!form.current[n]) { - form.current[n] = {}; - } - - originalValues[n] = {}; - originalValues[n].peercomment = form.current[n].peercomment || ''; - originalValues[n].number = field.number; // eslint-disable-line id-blacklist - - form.current[n].grade = form.current[n].grade ? parseInt(String(form.current[n].grade), 10) : -1; - - const gradingType = parseInt(String(field.grade), 10); - const dimension = form.dimensionsinfo.find((dimension) => dimension.id == parseInt(field.dimensionid, 10)); - const scale = dimension && gradingType < 0 ? dimension.scale : undefined; - - promises.push(CoreGradesHelper.makeGradesMenu(gradingType, undefined, defaultGrade, -1, scale).then((grades) => { - field.grades = grades; - originalValues[n].grade = form.current[n].grade; - - return; - })); - }); - - await Promise.all(promises); - - return originalValues; - } - - /** - * @inheritdoc - */ - hasDataChanged( - originalValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], - currentValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], - ): boolean { - for (const x in originalValues) { - if (originalValues[x].grade != currentValues[x].grade) { - return true; - } - if (originalValues[x].peercomment != currentValues[x].peercomment) { - return true; - } - } - - return false; - } - - /** - * @inheritdoc - */ - async prepareAssessmentData( - currentValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], - form: AddonModWorkshopGetAssessmentFormDefinitionData, - ): Promise { - const data: CoreFormFields = {}; - const errors: AddonModWorkshopAssessmentStrategyFieldErrors = {}; - let hasErrors = false; - - form.fields.forEach((field, idx) => { - if (idx < form.dimenssionscount) { - const grade = parseInt(String(currentValues[idx].grade), 10); - if (!isNaN(grade) && grade >= 0) { - data['grade__idx_' + idx] = grade; - } else { - errors['grade_' + idx] = Translate.instant('addon.mod_workshop_assessment_accumulative.mustchoosegrade'); - hasErrors = true; - } - - data['peercomment__idx_' + idx] = currentValues[idx].peercomment ?? ''; - - data['gradeid__idx_' + idx] = parseInt(form.current[idx].gradeid, 10) || 0; - data['dimensionid__idx_' + idx] = parseInt(field.dimensionid, 10); - data['weight__idx_' + idx] = parseInt(field.weight, 10) || 0; - } - }); - - if (hasErrors) { - throw errors; - } - - return data; - } + name = ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_ACCUMULATIVE_NAME; + strategyName = ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_ACCUMULATIVE_STRATEGY_NAME; } -export const AddonModWorkshopAssessmentStrategyAccumulativeHandler = - makeSingleton(AddonModWorkshopAssessmentStrategyAccumulativeHandlerService); + +/** + * Get assessment strategy handler instance. + * + * @returns Assessment strategy handler. + */ +export function getAssessmentStrategyHandlerInstance(): AddonWorkshopAssessmentStrategyHandler { + const lazyHandler = asyncInstance(async () => { + const { AddonModWorkshopAssessmentStrategyAccumulativeHandler } = await import('./handler-lazy'); + + return AddonModWorkshopAssessmentStrategyAccumulativeHandler.instance; + }); + + lazyHandler.setEagerInstance(new AddonModWorkshopAssessmentStrategyAccumulativeHandlerService()); + + return lazyHandler; +} diff --git a/src/addons/mod/workshop/assessment/assesment-components.module.ts b/src/addons/mod/workshop/assessment/assesment-components.module.ts new file mode 100644 index 000000000..f702edacd --- /dev/null +++ b/src/addons/mod/workshop/assessment/assesment-components.module.ts @@ -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 { AddonModWorkshopAssessmentStrategyCommentsComponent } from './comments/component/comments'; +import { AddonModWorkshopAssessmentStrategyAccumulativeComponent } from './accumulative/component/accumulative'; +import { AddonModWorkshopAssessmentStrategyNumErrorsComponent } from './numerrors/component/numerrors'; +import { AddonModWorkshopAssessmentStrategyRubricComponent } from './rubric/component/rubric'; +import { NgModule } from '@angular/core'; +import { CoreSharedModule } from '@/core/shared.module'; + +@NgModule({ + imports: [ + CoreSharedModule, + ], + declarations: [ + AddonModWorkshopAssessmentStrategyAccumulativeComponent, + AddonModWorkshopAssessmentStrategyCommentsComponent, + AddonModWorkshopAssessmentStrategyNumErrorsComponent, + AddonModWorkshopAssessmentStrategyRubricComponent, + ], + exports: [ + AddonModWorkshopAssessmentStrategyAccumulativeComponent, + AddonModWorkshopAssessmentStrategyCommentsComponent, + AddonModWorkshopAssessmentStrategyNumErrorsComponent, + AddonModWorkshopAssessmentStrategyRubricComponent, + ], +}) +export class AddonModWorkshopAssessmentComponentsModule {} diff --git a/src/addons/mod/workshop/assessment/comments/comments.module.ts b/src/addons/mod/workshop/assessment/comments/comments.module.ts index cb56fc9cc..33d72d4b5 100644 --- a/src/addons/mod/workshop/assessment/comments/comments.module.ts +++ b/src/addons/mod/workshop/assessment/comments/comments.module.ts @@ -15,13 +15,9 @@ import { CoreSharedModule } from '@/core/shared.module'; import { APP_INITIALIZER, NgModule } from '@angular/core'; import { AddonWorkshopAssessmentStrategyDelegate } from '../../services/assessment-strategy-delegate'; -import { AddonModWorkshopAssessmentStrategyCommentsComponent } from './component/comments'; -import { AddonModWorkshopAssessmentStrategyCommentsHandler } from './services/handler'; +import { getAssessmentStrategyHandlerInstance } from './services/handler'; @NgModule({ - declarations: [ - AddonModWorkshopAssessmentStrategyCommentsComponent, - ], imports: [ CoreSharedModule, ], @@ -30,14 +26,9 @@ import { AddonModWorkshopAssessmentStrategyCommentsHandler } from './services/ha provide: APP_INITIALIZER, multi: true, useValue: () => { - AddonWorkshopAssessmentStrategyDelegate.registerHandler( - AddonModWorkshopAssessmentStrategyCommentsHandler.instance, - ); + AddonWorkshopAssessmentStrategyDelegate.registerHandler(getAssessmentStrategyHandlerInstance()); }, }, ], - exports: [ - AddonModWorkshopAssessmentStrategyCommentsComponent, - ], }) export class AddonModWorkshopAssessmentStrategyCommentsModule {} diff --git a/src/addons/mod/workshop/assessment/comments/services/handler-lazy.ts b/src/addons/mod/workshop/assessment/comments/services/handler-lazy.ts new file mode 100644 index 000000000..702c83393 --- /dev/null +++ b/src/addons/mod/workshop/assessment/comments/services/handler-lazy.ts @@ -0,0 +1,124 @@ +// (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 { + AddonModWorkshopAssessmentStrategyFieldErrors, +} from '@addons/mod/workshop/components/assessment-strategy/assessment-strategy'; +import { AddonWorkshopAssessmentStrategyHandler } from '@addons/mod/workshop/services/assessment-strategy-delegate'; +import { + AddonModWorkshopGetAssessmentFormDefinitionData, + AddonModWorkshopGetAssessmentFormFieldsParsedData, +} from '@addons/mod/workshop/services/workshop'; +import { Injectable, Type } from '@angular/core'; +import { makeSingleton, Translate } from '@singletons'; +import { CoreFormFields } from '@singletons/form'; +import { AddonModWorkshopAssessmentStrategyCommentsComponent } from '../component/comments'; +import { AddonModWorkshopAssessmentStrategyCommentsHandlerService } from './handler'; + +/** + * Handler for comments assessment strategy plugin. + */ +@Injectable({ providedIn: 'root' }) +export class AddonModWorkshopAssessmentStrategyCommentsHandlerLazyService + extends AddonModWorkshopAssessmentStrategyCommentsHandlerService + implements AddonWorkshopAssessmentStrategyHandler { + + /** + * @inheritdoc + */ + async isEnabled(): Promise { + return true; + } + + /** + * @inheritdoc + */ + getComponent(): Type { + return AddonModWorkshopAssessmentStrategyCommentsComponent; + } + + /** + * @inheritdoc + */ + async getOriginalValues( + form: AddonModWorkshopGetAssessmentFormDefinitionData, + ): Promise { + const originalValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[] = []; + + form.fields.forEach((field, n) => { + field.dimtitle = Translate.instant('addon.mod_workshop_assessment_comments.dimensionnumber', { $a: field.number }); + + if (!form.current[n]) { + form.current[n] = {}; + } + + originalValues[n] = {}; + originalValues[n].peercomment = form.current[n].peercomment || ''; + originalValues[n].number = field.number; // eslint-disable-line id-blacklist + }); + + return originalValues; + } + + /** + * @inheritdoc + */ + hasDataChanged( + originalValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], + currentValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], + ): boolean { + for (const x in originalValues) { + if (originalValues[x].peercomment != currentValues[x].peercomment) { + return true; + } + } + + return false; + } + + /** + * @inheritdoc + */ + async prepareAssessmentData( + currentValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], + form: AddonModWorkshopGetAssessmentFormDefinitionData, + ): Promise { + const data: CoreFormFields = {}; + const errors: AddonModWorkshopAssessmentStrategyFieldErrors = {}; + let hasErrors = false; + + form.fields.forEach((field, idx) => { + if (idx < form.dimenssionscount) { + if (currentValues[idx].peercomment) { + data['peercomment__idx_' + idx] = currentValues[idx].peercomment; + } else { + errors['peercomment_' + idx] = Translate.instant('core.err_required'); + hasErrors = true; + } + + data['gradeid__idx_' + idx] = parseInt(form.current[idx].gradeid, 10) || 0; + data['dimensionid__idx_' + idx] = parseInt(field.dimensionid, 10); + } + }); + + if (hasErrors) { + throw errors; + } + + return data; + } + +} +export const AddonModWorkshopAssessmentStrategyCommentsHandler = + makeSingleton(AddonModWorkshopAssessmentStrategyCommentsHandlerLazyService); diff --git a/src/addons/mod/workshop/assessment/comments/services/handler.ts b/src/addons/mod/workshop/assessment/comments/services/handler.ts index cca1ac768..a8b08cc8f 100644 --- a/src/addons/mod/workshop/assessment/comments/services/handler.ts +++ b/src/addons/mod/workshop/assessment/comments/services/handler.ts @@ -12,113 +12,33 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { - AddonModWorkshopAssessmentStrategyFieldErrors, -} from '@addons/mod/workshop/components/assessment-strategy/assessment-strategy'; +import { asyncInstance } from '@/core/utils/async-instance'; import { AddonWorkshopAssessmentStrategyHandler } from '@addons/mod/workshop/services/assessment-strategy-delegate'; import { - AddonModWorkshopGetAssessmentFormDefinitionData, - AddonModWorkshopGetAssessmentFormFieldsParsedData, -} from '@addons/mod/workshop/services/workshop'; -import { Injectable, Type } from '@angular/core'; -import { makeSingleton, Translate } from '@singletons'; -import { CoreFormFields } from '@singletons/form'; -import { AddonModWorkshopAssessmentStrategyCommentsComponent } from '../component/comments'; + ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_COMMENTS_NAME, + ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_COMMENTS_STRATEGY_NAME, +} from '@addons/mod/workshop/assessment/constants'; -/** - * Handler for comments assessment strategy plugin. - */ -@Injectable({ providedIn: 'root' }) -export class AddonModWorkshopAssessmentStrategyCommentsHandlerService implements AddonWorkshopAssessmentStrategyHandler { +export class AddonModWorkshopAssessmentStrategyCommentsHandlerService { - name = 'AddonModWorkshopAssessmentStrategyComments'; - strategyName = 'comments'; - - /** - * @inheritdoc - */ - async isEnabled(): Promise { - return true; - } - - /** - * @inheritdoc - */ - getComponent(): Type { - return AddonModWorkshopAssessmentStrategyCommentsComponent; - } - - /** - * @inheritdoc - */ - async getOriginalValues( - form: AddonModWorkshopGetAssessmentFormDefinitionData, - ): Promise { - const originalValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[] = []; - - form.fields.forEach((field, n) => { - field.dimtitle = Translate.instant('addon.mod_workshop_assessment_comments.dimensionnumber', { $a: field.number }); - - if (!form.current[n]) { - form.current[n] = {}; - } - - originalValues[n] = {}; - originalValues[n].peercomment = form.current[n].peercomment || ''; - originalValues[n].number = field.number; // eslint-disable-line id-blacklist - }); - - return originalValues; - } - - /** - * @inheritdoc - */ - hasDataChanged( - originalValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], - currentValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], - ): boolean { - for (const x in originalValues) { - if (originalValues[x].peercomment != currentValues[x].peercomment) { - return true; - } - } - - return false; - } - - /** - * @inheritdoc - */ - async prepareAssessmentData( - currentValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], - form: AddonModWorkshopGetAssessmentFormDefinitionData, - ): Promise { - const data: CoreFormFields = {}; - const errors: AddonModWorkshopAssessmentStrategyFieldErrors = {}; - let hasErrors = false; - - form.fields.forEach((field, idx) => { - if (idx < form.dimenssionscount) { - if (currentValues[idx].peercomment) { - data['peercomment__idx_' + idx] = currentValues[idx].peercomment; - } else { - errors['peercomment_' + idx] = Translate.instant('core.err_required'); - hasErrors = true; - } - - data['gradeid__idx_' + idx] = parseInt(form.current[idx].gradeid, 10) || 0; - data['dimensionid__idx_' + idx] = parseInt(field.dimensionid, 10); - } - }); - - if (hasErrors) { - throw errors; - } - - return data; - } + name = ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_COMMENTS_NAME; + strategyName = ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_COMMENTS_STRATEGY_NAME; } -export const AddonModWorkshopAssessmentStrategyCommentsHandler = - makeSingleton(AddonModWorkshopAssessmentStrategyCommentsHandlerService); + +/** + * Get assessment strategy handler instance. + * + * @returns Assessment strategy handler. + */ +export function getAssessmentStrategyHandlerInstance(): AddonWorkshopAssessmentStrategyHandler { + const lazyHandler = asyncInstance(async () => { + const { AddonModWorkshopAssessmentStrategyCommentsHandler } = await import('./handler-lazy'); + + return AddonModWorkshopAssessmentStrategyCommentsHandler.instance; + }); + + lazyHandler.setEagerInstance(new AddonModWorkshopAssessmentStrategyCommentsHandlerService()); + + return lazyHandler; +} diff --git a/src/addons/mod/workshop/assessment/constants.ts b/src/addons/mod/workshop/assessment/constants.ts new file mode 100644 index 000000000..5fcbbed55 --- /dev/null +++ b/src/addons/mod/workshop/assessment/constants.ts @@ -0,0 +1,25 @@ +// (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. + +export const ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_ACCUMULATIVE_NAME = 'AddonModWorkshopAssessmentStrategyAccumulative'; +export const ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_ACCUMULATIVE_STRATEGY_NAME = 'accumulative'; + +export const ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_COMMENTS_NAME = 'AddonModWorkshopAssessmentStrategyComments'; +export const ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_COMMENTS_STRATEGY_NAME = 'comments'; + +export const ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_NUMERRORS_NAME = 'AddonModWorkshopAssessmentStrategyNumErrors'; +export const ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_NUMERRORS_STRATEGY_NAME = 'numerrors'; + +export const ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_RUBRIC_NAME = 'AddonModWorkshopAssessmentStrategyRubric'; +export const ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_RUBRIC_STRATEGY_NAME = 'rubric'; diff --git a/src/addons/mod/workshop/assessment/numerrors/numerrors.module.ts b/src/addons/mod/workshop/assessment/numerrors/numerrors.module.ts index 7d2591c2a..3fdcc9df2 100644 --- a/src/addons/mod/workshop/assessment/numerrors/numerrors.module.ts +++ b/src/addons/mod/workshop/assessment/numerrors/numerrors.module.ts @@ -15,13 +15,9 @@ import { CoreSharedModule } from '@/core/shared.module'; import { APP_INITIALIZER, NgModule } from '@angular/core'; import { AddonWorkshopAssessmentStrategyDelegate } from '../../services/assessment-strategy-delegate'; -import { AddonModWorkshopAssessmentStrategyNumErrorsComponent } from './component/numerrors'; -import { AddonModWorkshopAssessmentStrategyNumErrorsHandler } from './services/handler'; +import { getAssessmentStrategyHandlerInstance } from './services/handler'; @NgModule({ - declarations: [ - AddonModWorkshopAssessmentStrategyNumErrorsComponent, - ], imports: [ CoreSharedModule, ], @@ -30,14 +26,9 @@ import { AddonModWorkshopAssessmentStrategyNumErrorsHandler } from './services/h provide: APP_INITIALIZER, multi: true, useValue: () => { - AddonWorkshopAssessmentStrategyDelegate.registerHandler( - AddonModWorkshopAssessmentStrategyNumErrorsHandler.instance, - ); + AddonWorkshopAssessmentStrategyDelegate.registerHandler(getAssessmentStrategyHandlerInstance()); }, }, ], - exports: [ - AddonModWorkshopAssessmentStrategyNumErrorsComponent, - ], }) export class AddonModWorkshopAssessmentStrategyNumErrorsModule {} diff --git a/src/addons/mod/workshop/assessment/numerrors/services/handler-lazy.ts b/src/addons/mod/workshop/assessment/numerrors/services/handler-lazy.ts new file mode 100644 index 000000000..6ec33f065 --- /dev/null +++ b/src/addons/mod/workshop/assessment/numerrors/services/handler-lazy.ts @@ -0,0 +1,132 @@ +// (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 { + AddonModWorkshopAssessmentStrategyFieldErrors, +} from '@addons/mod/workshop/components/assessment-strategy/assessment-strategy'; +import { AddonWorkshopAssessmentStrategyHandler } from '@addons/mod/workshop/services/assessment-strategy-delegate'; +import { + AddonModWorkshopGetAssessmentFormDefinitionData, + AddonModWorkshopGetAssessmentFormFieldsParsedData, +} from '@addons/mod/workshop/services/workshop'; +import { Injectable, Type } from '@angular/core'; +import { Translate, makeSingleton } from '@singletons'; +import { CoreFormFields } from '@singletons/form'; +import { AddonModWorkshopAssessmentStrategyNumErrorsComponent } from '../component/numerrors'; +import { AddonModWorkshopAssessmentStrategyNumErrorsHandlerService } from './handler'; + +/** + * Handler for numerrors assessment strategy plugin. + */ +@Injectable({ providedIn: 'root' }) +export class AddonModWorkshopAssessmentStrategyNumErrorsHandlerLazyService + extends AddonModWorkshopAssessmentStrategyNumErrorsHandlerService + implements AddonWorkshopAssessmentStrategyHandler { + + /** + * @inheritdoc + */ + async isEnabled(): Promise { + return true; + } + + /** + * @inheritdoc + */ + getComponent(): Type { + return AddonModWorkshopAssessmentStrategyNumErrorsComponent; + } + + /** + * @inheritdoc + */ + async getOriginalValues( + form: AddonModWorkshopGetAssessmentFormDefinitionData, + ): Promise { + const originalValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[] = []; + + form.fields.forEach((field, n) => { + field.dimtitle = Translate.instant('addon.mod_workshop_assessment_numerrors.dimensionnumber', { $a: field.number }); + + if (!form.current[n]) { + form.current[n] = {}; + } + + originalValues[n] = {}; + originalValues[n].peercomment = form.current[n].peercomment || ''; + originalValues[n].number = field.number; // eslint-disable-line id-blacklist + originalValues[n].grade = form.current[n].grade || ''; + }); + + return originalValues; + } + + /** + * @inheritdoc + */ + hasDataChanged( + originalValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], + currentValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], + ): boolean { + for (const x in originalValues) { + if (originalValues[x].grade != currentValues[x].grade) { + return true; + } + if (originalValues[x].peercomment != currentValues[x].peercomment) { + return true; + } + } + + return false; + } + + /** + * @inheritdoc + */ + async prepareAssessmentData( + currentValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], + form: AddonModWorkshopGetAssessmentFormDefinitionData, + ): Promise { + const data: CoreFormFields = {}; + const errors: AddonModWorkshopAssessmentStrategyFieldErrors = {}; + let hasErrors = false; + + form.fields.forEach((field, idx) => { + if (idx < form.dimenssionscount) { + const grade = parseInt(String(currentValues[idx].grade), 10); + if (!isNaN(grade) && (grade == 1 || grade == -1)) { + data['grade__idx_' + idx] = grade; + } else { + errors['grade_' + idx] = Translate.instant('core.required'); + hasErrors = true; + } + + data['peercomment__idx_' + idx] = currentValues[idx].peercomment ?? ''; + + data['gradeid__idx_' + idx] = parseInt(form.current[idx].gradeid, 10) || 0; + data['dimensionid__idx_' + idx] = parseInt(field.dimensionid, 10); + data['weight__idx_' + idx] = parseInt(field.weight, 10) || 0; + } + }); + + if (hasErrors) { + throw errors; + } + + return data; + } + +} +export const AddonModWorkshopAssessmentStrategyNumErrorsHandler = + makeSingleton(AddonModWorkshopAssessmentStrategyNumErrorsHandlerLazyService); diff --git a/src/addons/mod/workshop/assessment/numerrors/services/handler.ts b/src/addons/mod/workshop/assessment/numerrors/services/handler.ts index ce7e5dc39..7e946a41f 100644 --- a/src/addons/mod/workshop/assessment/numerrors/services/handler.ts +++ b/src/addons/mod/workshop/assessment/numerrors/services/handler.ts @@ -12,121 +12,33 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { - AddonModWorkshopAssessmentStrategyFieldErrors, -} from '@addons/mod/workshop/components/assessment-strategy/assessment-strategy'; +import { asyncInstance } from '@/core/utils/async-instance'; import { AddonWorkshopAssessmentStrategyHandler } from '@addons/mod/workshop/services/assessment-strategy-delegate'; import { - AddonModWorkshopGetAssessmentFormDefinitionData, - AddonModWorkshopGetAssessmentFormFieldsParsedData, -} from '@addons/mod/workshop/services/workshop'; -import { Injectable, Type } from '@angular/core'; -import { Translate, makeSingleton } from '@singletons'; -import { CoreFormFields } from '@singletons/form'; -import { AddonModWorkshopAssessmentStrategyNumErrorsComponent } from '../component/numerrors'; + ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_NUMERRORS_NAME, + ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_NUMERRORS_STRATEGY_NAME, +} from '@addons/mod/workshop/assessment/constants'; -/** - * Handler for numerrors assessment strategy plugin. - */ -@Injectable({ providedIn: 'root' }) -export class AddonModWorkshopAssessmentStrategyNumErrorsHandlerService implements AddonWorkshopAssessmentStrategyHandler { +export class AddonModWorkshopAssessmentStrategyNumErrorsHandlerService { - name = 'AddonModWorkshopAssessmentStrategyNumErrors'; - strategyName = 'numerrors'; - - /** - * @inheritdoc - */ - async isEnabled(): Promise { - return true; - } - - /** - * @inheritdoc - */ - getComponent(): Type { - return AddonModWorkshopAssessmentStrategyNumErrorsComponent; - } - - /** - * @inheritdoc - */ - async getOriginalValues( - form: AddonModWorkshopGetAssessmentFormDefinitionData, - ): Promise { - const originalValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[] = []; - - form.fields.forEach((field, n) => { - field.dimtitle = Translate.instant('addon.mod_workshop_assessment_numerrors.dimensionnumber', { $a: field.number }); - - if (!form.current[n]) { - form.current[n] = {}; - } - - originalValues[n] = {}; - originalValues[n].peercomment = form.current[n].peercomment || ''; - originalValues[n].number = field.number; // eslint-disable-line id-blacklist - originalValues[n].grade = form.current[n].grade || ''; - }); - - return originalValues; - } - - /** - * @inheritdoc - */ - hasDataChanged( - originalValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], - currentValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], - ): boolean { - for (const x in originalValues) { - if (originalValues[x].grade != currentValues[x].grade) { - return true; - } - if (originalValues[x].peercomment != currentValues[x].peercomment) { - return true; - } - } - - return false; - } - - /** - * @inheritdoc - */ - async prepareAssessmentData( - currentValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], - form: AddonModWorkshopGetAssessmentFormDefinitionData, - ): Promise { - const data: CoreFormFields = {}; - const errors: AddonModWorkshopAssessmentStrategyFieldErrors = {}; - let hasErrors = false; - - form.fields.forEach((field, idx) => { - if (idx < form.dimenssionscount) { - const grade = parseInt(String(currentValues[idx].grade), 10); - if (!isNaN(grade) && (grade == 1 || grade == -1)) { - data['grade__idx_' + idx] = grade; - } else { - errors['grade_' + idx] = Translate.instant('core.required'); - hasErrors = true; - } - - data['peercomment__idx_' + idx] = currentValues[idx].peercomment ?? ''; - - data['gradeid__idx_' + idx] = parseInt(form.current[idx].gradeid, 10) || 0; - data['dimensionid__idx_' + idx] = parseInt(field.dimensionid, 10); - data['weight__idx_' + idx] = parseInt(field.weight, 10) || 0; - } - }); - - if (hasErrors) { - throw errors; - } - - return data; - } + name = ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_NUMERRORS_NAME; + strategyName = ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_NUMERRORS_STRATEGY_NAME; } -export const AddonModWorkshopAssessmentStrategyNumErrorsHandler = - makeSingleton(AddonModWorkshopAssessmentStrategyNumErrorsHandlerService); + +/** + * Get assessment strategy handler instance. + * + * @returns Assessment strategy handler. + */ +export function getAssessmentStrategyHandlerInstance(): AddonWorkshopAssessmentStrategyHandler { + const lazyHandler = asyncInstance(async () => { + const { AddonModWorkshopAssessmentStrategyNumErrorsHandler } = await import('./handler-lazy'); + + return AddonModWorkshopAssessmentStrategyNumErrorsHandler.instance; + }); + + lazyHandler.setEagerInstance(new AddonModWorkshopAssessmentStrategyNumErrorsHandlerService()); + + return lazyHandler; +} diff --git a/src/addons/mod/workshop/assessment/rubric/rubric.module.ts b/src/addons/mod/workshop/assessment/rubric/rubric.module.ts index 26ef01711..62328560e 100644 --- a/src/addons/mod/workshop/assessment/rubric/rubric.module.ts +++ b/src/addons/mod/workshop/assessment/rubric/rubric.module.ts @@ -15,13 +15,9 @@ import { CoreSharedModule } from '@/core/shared.module'; import { APP_INITIALIZER, NgModule } from '@angular/core'; import { AddonWorkshopAssessmentStrategyDelegate } from '../../services/assessment-strategy-delegate'; -import { AddonModWorkshopAssessmentStrategyRubricComponent } from './component/rubric'; -import { AddonModWorkshopAssessmentStrategyRubricHandler } from './services/handler'; +import { getAssessmentStrategyHandlerInstance } from './services/handler'; @NgModule({ - declarations: [ - AddonModWorkshopAssessmentStrategyRubricComponent, - ], imports: [ CoreSharedModule, ], @@ -30,14 +26,9 @@ import { AddonModWorkshopAssessmentStrategyRubricHandler } from './services/hand provide: APP_INITIALIZER, multi: true, useValue: () => { - AddonWorkshopAssessmentStrategyDelegate.registerHandler( - AddonModWorkshopAssessmentStrategyRubricHandler.instance, - ); + AddonWorkshopAssessmentStrategyDelegate.registerHandler(getAssessmentStrategyHandlerInstance()); }, }, ], - exports: [ - AddonModWorkshopAssessmentStrategyRubricComponent, - ], }) export class AddonModWorkshopAssessmentStrategyRubricModule {} diff --git a/src/addons/mod/workshop/assessment/rubric/services/handler-lazy.ts b/src/addons/mod/workshop/assessment/rubric/services/handler-lazy.ts new file mode 100644 index 000000000..c001975f1 --- /dev/null +++ b/src/addons/mod/workshop/assessment/rubric/services/handler-lazy.ts @@ -0,0 +1,125 @@ +// (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 { + AddonModWorkshopAssessmentStrategyFieldErrors, +} from '@addons/mod/workshop/components/assessment-strategy/assessment-strategy'; +import { AddonWorkshopAssessmentStrategyHandler } from '@addons/mod/workshop/services/assessment-strategy-delegate'; +import { + AddonModWorkshopGetAssessmentFormDefinitionData, + AddonModWorkshopGetAssessmentFormFieldsParsedData, +} from '@addons/mod/workshop/services/workshop'; +import { Injectable, Type } from '@angular/core'; +import { Translate, makeSingleton } from '@singletons'; +import { CoreFormFields } from '@singletons/form'; +import { AddonModWorkshopAssessmentStrategyRubricComponent } from '../component/rubric'; +import { AddonModWorkshopAssessmentStrategyRubricHandlerService } from './handler'; + +/** + * Handler for rubric assessment strategy plugin. + */ +@Injectable({ providedIn: 'root' }) +export class AddonModWorkshopAssessmentStrategyRubricHandlerLazyService + extends AddonModWorkshopAssessmentStrategyRubricHandlerService + implements AddonWorkshopAssessmentStrategyHandler { + + /** + * @inheritdoc + */ + async isEnabled(): Promise { + return true; + } + + /** + * @inheritdoc + */ + getComponent(): Type { + return AddonModWorkshopAssessmentStrategyRubricComponent; + } + + /** + * @inheritdoc + */ + async getOriginalValues( + form: AddonModWorkshopGetAssessmentFormDefinitionData, + ): Promise { + const originalValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[] = []; + + form.fields.forEach((field, n) => { + field.dimtitle = Translate.instant('addon.mod_workshop_assessment_rubric.dimensionnumber', { $a: field.number }); + + if (!form.current[n]) { + form.current[n] = {}; + } + + originalValues[n] = {}; + originalValues[n].chosenlevelid = form.current[n].chosenlevelid || ''; + originalValues[n].number = field.number; // eslint-disable-line id-blacklist + }); + + return originalValues; + } + + /** + * @inheritdoc + */ + hasDataChanged( + originalValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], + currentValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], + ): boolean { + for (const x in originalValues) { + if (originalValues[x].chosenlevelid != (currentValues[x].chosenlevelid || '')) { + return true; + } + } + + return false; + } + + /** + * @inheritdoc + */ + async prepareAssessmentData( + currentValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], + form: AddonModWorkshopGetAssessmentFormDefinitionData, + ): Promise { + const data: CoreFormFields = {}; + const errors: AddonModWorkshopAssessmentStrategyFieldErrors = {}; + let hasErrors = false; + + form.fields.forEach((field, idx) => { + if (idx < form.dimenssionscount) { + const id = parseInt(currentValues[idx].chosenlevelid, 10); + if (!isNaN(id) && id >= 0) { + data['chosenlevelid__idx_' + idx] = id; + } else { + errors['chosenlevelid_' + idx] = Translate.instant('addon.mod_workshop_assessment_rubric.mustchooseone'); + hasErrors = true; + } + + data['gradeid__idx_' + idx] = parseInt(form.current[idx].gradeid, 10) || 0; + data['dimensionid__idx_' + idx] = parseInt(field.dimensionid, 10); + } + }); + + if (hasErrors) { + throw errors; + } + + return data; + } + +} +export const AddonModWorkshopAssessmentStrategyRubricHandler = + makeSingleton(AddonModWorkshopAssessmentStrategyRubricHandlerLazyService); diff --git a/src/addons/mod/workshop/assessment/rubric/services/handler.ts b/src/addons/mod/workshop/assessment/rubric/services/handler.ts index e824862b0..aa45828f8 100644 --- a/src/addons/mod/workshop/assessment/rubric/services/handler.ts +++ b/src/addons/mod/workshop/assessment/rubric/services/handler.ts @@ -12,114 +12,33 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { - AddonModWorkshopAssessmentStrategyFieldErrors, -} from '@addons/mod/workshop/components/assessment-strategy/assessment-strategy'; +import { asyncInstance } from '@/core/utils/async-instance'; import { AddonWorkshopAssessmentStrategyHandler } from '@addons/mod/workshop/services/assessment-strategy-delegate'; import { - AddonModWorkshopGetAssessmentFormDefinitionData, - AddonModWorkshopGetAssessmentFormFieldsParsedData, -} from '@addons/mod/workshop/services/workshop'; -import { Injectable, Type } from '@angular/core'; -import { Translate, makeSingleton } from '@singletons'; -import { CoreFormFields } from '@singletons/form'; -import { AddonModWorkshopAssessmentStrategyRubricComponent } from '../component/rubric'; + ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_RUBRIC_NAME, + ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_RUBRIC_STRATEGY_NAME, +} from '@addons/mod/workshop/assessment/constants'; -/** - * Handler for rubric assessment strategy plugin. - */ -@Injectable({ providedIn: 'root' }) -export class AddonModWorkshopAssessmentStrategyRubricHandlerService implements AddonWorkshopAssessmentStrategyHandler { +export class AddonModWorkshopAssessmentStrategyRubricHandlerService { - name = 'AddonModWorkshopAssessmentStrategyRubric'; - strategyName = 'rubric'; - - /** - * @inheritdoc - */ - async isEnabled(): Promise { - return true; - } - - /** - * @inheritdoc - */ - getComponent(): Type { - return AddonModWorkshopAssessmentStrategyRubricComponent; - } - - /** - * @inheritdoc - */ - async getOriginalValues( - form: AddonModWorkshopGetAssessmentFormDefinitionData, - ): Promise { - const originalValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[] = []; - - form.fields.forEach((field, n) => { - field.dimtitle = Translate.instant('addon.mod_workshop_assessment_rubric.dimensionnumber', { $a: field.number }); - - if (!form.current[n]) { - form.current[n] = {}; - } - - originalValues[n] = {}; - originalValues[n].chosenlevelid = form.current[n].chosenlevelid || ''; - originalValues[n].number = field.number; // eslint-disable-line id-blacklist - }); - - return originalValues; - } - - /** - * @inheritdoc - */ - hasDataChanged( - originalValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], - currentValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], - ): boolean { - for (const x in originalValues) { - if (originalValues[x].chosenlevelid != (currentValues[x].chosenlevelid || '')) { - return true; - } - } - - return false; - } - - /** - * @inheritdoc - */ - async prepareAssessmentData( - currentValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], - form: AddonModWorkshopGetAssessmentFormDefinitionData, - ): Promise { - const data: CoreFormFields = {}; - const errors: AddonModWorkshopAssessmentStrategyFieldErrors = {}; - let hasErrors = false; - - form.fields.forEach((field, idx) => { - if (idx < form.dimenssionscount) { - const id = parseInt(currentValues[idx].chosenlevelid, 10); - if (!isNaN(id) && id >= 0) { - data['chosenlevelid__idx_' + idx] = id; - } else { - errors['chosenlevelid_' + idx] = Translate.instant('addon.mod_workshop_assessment_rubric.mustchooseone'); - hasErrors = true; - } - - data['gradeid__idx_' + idx] = parseInt(form.current[idx].gradeid, 10) || 0; - data['dimensionid__idx_' + idx] = parseInt(field.dimensionid, 10); - } - }); - - if (hasErrors) { - throw errors; - } - - return data; - } + name = ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_RUBRIC_NAME; + strategyName = ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_RUBRIC_STRATEGY_NAME; } -export const AddonModWorkshopAssessmentStrategyRubricHandler = - makeSingleton(AddonModWorkshopAssessmentStrategyRubricHandlerService); + +/** + * Get assessment strategy handler instance. + * + * @returns Assessment strategy handler. + */ +export function getAssessmentStrategyHandlerInstance(): AddonWorkshopAssessmentStrategyHandler { + const lazyHandler = asyncInstance(async () => { + const { AddonModWorkshopAssessmentStrategyRubricHandler } = await import('./handler-lazy'); + + return AddonModWorkshopAssessmentStrategyRubricHandler.instance; + }); + + lazyHandler.setEagerInstance(new AddonModWorkshopAssessmentStrategyRubricHandlerService()); + + return lazyHandler; +} diff --git a/src/addons/mod/workshop/components/assessment-strategy/assessment-strategy.ts b/src/addons/mod/workshop/components/assessment-strategy/assessment-strategy.ts index 892095ad3..2c53eea33 100644 --- a/src/addons/mod/workshop/components/assessment-strategy/assessment-strategy.ts +++ b/src/addons/mod/workshop/components/assessment-strategy/assessment-strategy.ts @@ -111,7 +111,7 @@ export class AddonModWorkshopAssessmentStrategyComponent implements OnInit, OnDe this.data.moduleId = this.workshop.coursemodule; this.data.courseId = this.workshop.course; - this.componentClass = AddonWorkshopAssessmentStrategyDelegate.getComponentForPlugin(this.strategy); + this.componentClass = await AddonWorkshopAssessmentStrategyDelegate.getComponentForPlugin(this.strategy); if (this.componentClass) { this.overallFeedkback = this.workshop.overallfeedbackmode != AddonModWorkshopOverallFeedbackMode.DISABLED; this.overallFeedkbackRequired = diff --git a/src/addons/mod/workshop/components/components.module.ts b/src/addons/mod/workshop/components/components.module.ts index cc3cb1961..46f93d91f 100644 --- a/src/addons/mod/workshop/components/components.module.ts +++ b/src/addons/mod/workshop/components/components.module.ts @@ -18,6 +18,7 @@ import { AddonModWorkshopSubmissionComponent } from './submission/submission'; import { CoreCourseComponentsModule } from '@features/course/components/components.module'; import { CoreEditorComponentsModule } from '@features/editor/components/components.module'; import { CoreSharedModule } from '@/core/shared.module'; +import { AddonModWorkshopAssessmentComponentsModule } from '@addons/mod/workshop/assessment/assesment-components.module'; import { AddonModWorkshopPhaseInfoComponent } from './phase/phase'; import { AddonModWorkshopAssessmentComponent } from './assessment/assessment'; import { AddonModWorkshopAssessmentStrategyComponent } from './assessment-strategy/assessment-strategy'; @@ -34,6 +35,7 @@ import { AddonModWorkshopAssessmentStrategyComponent } from './assessment-strate CoreSharedModule, CoreCourseComponentsModule, CoreEditorComponentsModule, + AddonModWorkshopAssessmentComponentsModule, ], exports: [ AddonModWorkshopIndexComponent, diff --git a/src/addons/mod/workshop/services/assessment-strategy-delegate.ts b/src/addons/mod/workshop/services/assessment-strategy-delegate.ts index 913154672..f28f8556d 100644 --- a/src/addons/mod/workshop/services/assessment-strategy-delegate.ts +++ b/src/addons/mod/workshop/services/assessment-strategy-delegate.ts @@ -34,7 +34,7 @@ export interface AddonWorkshopAssessmentStrategyHandler extends CoreDelegateHand * @param injector Injector. * @returns The component (or promise resolved with component) to use, undefined if not found. */ - getComponent?(): Type; + getComponent?(): Promise> | Type; /** * Prepare original values to be shown and compared. @@ -58,7 +58,7 @@ export interface AddonWorkshopAssessmentStrategyHandler extends CoreDelegateHand hasDataChanged?( originalValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], currentValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[], - ): boolean; + ): Promise | boolean; /** * Prepare assessment data to be sent to the server depending on the strategy selected. @@ -102,7 +102,7 @@ export class AddonWorkshopAssessmentStrategyDelegateService extends CoreDelegate * @param workshopStrategy Assessment strategy name. * @returns The component, undefined if not found. */ - getComponentForPlugin(workshopStrategy: string): Type | undefined { + getComponentForPlugin(workshopStrategy: string): Promise> | Type | undefined { return this.executeFunctionOnEnabled(workshopStrategy, 'getComponent'); }