Skip to content

Commit 4e7c96f

Browse files
authored
refactor(TeacherDataService): Extract runStatus-related functions to RunStatusService (#1907)
1 parent 76aa224 commit 4e7c96f

File tree

8 files changed

+121
-92
lines changed

8 files changed

+121
-92
lines changed

src/app/teacher/teacher-tools.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { MilestoneReportService } from '../../assets/wise5/services/milestoneRep
3232
import { RouterModule } from '@angular/router';
3333
import { TeacherToolsRoutingModule } from './teacher-tools-routing.module';
3434
import { TeacherPauseScreenService } from '../../assets/wise5/services/teacherPauseScreenService';
35+
import { RunStatusService } from '../../assets/wise5/services/runStatusService';
3536

3637
@NgModule({
3738
imports: [
@@ -56,6 +57,7 @@ import { TeacherPauseScreenService } from '../../assets/wise5/services/teacherPa
5657
MoveNodesService,
5758
{ provide: NodeService, useExisting: TeacherNodeService },
5859
ProjectAssetService,
60+
RunStatusService,
5961
SpaceService,
6062
{ provide: PeerGroupService, useExisting: TeacherPeerGroupService },
6163
{ provide: ProjectService, useExisting: TeacherProjectService },

src/app/teacher/teacher-tools.resolver.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { NotificationService } from '../../assets/wise5/services/notificationSer
99
import { TeacherWebSocketService } from '../../assets/wise5/services/teacherWebSocketService';
1010
import { NotebookService } from '../../assets/wise5/services/notebookService';
1111
import { AchievementService } from '../../assets/wise5/services/achievementService';
12+
import { RunStatusService } from '../../assets/wise5/services/runStatusService';
1213

1314
export const TeacherToolsResolver: ResolveFn<any> = (
1415
route: ActivatedRouteSnapshot,
@@ -20,14 +21,15 @@ export const TeacherToolsResolver: ResolveFn<any> = (
2021
notebookService: NotebookService = inject(NotebookService),
2122
notificationService: NotificationService = inject(NotificationService),
2223
projectService: ProjectService = inject(ProjectService),
24+
runStatusService: RunStatusService = inject(RunStatusService),
2325
websocketService: TeacherWebSocketService = inject(TeacherWebSocketService)
2426
): Observable<any> =>
2527
configService
2628
.retrieveConfig(`/api/config/classroomMonitor/${route.params['unitId']}`)
2729
.pipe(switchMap(() => projectService.retrieveProject()))
2830
.pipe(switchMap(() => classroomStatusService.retrieveStudentStatuses()))
2931
.pipe(switchMap(() => achievementService.retrieveStudentAchievements()))
30-
.pipe(switchMap(() => dataService.retrieveRunStatus()))
32+
.pipe(switchMap(() => runStatusService.retrieveRunStatus()))
3133
.pipe(switchMap(() => notificationService.retrieveNotifications()))
3234
.pipe(switchMap(() => dataService.retrieveAnnotations()))
3335
.pipe(switchMap(() => notebookService.retrieveNotebookItems()))

src/assets/wise5/classroomMonitor/classroom-monitor-testing.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { MatSnackBarModule } from '@angular/material/snack-bar';
1313
import { MilestoneReportService } from '../services/milestoneReportService';
1414
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
1515
import { TeacherPauseScreenService } from '../services/teacherPauseScreenService';
16+
import { RunStatusService } from '../services/runStatusService';
1617

1718
@NgModule({
1819
imports: [
@@ -30,6 +31,7 @@ import { TeacherPauseScreenService } from '../services/teacherPauseScreenService
3031
TeacherPeerGroupService,
3132
TeacherProjectService,
3233
TeacherWebSocketService,
34+
RunStatusService,
3335
provideHttpClient(withInterceptorsFromDi()),
3436
provideHttpClientTesting()
3537
]

src/assets/wise5/classroomMonitor/classroom-monitor.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { TeacherDataService } from '../services/teacherDataService';
1313
import { TeacherProjectService } from '../services/teacherProjectService';
1414
import { TeacherPauseScreenService } from '../services/teacherPauseScreenService';
1515
import { NavigationEnd, Router } from '@angular/router';
16+
import { RunStatusService } from '../services/runStatusService';
1617

1718
@Component({
1819
selector: 'classroom-monitor',
@@ -46,6 +47,7 @@ export class ClassroomMonitorComponent implements OnInit {
4647
private pauseScreenService: TeacherPauseScreenService,
4748
private projectService: TeacherProjectService,
4849
private router: Router,
50+
private runStatusService: RunStatusService,
4951
private sessionService: SessionService,
5052
private snackBar: MatSnackBar
5153
) {}
@@ -204,7 +206,7 @@ export class ClassroomMonitorComponent implements OnInit {
204206

205207
@HostListener('window:beforeunload')
206208
protected unPauseAllPeriods(): void {
207-
this.dataService.getRunStatus().periods.forEach((period) => {
209+
this.runStatusService.getRunStatus().periods.forEach((period) => {
208210
if (period.periodId !== -1 && period.paused) {
209211
this.pauseScreenService.pauseScreensChanged(period.periodId, false);
210212
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { HttpClient, HttpParams } from '@angular/common/http';
2+
import { Injectable } from '@angular/core';
3+
import { Observable, tap } from 'rxjs';
4+
import { ConfigService } from './configService';
5+
import { RunStatus } from '../common/RunStatus';
6+
import { TeacherDataService } from './teacherDataService';
7+
8+
@Injectable()
9+
export class RunStatusService {
10+
private runStatus: RunStatus;
11+
12+
constructor(
13+
private configService: ConfigService,
14+
private dataService: TeacherDataService,
15+
private http: HttpClient
16+
) {
17+
this.configService.configRetrieved$.subscribe(() => {
18+
if (this.configService.isClassroomMonitor()) {
19+
this.retrieveRunStatus();
20+
}
21+
});
22+
}
23+
24+
retrieveRunStatus(): Observable<RunStatus> {
25+
const options = {
26+
params: new HttpParams().set('runId', this.configService.getConfigParam('runId')),
27+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
28+
};
29+
return this.http.get(this.configService.getConfigParam('runStatusURL'), options).pipe(
30+
tap((runStatus: RunStatus) => {
31+
this.runStatus = runStatus;
32+
this.initializePeriods();
33+
})
34+
);
35+
}
36+
37+
private initializePeriods(): void {
38+
const periods = [...this.configService.getPeriods()];
39+
if (this.dataService.getCurrentPeriod() == null) {
40+
this.dataService.setCurrentPeriod(periods[0]);
41+
}
42+
periods.unshift({ periodId: -1, periodName: $localize`All Periods` });
43+
let mergedPeriods = periods;
44+
if (this.runStatus.periods != null) {
45+
mergedPeriods = this.mergeConfigAndRunStatusPeriods(periods, this.runStatus.periods);
46+
}
47+
this.runStatus.periods = mergedPeriods;
48+
this.dataService.setPeriods(mergedPeriods);
49+
}
50+
51+
private mergeConfigAndRunStatusPeriods(configPeriods: any[], runStatusPeriods: any[]): any[] {
52+
const mergedPeriods = [];
53+
configPeriods.forEach((configPeriod) => {
54+
const runStatusPeriod = runStatusPeriods.find(
55+
(runStatusPeriod) => runStatusPeriod.periodId === configPeriod.periodId
56+
);
57+
mergedPeriods.push(runStatusPeriod != null ? runStatusPeriod : configPeriod);
58+
});
59+
return mergedPeriods;
60+
}
61+
62+
getRunStatus(): RunStatus {
63+
return this.runStatus;
64+
}
65+
66+
setRunStatus(runStatus: RunStatus): void {
67+
this.runStatus = runStatus;
68+
}
69+
70+
saveRunStatus(): Observable<void> {
71+
const url = this.configService.getConfigParam('runStatusURL');
72+
const body = new HttpParams()
73+
.set('runId', this.configService.getConfigParam('runId'))
74+
.set('status', JSON.stringify(this.runStatus));
75+
const options = {
76+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
77+
};
78+
return this.http.post<void>(url, body, options);
79+
}
80+
81+
createRunStatus(): RunStatus {
82+
const periods = this.configService.getPeriods();
83+
periods.forEach((period) => (period.paused = false));
84+
return {
85+
runId: this.configService.getConfigParam('runId'),
86+
periods: periods
87+
};
88+
}
89+
}

src/assets/wise5/services/teacherDataService.ts

Lines changed: 3 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { isMatchingPeriods } from '../common/period/period';
1414
import { getIntersectOfArrays } from '../common/array/array';
1515
import { serverSaveTimeComparator } from '../common/object/object';
1616
import { Annotation } from '../common/Annotation';
17-
import { RunStatus } from '../common/RunStatus';
1817

1918
@Injectable()
2019
export class TeacherDataService extends DataService {
@@ -23,7 +22,6 @@ export class TeacherDataService extends DataService {
2322
currentWorkgroup = null;
2423
currentStep = null;
2524
previousStep = null;
26-
private runStatus: RunStatus = null;
2725
periods = [];
2826
nodeGradingSort = 'team';
2927
studentGradingSort = 'step';
@@ -65,12 +63,6 @@ export class TeacherDataService extends DataService {
6563
this.addOrUpdateComponentState(studentWork);
6664
this.broadcastStudentWorkReceived({ studentWork: studentWork });
6765
});
68-
69-
this.ConfigService.configRetrieved$.subscribe(() => {
70-
if (this.ConfigService.isClassroomMonitor()) {
71-
this.retrieveRunStatus();
72-
}
73-
});
7466
}
7567

7668
private handleAnnotationReceived(annotation: Annotation): void {
@@ -396,19 +388,6 @@ export class TeacherDataService extends DataService {
396388
return -1;
397389
}
398390

399-
retrieveRunStatus(): Observable<any> {
400-
const options = {
401-
params: new HttpParams().set('runId', this.ConfigService.getConfigParam('runId')),
402-
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
403-
};
404-
return this.http.get(this.ConfigService.getConfigParam('runStatusURL'), options).pipe(
405-
tap((runStatus: RunStatus) => {
406-
this.runStatus = runStatus;
407-
this.initializePeriods();
408-
})
409-
);
410-
}
411-
412391
getComponentStatesByWorkgroupId(workgroupId) {
413392
return this.studentData.componentStatesByWorkgroupId[workgroupId] || [];
414393
}
@@ -569,31 +548,6 @@ export class TeacherDataService extends DataService {
569548
}
570549
}
571550

572-
private initializePeriods(): void {
573-
const periods = [...this.ConfigService.getPeriods()];
574-
if (this.currentPeriod == null) {
575-
this.setCurrentPeriod(periods[0]);
576-
}
577-
periods.unshift({ periodId: -1, periodName: $localize`All Periods` });
578-
let mergedPeriods = periods;
579-
if (this.runStatus.periods != null) {
580-
mergedPeriods = this.mergeConfigAndRunStatusPeriods(periods, this.runStatus.periods);
581-
}
582-
this.periods = mergedPeriods;
583-
this.runStatus.periods = mergedPeriods;
584-
}
585-
586-
private mergeConfigAndRunStatusPeriods(configPeriods: any[], runStatusPeriods: any[]): any[] {
587-
const mergedPeriods = [];
588-
configPeriods.forEach((configPeriod) => {
589-
const runStatusPeriod = runStatusPeriods.find(
590-
(runStatusPeriod) => runStatusPeriod.periodId === configPeriod.periodId
591-
);
592-
mergedPeriods.push(runStatusPeriod != null ? runStatusPeriod : configPeriod);
593-
});
594-
return mergedPeriods;
595-
}
596-
597551
setCurrentPeriod(period) {
598552
const previousPeriod = this.currentPeriod;
599553
this.currentPeriod = period;
@@ -631,16 +585,12 @@ export class TeacherDataService extends DataService {
631585
return this.currentPeriod.periodId;
632586
}
633587

634-
getPeriods() {
588+
getPeriods(): any[] {
635589
return this.periods;
636590
}
637591

638-
getRunStatus(): RunStatus {
639-
return this.runStatus;
640-
}
641-
642-
setRunStatus(runStatus: RunStatus): void {
643-
this.runStatus = runStatus;
592+
setPeriods(periods: any[]): void {
593+
this.periods = periods;
644594
}
645595

646596
getVisiblePeriodsById(currentPeriodId: number): any {
@@ -682,26 +632,6 @@ export class TeacherDataService extends DataService {
682632
return this.getPeriods().find((period) => period.periodId === periodId);
683633
}
684634

685-
saveRunStatus(): Observable<void> {
686-
const url = this.ConfigService.getConfigParam('runStatusURL');
687-
const body = new HttpParams()
688-
.set('runId', this.ConfigService.getConfigParam('runId'))
689-
.set('status', JSON.stringify(this.runStatus));
690-
const options = {
691-
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
692-
};
693-
return this.http.post<void>(url, body, options);
694-
}
695-
696-
createRunStatus(): RunStatus {
697-
const periods = this.ConfigService.getPeriods();
698-
periods.forEach((period) => (period.paused = false));
699-
return {
700-
runId: this.ConfigService.getConfigParam('runId'),
701-
periods: periods
702-
};
703-
}
704-
705635
isWorkgroupShown(workgroup): boolean {
706636
return (
707637
this.isWorkgroupInCurrentPeriod(workgroup) &&

src/assets/wise5/services/teacherPauseScreenService.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { Injectable } from '@angular/core';
22
import { TeacherDataService } from './teacherDataService';
33
import { TeacherWebSocketService } from './teacherWebSocketService';
4+
import { RunStatusService } from './runStatusService';
45

56
@Injectable()
67
export class TeacherPauseScreenService {
78
constructor(
89
private dataService: TeacherDataService,
10+
private runStatusService: RunStatusService,
911
private webSocketService: TeacherWebSocketService
1012
) {}
1113

@@ -28,7 +30,7 @@ export class TeacherPauseScreenService {
2830
}
2931

3032
private saveRunStatusThenHandlePauseScreen(periodId: number, isPaused: boolean): void {
31-
this.dataService.saveRunStatus().subscribe(() => {
33+
this.runStatusService.saveRunStatus().subscribe(() => {
3234
if (isPaused) {
3335
this.webSocketService.pauseScreens(periodId);
3436
} else {
@@ -43,8 +45,8 @@ export class TeacherPauseScreenService {
4345
* @param isPaused whether the period is paused or not
4446
*/
4547
private updatePausedRunStatusValue(periodId: number, isPaused: boolean): void {
46-
if (this.dataService.getRunStatus() == null) {
47-
this.dataService.setRunStatus(this.dataService.createRunStatus());
48+
if (this.runStatusService.getRunStatus() == null) {
49+
this.runStatusService.setRunStatus(this.runStatusService.createRunStatus());
4850
}
4951
if (periodId === -1) {
5052
this.updateAllPeriodsPausedValue(isPaused);
@@ -54,13 +56,13 @@ export class TeacherPauseScreenService {
5456
}
5557

5658
private updateAllPeriodsPausedValue(isPaused: boolean): void {
57-
for (const period of this.dataService.getRunStatus().periods) {
59+
for (const period of this.runStatusService.getRunStatus().periods) {
5860
period.paused = isPaused;
5961
}
6062
}
6163

6264
private updatePeriodPausedValue(periodId: number, isPaused: boolean): void {
63-
for (const period of this.dataService.getRunStatus().periods) {
65+
for (const period of this.runStatusService.getRunStatus().periods) {
6466
if (period.periodId === periodId) {
6567
period.paused = isPaused;
6668
}

0 commit comments

Comments
 (0)