From 46bea79a50e906dfba6ce04768e1aba52fb9a90e Mon Sep 17 00:00:00 2001 From: Hiroki Terashima Date: Mon, 23 Oct 2023 09:17:34 -0700 Subject: [PATCH] refactor(AnnotationService): Rewrite functions using declarative syntax (#1481) --- .../wise5/services/annotationService.ts | 83 +++---------------- 1 file changed, 12 insertions(+), 71 deletions(-) diff --git a/src/assets/wise5/services/annotationService.ts b/src/assets/wise5/services/annotationService.ts index 7fb687dbfa7..f5c3191e4e5 100644 --- a/src/assets/wise5/services/annotationService.ts +++ b/src/assets/wise5/services/annotationService.ts @@ -28,17 +28,8 @@ export class AnnotationService { return this.annotations; } - /** - * Get the annotation with the specified id, or null if not found - * @param annotationId - */ - getAnnotationById(annotationId) { - for (let annotation of this.annotations) { - if (annotation.id === annotationId) { - return annotation; - } - } - return null; + getAnnotationById(annotationId: number): Annotation { + return this.annotations.find((annotation) => annotation.id === annotationId) || null; } /** @@ -645,21 +636,14 @@ export class AnnotationService { * @param type the type of annotation * @return the latest annotation for the given student work and annotation type */ - getLatestAnnotationByStudentWorkIdAndType(studentWorkId, type) { - for (let a = this.annotations.length - 1; a >= 0; a--) { - const annotation = this.annotations[a]; - - if (annotation != null) { - if (studentWorkId == annotation.studentWorkId && type == annotation.type) { - /* - * we have found an annotation with the given student work - * id and annotation type - */ - return annotation; - } - } - } - return null; + getLatestAnnotationByStudentWorkIdAndType(studentWorkId: number, type: string): Annotation { + return ( + this.annotations + .filter( + (annotation) => annotation.studentWorkId === studentWorkId && annotation.type === type + ) + .at(-1) || null + ); } /** @@ -667,38 +651,8 @@ export class AnnotationService { * @param studentWorkId the student work id * @return array of annotations for the given student work */ - getAnnotationsByStudentWorkId(studentWorkId) { - let annotations = []; - for (let annotation of this.annotations) { - if (annotation && studentWorkId == annotation.studentWorkId) { - annotations.push(annotation); - } - } - return annotations; - } - - getAverageAutoScore(nodeId, componentId, periodId = -1, type = null) { - let totalScoreSoFar = 0; - let annotationsCounted = 0; - for (let annotation of this.getAllLatestScoreAnnotations(nodeId, componentId, periodId)) { - if ( - annotation.nodeId === nodeId && - annotation.componentId === componentId && - (periodId === -1 || annotation.periodId === periodId) - ) { - let score = null; - if (type != null) { - score = this.getSubScore(annotation, type); - } else { - score = this.getScoreFromAnnotation(annotation); - } - if (score != null) { - totalScoreSoFar += score; - annotationsCounted++; - } - } - } - return totalScoreSoFar / annotationsCounted; + getAnnotationsByStudentWorkId(studentWorkId: number): Annotation[] { + return this.annotations.filter((annotation) => annotation.studentWorkId === studentWorkId); } getAllLatestScoreAnnotations(nodeId, componentId, periodId) { @@ -721,19 +675,6 @@ export class AnnotationService { return latestScoreAnnotations; } - getScoreFromAnnotation(annotation) { - return annotation.data.value; - } - - getSubScore(annotation, type) { - for (let score of annotation.data.scores) { - if (score.id === type) { - return score.score; - } - } - return null; - } - broadcastAnnotationSavedToServer(annotation: Annotation): void { this.annotationSavedToServerSource.next(annotation); }