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

Commit f3d1a2a

Browse files
committed
New - welcome WW problems to the family
1 parent f5d45dc commit f3d1a2a

File tree

1 file changed

+101
-5
lines changed

1 file changed

+101
-5
lines changed

runestone/webwork/js/webwork.js

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,99 @@
11
import RunestoneBase from "../../common/js/runestonebase";
22

3+
window.wwList = {}; // Multiple Choice dictionary
4+
5+
36
class WebWork extends RunestoneBase {
7+
48
constructor(opts) {
59
super(opts);
10+
this.useRunestoneServices = true;
11+
this.multipleanswers = false;
12+
this.divid = opts.orig.id;
13+
this.correct = null;
14+
this.answerList = [];
15+
this.correctList = [];
16+
this.question = null;
17+
this.caption = "WebWork";
18+
this.containerDiv = opts.orig
19+
//this.addCaption("runestone");
20+
if (this.divid !== "fakeww-ww-rs") {
21+
this.checkServer("webwork", true);
22+
}
623
}
24+
25+
restoreAnswers(data) {
26+
// Restore answers from storage retrieval done in RunestoneBase
27+
// sometimes data.answer can be null
28+
if (!data.answer) {
29+
data.answer = "";
30+
}
31+
// check:actual:3:expected:3:actual:1:expected:1:actual:3:expected:3:actual:1:expected:1:correct:4:count:4:pct:1
32+
this.answers = JSON.parse(data.answer);
33+
}
34+
35+
checkLocalStorage() {
36+
// Repopulates MCMA questions with a user's previous answers,
37+
// which were stored into local storage.
38+
var storedData;
39+
var answers;
40+
if (this.graderactive) {
41+
return;
42+
}
43+
var len = localStorage.length;
44+
var ex = localStorage.getItem(this.localStorageKey());
45+
46+
if (ex !== null) {
47+
try {
48+
storedData = JSON.parse(ex);
49+
answers = storedData.answer.split(":");
50+
} catch (err) {
51+
// error while parsing; likely due to bad value stored in storage
52+
console.log(err.message);
53+
localStorage.removeItem(this.localStorageKey());
54+
return;
55+
}
56+
}
57+
}
58+
59+
setLocalStorage(data) {
60+
var timeStamp = new Date();
61+
var storageObj = {
62+
answer: data.answer,
63+
timestamp: timeStamp,
64+
correct: data.correct,
65+
};
66+
localStorage.setItem(
67+
this.localStorageKey(),
68+
JSON.stringify(storageObj)
69+
);
70+
}
71+
72+
async logCurrentAnswer(sid) {
73+
// todo
74+
}
75+
76+
checkCurrentAnswer() {
77+
78+
}
79+
780
}
881

9-
let rb = new WebWork();
82+
// TODO - it would be better if we can get rid of this, and use the runestone object corresponding to the webwork question to make the logging calls. Or this could be a tiny wrapper because we can look up the object using inputs_ref.problemUUID.replace("-ww-rs","")
83+
let rb = new WebWork({orig:{id:"fakeww-ww-rs"}});
1084

1185
function logWebWork(e, data) {
1286
var correct = false;
1387
let correctCount = 0;
1488
let qCount = 0;
1589
let actString = "check:";
16-
for (let k of Object.keys(data.rh_result.answers)) {
90+
let answerObj = {}
91+
for (let k of Object.keys(data.rh_result.answers).sort()) {
1792
qCount += 1;
1893
if (data.rh_result.answers[k].score == 1) {
1994
correctCount += 1;
2095
}
96+
answerObj[k] = `${data.rh_result.answers[k].original_student_ans}`
2197
actString += `actual:${data.rh_result.answers[k].original_student_ans}:expected:${data.rh_result.answers[k].correct_value}:`;
2298
}
2399
let pct = correctCount / qCount;
@@ -27,9 +103,10 @@ function logWebWork(e, data) {
27103
}
28104
rb.logBookEvent({
29105
event: "webwork",
30-
div_id: data.inputs_ref.problemUUID,
106+
div_id: data.inputs_ref.problemUUID.replace("-ww-rs",""), //todo unmangle problemid
31107
act: actString,
32108
correct: correct,
109+
answer: JSON.stringify(answerObj),
33110
});
34111
}
35112

@@ -41,15 +118,34 @@ function logShowCorrect(e, data) {
41118
});
42119
}
43120

121+
async function getScores(sid, wwId) {
122+
123+
}
124+
44125
if (typeof window.component_factory === "undefined") {
45126
window.component_factory = {};
46127
}
47128

48-
window.component_factory.webwork = function (opts) {
129+
window.component_factory.webwork = function(opts) {
49130
return new WebWork();
50131
};
51132

52-
$(function () {
133+
$(function() {
53134
$("body").on("runestone_ww_check", logWebWork);
54135
$("body").on("runestone_show_correct", logShowCorrect);
55136
});
137+
138+
139+
$(document).on("runestone:login-complete", function () {
140+
$("[data-component=webwork]").each(function (index) {
141+
// MC
142+
var opts = {
143+
orig: this,
144+
useRunestoneServices: eBookConfig.useRunestoneServices,
145+
};
146+
if ($(this).closest("[data-component=timedAssessment]").length == 0) {
147+
// If this element exists within a timed component, don't render it here
148+
window.wwList[this.id] = new WebWork(opts);
149+
}
150+
});
151+
});

0 commit comments

Comments
 (0)