This repository was archived by the owner on Jun 7, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathBlockFeedback.js
More file actions
166 lines (153 loc) · 5.98 KB
/
BlockFeedback.js
File metadata and controls
166 lines (153 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import HParsonsFeedback from "./hparsonsFeedback";
import BlockBasedGrader from "./blockGrader.js";
import "../../parsons/js/parsons-i18n.en.js";
import "../../parsons/js/parsons-i18n.pt-br.js";
export default class BlockFeedback extends HParsonsFeedback {
createOutput() {
// Block based grading output
this.messageDiv = document.createElement("div");
this.hparsons.outerDiv.appendChild(this.messageDiv);
}
customizeUI() {
$(this.hparsons.runButton).text('Check Me');
}
init() {
this.checkCount = 0;
this.solved = false;
// TODO: not sure what is the best way to do this
this.grader = new BlockBasedGrader();
let solutionContent = [];
for (let i = 0; i < this.hparsons.blockAnswer.length; ++i) {
if (this.hparsons.renderRaw) {
// if rendering raw (html): answer is text content instead of the original string
solutionContent.push(this.getContentFromRawString(this.hparsons.originalBlocks[this.hparsons.blockAnswer[i]]));
} else {
solutionContent.push(this.hparsons.originalBlocks[this.hparsons.blockAnswer[i]]);
}
}
this.solution = solutionContent;
this.grader.solution = solutionContent;
this.answerArea = this.hparsons.hparsonsInput.querySelector('.drop-area');
}
// Called when check button clicked (block-based Feedback)
async runButtonHandler() {
this.checkCurrentAnswer();
this.logCurrentAnswer();
this.renderFeedback();
}
async logCurrentAnswer() {
let act = {
scheme: "block",
correct: this.grader.graderState == 'correct' ? "T" : "F",
answer: this.hparsons.hparsonsInput.getParsonsTextArray(),
percent: this.grader.percent
}
let logData = {
event: "hparsonsAnswer",
div_id: this.hparsons.divid,
act: JSON.stringify(act),
answer: JSON.stringify({"blocks": act.answer}),
percent: this.grader.percent,
correct: act.correct,
}
await this.hparsons.logBookEvent(logData);
}
// Used for block-based feedback
checkCurrentAnswer() {
if (!this.solved) {
this.checkCount++;
this.clearFeedback();
if (this.hparsons.renderRaw) {
// when rendering raw: the answer is actually the text content.
this.grader.answer = this.hparsons.hparsonsInput.getParsonsTextArray().map((blockHTML) => {
return this.getContentFromRawString(blockHTML);
});
} else {
this.grader.answer = this.hparsons.hparsonsInput.getParsonsTextArray();
}
this.grade = this.grader.grade();
if (this.grade == "correct") {
$(this.hparsons.runButton).prop("disabled", true);
this.solved = true;
}
}
}
renderFeedback() {
this.grade = this.grader.graderState;
var feedbackArea;
var answerArea = $(this.answerArea);
feedbackArea = $(this.messageDiv);
if (this.grade === "correct") {
answerArea.addClass("correct");
feedbackArea.fadeIn(100);
feedbackArea.attr("class", "alert alert-info");
if (this.checkCount > 1) {
feedbackArea.html(
$.i18n("msg_parson_correct", this.checkCount)
);
} else {
feedbackArea.html($.i18n("msg_parson_correct_first_try"));
}
this.checkCount = 0;
}
if (this.grade === "incorrectTooShort") {
// too little code
answerArea.addClass("incorrect");
feedbackArea.fadeIn(500);
feedbackArea.attr("class", "alert alert-danger");
feedbackArea.html($.i18n("msg_parson_too_short"));
}
if (this.grade === "incorrectMoveBlocks") {
var answerBlocks = this.answerArea.children;
var inSolution = [];
var inSolutionIndexes = [];
var notInSolution = [];
for (let i = 0; i < answerBlocks.length; i++) {
var block = answerBlocks[i];
var index = this.hparsons.renderRaw ?
this.solution.indexOf(this.getContentFromRawString(block.innerHTML))
: this.solution.indexOf(block.textContent);
if (index == -1) {
notInSolution.push(block);
} else {
inSolution.push(block);
inSolutionIndexes.push(index);
}
}
var lisIndexes = this.grader.inverseLISIndices(inSolutionIndexes);
for (let i = 0; i < lisIndexes.length; i++) {
notInSolution.push(inSolution[lisIndexes[i]]);
}
answerArea.addClass("incorrect");
feedbackArea.fadeIn(500);
feedbackArea.attr("class", "alert alert-danger");
for (let i = 0; i < notInSolution.length; i++) {
$(notInSolution[i]).addClass("incorrectPosition");
}
feedbackArea.html($.i18n("msg_parson_wrong_order"));
}
}
getContentFromRawString(html) {
let doc = new DOMParser().parseFromString(html, "text/html");
return doc.body.textContent;
}
// Feedback UI for Block-based Feedback
clearFeedback() {
$(this.answerArea).removeClass("incorrect correct");
var children = this.answerArea.childNodes;
for (var i = 0; i < children.length; i++) {
$(children[i]).removeClass(
"correctPosition incorrectPosition"
);
}
$(this.messageDiv).hide();
}
reset() {
if (this.solved) {
this.checkCount = 0;
$(this.hparsons.runButton).prop("disabled", false);
this.solved = false;
}
this.clearFeedback();
}
}