Skip to content

Commit

Permalink
Clean up code. #1409
Browse files Browse the repository at this point in the history
  • Loading branch information
hirokiterashima committed Sep 13, 2023
1 parent 442336e commit 10b4dff
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 173 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Observable, concatMap, map } from 'rxjs';
import { Component } from '../../common/Component';
import { PeerGroupStudentData } from '../../../../app/domain/peerGroupStudentData';
import { PeerGroup } from '../../components/peerChat/PeerGroup';
import { DynamicPromptComponent } from './dynamic-prompt.component';
import { FeedbackRuleEvaluator } from '../../components/common/feedbackRule/FeedbackRuleEvaluator';
import { Response } from '../../components/common/feedbackRule/Response';
import { FeedbackRuleEvaluatorMultipleStudents } from '../../components/common/feedbackRule/FeedbackRuleEvaluatorMultipleStudents';
import { FeedbackRuleComponent } from '../../components/feedbackRule/FeedbackRuleComponent';
import { FeedbackRule } from '../../components/common/feedbackRule/FeedbackRule';

export abstract class DynamicPromptEvaluator {
constructor(protected component: DynamicPromptComponent) {}

evaluate(referenceComponent: Component): void {
if (this.component.dynamicPrompt.isPeerGroupingTagSpecified()) {
this.evaluatePeerGroup(referenceComponent);
} else {
this.evaluatePersonal(referenceComponent);
}
}

protected getFeedbackRuleEvaluator(
referenceComponent: Component
): FeedbackRuleEvaluator<Response[]> {
const evaluator = this.component.dynamicPrompt.isPeerGroupingTagSpecified()
? new FeedbackRuleEvaluatorMultipleStudents(
new FeedbackRuleComponent(
this.component.dynamicPrompt.getRules(),
referenceComponent.content.maxSubmitCount,
false
),
this.component.constraintService
)
: new FeedbackRuleEvaluator(
new FeedbackRuleComponent(
this.component.dynamicPrompt.getRules(),
referenceComponent.content.maxSubmitCount,
false
),
this.component.constraintService
);
evaluator.setReferenceComponent(referenceComponent);
return evaluator;
}

protected getPeerGroupData(): Observable<PeerGroupStudentData[]> {
return this.component.peerGroupService
.retrievePeerGroup(this.component.dynamicPrompt.getPeerGroupingTag())
.pipe(
concatMap((peerGroup: PeerGroup) => {
return this.component.peerGroupService
.retrieveDynamicPromptStudentData(
peerGroup.id,
this.component.nodeId,
this.component.componentId
)
.pipe(
map((peerGroupStudentData: PeerGroupStudentData[]) => {
return peerGroupStudentData;
})
);
})
);
}

protected getSubmitCounter(componentState: any): number {
return componentState.studentData.submitCounter;
}

protected setPromptAndEmitRule(feedbackRule: FeedbackRule): void {
this.component.prompt = feedbackRule.prompt;
this.component.dynamicPromptChanged.emit(feedbackRule); // TODO: change to two-way binding variable
}

abstract evaluatePeerGroup(referenceComponent: Component): void;
abstract evaluatePersonal(referenceComponent: Component): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { PeerGroupStudentData } from '../../../../app/domain/peerGroupStudentData';
import { Component } from '../../common/Component';
import { Response } from '../../components/common/feedbackRule/Response';
import { DynamicPromptEvaluator } from './DynamicPromptEvaluator';

export class DynamicPromptMultipleChoiceEvaluator extends DynamicPromptEvaluator {
evaluatePeerGroup(referenceComponent: Component): void {
this.getPeerGroupData().subscribe((peerGroupStudentData: PeerGroupStudentData[]) => {
const responses = peerGroupStudentData.map((peerMemberData: PeerGroupStudentData) => {
return new Response({
submitCounter: this.getSubmitCounter(peerMemberData.studentWork)
});
});
const feedbackRuleEvaluator = this.getFeedbackRuleEvaluator(referenceComponent);
this.setPromptAndEmitRule(feedbackRuleEvaluator.getFeedbackRule(responses));
});
}

evaluatePersonal(referenceComponent: Component): void {
const feedbackRuleEvaluator = this.getFeedbackRuleEvaluator(referenceComponent);
const nodeId = this.component.dynamicPrompt.getReferenceNodeId();
const componentId = referenceComponent.content.id;
const latestComponentState = this.component.dataService.getLatestComponentStateByNodeIdAndComponentId(
nodeId,
componentId
);
const response = new Response({
submitCounter: this.getSubmitCounter(latestComponentState)
});
this.setPromptAndEmitRule(feedbackRuleEvaluator.getFeedbackRule([response]));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { PeerGroupStudentData } from '../../../../app/domain/peerGroupStudentData';
import { Component } from '../../common/Component';
import { CRaterResponse } from '../../components/common/cRater/CRaterResponse';
import { DynamicPromptEvaluator } from './DynamicPromptEvaluator';

export class DynamicPromptOpenResponseEvaluator extends DynamicPromptEvaluator {
evaluatePeerGroup(referenceComponent: Component): void {
this.getPeerGroupData().subscribe((peerGroupStudentData: PeerGroupStudentData[]) => {
const cRaterResponses = peerGroupStudentData.map((peerMemberData: PeerGroupStudentData) => {
return new CRaterResponse({
ideas: peerMemberData.annotation.data.ideas,
scores: peerMemberData.annotation.data.scores,
submitCounter: this.getSubmitCounter(peerMemberData.studentWork)
});
});
const feedbackRuleEvaluator = this.getFeedbackRuleEvaluator(referenceComponent);
this.setPromptAndEmitRule(feedbackRuleEvaluator.getFeedbackRule(cRaterResponses));
});
}

evaluatePersonal(referenceComponent: Component): void {
const nodeId = this.component.dynamicPrompt.getReferenceNodeId();
const componentId = referenceComponent.content.id;
const latestComponentState = this.component.dataService.getLatestComponentStateByNodeIdAndComponentId(
nodeId,
componentId
);
const latestAutoScoreAnnotation = this.component.annotationService.getLatestScoreAnnotation(
nodeId,
componentId,
this.component.configService.getWorkgroupId(),
'autoScore'
);
if (latestComponentState != null && latestAutoScoreAnnotation != null) {
const cRaterResponse = new CRaterResponse({
ideas: latestAutoScoreAnnotation.data.ideas,
scores: latestAutoScoreAnnotation.data.scores,
submitCounter: this.getSubmitCounter(latestComponentState)
});
const feedbackRuleEvaluator = this.getFeedbackRuleEvaluator(referenceComponent);
this.setPromptAndEmitRule(feedbackRuleEvaluator.getFeedbackRule([cRaterResponse]));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ function peerGroupingTagDisabled(): void {
});

it('should display the post prompt', () => {
const prompts = fixture.debugElement.nativeElement.querySelectorAll('.prompt');
const lastPrompt = prompts[prompts.length - 1];
expect(lastPrompt.textContent).toEqual(postPrompt);
expect(fixture.debugElement.nativeElement.querySelectorAll('.prompt')[2].textContent).toEqual(
postPrompt
);
});

it('should display the dynamic prompt', () => {
Expand Down Expand Up @@ -114,9 +114,9 @@ function createPeerGroupStudentData(
}

function expectDynamicPromptToEqual(text: string): void {
const prompts = fixture.debugElement.nativeElement.querySelectorAll('.prompt');
const dynamicPrompt = prompts[prompts.length - 2];
expect(dynamicPrompt.textContent).toEqual(text);
expect(fixture.debugElement.nativeElement.querySelectorAll('.prompt')[1].textContent).toEqual(
text
);
}

function createDynamicPrompt(): DynamicPrompt {
Expand Down
Loading

0 comments on commit 10b4dff

Please sign in to comment.