From 568e0992e55a20a03043ac300688ffa25341abb3 Mon Sep 17 00:00:00 2001 From: Timothy Lim Date: Mon, 14 Nov 2016 15:22:43 +0100 Subject: [PATCH] Add comments and ensure questions are created without previous state. [HFP-56] --- js/questionset.js | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/js/questionset.js b/js/questionset.js index f2bd8b4..a2bea2e 100644 --- a/js/questionset.js +++ b/js/questionset.js @@ -146,11 +146,9 @@ H5P.QuestionSet = function (options, contentId, contentData) { questionOrder = contentData.previousState.order; } - // Create a pool of questions if necessary /** * Randomizes questions in an array and updates an array containing their order * @param {array} questions - * @param {array} questionOrder * @return {Object.} questionOrdering */ var randomizeQuestionOrdering = function (questions) { @@ -239,21 +237,26 @@ H5P.QuestionSet = function (options, contentId, contentData) { } } - var createQuestionInstancesFromQuestions = function(questions){ - + /** + * Generates question instances from H5P objects + * + * @param {object} questions H5P content types to be created as instances + * @return {array} Array of questions instances + */ + var createQuestionInstancesFromQuestions = function(questions) { var result = []; // Create question instances from questions // Instantiate question instances - for (var i = 0; i < params.questions.length; i++) { + for (var i = 0; i < questions.length; i++) { var question; // If a previous order exists, use it if (questionOrder !== undefined) { - question = params.questions[questionOrder[i]]; + question = questions[questionOrder[i]]; } else { // Use a generic order when initialzing for the first time - question = params.questions[i]; + question = questions[i]; } if (override) { @@ -451,7 +454,12 @@ H5P.QuestionSet = function (options, contentId, contentData) { * @public */ var resetTask = function () { + + // Clear previous state to ensure questions are created cleanly + contentData.previousState = []; + showingSolutions = false; + for (var i = 0; i < questionInstances.length; i++) { try { questionInstances[i].resetTask(); @@ -533,6 +541,12 @@ H5P.QuestionSet = function (options, contentId, contentData) { replaceQuestionsInDOM(questionInstances); }; + + /** + * Empty the DOM of all questions, attach new questions and update buttons + * + * @param {type} questionInstances Array of questions to be attached to the DOM + */ var replaceQuestionsInDOM = function (questionInstances) { // Find all question containers and detach questions from them @@ -803,7 +817,11 @@ H5P.QuestionSet = function (options, contentId, contentData) { }); }; - // Function for attaching the multichoice to a DOM element. + + /** + * Initialize a question and attach it to the DOM + * + */ function initializeQuestion() { // Attach questions for (var i = 0; i < questionInstances.length; i++) {