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 pathmchoice.js
More file actions
576 lines (504 loc) · 20 KB
/
mchoice.js
File metadata and controls
576 lines (504 loc) · 20 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*==========================================
======== Master mchoice.js =========
============================================
=== This file contains the JS for the ===
=== Runestone multiple choice component. ===
============================================
=== Created By ===
=== Isaiah Mayerchak ===
=== and ===
=== Kirby Olson ===
=== 6/4/15 ===
==========================================*/
var mcList = {}; // Multiple Choice dictionary
// MC constructor
function MultipleChoice (opts) {
if (opts) {
this.init(opts);
}
}
MultipleChoice.prototype = new RunestoneBase();
/*===================================
=== Setting MC variables ===
===================================*/
MultipleChoice.prototype.init = function (opts) {
RunestoneBase.apply(this, arguments);
RunestoneBase.prototype.init.apply(this, arguments);
var orig = opts.orig; // entire <ul> element
this.origElem = orig;
this.useRunestoneServices = opts.useRunestoneServices;
this.multipleanswers = false;
this.divid = orig.id;
this.caption = 'Multiple Choice'
this.showcomparebutton = $(orig).data('showcomparebutton');
if ($(this.origElem).data("multipleanswers") === true) {
this.multipleanswers = true;
}
this.children = this.origElem.childNodes;
this.random = false;
if ($(this.origElem).is("[data-random]")) {
this.random = true;
}
this.correct = null;
this.answerList = [];
this.correctList = [];
this.correctIndexList = [];
this.feedbackList = [];
this.question = null;
this.findAnswers();
this.findQuestion();
this.findFeedbacks();
this.createCorrectList();
this.createMCForm();
this.checkServer("mChoice");
this.addCaption('runestone');
};
/*====================================
==== Functions parsing variables ====
==== out of intermediate HTML ====
====================================*/
MultipleChoice.prototype.findQuestion = function () { // Takes full text
var delimiter;
for (var i = 0; i < this.origElem.childNodes.length; i++) {
if (this.origElem.childNodes[i].nodeName === "LI") {
delimiter = this.origElem.childNodes[i].outerHTML;
break;
}
}
var fulltext = $(this.origElem).html();
var temp = fulltext.split(delimiter);
this.question = temp[0];
};
MultipleChoice.prototype.findAnswers = function () {
// Creates answer objects and pushes them to answerList
// format: ID, Correct bool, Content (text)
var ChildAnswerList = [];
for (var i = 0; i < this.children.length; i++) {
if ($(this.children[i]).is("[data-component=answer]")) {
ChildAnswerList.push(this.children[i]);
}
}
for (var j = 0; j < ChildAnswerList.length; j++) {
var answer_id = $(ChildAnswerList[j]).attr("id");
var is_correct = false;
if ($(ChildAnswerList[j]).is("[data-correct]")) { // If data-correct attribute exists, answer is correct
is_correct = true;
}
var answer_text = $(ChildAnswerList[j]).html();
var answer_object = {id: answer_id, correct: is_correct, content: answer_text};
this.answerList.push(answer_object);
}
};
MultipleChoice.prototype.findFeedbacks = function () {
for (var i = 0; i < this.children.length; i++) {
if ($(this.children[i]).is("[data-component=feedback]")) {
this.feedbackList.push(this.children[i].innerHTML);
}
}
};
MultipleChoice.prototype.createCorrectList = function () {
// Creates array that holds the ID"s of correct answers
// Also populates an array that holds the indeces of correct answers
for (var i = 0; i < this.answerList.length; i++) {
if (this.answerList[i].correct) {
this.correctList.push(this.answerList[i].id);
this.correctIndexList.push(i);
}
}
};
/*===========================================
==== Functions generating final HTML ====
===========================================*/
MultipleChoice.prototype.createMCForm = function () {
this.renderMCContainer();
this.renderMCForm(); // renders the form with options and buttons
this.renderMCfeedbackDiv();
// replaces intermediate HTML with rendered HTML
$(this.origElem).replaceWith(this.containerDiv);
};
MultipleChoice.prototype.renderMCContainer = function () {
this.containerDiv = document.createElement("div");
$(this.containerDiv).html(this.question);
$(this.containerDiv).addClass(this.origElem.getAttribute("class"));
this.containerDiv.id = this.divid;
};
MultipleChoice.prototype.renderMCForm = function () {
this.optsForm = document.createElement("form");
this.optsForm.id = this.divid + "_form";
$(this.optsForm).attr({
"method": "get",
"action": "",
"onsubmit": "return false;"
});
// generate form options
this.renderMCFormOpts();
this.renderMCFormButtons();
// Append the form to the container
this.containerDiv.appendChild(this.optsForm);
};
MultipleChoice.prototype.renderMCFormOpts = function () {
// creates input DOM elements
this.optionArray = []; // array with an object for each option containing the input and label for that option
var input_type = "radio";
if (this.multipleanswers) {
input_type = "checkbox";
}
// this.indexArray is used to index through the answers
// it is just 0-n normally, but the order is shuffled if the random option is present
this.indexArray = [];
for (var i = 0; i < this.answerList.length; i++) {
this.indexArray.push(i);
}
if (this.random) {
this.randomizeAnswers();
}
for (var j = 0; j < this.answerList.length; j++) {
var k = this.indexArray[j];
var optid = this.divid + "_opt_" + k;
// Create the label for the input
var label = document.createElement("label");
// If the content begins with a ``<p>``, put the label inside of it. (Sphinx 2.0 puts all content in a ``<p>``, while Sphinx 1.8 doesn't).
var content = this.answerList[k].content;
var prefix = '';
if (content.startsWith('<p>')) {
prefix = '<p>';
content = content.slice(3);
}
$(label).html(`${prefix}<input type="${input_type}" name="group1" value=${k} id=${optid}>${String.fromCharCode('A'.charCodeAt(0) + j)}. ${content}`);
// create the object to store in optionArray
var optObj = {
input: $(label).find('input')[0],
label: label
};
this.optionArray.push(optObj);
// add the option to the form
this.optsForm.appendChild(label);
this.optsForm.appendChild(document.createElement("br"));
}
};
MultipleChoice.prototype.renderMCFormButtons = function () {
// submit and compare me buttons
// Create submit button
this.submitButton = document.createElement("button");
this.submitButton.textContent = "Check Me";
$(this.submitButton).attr({
"class": "btn btn-success",
"name": "do answer",
"type": "button"
});
if (this.multipleanswers) {
this.submitButton.addEventListener("click", function () {
this.processMCMASubmission(true);
}.bind(this), false);
} else {
this.submitButton.addEventListener("click", function (ev) {
ev.preventDefault();
this.processMCMFSubmission(true);
}.bind(this), false);
} // end else
this.optsForm.appendChild(this.submitButton);
// Create compare button
if (this.useRunestoneServices && this.showcomparebutton) {
this.compareButton = document.createElement("button");
$(this.compareButton).attr({
"class": "btn btn-default",
"id": this.divid + "_bcomp",
"disabled": "",
"name": "compare"
});
this.compareButton.textContent = "Compare me";
this.compareButton.addEventListener("click", function () {
this.compareAnswers(this.divid);
}.bind(this), false);
this.optsForm.appendChild(this.compareButton);
}
};
MultipleChoice.prototype.renderMCfeedbackDiv = function () {
this.feedBackDiv = document.createElement("div");
this.feedBackDiv.id = this.divid + "_feedback";
this.containerDiv.appendChild(document.createElement("br"));
this.containerDiv.appendChild(this.feedBackDiv);
};
MultipleChoice.prototype.randomizeAnswers = function () {
// Makes the ordering of the answer choices random
var currentIndex = this.indexArray.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (currentIndex !== 0) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = this.indexArray[currentIndex];
this.indexArray[currentIndex] = this.indexArray[randomIndex];
this.indexArray[randomIndex] = temporaryValue;
var temporaryFeedback = this.feedbackList[currentIndex];
this.feedbackList[currentIndex] = this.feedbackList[randomIndex];
this.feedbackList[randomIndex] = temporaryFeedback;
}
};
/*===================================
=== Checking/loading from storage ===
===================================*/
MultipleChoice.prototype.restoreAnswers = function (data) {
// Restore answers from storage retrieval done in RunestoneBase
// sometimes data.answer can be null
if (!data.answer) {
data.answer = "";
}
var answers = data.answer.split(",");
for (var a = 0; a < answers.length; a++) {
var index = answers[a];
for (var b = 0; b < this.optionArray.length; b++) {
if (this.optionArray[b].input.value == index) {
$(this.optionArray[b].input).attr("checked", "true");
}
}
}
if (this.multipleanswers) {
this.processMCMASubmission(false);
} else {
this.processMCMFSubmission(false);
}
};
MultipleChoice.prototype.checkLocalStorage = function () {
// Repopulates MCMA questions with a user's previous answers,
// which were stored into local storage.
if (this.graderactive) {
return;
}
var len = localStorage.length;
if (len > 0) {
var ex = localStorage.getItem(this.localStorageKey());
if (ex !== null) {
try {
var storedData = JSON.parse(ex);
var answers = storedData.answer.split(",");
} catch (err) {
// error while parsing; likely due to bad value stored in storage
console.log(err.message);
localStorage.removeItem(this.localStorageKey());
return;
}
for (var a = 0; a < answers.length; a++) {
var index = answers[a];
for (var b = 0; b < this.optionArray.length; b++) {
if (this.optionArray[b].input.value == index) {
$(this.optionArray[b].input).attr("checked", "true");
}
}
}
if (this.useRunestoneServices) {
this.enableMCComparison();
this.getSubmittedOpts(); // to populate givenlog for logging
if (this.multipleanswers) {
this.logMCMAsubmission(storedData);
} else {
this.logMCMFsubmission(storedData);
}
}
}
}
};
MultipleChoice.prototype.setLocalStorage = function (data) {
var timeStamp = new Date();
var storageObj = {"answer": data.answer, "timestamp": timeStamp, "correct": data.correct};
localStorage.setItem(this.localStorageKey(), JSON.stringify(storageObj));
};
/*===============================
=== Processing MC Submissions ===
===============================*/
MultipleChoice.prototype.processMCMASubmission = function (logFlag) {
// Called when the submit button is clicked
this.getSubmittedOpts(); // make sure this.givenArray is populated
this.scoreMCMASubmission();
this.setLocalStorage({"correct": (this.correct ? "T" : "F"), "answer": this.givenArray.join(",")});
if (logFlag) {
var answer = this.givenArray.join(",");
this.logMCMAsubmission({"answer": answer, "correct": this.correct});
}
this.renderMCMAFeedBack();
if (this.useRunestoneServices) {
this.enableMCComparison();
}
};
MultipleChoice.prototype.getSubmittedOpts = function () {
var given;
this.singlefeedback = ""; // Used for MCMF questions
this.feedbackString = ""; // Used for MCMA questions
this.givenArray = [];
this.givenlog = "";
var buttonObjs = this.optsForm.elements.group1;
for (var i = 0; i < buttonObjs.length; i++) {
if (buttonObjs[i].checked) {
given = buttonObjs[i].value;
this.givenArray.push(given);
this.feedbackString += '<li value="' + (i + 1) + '">' + this.feedbackList[i] + "</li>";
this.givenlog += given + ",";
this.singlefeedback = this.feedbackList[i];
}
}
this.givenArray.sort();
};
MultipleChoice.prototype.scoreMCMASubmission = function () {
this.correctCount = 0;
var correctIndex = 0;
var givenIndex = 0;
while (correctIndex < this.correctIndexList.length && givenIndex < this.givenArray.length) {
if (this.givenArray[givenIndex] < this.correctIndexList[correctIndex]) {
givenIndex++;
} else if (this.givenArray[givenIndex] == this.correctIndexList[correctIndex]) {
this.correctCount++;
givenIndex++;
correctIndex++;
} else {
correctIndex++;
}
}
var numGiven = this.givenArray.length;
var numCorrect = this.correctCount;
var numNeeded = this.correctList.length;
this.correct = (numCorrect === numNeeded) && (numNeeded === numGiven);
};
MultipleChoice.prototype.logMCMAsubmission = function (data) {
var answer = data.answer;
var correct = data.correct;
var logAnswer = "answer:" + answer + ":" + (correct == "T" ? "correct" : "no");
this.logBookEvent({"event": "mChoice", "act": logAnswer, "answer":answer, "correct": correct, "div_id": this.divid});
};
MultipleChoice.prototype.renderMCMAFeedBack = function () {
var answerStr = "answers";
var numGiven = this.givenArray.length;
if (numGiven === 1) {
answerStr = "answer";
}
var numCorrect = this.correctCount;
var numNeeded = this.correctList.length;
var feedbackText = this.feedbackString;
if (this.correct) {
$(this.feedBackDiv).html('✔️ <ol type="A">' + feedbackText + "</ul>");
$(this.feedBackDiv).attr("class", "alert alert-info");
} else {
$(this.feedBackDiv).html("✖️ " + "You gave " + numGiven +
" " + answerStr + " and got " + numCorrect + " correct of " +
numNeeded + ' needed.<ol type="A">' + feedbackText + "</ul>");
$(this.feedBackDiv).attr("class", "alert alert-danger");
}
};
MultipleChoice.prototype.processMCMFSubmission = function (logFlag) {
// Called when the submit button is clicked
this.getSubmittedOpts(); // make sure this.givenArray is populated
this.scoreMCMFSubmission();
this.setLocalStorage({"correct": (this.correct ? "T" : "F"), "answer": this.givenArray.join(",")});
if (logFlag) {
this.logMCMFsubmission();
}
this.renderMCMFFeedback(this.givenArray[0] == this.correctIndexList[0], this.singlefeedback);
if (this.useRunestoneServices) {
this.enableMCComparison();
}
};
MultipleChoice.prototype.scoreMCMFSubmission = function () {
if (this.givenArray[0] == this.correctIndexList[0]) {
this.correct = true;
} else if (this.givenArray[0] != null) { // if given is null then the question wasn"t answered and should be counted as skipped
this.correct = false;
}
};
MultipleChoice.prototype.logMCMFsubmission = function () {
var answer = this.givenArray[0];
var correct = (this.givenArray[0] == this.correctIndexList[0] ? "T" : "F");
var logAnswer = "answer:" + answer + ":" + (correct == "T" ? "correct" : "no"); // backward compatible
this.logBookEvent({"event": "mChoice", "act": logAnswer, "answer": answer, "correct": correct, "div_id": this.divid});
};
MultipleChoice.prototype.renderMCMFFeedback = function (correct, feedbackText) {
if (correct) {
$(this.feedBackDiv).html("✔️ " + feedbackText);
$(this.feedBackDiv).attr("class", "alert alert-info"); // use blue for better red/green blue color blindness
} else {
if (feedbackText == null) {
feedbackText = "";
}
$(this.feedBackDiv).html("✖️ " + feedbackText);
$(this.feedBackDiv).attr("class", "alert alert-danger");
}
};
MultipleChoice.prototype.enableMCComparison = function () {
this.compareButton.disabled = false;
};
MultipleChoice.prototype.instructorMchoiceModal = function (data) {
// data.reslist -- student and their answers
// data.answerDict -- answers and count
// data.correct - correct answer
var res = "<table><tr><th>Student</th><th>Answer(s)</th></tr>";
for (var i in data) {
res += "<tr><td>" + data[i][0] + "</td><td>" + data[i][1] + "</td></tr>";
}
res += "</table>";
return res;
};
MultipleChoice.prototype.compareModal = function (data, status, whatever) {
var datadict = eval(data)[0];
var answers = datadict.answerDict;
var misc = datadict.misc;
var kl = Object.keys(answers).sort();
var body = "<table>";
body += "<tr><th>Answer</th><th>Percent</th></tr>";
var theClass = "";
for (var k in kl) {
if (kl[k] === misc.correct) {
theClass = "success";
} else {
theClass = "info";
}
body += "<tr><td>" + kl[k] + "</td><td class='compare-me-progress'>";
var pct = answers[kl[k]] + "%";
body += "<div class='progress'>";
body += " <div class='progress-bar progress-bar-" + theClass + "' style='width:" + pct + ";'>" + pct;
body += " </div>";
body += "</div></td></tr>";
}
body += "</table>";
if (misc["yourpct"] !== "unavailable") {
body += "<br /><p>You have " + misc["yourpct"] + "% correct for all questions</p>";
}
if (datadict.reslist !== undefined) {
body += this.instructorMchoiceModal(datadict.reslist);
}
var html = "<div class='modal fade'>" +
" <div class='modal-dialog compare-modal'>" +
" <div class='modal-content'>" +
" <div class='modal-header'>" +
" <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>×</button>" +
" <h4 class='modal-title'>Distribution of Answers</h4>" +
" </div>" +
" <div class='modal-body'>" +
body +
" </div>" +
" </div>" +
" </div>" +
"</div>";
var el = $(html);
el.modal();
};
MultipleChoice.prototype.compareAnswers = function () {
var data = {};
data.div_id = this.divid;
data.course = eBookConfig.course;
jQuery.get(eBookConfig.ajaxURL + "getaggregateresults", data, this.compareModal.bind(this));
};
/*=================================
== Find the custom HTML tags and ==
== execute our code on them ==
=================================*/
$(document).bind("runestone:login-complete", function () {
$("[data-component=multiplechoice]").each(function (index) { // MC
var opts = {"orig": this, 'useRunestoneServices':eBookConfig.useRunestoneServices};
if ($(this).closest('[data-component=timedAssessment]').length == 0) { // If this element exists within a timed component, don't render it here
mcList[this.id] = new MultipleChoice(opts);
}
});
});
if (typeof component_factory === 'undefined') {
component_factory = {}
}
component_factory['multiplechoice'] = function(opts) { return new MultipleChoice(opts)}