Skip to content

Commit

Permalink
refactor(TeacherDataService): Extract TeacherPauseScreenService (#1891)
Browse files Browse the repository at this point in the history
  • Loading branch information
breity authored Aug 19, 2024
1 parent bbf6256 commit bac6e61
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 102 deletions.
2 changes: 2 additions & 0 deletions src/app/teacher/teacher-tools.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { TeacherNodeService } from '../../assets/wise5/services/teacherNodeServi
import { MilestoneReportService } from '../../assets/wise5/services/milestoneReportService';
import { RouterModule } from '@angular/router';
import { TeacherToolsRoutingModule } from './teacher-tools-routing.module';
import { TeacherPauseScreenService } from '../../assets/wise5/services/teacherPauseScreenService';

@NgModule({
imports: [
Expand Down Expand Up @@ -61,6 +62,7 @@ import { TeacherToolsRoutingModule } from './teacher-tools-routing.module';
TeacherDataService,
TeacherDiscussionService,
TeacherNodeService,
TeacherPauseScreenService,
TeacherPeerGroupService,
TeacherProjectService,
TeacherWebSocketService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,26 @@ import { TeacherPeerGroupService } from '../services/teacherPeerGroupService';
import { MatSnackBarModule } from '@angular/material/snack-bar';
import { MilestoneReportService } from '../services/milestoneReportService';
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
import { TeacherPauseScreenService } from '../services/teacherPauseScreenService';

@NgModule({ imports: [BrowserAnimationsModule,
MatDialogModule,
MatSnackBarModule,
StudentTeacherCommonServicesModule], providers: [
ClassroomStatusService,
MilestoneService,
MilestoneReportService,
TeacherDataService,
TeacherPeerGroupService,
TeacherProjectService,
TeacherWebSocketService,
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting()
] })
@NgModule({
imports: [
BrowserAnimationsModule,
MatDialogModule,
MatSnackBarModule,
StudentTeacherCommonServicesModule
],
providers: [
ClassroomStatusService,
MilestoneService,
MilestoneReportService,
TeacherDataService,
TeacherPauseScreenService,
TeacherPeerGroupService,
TeacherProjectService,
TeacherWebSocketService,
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting()
]
})
export class ClassroomMonitorTestingModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { NotificationService } from '../services/notificationService';
import { SessionService } from '../services/sessionService';
import { TeacherDataService } from '../services/teacherDataService';
import { TeacherProjectService } from '../services/teacherProjectService';
import { TeacherPauseScreenService } from '../services/teacherPauseScreenService';
import { NavigationEnd, Router } from '@angular/router';

@Component({
Expand Down Expand Up @@ -42,6 +43,7 @@ export class ClassroomMonitorComponent implements OnInit {
private nodeService: NodeService,
private notebookService: NotebookService,
private notificationService: NotificationService,
private pauseScreenService: TeacherPauseScreenService,
private projectService: TeacherProjectService,
private router: Router,
private sessionService: SessionService,
Expand Down Expand Up @@ -204,7 +206,7 @@ export class ClassroomMonitorComponent implements OnInit {
protected unPauseAllPeriods(): void {
this.dataService.getRunStatus().periods.forEach((period) => {
if (period.periodId !== -1 && period.paused) {
this.dataService.pauseScreensChanged(period.periodId, false);
this.pauseScreenService.pauseScreensChanged(period.periodId, false);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Component } from '@angular/core';
import { TeacherDataService } from '../../../services/teacherDataService';
import { TeacherPauseScreenService } from '../../../services/teacherPauseScreenService';

class Period {
paused: boolean;
Expand All @@ -15,17 +16,20 @@ export class PauseScreensMenuComponent {
allPeriodsPaused: boolean;
periods: Period[];

constructor(private dataService: TeacherDataService) {
constructor(
private dataService: TeacherDataService,
private pauseScreenService: TeacherPauseScreenService
) {
this.periods = this.dataService.getPeriods().filter((period) => period.periodId !== -1);
}

togglePeriod(period: Period): void {
this.dataService.pauseScreensChanged(period.periodId, period.paused);
this.pauseScreenService.pauseScreensChanged(period.periodId, period.paused);
}

toggleAllPeriods(): void {
this.periods.forEach((period) => {
this.dataService.pauseScreensChanged(period.periodId, this.allPeriodsPaused);
this.pauseScreenService.pauseScreensChanged(period.periodId, this.allPeriodsPaused);
});
}
}
4 changes: 4 additions & 0 deletions src/assets/wise5/common/RunStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface RunStatus {
runId?: any;
periods: any[];
}
7 changes: 5 additions & 2 deletions src/assets/wise5/services/configService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export class ConfigService {
private configRetrievedSource: Subject<any> = new Subject<any>();
public configRetrieved$: Observable<any> = this.configRetrievedSource.asObservable();

constructor(private http: HttpClient, @Inject(LOCALE_ID) private localeID: string) {}
constructor(
private http: HttpClient,
@Inject(LOCALE_ID) private localeID: string
) {}

setConfig(config) {
this.config = config;
Expand Down Expand Up @@ -182,7 +185,7 @@ export class ConfigService {
return null;
}

getPeriods() {
getPeriods(): any[] {
const myUserInfo = this.getMyUserInfo();
if (myUserInfo != null) {
const myClassInfo = myUserInfo.myClassInfo;
Expand Down
7 changes: 4 additions & 3 deletions src/assets/wise5/services/studentDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable, Subject } from 'rxjs';
import { DataService } from '../../../app/services/data.service';
import { generateRandomKey } from '../common/string/string';
import { RunStatus } from '../common/RunStatus';

@Injectable()
export class StudentDataService extends DataService {
dummyStudentWorkId: number = 1;
nodeStatuses: any = {};
previousStep = null;
runStatus: any = null;
runStatus: RunStatus = null;
saveToServerRequestCount: number = 0;
stackHistory = []; // array of node id's
studentData: any = {
Expand Down Expand Up @@ -139,7 +140,7 @@ export class StudentDataService extends DataService {
return this.http
.get(this.ConfigService.getConfigParam('runStatusURL'), options)
.toPromise()
.then((runStatus: any) => {
.then((runStatus: RunStatus) => {
this.runStatus = runStatus;
if (this.runStatus != null && this.runStatus.periods == null) {
this.runStatus.periods = [];
Expand Down Expand Up @@ -603,7 +604,7 @@ export class StudentDataService extends DataService {
return this.AnnotationService.getTotalScore(this.studentData.annotations);
}

getRunStatus() {
getRunStatus(): RunStatus {
return this.runStatus;
}

Expand Down
73 changes: 10 additions & 63 deletions src/assets/wise5/services/teacherDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { isMatchingPeriods } from '../common/period/period';
import { getIntersectOfArrays } from '../common/array/array';
import { serverSaveTimeComparator } from '../common/object/object';
import { Annotation } from '../common/Annotation';
import { RunStatus } from '../common/RunStatus';

@Injectable()
export class TeacherDataService extends DataService {
Expand All @@ -22,7 +23,7 @@ export class TeacherDataService extends DataService {
currentWorkgroup = null;
currentStep = null;
previousStep = null;
runStatus = null;
private runStatus: RunStatus = null;
periods = [];
nodeGradingSort = 'team';
studentGradingSort = 'step';
Expand Down Expand Up @@ -401,7 +402,7 @@ export class TeacherDataService extends DataService {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
};
return this.http.get(this.ConfigService.getConfigParam('runStatusURL'), options).pipe(
tap((runStatus: any) => {
tap((runStatus: RunStatus) => {
this.runStatus = runStatus;
this.initializePeriods();
})
Expand Down Expand Up @@ -634,10 +635,14 @@ export class TeacherDataService extends DataService {
return this.periods;
}

getRunStatus() {
getRunStatus(): RunStatus {
return this.runStatus;
}

setRunStatus(runStatus: RunStatus): void {
this.runStatus = runStatus;
}

getVisiblePeriodsById(currentPeriodId: number): any {
if (currentPeriodId === -1) {
return this.getPeriods().slice(1);
Expand Down Expand Up @@ -677,35 +682,7 @@ export class TeacherDataService extends DataService {
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: number, isPaused: boolean): void {
this.updatePausedRunStatusValue(periodId, isPaused);
this.saveRunStatusThenHandlePauseScreen(periodId, isPaused);
const context = 'ClassroomMonitor',
nodeId = null,
componentId = null,
componentType = null,
category = 'TeacherAction',
data = { periodId: periodId },
event = isPaused ? 'pauseScreen' : 'unPauseScreen';
this.saveEvent(context, nodeId, componentId, componentType, category, event, data);
}

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

private saveRunStatus(): Observable<void> {
saveRunStatus(): Observable<void> {
const url = this.ConfigService.getConfigParam('runStatusURL');
const body = new HttpParams()
.set('runId', this.ConfigService.getConfigParam('runId'))
Expand All @@ -716,23 +693,7 @@ export class TeacherDataService extends DataService {
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 isPaused whether the period is paused or not
*/
private updatePausedRunStatusValue(periodId: number, isPaused: boolean): void {
if (this.runStatus == null) {
this.runStatus = this.createRunStatus();
}
if (periodId === -1) {
this.updateAllPeriodsPausedValue(isPaused);
} else {
this.updatePeriodPausedValue(periodId, isPaused);
}
}

private createRunStatus(): any {
createRunStatus(): RunStatus {
const periods = this.ConfigService.getPeriods();
periods.forEach((period) => (period.paused = false));
return {
Expand All @@ -741,20 +702,6 @@ export class TeacherDataService extends DataService {
};
}

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

private updatePeriodPausedValue(periodId: number, isPaused: boolean): void {
for (const period of this.runStatus.periods) {
if (period.periodId === periodId) {
period.paused = isPaused;
}
}
}

isWorkgroupShown(workgroup): boolean {
return (
this.isWorkgroupInCurrentPeriod(workgroup) &&
Expand Down
69 changes: 69 additions & 0 deletions src/assets/wise5/services/teacherPauseScreenService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Injectable } from '@angular/core';
import { TeacherDataService } from './teacherDataService';
import { TeacherWebSocketService } from './teacherWebSocketService';

@Injectable()
export class TeacherPauseScreenService {
constructor(
private dataService: TeacherDataService,
private webSocketService: TeacherWebSocketService
) {}

/**
* 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: number, isPaused: boolean): void {
this.updatePausedRunStatusValue(periodId, isPaused);
this.saveRunStatusThenHandlePauseScreen(periodId, isPaused);
const context = 'ClassroomMonitor',
nodeId = null,
componentId = null,
componentType = null,
category = 'TeacherAction',
data = { periodId: periodId },
event = isPaused ? 'pauseScreen' : 'unPauseScreen';
this.dataService.saveEvent(context, nodeId, componentId, componentType, category, event, data);
}

private saveRunStatusThenHandlePauseScreen(periodId: number, isPaused: boolean): void {
this.dataService.saveRunStatus().subscribe(() => {
if (isPaused) {
this.webSocketService.pauseScreens(periodId);
} else {
this.webSocketService.unPauseScreens(periodId);
}
});
}

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

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

private updatePeriodPausedValue(periodId: number, isPaused: boolean): void {
for (const period of this.dataService.getRunStatus().periods) {
if (period.periodId === periodId) {
period.paused = isPaused;
}
}
}
}
Loading

0 comments on commit bac6e61

Please sign in to comment.