Skip to content

Commit

Permalink
refactor(StudentWorkDataExportStrategy, TeacherDataService): move and…
Browse files Browse the repository at this point in the history
… simplify logic for component state retrieval (#2013)
  • Loading branch information
hirokiterashima authored Nov 28, 2024
1 parent 2cc9cea commit 4415fd1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,10 @@ export class StudentWorkDataExportStrategy extends AbstractDataExportStrategy {
if (this.exportType === 'allStudentWork') {
componentStates = this.teacherDataService.getComponentStatesByWorkgroupId(workgroupId);
} else if (this.exportType === 'latestStudentWork') {
this.teacherDataService.injectRevisionCounterIntoComponentStates(
this.injectRevisionCounterIntoComponentStates(
this.teacherDataService.getComponentStatesByWorkgroupId(workgroupId)
);
componentStates =
this.teacherDataService.getLatestComponentStatesByWorkgroupId(workgroupId);
componentStates = this.getLatestComponentStatesByWorkgroupId(workgroupId);
}
if (componentStates != null) {
for (var c = 0; c < componentStates.length; c++) {
Expand Down Expand Up @@ -151,6 +150,40 @@ export class StudentWorkDataExportStrategy extends AbstractDataExportStrategy {
});
}

private injectRevisionCounterIntoComponentStates(componentStates: any[]): void {
const componentRevisionCounter = {};
componentStates.forEach((componentState) => {
const key = componentState.nodeId + '-' + componentState.componentId;
if (componentRevisionCounter[key] == null) {
componentRevisionCounter[key] = 1;
}
componentState.revisionCounter = componentRevisionCounter[key];
componentRevisionCounter[key]++;
});
}

/**
* @param workgroupId the workgroup id
* @return An array of component states. Each component state will be the latest component state
* for a component.
*/
private getLatestComponentStatesByWorkgroupId(workgroupId: number): any[] {
const componentStates = [];
const componentsFound = {};
const componentStatesForWorkgroup =
this.teacherDataService.getComponentStatesByWorkgroupId(workgroupId);
for (let csb = componentStatesForWorkgroup.length - 1; csb >= 0; csb--) {
const componentState = componentStatesForWorkgroup[csb];
const key = componentState.nodeId + '-' + componentState.componentId;
if (componentsFound[key] == null) {
componentStates.push(componentState);
componentsFound[key] = true;
}
}
componentStates.reverse();
return componentStates;
}

/**
* Create the array that will be used as a row in the student work export
* @param columnNames all the header column name
Expand Down
38 changes: 0 additions & 38 deletions src/assets/wise5/services/teacherDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,44 +430,6 @@ export class TeacherDataService extends DataService {
return getIntersectOfArrays(componentStatesByWorkgroupId, componentStatesByNodeId);
}

/**
* @param workgroupId the workgroup id
* @return An array of component states. Each component state will be the latest component state
* for a component.
*/
getLatestComponentStatesByWorkgroupId(workgroupId) {
const componentStates = [];
const componentsFound = {};
const componentStatesForWorkgroup = this.getComponentStatesByWorkgroupId(workgroupId);
for (let csb = componentStatesForWorkgroup.length - 1; csb >= 0; csb--) {
const componentState = componentStatesForWorkgroup[csb];
const key = this.getComponentStateNodeIdComponentIdKey(componentState);
if (componentsFound[key] == null) {
componentStates.push(componentState);
componentsFound[key] = true;
}
}
componentStates.reverse();
return componentStates;
}

injectRevisionCounterIntoComponentStates(componentStates) {
const componentRevisionCounter = {};
for (const componentState of componentStates) {
const key = this.getComponentStateNodeIdComponentIdKey(componentState);
if (componentRevisionCounter[key] == null) {
componentRevisionCounter[key] = 1;
}
const revisionCounter = componentRevisionCounter[key];
componentState.revisionCounter = revisionCounter;
componentRevisionCounter[key] = revisionCounter + 1;
}
}

getComponentStateNodeIdComponentIdKey(componentState) {
return componentState.nodeId + '-' + componentState.componentId;
}

getComponentStatesByWorkgroupIdAndComponentId(workgroupId, componentId) {
const componentStatesByWorkgroupId = this.getComponentStatesByWorkgroupId(workgroupId);
const componentStatesByComponentId = this.getComponentStatesByComponentId(componentId);
Expand Down

0 comments on commit 4415fd1

Please sign in to comment.