From 4415fd1480e17ba2cfbc5f7264a1591ff6dafd9e Mon Sep 17 00:00:00 2001 From: Hiroki Terashima Date: Wed, 27 Nov 2024 16:58:02 -0800 Subject: [PATCH] refactor(StudentWorkDataExportStrategy, TeacherDataService): move and simplify logic for component state retrieval (#2013) --- .../StudentWorkDataExportStrategy.ts | 39 +++++++++++++++++-- .../wise5/services/teacherDataService.ts | 38 ------------------ 2 files changed, 36 insertions(+), 41 deletions(-) diff --git a/src/assets/wise5/classroomMonitor/dataExport/strategies/StudentWorkDataExportStrategy.ts b/src/assets/wise5/classroomMonitor/dataExport/strategies/StudentWorkDataExportStrategy.ts index 807e317c9c8..92606fc2cab 100644 --- a/src/assets/wise5/classroomMonitor/dataExport/strategies/StudentWorkDataExportStrategy.ts +++ b/src/assets/wise5/classroomMonitor/dataExport/strategies/StudentWorkDataExportStrategy.ts @@ -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++) { @@ -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 diff --git a/src/assets/wise5/services/teacherDataService.ts b/src/assets/wise5/services/teacherDataService.ts index a2e6842d09b..c6489d3015f 100644 --- a/src/assets/wise5/services/teacherDataService.ts +++ b/src/assets/wise5/services/teacherDataService.ts @@ -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);