Add functionality to create question pools

[HFP-57]
pull/4/head^2
Timothy Lim 2016-10-20 13:36:40 +02:00
parent 024d46da38
commit 3285a8fa4a
2 changed files with 91 additions and 34 deletions

View File

@ -126,6 +126,9 @@ H5P.QuestionSet = function (options, contentId, contentData) {
var endTemplate = new EJS({text: resulttemplate}); var endTemplate = new EJS({text: resulttemplate});
var params = $.extend(true, {}, defaults, options); var params = $.extend(true, {}, defaults, options);
var poolSize; // Number of questions to be pooled into a subset
var poolQuestions; // Questions in a pool
var poolOrder; // Order of questions in a pool
var currentQuestion = 0; var currentQuestion = 0;
var questionInstances = []; var questionInstances = [];
var questionOrder; //Stores order of questions to allow resuming of question set var questionOrder; //Stores order of questions to allow resuming of question set
@ -144,59 +147,7 @@ H5P.QuestionSet = function (options, contentId, contentData) {
questionOrder = contentData.previousState.order; questionOrder = contentData.previousState.order;
} }
var $template = $(template.render(params)); // Create a pool of questions if necessary
// Set overrides for questions
var override;
if (params.override.showSolutionButton || params.override.retryButton) {
override = {};
if (params.override.showSolutionButton) {
// Force "Show solution" button to be on or off for all interactions
override.enableSolutionsButton =
(params.override.showSolutionButton === 'on' ? true : false);
}
if (params.override.retryButton) {
// Force "Retry" button to be on or off for all interactions
override.enableRetry =
(params.override.retryButton === 'on' ? true : false);
}
}
// Instantiate question instances
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 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);
}
/** /**
* Randomizes questions in an array and updates an array containing their order * Randomizes questions in an array and updates an array containing their order
* @param {array} questions * @param {array} questions
@ -229,6 +180,104 @@ H5P.QuestionSet = function (options, contentId, contentData) {
} }
// Create a pool (a subset) of questions if necessary
if (params.poolSize) {
// If a previous pool exists, recreate it
if(contentData.previousState && contentData.previousState.poolOrder) {
poolOrder = contentData.previousState.poolOrder;
// Recreate the pool from the saved data
var pool = [];
for (var i = 0; i < poolOrder.length; i++) {
pool[i] = params.questions[poolOrder[i]];
}
// Replace original questions with just the ones in the pool
params.questions = pool;
}
// Otherwise create a new pool
else {
// Sanitize input
if (params.poolSize > params.questions.length) {
poolSize = params.questions.length;
}
else {
poolSize = params.poolSize;
}
// Randomize and get the results
var poolResult = randomizeQuestionOrdering(params.questions, poolOrder);
poolQuestions = poolResult.questions;
poolOrder = poolResult.questionOrder;
// Discard extra questions
poolQuestions = poolQuestions.slice(0,poolSize);
poolOrder = poolOrder.slice(0,poolSize);
// Replace original questions with just the ones in the pool
params.questions = poolQuestions;
}
}
// Create the html template for the question container
var $template = $(template.render(params));
// Set overrides for questions
var override;
if (params.override.showSolutionButton || params.override.retryButton) {
override = {};
if (params.override.showSolutionButton) {
// Force "Show solution" button to be on or off for all interactions
override.enableSolutionsButton =
(params.override.showSolutionButton === 'on' ? true : false);
}
if (params.override.retryButton) {
// Force "Retry" button to be on or off for all interactions
override.enableRetry =
(params.override.retryButton === 'on' ? true : false);
}
}
// Instantiate question instances
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);
}
// 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,questionOrder);
@ -1015,7 +1064,8 @@ H5P.QuestionSet = function (options, contentId, contentData) {
answers: questionInstances.map(function (qi) { answers: questionInstances.map(function (qi) {
return qi.getCurrentState(); return qi.getCurrentState();
}), }),
order: questionOrder order: questionOrder,
poolOrder: poolOrder
}; };
}; };
}; };

View File

@ -209,6 +209,13 @@
"description": "Enable to randomize the order of questions on display.", "description": "Enable to randomize the order of questions on display.",
"default": false "default": false
}, },
{
"name": "poolSize",
"type": "number",
"label": "Number of questions to be shown:",
"description": "Create a randomized batch of questions from the total.",
"optional": true
},
{ {
"name": "endGame", "name": "endGame",
"type": "group", "type": "group",