Rename variables in randomize function

[HFP-56]
pull/4/head^2
Timothy Lim 2016-10-19 08:50:04 +02:00
parent 6fdeb26e0d
commit 2ebe9ad987
2 changed files with 21 additions and 25 deletions

View File

@ -198,37 +198,34 @@ H5P.QuestionSet = function (options, contentId, contentData) {
} }
/** /**
* Randomizes elements in an array and updates a map of the order * Randomizes questions in an array and updates an array containing their order
* @param {array} elements * @param {array} questions
* @param {array} map * @param {array} questionOrder
* @return {Object.<array, array>} elements and indexes * @return {Object.<array, array>} questionOrdering
*/ */
var randomizeElements = function (elements, map) { var randomizeQuestionOrdering = function (questions, questionOrder) {
// Save the original order of the elements in a nested array [[element1,0],[element2,1]... // Save the original order of the questions in a nested array [[question0,0],[question1,1]...
var elementsAndIndexes = elements.map(function(object, index) { return [object, index] }); var questionOrdering = questions.map(function(object, index) { return [object, index] });
elementsAndIndexes = H5P.shuffleArray(elementsAndIndexes); questionOrdering = H5P.shuffleArray(questionOrdering);
// Retrieve elements and indexes // Retrieve questions and indexes
elements = elementsAndIndexes.map(d => d[0]); questions = questionOrdering.map(d => d[0]);
map = elementsAndIndexes.map(d => d[1]); questionOrder = questionOrdering.map(d => d[1]);
return { return {
elements:elements, questions:questions,
map:map questionOrder:questionOrder
}; };
} }
// 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);
$('.question-container.h5p-question', $myDom).attr('class',''); questionInstances = result.questions;
questionOrder = result.questionOrder;
var result = randomizeElements(questionInstances,questionOrder);
questionInstances = result.elements;
questionOrder = result.map;
} }
// Resize all interactions on resize // Resize all interactions on resize
@ -445,9 +442,9 @@ H5P.QuestionSet = function (options, contentId, contentData) {
*/ */
var randomizeQuestions = function () { var randomizeQuestions = function () {
var result = randomizeElements(questionInstances,questionOrder); var result = randomizeQuestionOrdering(questionInstances,questionOrder);
questionInstances = result.elements; questionInstances = result.questions;
questionOrder = result.map; questionOrder = result.questionOrder;
// Find all question containers and detach questions from them // Find all question containers and detach questions from them
$('.question-container', $myDom).each(function (){ $('.question-container', $myDom).each(function (){
@ -460,8 +457,7 @@ H5P.QuestionSet = function (options, contentId, contentData) {
var question = questionInstances[i]; var question = questionInstances[i];
// Make sure styles are not being added twice // Make sure styles are not being added twice
var lastClass = $('.question-container:eq(' + i + ')', $myDom).attr("class").split(' ').pop(); $('.question-container:eq(' + i + ')', $myDom).attr('class', 'question-container');
$('.question-container:eq(' + i + ')', $myDom).removeClass(lastClass);
question.attach($('.question-container:eq(' + i + ')', $myDom)); question.attach($('.question-container:eq(' + i + ')', $myDom));

View File

@ -207,7 +207,7 @@
"type": "boolean", "type": "boolean",
"label": "Randomize questions", "label": "Randomize questions",
"description": "Enable to randomize the order of questions on display.", "description": "Enable to randomize the order of questions on display.",
"default": true "default": false
}, },
{ {
"name": "endGame", "name": "endGame",