Skip to content

Commit

Permalink
refactor(AnnotationService): Rewrite functions using declarative synt…
Browse files Browse the repository at this point in the history
…ax (#1481)
  • Loading branch information
hirokiterashima authored Oct 23, 2023
1 parent 5780c73 commit 46bea79
Showing 1 changed file with 12 additions and 71 deletions.
83 changes: 12 additions & 71 deletions src/assets/wise5/services/annotationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -645,60 +636,23 @@ 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
);
}

/**
* Get the annotations for the given student work
* @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) {
Expand All @@ -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);
}
Expand Down

0 comments on commit 46bea79

Please sign in to comment.