Skip to content

Commit

Permalink
refactor(NodeService): Clean up code (#2020)
Browse files Browse the repository at this point in the history
  • Loading branch information
hirokiterashima authored Dec 17, 2024
1 parent 5ceb195 commit ec3cb34
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/assets/wise5/services/gradingNodeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class GradingNodeService extends TeacherNodeService {
getNextNodeId(currentId = null): Promise<string> {
return super.getNextNodeId(currentId).then((nextNodeId: string) => {
if (!nextNodeId) return null;
return this.ProjectService.nodeHasWork(nextNodeId)
return this.projectService.nodeHasWork(nextNodeId)
? nextNodeId
: this.getNextNodeId(nextNodeId);
});
Expand Down Expand Up @@ -45,7 +45,7 @@ export class GradingNodeService extends TeacherNodeService {
getPrevNodeId(currentId = null) {
const prevNodeId = super.getPrevNodeId(currentId);
if (!prevNodeId) return null;
return this.ProjectService.nodeHasWork(prevNodeId)
return this.projectService.nodeHasWork(prevNodeId)
? prevNodeId
: this.getPrevNodeId(prevNodeId);
}
Expand Down
68 changes: 27 additions & 41 deletions src/assets/wise5/services/nodeService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

import { Injectable } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { ConfigService } from './configService';
Expand All @@ -21,20 +19,19 @@ export class NodeService {

constructor(
protected dialog: MatDialog,
protected ConfigService: ConfigService,
protected configService: ConfigService,
protected constraintService: ConstraintService,
protected ProjectService: ProjectService,
protected DataService: DataService
protected projectService: ProjectService,
protected dataService: DataService
) {}

setCurrentNode(nodeId: string): void {
this.DataService.setCurrentNodeByNodeId(nodeId);
this.dataService.setCurrentNodeByNodeId(nodeId);
}

goToNextNode() {
goToNextNode(): Promise<string> {
return this.getNextNodeId().then((nextNodeId) => {
if (nextNodeId != null) {
const mode = this.ConfigService.getMode();
this.setCurrentNode(nextNodeId);
}
return nextNodeId;
Expand All @@ -59,30 +56,30 @@ export class NodeService {
*/
getPrevNodeId(currentId?: string): string {
let prevNodeId = null;
const currentNodeId = currentId ?? this.DataService.getCurrentNodeId();
const currentNodeId = currentId ?? this.dataService.getCurrentNodeId();
if (currentNodeId) {
if (['author', 'classroomMonitor'].includes(this.ConfigService.getMode())) {
const currentNodeOrder = this.ProjectService.getNodeOrderById(currentNodeId);
if (['author', 'classroomMonitor'].includes(this.configService.getMode())) {
const currentNodeOrder = this.projectService.getNodeOrderById(currentNodeId);
if (currentNodeOrder) {
const prevId = this.ProjectService.getNodeIdByOrder(currentNodeOrder - 1);
const prevId = this.projectService.getNodeIdByOrder(currentNodeOrder - 1);
if (prevId) {
prevNodeId = this.ProjectService.isApplicationNode(prevId)
prevNodeId = this.projectService.isApplicationNode(prevId)
? prevId
: this.getPrevNodeId(prevId);
}
}
} else {
// get all the nodes that transition to the current node
const nodeIdsByToNodeId = this.ProjectService.getNodesByToNodeId(currentNodeId).map(
(node) => node.id
);
const nodeIdsByToNodeId = this.projectService
.getNodesByToNodeId(currentNodeId)
.map((node) => node.id);
if (nodeIdsByToNodeId.length === 1) {
// there is only one node that transitions to the current node
prevNodeId = nodeIdsByToNodeId[0];
} else if (nodeIdsByToNodeId.length > 1) {
// there are multiple nodes that transition to the current node

const stackHistory = this.DataService.getStackHistory();
const stackHistory = this.dataService.getStackHistory();

// loop through the stack history node ids from newest to oldest
for (let s = stackHistory.length - 1; s >= 0; s--) {
Expand All @@ -104,10 +101,10 @@ export class NodeService {
*/
closeNode() {
let currentNode = null;
currentNode = this.DataService.getCurrentNode();
currentNode = this.dataService.getCurrentNode();
if (currentNode) {
let currentNodeId = currentNode.id;
let parentNode = this.ProjectService.getParentGroup(currentNodeId);
let parentNode = this.projectService.getParentGroup(currentNodeId);
let parentNodeId = parentNode.id;
this.setCurrentNode(parentNodeId);
}
Expand All @@ -121,11 +118,11 @@ export class NodeService {
* @returns a promise that will return a transition
*/
protected chooseTransition(nodeId: string, transitionLogic: TransitionLogic): Promise<any> {
if (this.ConfigService.isPreview() && this.chooseTransitionPromises[nodeId] != null) {
if (this.configService.isPreview() && this.chooseTransitionPromises[nodeId] != null) {
return this.chooseTransitionPromises[nodeId];
}
const promise = this.getChooseTransitionPromise(nodeId, transitionLogic);
if (this.ConfigService.isPreview()) {
if (this.configService.isPreview()) {
const availableTransitions = this.getAvailableTransitions(transitionLogic.transitions);
const transitionResult = this.transitionResults[nodeId];
if (availableTransitions.length > 1 && transitionResult == null) {
Expand Down Expand Up @@ -154,7 +151,7 @@ export class NodeService {
} else if (availableTransitions.length == 1) {
transitionResult = availableTransitions[0];
} else if (availableTransitions.length > 1) {
if (this.ConfigService.isPreview()) {
if (this.configService.isPreview()) {
// we are in preview mode so we will let the user choose the branch path to go to
if (transitionResult != null) {
/*
Expand Down Expand Up @@ -197,7 +194,7 @@ export class NodeService {
const toNodeId = availableTransition.to;
const path = {
nodeId: toNodeId,
nodeTitle: this.ProjectService.getNodePositionAndTitle(toNodeId),
nodeTitle: this.projectService.getNodePositionAndTitle(toNodeId),
transition: availableTransition
};
paths.push(path);
Expand All @@ -220,7 +217,7 @@ export class NodeService {
const randomIndex = Math.floor(Math.random() * availableTransitions.length);
transitionResult = availableTransitions[randomIndex];
} else if (howToChooseAmongAvailablePaths === 'workgroupId') {
const index = this.ConfigService.getWorkgroupId() % availableTransitions.length;
const index = this.configService.getWorkgroupId() % availableTransitions.length;
transitionResult = availableTransitions[index];
} else if (howToChooseAmongAvailablePaths === 'firstAvailable') {
transitionResult = availableTransitions[0];
Expand All @@ -235,35 +232,24 @@ export class NodeService {
* path taken event if necessary.
*/
evaluateTransitionLogic(): void {
const currentNode = this.ProjectService.getNode(this.DataService.getCurrentNodeId());
const currentNode = this.projectService.getNode(this.dataService.getCurrentNodeId());
const transitionLogic = currentNode.getTransitionLogic();
const branchEvents = this.DataService.getBranchPathTakenEventsByNodeId(currentNode.id);
const branchEvents = this.dataService.getBranchPathTakenEventsByNodeId(currentNode.id);
const alreadyBranched = branchEvents.length > 0;
if ((alreadyBranched && transitionLogic.canChangePath) || !alreadyBranched) {
this.chooseTransition(currentNode.id, transitionLogic).then((transition) => {
if (transition != null) {
this.createBranchPathTakenEvent(currentNode.id, transition.to);
this.saveBranchPathTakenEvent(currentNode.id, transition.to);
}
});
}
}

/**
* Create a branchPathTaken event
* @param fromNodeId the from node id
* @param toNodeid the to node id
*/
createBranchPathTakenEvent(fromNodeId, toNodeId) {
const nodeId = fromNodeId;
const componentId = null;
const componentType = null;
const category = 'Navigation';
const event = 'branchPathTaken';
const eventData = {
private saveBranchPathTakenEvent(fromNodeId: string, toNodeId: string): void {
this.dataService.saveVLEEvent(fromNodeId, null, null, 'Navigation', 'branchPathTaken', {
fromNodeId: fromNodeId,
toNodeId: toNodeId
};
this.DataService.saveVLEEvent(nodeId, componentId, componentType, category, event, eventData);
});
}

broadcastNodeSubmitClicked(args: any) {
Expand Down
16 changes: 8 additions & 8 deletions src/assets/wise5/services/studentNodeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ export class StudentNodeService extends NodeService {
*/
getNextNodeId(currentId?: string): Promise<any> {
return new Promise((resolve, reject) => {
const currentNodeId = currentId ?? this.DataService.getCurrentNodeId();
const transitionLogic = this.ProjectService.getNode(currentNodeId).getTransitionLogic();
const currentNodeId = currentId ?? this.dataService.getCurrentNodeId();
const transitionLogic = this.projectService.getNode(currentNodeId).getTransitionLogic();
const branchPathTakenEvents =
this.DataService.getBranchPathTakenEventsByNodeId(currentNodeId);
this.dataService.getBranchPathTakenEventsByNodeId(currentNodeId);
if (this.hasPreviouslyBranchedAndCannotChange(branchPathTakenEvents, transitionLogic)) {
resolve(branchPathTakenEvents.at(-1).data.toNodeId);
} else {
Expand All @@ -100,7 +100,7 @@ export class StudentNodeService extends NodeService {
}

private resolveNextNodeIdFromTransition(resolve: any, currentNodeId: string): void {
const transitionLogic = this.ProjectService.getNode(currentNodeId).getTransitionLogic();
const transitionLogic = this.projectService.getNode(currentNodeId).getTransitionLogic();
if (transitionLogic.transitions.length == 0) {
this.getNextNodeIdFromParent(resolve, currentNodeId);
} else {
Expand All @@ -111,13 +111,13 @@ export class StudentNodeService extends NodeService {
}

private getNextNodeIdFromParent(resolve: any, currentNodeId: string): void {
const parentGroupId = this.ProjectService.getParentGroupId(currentNodeId);
const parentGroupId = this.projectService.getParentGroupId(currentNodeId);
if (parentGroupId != null) {
const parentTransitionLogic = this.ProjectService.getNode(parentGroupId).getTransitionLogic();
const parentTransitionLogic = this.projectService.getNode(parentGroupId).getTransitionLogic();
this.chooseTransition(parentGroupId, parentTransitionLogic).then((transition: any) => {
const transitionToNodeId = transition.to;
const startId = this.ProjectService.isGroupNode(transitionToNodeId)
? this.ProjectService.getGroupStartId(transitionToNodeId)
const startId = this.projectService.isGroupNode(transitionToNodeId)
? this.projectService.getGroupStartId(transitionToNodeId)
: null;
resolve(startId == null || startId === '' ? transitionToNodeId : startId);
});
Expand Down
11 changes: 6 additions & 5 deletions src/assets/wise5/services/teacherNodeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { Subject, Observable } from 'rxjs';
@Injectable()
export class TeacherNodeService extends NodeService {
private componentShowSubmitButtonValueChangedSource: Subject<any> = new Subject<any>();
public componentShowSubmitButtonValueChanged$: Observable<any> = this.componentShowSubmitButtonValueChangedSource.asObservable();
public componentShowSubmitButtonValueChanged$: Observable<any> =
this.componentShowSubmitButtonValueChangedSource.asObservable();
private deleteStarterStateSource: Subject<any> = new Subject<any>();
public deleteStarterState$: Observable<any> = this.deleteStarterStateSource.asObservable();
private starterStateResponseSource: Subject<any> = new Subject<any>();
Expand All @@ -26,12 +27,12 @@ export class TeacherNodeService extends NodeService {
getNextNodeId(currentId?: string): Promise<any> {
return new Promise((resolve, reject) => {
let nextNodeId = null;
const currentNodeId = currentId ?? this.DataService.getCurrentNodeId();
const currentNodeOrder = this.ProjectService.getNodeOrderById(currentNodeId);
const currentNodeId = currentId ?? this.dataService.getCurrentNodeId();
const currentNodeOrder = this.projectService.getNodeOrderById(currentNodeId);
if (currentNodeOrder) {
const nextId = this.ProjectService.getNodeIdByOrder(currentNodeOrder + 1);
const nextId = this.projectService.getNodeIdByOrder(currentNodeOrder + 1);
if (nextId) {
nextNodeId = this.ProjectService.isApplicationNode(nextId)
nextNodeId = this.projectService.isApplicationNode(nextId)
? nextId
: this.getNextNodeId(nextId);
}
Expand Down

0 comments on commit ec3cb34

Please sign in to comment.