Skip to content

Commit

Permalink
refactor(TeacherDataService): Clean up pause period code (#1460)
Browse files Browse the repository at this point in the history
  • Loading branch information
hirokiterashima authored Oct 11, 2023
1 parent 16b4c99 commit 896f62b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,8 @@ export class TopBarComponent implements OnInit {
);
}

/**
* Check whether any period in the run is paused
* @return Boolean whether any of the periods are paused
*/
protected isAnyPeriodPaused(): boolean {
return this.dataService.isAnyPeriodPaused();
return this.dataService.getPeriods().some((period) => period.paused);
}

protected switchToAuthoringView(): void {
Expand Down
93 changes: 31 additions & 62 deletions src/assets/wise5/services/teacherDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -738,45 +738,18 @@ export class TeacherDataService extends DataService {
);
}

isAnyPeriodPaused() {
return this.getPeriods().some((period) => {
return period.paused;
});
}

isPeriodPaused(periodId: number): boolean {
if (periodId === -1) {
return this.isAllPeriodsPaused();
} else {
return this.getPeriodById(periodId).paused;
}
}

isAllPeriodsPaused() {
let numPausedPeriods = 0;
const periods = this.getPeriods();
for (const period of periods) {
if (period.paused) {
numPausedPeriods++;
}
}
return numPausedPeriods === periods.length;
}

getPeriodById(periodId: number): any {
return this.getPeriods().find((period) => {
return period.periodId === periodId;
});
private getPeriodById(periodId: number): any {
return this.getPeriods().find((period) => period.periodId === periodId);
}

/**
* The pause screen status was changed for the given periodId. Update period accordingly.
* @param periodId the id of the period to toggle
* @param isPaused Boolean whether the period should be paused or not
*/
pauseScreensChanged(periodId, isPaused) {
pauseScreensChanged(periodId: number, isPaused: boolean): void {
this.updatePausedRunStatusValue(periodId, isPaused);
this.sendRunStatusThenHandlePauseScreen(periodId, isPaused);
this.saveRunStatusThenHandlePauseScreen(periodId, isPaused);
const context = 'ClassroomMonitor',
nodeId = null,
componentId = null,
Expand All @@ -787,66 +760,62 @@ export class TeacherDataService extends DataService {
this.saveEvent(context, nodeId, componentId, componentType, category, event, data);
}

sendRunStatusThenHandlePauseScreen(periodId, isPaused) {
this.sendRunStatus()
.toPromise()
.then(() => {
if (isPaused) {
this.TeacherWebSocketService.pauseScreens(periodId);
} else {
this.TeacherWebSocketService.unPauseScreens(periodId);
}
});
private saveRunStatusThenHandlePauseScreen(periodId: number, isPaused: boolean): void {
this.saveRunStatus().subscribe(() => {
if (isPaused) {
this.TeacherWebSocketService.pauseScreens(periodId);
} else {
this.TeacherWebSocketService.unPauseScreens(periodId);
}
});
}

sendRunStatus() {
private saveRunStatus(): Observable<void> {
const url = this.ConfigService.getConfigParam('runStatusURL');
const body = new HttpParams()
.set('runId', this.ConfigService.getConfigParam('runId'))
.set('status', JSON.stringify(this.runStatus));
const options = {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
};
return this.http.post(url, body, options);
}

createRunStatus() {
const periods = this.ConfigService.getPeriods();
for (const period of periods) {
period.paused = false;
}
return {
runId: this.ConfigService.getConfigParam('runId'),
periods: periods
};
return this.http.post<void>(url, body, options);
}

/**
* Update the paused value for a period in our run status
* @param periodId the period id or -1 for all periods
* @param value whether the period is paused or not
* @param isPaused whether the period is paused or not
*/
updatePausedRunStatusValue(periodId, value) {
private updatePausedRunStatusValue(periodId: number, isPaused: boolean): void {
if (this.runStatus == null) {
this.runStatus = this.createRunStatus();
}
if (periodId === -1) {
this.updateAllPeriodsPausedValue(value);
this.updateAllPeriodsPausedValue(isPaused);
} else {
this.updatePeriodPausedValue(periodId, value);
this.updatePeriodPausedValue(periodId, isPaused);
}
}

updateAllPeriodsPausedValue(value) {
private createRunStatus(): any {
const periods = this.ConfigService.getPeriods();
periods.forEach((period) => (period.paused = false));
return {
runId: this.ConfigService.getConfigParam('runId'),
periods: periods
};
}

private updateAllPeriodsPausedValue(isPaused: boolean): void {
for (const period of this.runStatus.periods) {
period.paused = value;
period.paused = isPaused;
}
}

updatePeriodPausedValue(periodId, value) {
private updatePeriodPausedValue(periodId: number, isPaused: boolean): void {
for (const period of this.runStatus.periods) {
if (period.periodId === periodId) {
period.paused = value;
period.paused = isPaused;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/messages.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -13909,7 +13909,7 @@ Click &quot;Cancel&quot; to keep the invalid JSON open so you can fix it.</sourc
Are you sure you want to proceed?</source>
<context-group purpose="location">
<context context-type="sourcefile">src/assets/wise5/classroomMonitor/classroomMonitorComponents/shared/top-bar/top-bar.component.ts</context>
<context context-type="linenumber">105</context>
<context context-type="linenumber">101</context>
</context-group>
</trans-unit>
<trans-unit id="2533bf42152adabc1092cc52494ab0cc211c350a" datatype="html">
Expand Down

0 comments on commit 896f62b

Please sign in to comment.