Simplify code. Relates to: #HFP-200

ramdomize
Tom Arild Jakobsen 2016-11-11 16:13:13 +01:00
parent e8a37b0632
commit 9250903a5f
1 changed files with 45 additions and 45 deletions

View File

@ -17,6 +17,7 @@ H5P.QuestionSet = (function($, EventDispatcher) {
EventDispatcher.call(this); EventDispatcher.call(this);
self.contentId = contentId; self.contentId = contentId;
self.isEndgameRendered = false; self.isEndgameRendered = false;
self.contentData = contentData || {};
// set default params // set default params
var params = $.extend(true, {}, getDefaults(), options); var params = $.extend(true, {}, getDefaults(), options);
@ -24,18 +25,18 @@ H5P.QuestionSet = (function($, EventDispatcher) {
// add the params to the this scope // add the params to the this scope
$.extend(true, this, params); $.extend(true, this, params);
contentData = contentData || {};
// Bring question set up to date when resuming // Bring question set up to date when resuming
if (contentData.previousState) { if (self.contentData.previousState) {
if (contentData.previousState.progress) { if (self.contentData.previousState.progress) {
currentQuestion = contentData.previousState.progress; currentQuestionIndex = self.contentData.previousState.progress;
} }
questionOrder = contentData.previousState.order; questionOrder = self.contentData.previousState.order;
} }
this.instances = this.init(params, contentId, contentData); this.instances = this.init(params, contentId, self.contentData);
}; };
QuestionSet.prototype = Object.create(EventDispatcher.prototype); QuestionSet.prototype = Object.create(EventDispatcher.prototype);
@ -106,7 +107,7 @@ H5P.QuestionSet = (function($, EventDispatcher) {
var endTemplate = new EJS({text: resulttemplate}); var endTemplate = new EJS({text: resulttemplate});
// TODO Move to QuestionSet // TODO Move to QuestionSet
var currentQuestion = 0; var currentQuestionIndex = 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
var $myDom; var $myDom;
@ -166,7 +167,7 @@ H5P.QuestionSet = (function($, EventDispatcher) {
} }
}; };
var createQuestionInstance = function (questionSet, question, questionIndex, contentId, params, contentData) { var createQuestionInstance = function (questionSet, question, questionIndex, contentId, contentData) {
var previousState = getAnswersPreviousState(contentData, questionIndex); var previousState = getAnswersPreviousState(contentData, questionIndex);
var instance = H5P.newRunnable(question, contentId, undefined, undefined, { var instance = H5P.newRunnable(question, contentId, undefined, undefined, {
previousState: previousState, previousState: previousState,
@ -200,7 +201,7 @@ H5P.QuestionSet = (function($, EventDispatcher) {
questionInstances = questions.map(function (question, index) { questionInstances = questions.map(function (question, index) {
question = overrideBehaviourAndSettings(question, params.override, $template, self); question = overrideBehaviourAndSettings(question, params.override, $template, self);
return createQuestionInstance(self, question, index, contentId, params, contentData); return createQuestionInstance(self, question, index, contentId, contentData);
}); });
// Resize all interactions on resize // Resize all interactions on resize
@ -223,33 +224,24 @@ H5P.QuestionSet = (function($, EventDispatcher) {
// Update button state. // Update button state.
QuestionSet.prototype._updateButtons = function () { QuestionSet.prototype._updateButtons = function () {
var self = this; var self = this;
var currentQuestion = questionInstances[currentQuestionIndex];
var isLast = currentQuestionIndex === (questionInstances.length - 1);
// Verify that current question is answered when backward nav is disabled // Verify that current question is answered when backward nav is disabled
if (self.disableBackwardsNavigation) { if (self.disableBackwardsNavigation) {
if (questionInstances[currentQuestion].getAnswerGiven() var showNextButton = currentQuestion.getAnswerGiven() && !isLast;
&& questionInstances.length - 1 !== currentQuestion) { currentQuestion[showNextButton ? 'showButton' : 'hideButton']('next');
questionInstances[currentQuestion].showButton('next');
}
else {
questionInstances[currentQuestion].hideButton('next');
}
} }
var answered = true; // checks if all questions are answered
for (var i = questionInstances.length - 1; i >= 0; i--) { var isAllAnswered = questionInstances.every(function(question){
answered = answered && (questionInstances[i]).getAnswerGiven(); return question.getAnswerGiven();
} });
if (currentQuestion === (self.questions.length - 1) && if (isLast && currentQuestion) {
questionInstances[currentQuestion]) { currentQuestion[isAllAnswered ? 'showButton' : 'hideButton']('finish');
if (answered) {
questionInstances[currentQuestion].showButton('finish');
}
else {
questionInstances[currentQuestion].hideButton('finish');
}
} }
}; };
@ -270,7 +262,7 @@ H5P.QuestionSet = (function($, EventDispatcher) {
questionNumber = self.questions.length - 1; questionNumber = self.questions.length - 1;
} }
currentQuestion = questionNumber; currentQuestionIndex = questionNumber;
// Hide all questions // Hide all questions
$('.question-container', $myDom).hide().eq(questionNumber).show(); $('.question-container', $myDom).hide().eq(questionNumber).show();
@ -303,7 +295,7 @@ H5P.QuestionSet = (function($, EventDispatcher) {
// Announce question number of total, must use timeout because of buttons logic // Announce question number of total, must use timeout because of buttons logic
setTimeout(function () { setTimeout(function () {
var humanizedProgress = self.texts.readSpeakerProgress var humanizedProgress = self.texts.readSpeakerProgress
.replace('@current', (currentQuestion + 1).toString()) .replace('@current', (currentQuestionIndex + 1).toString())
.replace('@total', questionInstances.length.toString()); .replace('@total', questionInstances.length.toString());
$('.qs-progress-announcer', $myDom) $('.qs-progress-announcer', $myDom)
@ -319,7 +311,7 @@ H5P.QuestionSet = (function($, EventDispatcher) {
// Remember where we are // Remember where we are
self._updateButtons(); self._updateButtons();
self.trigger('resize'); self.trigger('resize');
return currentQuestion; return currentQuestionIndex;
}; };
/** /**
@ -406,7 +398,15 @@ H5P.QuestionSet = (function($, EventDispatcher) {
//Force the last page to be reRendered //Force the last page to be reRendered
self.rendered = false; self.rendered = false;
if (self.randomQuestions) { if(self.poolSize > 0){
var questions = H5P.shuffleArray(self.questions);
questionInstances = questions.map(function (question, index) {
question = overrideBehaviourAndSettings(question, self.override, $template, self);
return createQuestionInstance(self, question, index, self.contentId, self.contentData);
});
}
if (self.randomQuestions || self.poolSize > 0) {
self.renderRandomizedQuestions(); self.renderRandomizedQuestions();
} }
}; };
@ -457,19 +457,19 @@ H5P.QuestionSet = (function($, EventDispatcher) {
QuestionSet.prototype.moveQuestion = function (direction) { QuestionSet.prototype.moveQuestion = function (direction) {
var self = this; var self = this;
if (self.disableBackwardsNavigation && !questionInstances[currentQuestion].getAnswerGiven()) { if (self.disableBackwardsNavigation && !questionInstances[currentQuestionIndex].getAnswerGiven()) {
questionInstances[currentQuestion].hideButton('next'); questionInstances[currentQuestionIndex].hideButton('next');
questionInstances[currentQuestion].hideButton('finish'); questionInstances[currentQuestionIndex].hideButton('finish');
return; return;
} }
_stopQuestion(currentQuestion); _stopQuestion(currentQuestionIndex);
if (currentQuestion + direction >= questionInstances.length) { if (currentQuestionIndex + direction >= questionInstances.length) {
self._displayEndGame(); self._displayEndGame();
} }
else { else {
// Allow movement if backward navigation enabled or answer given // Allow movement if backward navigation enabled or answer given
self._showQuestion(currentQuestion + direction); self._showQuestion(currentQuestionIndex + direction);
} }
}; };
@ -753,8 +753,8 @@ H5P.QuestionSet = (function($, EventDispatcher) {
if (shortVerb === 'interacted' || if (shortVerb === 'interacted' ||
shortVerb === 'answered' || shortVerb === 'answered' ||
shortVerb === 'attempted') { shortVerb === 'attempted') {
self.toggleAnsweredDot(currentQuestion, self.toggleAnsweredDot(currentQuestionIndex,
questionInstances[currentQuestion].getAnswerGiven()); questionInstances[currentQuestionIndex].getAnswerGiven());
self._updateButtons(); self._updateButtons();
} }
if (shortVerb === 'completed') { if (shortVerb === 'completed') {
@ -764,7 +764,7 @@ H5P.QuestionSet = (function($, EventDispatcher) {
if (event.data.statement.context.extensions === undefined) { if (event.data.statement.context.extensions === undefined) {
event.data.statement.context.extensions = {}; event.data.statement.context.extensions = {};
} }
event.data.statement.context.extensions['http://id.tincanapi.com/extension/ending-point'] = currentQuestion + 1; event.data.statement.context.extensions['http://id.tincanapi.com/extension/ending-point'] = currentQuestionIndex + 1;
}); });
// Mark question if answered // Mark question if answered
@ -792,7 +792,7 @@ H5P.QuestionSet = (function($, EventDispatcher) {
if (self.disableBackwardsNavigation && !showingSolutions) { if (self.disableBackwardsNavigation && !showingSolutions) {
return; return;
} }
_stopQuestion(currentQuestion); _stopQuestion(currentQuestionIndex);
_showQuestion($(this).parent().index()); _showQuestion($(this).parent().index());
}; };
@ -829,7 +829,7 @@ H5P.QuestionSet = (function($, EventDispatcher) {
// Hide all but current question // Hide all but current question
self._showQuestion(currentQuestion, true); self._showQuestion(currentQuestionIndex, true);
if (renderSolutions) { if (renderSolutions) {
showSolutions(); showSolutions();
@ -970,7 +970,7 @@ H5P.QuestionSet = (function($, EventDispatcher) {
*/ */
QuestionSet.prototype.getCurrentState = function () { QuestionSet.prototype.getCurrentState = function () {
return { return {
progress: showingSolutions ? questionInstances.length - 1 : currentQuestion, progress: showingSolutions ? questionInstances.length - 1 : currentQuestionIndex,
answers: questionInstances.map(function (qi) { answers: questionInstances.map(function (qi) {
return qi.getCurrentState(); return qi.getCurrentState();
}), }),