Refactor randomizing on initialization

pull/4/head^2
Timothy Lim 2016-10-16 11:14:52 +02:00
parent 491ed24c0b
commit 46753e2b5e
1 changed files with 43 additions and 39 deletions

View File

@ -128,7 +128,7 @@ H5P.QuestionSet = function (options, contentId, contentData) {
var currentQuestion = 0; var currentQuestion = 0;
var questionInstances = []; var questionInstances = [];
var idMap = []; var questionOrder = []; //Stores order of questions to allow resuming of question set
var $myDom; var $myDom;
var scoreBar; var scoreBar;
var up; var up;
@ -136,11 +136,20 @@ H5P.QuestionSet = function (options, contentId, contentData) {
var showingSolutions = false; var showingSolutions = false;
contentData = contentData || {}; contentData = contentData || {};
var answerIndex; var answerIndex;
// Bring question set up to date when resuming
if (contentData.previousState) { if (contentData.previousState) {
if (contentData.previousState.progress) { if (contentData.previousState.progress) {
currentQuestion = contentData.previousState.progress; currentQuestion = contentData.previousState.progress;
} }
answerIndex = contentData.previousState.order; questionOrder = contentData.previousState.order;
}
else {
// Set a generic order of 0...N if not resuming
for (var i = 0; i < params.questions.length; i++) {
questionOrder[i] = i;
}
} }
var $template = $(template.render(params)); var $template = $(template.render(params));
@ -163,7 +172,9 @@ H5P.QuestionSet = function (options, contentId, contentData) {
// Instantiate question instances // Instantiate question instances
for (var i = 0; i < params.questions.length; i++) { for (var i = 0; i < params.questions.length; i++) {
var question = params.questions[answerIndex[i]];
// Create questions in either a generic or randomized order
var question = params.questions[questionOrder[i]];
if (override) { if (override) {
// Extend subcontent with the overrided settings. // Extend subcontent with the overrided settings.
@ -177,7 +188,7 @@ H5P.QuestionSet = function (options, contentId, contentData) {
var hasAnswers = contentData.previousState && contentData.previousState.answers; var hasAnswers = contentData.previousState && contentData.previousState.answers;
var questionInstance = H5P.newRunnable(question, contentId, undefined, undefined, var questionInstance = H5P.newRunnable(question, contentId, undefined, undefined,
{ {
previousState: hasAnswers ? contentData.previousState.answers[answerIndex[i]] : undefined, previousState: hasAnswers ? contentData.previousState.answers[questionOrder[i]] : undefined,
parent: self parent: self
}); });
questionInstance.on('resize', function () { questionInstance.on('resize', function () {
@ -187,38 +198,32 @@ H5P.QuestionSet = function (options, contentId, contentData) {
questionInstances.push(questionInstance); questionInstances.push(questionInstance);
} }
// Save the original order of questions // General purpose function to randomize elements and update a map of their indexes
for (var i = 0; i < questionInstances.length; i++) { // TODO: Move this function to a seperate file
questionInstances[i].originalOrder = i; var randomizeElements = function (elements, map) {
idMap[i] = i;
// Save the original order of the elements in a nested array [[element1,0],[element2,1]...
var tuples = elements.map(function(object, index) { return [object, index] });
tuples = H5P.shuffleArray(tuples);
// Retrieve elements and indexes
elements = tuples.map(d => d[0]);
map = tuples.map(d => d[1]);
return {
elements:elements,
map:map
};
} }
// Randomize questions only once // Randomize questions only on instantiation
if (params.randomQuestions && contentData.previousState === undefined) { if (params.randomQuestions && contentData.previousState === undefined) {
// Randomize order of the questions
questionInstances = H5P.shuffleArray(questionInstances);
// Save new randomized order in case the question is resumed var result = randomizeElements(questionInstances,questionOrder);
for (var i = 0; i < questionInstances.length; i++) { questionInstances = result.elements;
idMap[i] = questionInstances[i].originalOrder; questionOrder = result.map;
}
}
// Restore previous order of questions if necessary
if (contentData && contentData.previousState !== undefined
&& contentData.previousState.order !== undefined) {
var temp = [];
// Fill temporary array in the previous order
for (var i = 0; i < questionInstances.length; i++) {
temp[contentData.previousState.order[i]] = questionInstances[i];
}
// Update current arrays using the temporary array
for (var i = 0; i < questionInstances.length; i++) {
questionInstances[i] = temp[i];
idMap[i] = i;
}
} }
// Resize all interactions on resize // Resize all interactions on resize
@ -417,7 +422,10 @@ H5P.QuestionSet = function (options, contentId, contentData) {
//Force the last page to be reRendered //Force the last page to be reRendered
rendered = false; rendered = false;
randomizeQuestions(); if (params.randomQuestions) {
// randomizeQuestions();
}
}; };
var rendered = false; var rendered = false;
@ -428,16 +436,12 @@ H5P.QuestionSet = function (options, contentId, contentData) {
var randomizeQuestions = function () { var randomizeQuestions = function () {
if (!params.randomQuestions) {
return false;
}
// Randomize order of the questions // Randomize order of the questions
questionInstances = H5P.shuffleArray(questionInstances); questionInstances = H5P.shuffleArray(questionInstances);
// Save new randomized order in case the question is resumed // Save new randomized order in case the question is resumed
for (var i = 0; i < questionInstances.length; i++) { for (var i = 0; i < questionInstances.length; i++) {
idMap[i] = questionInstances[i].originalOrder; questionOrder[i] = questionInstances[i].originalOrder;
} }
// Find all question containers and detach questions from them // Find all question containers and detach questions from them
@ -1022,7 +1026,7 @@ H5P.QuestionSet = function (options, contentId, contentData) {
answers: questionInstances.map(function (qi) { answers: questionInstances.map(function (qi) {
return qi.getCurrentState(); return qi.getCurrentState();
}), }),
order: idMap order: questionOrder
}; };
}; };
}; };