Refactor createion of question pools. Refers to [H5P-56]

pull/8/head
Timothy Lim 2016-11-14 11:31:15 +01:00
parent 4742476074
commit a95d0be8e0
1 changed files with 51 additions and 76 deletions

View File

@ -1,4 +1,4 @@
var H5P = H5P || {}; H5P = H5P || {};
/** /**
* Will render a Question with multiple choices for answers. * Will render a Question with multiple choices for answers.
@ -153,7 +153,7 @@ H5P.QuestionSet = function (options, contentId, contentData) {
* @param {array} questionOrder * @param {array} questionOrder
* @return {Object.<array, array>} questionOrdering * @return {Object.<array, array>} questionOrdering
*/ */
var randomizeQuestionOrdering = function (questions, questionOrder) { var randomizeQuestionOrdering = function (questions) {
// Save the original order of the questions in a multidimensional array [[question0,0],[question1,1]... // Save the original order of the questions in a multidimensional array [[question0,0],[question1,1]...
var questionOrdering = questions.map(function(questionInstance, index) { return [questionInstance, index] }); var questionOrdering = questions.map(function(questionInstance, index) { return [questionInstance, index] });
@ -172,7 +172,7 @@ H5P.QuestionSet = function (options, contentId, contentData) {
for (var i = 0; i< questionOrdering.length; i++) { for (var i = 0; i< questionOrdering.length; i++) {
// Use a previous order if it exists // Use a previous order if it exists
if(questionOrder) { if(contentData.previousState && contentData.previousState.questionOrder) {
newOrder[i] = questionOrder[questionOrdering[i][1]]; newOrder[i] = questionOrder[questionOrdering[i][1]];
} }
else { else {
@ -180,7 +180,7 @@ H5P.QuestionSet = function (options, contentId, contentData) {
} }
} }
// Return the questions in their new order *with* their new order // Return the questions in their new order *with* their new indexes
return { return {
questions: questions, questions: questions,
questionOrder: newOrder questionOrder: newOrder
@ -188,7 +188,7 @@ H5P.QuestionSet = function (options, contentId, contentData) {
}; };
// Create a pool (a subset) of questions if necessary // Create a pool (a subset) of questions if necessary
if (params.poolSize && params.poolSize < params.questions.length) { if (params.poolSize > 0) {
// If a previous pool exists, recreate it // If a previous pool exists, recreate it
if(contentData.previousState && contentData.previousState.poolOrder) { if(contentData.previousState && contentData.previousState.poolOrder) {
@ -205,7 +205,7 @@ H5P.QuestionSet = function (options, contentId, contentData) {
} }
else { // Otherwise create a new pool else { // Otherwise create a new pool
// Randomize and get the results // Randomize and get the results
var poolResult = randomizeQuestionOrdering(params.questions, poolOrder); var poolResult = randomizeQuestionOrdering(params.questions);
var poolQuestions = poolResult.questions; var poolQuestions = poolResult.questions;
poolOrder = poolResult.questionOrder; poolOrder = poolResult.questionOrder;
@ -239,44 +239,54 @@ H5P.QuestionSet = function (options, contentId, contentData) {
} }
} }
// Instantiate question instances var createQuestionInstancesFromQuestions = function(questions){
for (var i = 0; i < params.questions.length; i++) {
var question; var result = [];
// If a previous order exists, use it // Create question instances from questions
if (questionOrder !== undefined) { // Instantiate question instances
question = params.questions[questionOrder[i]]; for (var i = 0; i < params.questions.length; i++) {
}
else {
// Use a generic order when initialzing for the first time
question = params.questions[i];
}
if (override) { var question;
// Extend subcontent with the overrided settings. // If a previous order exists, use it
$.extend(question.params.behaviour, override); if (questionOrder !== undefined) {
} question = params.questions[questionOrder[i]];
}
else {
// Use a generic order when initialzing for the first time
question = params.questions[i];
}
question.params = question.params || {}; if (override) {
question.params.overrideSettings = question.params.overrideSettings || {}; // Extend subcontent with the overrided settings.
question.params.overrideSettings.$confirmationDialogParent = $template.last(); $.extend(question.params.behaviour, override);
question.params.overrideSettings.instance = this; }
var hasAnswers = contentData.previousState && contentData.previousState.answers;
var questionInstance = H5P.newRunnable(question, contentId, undefined, undefined, question.params = question.params || {};
{ question.params.overrideSettings = question.params.overrideSettings || {};
previousState: hasAnswers ? contentData.previousState.answers[i] : undefined, question.params.overrideSettings.$confirmationDialogParent = $template.last();
parent: self question.params.overrideSettings.instance = this;
var hasAnswers = contentData.previousState && contentData.previousState.answers;
var questionInstance = H5P.newRunnable(question, contentId, undefined, undefined,
{
previousState: hasAnswers ? contentData.previousState.answers[i] : undefined,
parent: self
});
questionInstance.on('resize', function () {
up = true;
self.trigger('resize');
}); });
questionInstance.on('resize', function () { result.push(questionInstance);
up = true; }
self.trigger('resize');
}); return result;
questionInstances.push(questionInstance);
} }
// Create question instances from questions given by params
questionInstances = createQuestionInstancesFromQuestions(params.questions);
// Randomize questions only on instantiation // Randomize questions only on instantiation
if (params.randomQuestions && contentData.previousState === undefined) { if (params.randomQuestions && contentData.previousState === undefined) {
var result = randomizeQuestionOrdering(questionInstances,questionOrder); var result = randomizeQuestionOrdering(questionInstances);
questionInstances = result.questions; questionInstances = result.questions;
questionOrder = result.questionOrder; questionOrder = result.questionOrder;
} }
@ -479,11 +489,10 @@ H5P.QuestionSet = function (options, contentId, contentData) {
rendered = false; rendered = false;
if(params.poolSize > 0){ if(params.poolSize > 0){
questionInstances = [];
// Make new pool from params.questions // Make new pool from params.questions
// Randomize and get the results // Randomize and get the results
var poolResult = randomizeQuestionOrdering(initialParams.questions, poolOrder); var poolResult = randomizeQuestionOrdering(initialParams.questions);
var poolQuestions = poolResult.questions; var poolQuestions = poolResult.questions;
poolOrder = poolResult.questionOrder; poolOrder = poolResult.questionOrder;
@ -494,44 +503,12 @@ H5P.QuestionSet = function (options, contentId, contentData) {
// Replace original questions with just the ones in the pool // Replace original questions with just the ones in the pool
params.questions = poolQuestions; params.questions = poolQuestions;
// Create question instances from questions // Recreate the question instances
// Instantiate question instances questionInstances = createQuestionInstancesFromQuestions(params.questions);
for (var i = 0; i < params.questions.length; i++) {
var question;
// If a previous order exists, use it
if (questionOrder !== undefined) {
question = params.questions[questionOrder[i]];
}
else {
// Use a generic order when initialzing for the first time
question = params.questions[i];
}
if (override) {
// Extend subcontent with the overrided settings.
$.extend(question.params.behaviour, override);
}
question.params = question.params || {};
question.params.overrideSettings = question.params.overrideSettings || {};
question.params.overrideSettings.$confirmationDialogParent = $template.last();
question.params.overrideSettings.instance = this;
var hasAnswers = contentData.previousState && contentData.previousState.answers;
var questionInstance = H5P.newRunnable(question, contentId, undefined, undefined,
{
previousState: hasAnswers ? contentData.previousState.answers[i] : undefined,
parent: self
});
questionInstance.on('resize', function () {
up = true;
self.trigger('resize');
});
questionInstances.push(questionInstance);
}
// Update buttons // Update buttons
initializeQuestion(); initializeQuestion();
} else if (params.randomQuestions) { } else if (params.randomQuestions) {
randomizeQuestions(); randomizeQuestions();
} }
@ -549,7 +526,7 @@ H5P.QuestionSet = function (options, contentId, contentData) {
*/ */
var randomizeQuestions = function () { var randomizeQuestions = function () {
var result = randomizeQuestionOrdering(questionInstances,questionOrder); var result = randomizeQuestionOrdering(questionInstances);
questionInstances = result.questions; questionInstances = result.questions;
questionOrder = result.questionOrder; questionOrder = result.questionOrder;
@ -574,8 +551,6 @@ H5P.QuestionSet = function (options, contentId, contentData) {
question.attach($('.question-container:eq(' + i + ')', $myDom)); question.attach($('.question-container:eq(' + i + ')', $myDom));
//Show buttons if necessary //Show buttons if necessary
console.log('replaceQuestionsInDOM', questionInstances[0].getAnswerGiven());
debugger;
if(questionInstances[questionInstances.length -1] === question if(questionInstances[questionInstances.length -1] === question
&& question.hasButton('finish')) { && question.hasButton('finish')) {
question.showButton('finish'); question.showButton('finish');