1+ import HParsonsFeedback from "./hparsonsFeedback" ;
2+ import BlockBasedGrader from "./blockGrader.js" ;
3+
4+ export default class BlockFeedback extends HParsonsFeedback {
5+ createOutput ( ) {
6+ // Block based grading output
7+ this . hparsons . messageDiv = document . createElement ( "div" ) ;
8+ this . hparsons . outerDiv . appendChild ( this . hparsons . messageDiv ) ;
9+ }
10+ customizeUI ( ) {
11+ $ ( this . hparsons . runButton ) . text ( 'Check Me' ) ;
12+ }
13+
14+ init ( ) {
15+ this . hparsons . blockIndex = 0 ;
16+ this . hparsons . checkCount = 0 ;
17+ this . hparsons . numDistinct = 0 ;
18+ this . hparsons . hasSolved = false ;
19+ // TODO: not sure what is the best way to do this
20+ this . hparsons . grader = new BlockBasedGrader ( ) ;
21+ let solutionBlocks = [ ] ;
22+ for ( let i = 0 ; i < this . hparsons . blockAnswer . length ; ++ i ) {
23+ solutionBlocks . push ( this . hparsons . originalBlocks [ this . hparsons . blockAnswer [ i ] ] ) ;
24+ }
25+ this . hparsons . solution = solutionBlocks ;
26+ this . hparsons . grader . solution = solutionBlocks ;
27+ }
28+
29+ // Called when check button clicked (block-based Feedback)
30+ async runButtonHandler ( ) {
31+ this . checkCurrentAnswer ( ) ;
32+ this . renderFeedback ( ) ;
33+ }
34+
35+ // Used for block-based feedback
36+ checkCurrentAnswer ( ) {
37+ if ( ! this . hparsons . hasSolved ) {
38+ this . hparsons . checkCount ++ ;
39+ this . clearFeedback ( ) ;
40+ this . hparsons . grader . answer = this . hparsons . hparsonsInput . getParsonsTextArray ( ) ;
41+ this . hparsons . grade = this . hparsons . grader . grade ( ) ;
42+ if ( this . hparsons . grade == "correct" ) {
43+ this . hparsons . hasSolved = true ;
44+ this . hparsons . correct = true ;
45+ $ ( this . hparsons . runButton ) . prop ( "disabled" , true ) ;
46+ }
47+ }
48+ }
49+
50+ renderFeedback ( ) {
51+ this . hparsons . grade = this . hparsons . grader . graderState ;
52+ var feedbackArea ;
53+ var answerArea = $ ( this . hparsons . answerArea ) ;
54+ feedbackArea = $ ( this . hparsons . messageDiv ) ;
55+
56+ if ( this . hparsons . grade === "correct" ) {
57+ answerArea . addClass ( "correct" ) ;
58+ feedbackArea . fadeIn ( 100 ) ;
59+ feedbackArea . attr ( "class" , "alert alert-info" ) ;
60+ if ( this . hparsons . checkCount > 1 ) {
61+ feedbackArea . html (
62+ $ . i18n ( "msg_parson_correct" , this . hparsons . checkCount )
63+ ) ;
64+ } else {
65+ feedbackArea . html ( $ . i18n ( "msg_parson_correct_first_try" ) ) ;
66+ }
67+ }
68+
69+ if ( this . hparsons . grade === "incorrectTooShort" ) {
70+ // too little code
71+ answerArea . addClass ( "incorrect" ) ;
72+ feedbackArea . fadeIn ( 500 ) ;
73+ feedbackArea . attr ( "class" , "alert alert-danger" ) ;
74+ feedbackArea . html ( $ . i18n ( "msg_parson_too_short" ) ) ;
75+ }
76+
77+ if ( this . hparsons . grade === "incorrectMoveBlocks" ) {
78+ var answerBlocks = this . hparsons . answerArea . children ;
79+ var inSolution = [ ] ;
80+ var inSolutionIndexes = [ ] ;
81+ var notInSolution = [ ] ;
82+ for ( let i = 0 ; i < answerBlocks . length ; i ++ ) {
83+ var block = answerBlocks [ i ] ;
84+ var index = this . hparsons . solution . indexOf ( block . textContent ) ;
85+ if ( index == - 1 ) {
86+ notInSolution . push ( block ) ;
87+ } else {
88+ inSolution . push ( block ) ;
89+ inSolutionIndexes . push ( index ) ;
90+ }
91+ }
92+ var lisIndexes = this . hparsons . grader . inverseLISIndices ( inSolutionIndexes ) ;
93+ for ( let i = 0 ; i < lisIndexes . length ; i ++ ) {
94+ notInSolution . push ( inSolution [ lisIndexes [ i ] ] ) ;
95+ }
96+ answerArea . addClass ( "incorrect" ) ;
97+ feedbackArea . fadeIn ( 500 ) ;
98+ feedbackArea . attr ( "class" , "alert alert-danger" ) ;
99+ for ( let i = 0 ; i < notInSolution . length ; i ++ ) {
100+ $ ( notInSolution [ i ] ) . addClass ( "incorrectPosition" ) ;
101+ }
102+ feedbackArea . html ( $ . i18n ( "msg_parson_wrong_order" ) ) ;
103+ }
104+ }
105+
106+ // Feedback UI for Block-based Feedback
107+ clearFeedback ( ) {
108+ $ ( this . hparsons . answerArea ) . removeClass ( "incorrect correct" ) ;
109+ var children = this . hparsons . answerArea . childNodes ;
110+ for ( var i = 0 ; i < children . length ; i ++ ) {
111+ $ ( children [ i ] ) . removeClass (
112+ "correctPosition incorrectPosition"
113+ ) ;
114+ }
115+ $ ( this . hparsons . messageDiv ) . hide ( ) ;
116+
117+ // TODO: might need to change this
118+ $ ( this . runButton ) . prop ( "disabled" , false ) ;
119+ this . checkCount = 0 ;
120+ this . hasSolved = false ;
121+ }
122+
123+ }
0 commit comments