Skip to content

Commit

Permalink
Present sequence questions in order in practice tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonjlee committed Apr 11, 2014
1 parent 1e1abd8 commit 234bde9
Showing 1 changed file with 53 additions and 8 deletions.
61 changes: 53 additions & 8 deletions testindex.html
Expand Up @@ -109,7 +109,7 @@ <h4>Create a New Test</h4>
'<a id="delete" type="button" class="btn btn-sm btn-default">Delete</a>'+
'</div>';

function initUI() {
function initUI(data) {
$subjects = $('#subjects');
for (var i in data) {
var a = $('<a href="#" class="list-group-item"></a>');
Expand All @@ -125,7 +125,7 @@ <h4>Create a New Test</h4>
$('#selectall').click(function() { $('#subjects > .list-group-item').addClass('active'); })
$('#selectnone').click(function() { $('#subjects > .list-group-item').removeClass('active'); })
$('#subjects > .list-group-item').click(function() { $(this).toggleClass('active'); return false; });
$('#create').click(createTest);
$('#create').click(createTest.bind(null, data));

$(document).bind('keypress', 'shift+return', function() { window.location.href = "index.html" });
}
Expand Down Expand Up @@ -189,7 +189,7 @@ <h4>Create a New Test</h4>
})(i);
}
}
function createTest() {
function createTest(data) {
var subjects = $('.list-group-item.active > span#subject').map(function() { return $(this).text(); }).get(),
numquestions = parseInt($('#numquestions').val());

Expand All @@ -201,7 +201,7 @@ <h4>Create a New Test</h4>
test = {
date: (today.getMonth() + 1) + '/' + today.getDate() + '/' + today.getFullYear(),
numquestions: numquestions,
questions: getQuestions(subjects, numquestions),
questions: getQuestions(data, subjects, numquestions),
subjects: subjects
};
if (test.numquestions > test.questions.length) {
Expand Down Expand Up @@ -262,7 +262,7 @@ <h4>Create a New Test</h4>
}
}
}
function filterValidQuestionIds(ids) {
function filterValidQuestionIds(data, ids) {
if ($('#newonly').is(':checked')) {
var used = getUsedQuestionIds();
ids = ids.filter(function(id) { return used.indexOf(id) < 0; });
Expand All @@ -274,7 +274,51 @@ <h4>Create a New Test</h4>
});
return ids;
}
function getQuestions(sections, num) {
function questionIdsInSequence(data, seqId) {
// Collect IDs of all questions with the given sequence ID
// in order across all sections.
var ids = []
$.each(data, function(s, section) {
$.each(section.questions, function(q, question) {
if (question.sequence === seqId) {
ids.push(questionId(s, q));
}
});
});
return ids;
}
function collectSequences(data, ids, limit) {
var i = 0;

// Sequences are questions that need to be presented in a specific order.
// Only check for sequence questions in the first <limit> ids
while (i < Math.min(limit, ids.length)) {
var id = ids[i],
question = questionFromId(data, ids[i]);

if (question.sequence) {
// Get IDs for every question in the sequence in order
var seqIds = questionIdsInSequence(data, question.sequence)

// Swap out seqIds.length questions from the current position with
// the questions in this sequence
for (var j = 0; j < seqIds.length; j++) {
var idx = ids.indexOf(seqIds[j]);
if (idx >= 0) {
var temp = ids[i];
ids[i] = ids[idx];
ids[idx] = temp;
} else {
ids[i] = seqIds[j];
}
i++;
}
}
i++;
}
return ids;
}
function getQuestions(data, sections, num) {
var ids = [];
for (var i in data) {
var section = data[i];
Expand All @@ -285,8 +329,9 @@ <h4>Create a New Test</h4>
}
}
}
ids = filterValidQuestionIds(ids)
ids = filterValidQuestionIds(data, ids)
shuffleArray(ids);
ids = collectSequences(data, ids, num);
return ids.splice(0, num);
}
function shuffleArray(array) {
Expand All @@ -300,7 +345,7 @@ <h4>Create a New Test</h4>
}

// Set up keyboard shortcuts and button actions
initUI();
initUI(data);
-->
</script>
<!-- Google analytics -->
Expand Down

0 comments on commit 234bde9

Please sign in to comment.