From fa16f6e4f7ffa8eaa72ef4ade7424b37c54e3418 Mon Sep 17 00:00:00 2001 From: Hiroki Terashima Date: Thu, 26 Oct 2023 17:07:43 -0700 Subject: [PATCH] refactor(AnnotationService): ove getAllLatestScoreAnnotations to MilestoneReportService Also rewrite function using filter() and reduceRight() --- .../wise5/services/annotationService.ts | 20 ------------ .../wise5/services/milestoneReportService.ts | 31 ++++++++++++++++--- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/assets/wise5/services/annotationService.ts b/src/assets/wise5/services/annotationService.ts index 7e9c58b2715..06987e54ea7 100644 --- a/src/assets/wise5/services/annotationService.ts +++ b/src/assets/wise5/services/annotationService.ts @@ -640,26 +640,6 @@ export class AnnotationService { return this.annotations.filter((annotation) => annotation.studentWorkId === studentWorkId); } - getAllLatestScoreAnnotations(nodeId, componentId, periodId) { - const workgroupIdsFound = {}; - const latestScoreAnnotations = []; - for (let a = this.annotations.length - 1; a >= 0; a--) { - const annotation = this.annotations[a]; - const workgroupId = annotation.toWorkgroupId; - if ( - workgroupIdsFound[workgroupId] == null && - nodeId === annotation.nodeId && - componentId === annotation.componentId && - (periodId === -1 || periodId === annotation.periodId) && - ('score' === annotation.type || 'autoScore' === annotation.type) - ) { - workgroupIdsFound[workgroupId] = annotation; - latestScoreAnnotations.push(annotation); - } - } - return latestScoreAnnotations; - } - broadcastAnnotationSavedToServer(annotation: Annotation): void { this.annotationSavedToServerSource.next(annotation); } diff --git a/src/assets/wise5/services/milestoneReportService.ts b/src/assets/wise5/services/milestoneReportService.ts index 041c129ef53..56ebe394454 100644 --- a/src/assets/wise5/services/milestoneReportService.ts +++ b/src/assets/wise5/services/milestoneReportService.ts @@ -2,6 +2,8 @@ import { Injectable } from '@angular/core'; import { AnnotationService } from './annotationService'; import { ProjectService } from './projectService'; import { MilestoneCriteriaEvaluator } from '../classroomMonitor/milestones/milestoneCriteriaEvaluator'; +import { Annotation } from '../common/Annotation'; +import { isMatchingPeriods } from '../common/period/period'; @Injectable() export class MilestoneReportService { @@ -72,11 +74,7 @@ export class MilestoneReportService { reportSettings: any ) { const aggregate = {}; - const scoreAnnotations = this.annotationService.getAllLatestScoreAnnotations( - nodeId, - componentId, - periodId - ); + const scoreAnnotations = this.getAllLatestScoreAnnotations(nodeId, componentId, periodId); for (const scoreAnnotation of scoreAnnotations) { if (scoreAnnotation.type === 'autoScore') { this.addDataToAggregate(aggregate, scoreAnnotation, reportSettings); @@ -100,6 +98,29 @@ export class MilestoneReportService { return aggregate; } + private getAllLatestScoreAnnotations( + nodeId: string, + componentId: string, + periodId: number + ): Annotation[] { + return this.annotationService + .getAnnotationsByNodeIdComponentId(nodeId, componentId) + .filter( + (annotation) => + isMatchingPeriods(annotation.periodId, periodId) && + ['autoScore', 'score'].includes(annotation.type) + ) + .reduceRight( + (latestAnnotations, annotation) => + latestAnnotations.some( + (latestAnnotation) => latestAnnotation.toWorkgroupId === annotation.toWorkgroupId + ) + ? latestAnnotations + : latestAnnotations.concat(annotation), + [] + ); + } + private mergeAutoScoreAndTeacherScore( autoScoreAnnotation: any, teacherScoreAnnotation: any,