Skip to content

Commit

Permalink
refactor(AnnotationService): Simplify getLatestAnnotation logic (#2005)
Browse files Browse the repository at this point in the history
  • Loading branch information
hirokiterashima authored Nov 26, 2024
1 parent 1a10293 commit 03ccec3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 64 deletions.
88 changes: 27 additions & 61 deletions src/assets/wise5/services/annotationService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

import { Injectable } from '@angular/core';
import { ProjectService } from './projectService';
import { ConfigService } from './configService';
Expand All @@ -19,9 +17,9 @@ export class AnnotationService {
public annotationReceived$: Observable<Annotation> = this.annotationReceivedSource.asObservable();

constructor(
private configService: ConfigService,
private http: HttpClient,
private ConfigService: ConfigService,
private ProjectService: ProjectService
private projectService: ProjectService
) {}

getAnnotations(): Annotation[] {
Expand All @@ -43,60 +41,28 @@ export class AnnotationService {
* @param params an object containing the params to match
* @returns the latest annotation that matches the params
*/
getLatestAnnotation(params) {
let annotation = null;

if (params != null) {
let nodeId = params.nodeId;
let componentId = params.componentId;
let fromWorkgroupId = params.fromWorkgroupId;
let toWorkgroupId = params.toWorkgroupId;
let type = params.type;

let annotations = this.annotations;

if (annotations != null) {
for (let a = annotations.length - 1; a >= 0; a--) {
let tempAnnotation = annotations[a];

if (tempAnnotation != null) {
let match = true;

if (nodeId && tempAnnotation.nodeId !== nodeId) {
match = false;
}
if (match && componentId && tempAnnotation.componentId !== componentId) {
match = false;
}
if (match && fromWorkgroupId && tempAnnotation.fromWorkgroupId !== fromWorkgroupId) {
match = false;
}
if (match && toWorkgroupId && tempAnnotation.toWorkgroupId !== toWorkgroupId) {
match = false;
}
if (match && type) {
if (type.constructor === Array) {
for (let thisType of type) {
if (tempAnnotation.type !== thisType) {
match = false;
}
}
} else {
if (tempAnnotation.type !== type) {
match = false;
}
}
}

if (match) {
annotation = tempAnnotation;
break;
}
}
getLatestAnnotation(params): any {
for (let a = this.annotations.length - 1; a >= 0; a--) {
const annotation = this.annotations[a];
let match = true;
if (annotation.nodeId !== params.nodeId) {
match = false;
}
if (match && annotation.componentId !== params.componentId) {
match = false;
}
if (match) {
if (params.type.constructor === Array) {
match = params.type.every((thisType) => annotation.type === thisType);
} else {
match = annotation.type === params.type;
}
if (match) {
return annotation;
}
}
}
return annotation;
return null;
}

/**
Expand Down Expand Up @@ -155,7 +121,7 @@ export class AnnotationService {
annotation.requestToken = generateRandomKey(); // use this to keep track of unsaved annotations.
this.addOrUpdateAnnotation(annotation);
const annotations = [annotation];
if (this.ConfigService.isPreview()) {
if (this.configService.isPreview()) {
// if we're in preview, don't make any request to the server but pretend we did
let savedAnnotationDataResponse = {
annotations: annotations
Expand All @@ -164,13 +130,13 @@ export class AnnotationService {
return Promise.resolve(annotation);
} else {
const params = {
runId: this.ConfigService.getRunId(),
workgroupId: this.ConfigService.getWorkgroupId(),
runId: this.configService.getRunId(),
workgroupId: this.configService.getWorkgroupId(),
annotations: JSON.stringify(annotations)
};
const headers = new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' });
return this.http
.post(this.ConfigService.getConfigParam('teacherDataURL'), $.param(params), {
.post(this.configService.getConfigParam('teacherDataURL'), $.param(params), {
headers: headers
})
.toPromise()
Expand Down Expand Up @@ -206,7 +172,7 @@ export class AnnotationService {
localAnnotation.serverSaveTime = savedAnnotation.serverSaveTime;
localAnnotation.requestToken = null; // requestToken is no longer needed.

if (this.ConfigService.isPreview() && localAnnotation.id == null) {
if (this.configService.isPreview() && localAnnotation.id == null) {
/*
* we are in preview mode so we will set a dummy
* annotation id into the annotation
Expand Down Expand Up @@ -286,7 +252,7 @@ export class AnnotationService {
return annotations.filter((annotation) => {
return (
this.isScoreOrAutoScore(annotation) &&
this.ProjectService.shouldIncludeInTotalScore(annotation.nodeId, annotation.componentId)
this.projectService.shouldIncludeInTotalScore(annotation.nodeId, annotation.componentId)
);
});
}
Expand Down
5 changes: 2 additions & 3 deletions src/assets/wise5/vle/vle.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,11 @@ export class VLEComponent implements AfterViewInit {
* @returns {the|Object}
*/
getLatestAnnotationForComponent: (nodeId, componentId, annotationType) => {
let params = {
return this.annotationService.getLatestAnnotation({
nodeId: nodeId,
componentId: componentId,
type: annotationType
};
return this.annotationService.getLatestAnnotation(params);
});
},
/**
* Updates the annotation locally and on the server
Expand Down

0 comments on commit 03ccec3

Please sign in to comment.