Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit e57add5

Browse files
committed
✨ Add restoring local answer for hparsons directive
1 parent 736bda1 commit e57add5

File tree

4 files changed

+39
-27
lines changed

4 files changed

+39
-27
lines changed

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"handsontable": "7.2.2",
3737
"jexcel": "^3.9.1",
3838
"jquery-ui": "1.10.4",
39-
"micro-parsons": "https://github.com/amy21206/micro-parsons-element/releases/download/v0.1.3/micro-parsons-0.1.3.tgz",
39+
"micro-parsons": "https://github.com/amy21206/micro-parsons-element/releases/download/v0.1.4/micro-parsons-0.1.4.tgz",
4040
"select2": "^4.1.0-rc.0",
4141
"sql.js": "1.5.0",
4242
"vega-embed": "3.14.0",

runestone/hparsons/js/SQLFeedback.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,10 @@ export default class SQLFeedback extends HParsonsFeedback {
261261
// adapted from activecode
262262
async buildProg() {
263263
// assemble code from prefix, suffix, and editor for running.
264-
// TODO: fix or remove text entry
264+
// TODO: automatically joins the text array with space.
265+
// Should be joining without space when implementing regex.
265266
var prog;
266-
if (this.hparsons.textentry) {
267-
prog = this.hparsons.hparsonsInput.getCurrentInput();
268-
} else {
269-
prog = this.hparsons.hparsonsInput.getParsonsTextArray().join(' ') + "\n";
270-
}
267+
prog = this.hparsons.hparsonsInput.getParsonsTextArray().join(' ') + "\n";
271268
return Promise.resolve(prog);
272269
}
273270

runestone/hparsons/js/hparsons.js

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export default class HParsons extends RunestoneBase {
7676

7777
// initializing functionalities for different feedback
7878
this.feedbackController.init();
79+
this.checkServer('hparsons', true);
7980
}
8081

8182
// copied from activecode, already modified to add parsons
@@ -115,7 +116,6 @@ export default class HParsons extends RunestoneBase {
115116
this.feedbackController.createOutput();
116117
}
117118

118-
// copied from activecode
119119
createControls() {
120120
var ctrlDiv = document.createElement("div");
121121
$(ctrlDiv).addClass("hp_actions");
@@ -127,8 +127,10 @@ export default class HParsons extends RunestoneBase {
127127
ctrlDiv.appendChild(this.runButton);
128128
$(this.runButton).attr("type", "button");
129129
$(this.runButton).text("Run");
130+
var that = this;
130131
this.runButton.onclick = () => {
131-
this.feedbackController.runButtonHandler();
132+
that.feedbackController.runButtonHandler();
133+
that.setLocalStorage();
132134
};
133135

134136
// Reset button
@@ -139,8 +141,9 @@ export default class HParsons extends RunestoneBase {
139141
ctrlDiv.appendChild(resetBtn);
140142
this.resetButton = resetBtn;
141143
this.resetButton.onclick = () => {
142-
this.hparsonsInput.resetInput();
143-
this.feedbackController.reset();
144+
that.hparsonsInput.resetInput();
145+
that.setLocalStorage();
146+
that.feedbackController.reset();
144147
};
145148
$(resetBtn).attr("type", "button");
146149

@@ -149,6 +152,7 @@ export default class HParsons extends RunestoneBase {
149152
}
150153

151154
// Return previous answers in local storage
155+
//
152156
localData() {
153157
var data = localStorage.getItem(this.storageId);
154158
if (data !== null) {
@@ -164,28 +168,39 @@ export default class HParsons extends RunestoneBase {
164168
}
165169
// RunestoneBase: Sent when the server has data
166170
restoreAnswers(serverData) {
167-
this.loadData(serverData);
171+
if (serverData.answer){
172+
this.hparsonsInput.restoreAnswer(serverData.answer);
173+
}
168174
}
169175
// RunestoneBase: Load what is in local storage
170176
checkLocalStorage() {
171177
if (this.graderactive) {
178+
// Zihan: I think this means the component is still loading?
172179
return;
173180
}
174-
this.loadData(this.localData());
181+
let localData = this.localData();
182+
if (localData.answer) {
183+
this.hparsonsInput.restoreAnswer(localData.answer);
184+
}
185+
if (localData.count) {
186+
this.feedbackController.checkCount = localData.count;
187+
}
175188
}
176189
// RunestoneBase: Set the state of the problem in local storage
177190
setLocalStorage(data) {
178-
var toStore;
191+
let currentState = {};
179192
if (data == undefined) {
180-
toStore = {
181-
source: this.sourceHash(),
182-
answer: this.answerHash(),
183-
timestamp: new Date(),
184-
};
193+
currentState = {
194+
answer: this.hparsonsInput.getParsonsTextArray()
195+
}
196+
if (this.isBlockGrading) {
197+
// if this is block grading, add number of previous attempts too
198+
currentState.count = this.feedbackController.checkCount;
199+
}
185200
} else {
186-
toStore = data;
201+
currentState = data;
187202
}
188-
localStorage.setItem(this.storageId, JSON.stringify(toStore));
203+
localStorage.setItem(this.storageId, JSON.stringify(currentState));
189204
}
190205

191206
logHorizontalParsonsEvent(hparsonsEvent) {

0 commit comments

Comments
 (0)