Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
Make radio subexercises of multiple problems work.
Browse files Browse the repository at this point in the history
First, radio choices are defined as sibling to the solution element;
radio setup() uses that list to make a series of inputs with labels.
Multiple setup() copies everything in the solution (usually just a
couple of .sol subexercises) to the solution area. The choices list
needs to be removed not to appear "repeated" on the problem page.

Second, radio precalculates the index of the correct choice in the
setup() and passes this as additional info to its createValidator().
However, multiple passes just the respective .sol element to any of
its subproblem createValidator() functions. For now, radio-in-multiple
falls back to old radio code that does not need the additional info.

Some more things to do (deserving a separate branch):
* verify that choices are always defined just after the radio and
  turn .siblings('.choices') in radio setup() into .next('.choices'),
  so that a multiple can work with more than one radio subproblem;
* move the correct-index logic from radio setup() to createValidator();
* update the note on radios under Answer Types / multiple in the wiki.
  • Loading branch information
wrwrwr committed Nov 22, 2015
1 parent 9a25e47 commit e7f623c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
25 changes: 25 additions & 0 deletions test/answer-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,31 @@
start();
});

asyncTest("multiple with radio (category)", 12, function() {
setupSolutionArea();
var $problem = jQuery("#qunit-fixture .problem").append(
"<div class='solution' data-type='multiple'>" +
"<div class='sol' data-type='radio'>5<\/div>" +
"<ul class='choices' data-category='true'>" +
"<li>6<\/li>" +
"<li>5<\/li>" +
"<li>7<\/li>" +
"<\/ul>" +
"<\/div>"
);

var answerData = Khan.answerTypes.multiple.setup($("#solutionarea"),
$problem.children(".solution"));

testMultipleAnswer(answerData, [false, true, false], "right", "right answer is right");
testMultipleAnswer(answerData, [false, false, false], "empty", "empty answer is empty");
testMultipleAnswer(answerData, [false, false, true], "wrong", "wrong answer is wrong");
testMultipleAnswer(answerData, [true, false, false], "wrong", "wrong answer is wrong");

start();
});


asyncTest("set with no things", 15, function() {
setupSolutionArea();
var $problem = jQuery("#qunit-fixture .problem").append(
Expand Down
9 changes: 8 additions & 1 deletion utils/answer-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,9 @@ Khan.answerTypes = $.extend(Khan.answerTypes, {
// solutionarea
var solarea = $(this).empty();

// If it is a radio it also has choices as a sibling element.
$(this).next(".choices").remove();

// perform setup on each of the areas
var answerData = Khan.answerTypes[type].setup(solarea, sol);

Expand Down Expand Up @@ -1675,6 +1678,10 @@ Khan.answerTypes = $.extend(Khan.answerTypes, {
createValidator: function(solution) {
// TODO(emily): Remove this backwards compatible code sometime
// after 8/2013
// TODO(wrwrwr): Multiple setup calls createValidator() with .sol
// elements of subproblems. Therefore, index and noneIsCorrect will
// be missing (index will actually be a jQuery-defined function).
// This should be resolved before removing the compatibility code.
var correct = extractRawCode(solution.solution || solution);

function showReal() {
Expand Down Expand Up @@ -1706,7 +1713,7 @@ Khan.answerTypes = $.extend(Khan.answerTypes, {
return score;
}

if (guess.index) {
if (guess.index && solution.noneIsCorrect !== undefined) {
// New solutions include information about the correct
// answer like the correct index, etc. We can use that to
// make checking a lot simpler.
Expand Down

0 comments on commit e7f623c

Please sign in to comment.