Skip to content

Commit

Permalink
Merge pull request #3756 from NoelDeMartin/MOBILE-4272
Browse files Browse the repository at this point in the history
MOBILE-4272: Decouple workshop from initial bundle
  • Loading branch information
dpalou authored Aug 31, 2023
2 parents a6eeacf + 2a0a079 commit 475975f
Show file tree
Hide file tree
Showing 39 changed files with 1,384 additions and 989 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
Expand All @@ -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 {}
Original file line number Diff line number Diff line change
@@ -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<boolean> {
return true;
}

/**
* @inheritdoc
*/
getComponent(): Type<unknown> {
return AddonModWorkshopAssessmentStrategyAccumulativeComponent;
}

/**
* @inheritdoc
*/
async getOriginalValues(
form: AddonModWorkshopGetAssessmentFormDefinitionData,
): Promise<AddonModWorkshopGetAssessmentFormFieldsParsedData[]> {
const defaultGrade = Translate.instant('core.choosedots');
const originalValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[] = [];
const promises: Promise<void>[] = [];

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<CoreFormFields> {
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);
147 changes: 21 additions & 126 deletions src/addons/mod/workshop/assessment/accumulative/services/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
* Handler for accumulative assessment strategy plugin.
*/
@Injectable({ providedIn: 'root' })
export class AddonModWorkshopAssessmentStrategyAccumulativeHandlerService implements AddonWorkshopAssessmentStrategyHandler {

name = 'AddonModWorkshopAssessmentStrategyAccumulative';
strategyName = 'accumulative';

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

/**
* @inheritdoc
*/
getComponent(): Type<unknown> {
return AddonModWorkshopAssessmentStrategyAccumulativeComponent;
}

/**
* @inheritdoc
*/
async getOriginalValues(
form: AddonModWorkshopGetAssessmentFormDefinitionData,
): Promise<AddonModWorkshopGetAssessmentFormFieldsParsedData[]> {
const defaultGrade = Translate.instant('core.choosedots');
const originalValues: AddonModWorkshopGetAssessmentFormFieldsParsedData[] = [];
const promises: Promise<void>[] = [];

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
ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_ACCUMULATIVE_NAME,
ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_ACCUMULATIVE_STRATEGY_NAME,
} from '@addons/mod/workshop/assessment/constants';

form.current[n].grade = form.current[n].grade ? parseInt(String(form.current[n].grade), 10) : -1;
export class AddonModWorkshopAssessmentStrategyAccumulativeHandlerService {

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;
name = ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_ACCUMULATIVE_NAME;
strategyName = ADDON_MOD_WORKSHOP_ASSESSMENT_STRATEGY_ACCUMULATIVE_STRATEGY_NAME;

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<CoreFormFields> {
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;
}
});
/**
* Get assessment strategy handler instance.
*
* @returns Assessment strategy handler.
*/
export function getAssessmentStrategyHandlerInstance(): AddonWorkshopAssessmentStrategyHandler {
const lazyHandler = asyncInstance(async () => {
const { AddonModWorkshopAssessmentStrategyAccumulativeHandler } = await import('./handler-lazy');

if (hasErrors) {
throw errors;
}
return AddonModWorkshopAssessmentStrategyAccumulativeHandler.instance;
});

return data;
}
lazyHandler.setEagerInstance(new AddonModWorkshopAssessmentStrategyAccumulativeHandlerService());

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

import { 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 {}
Loading

0 comments on commit 475975f

Please sign in to comment.