2013-01-08 13:04:15 +01:00
|
|
|
// Will render a Question with multiple choices for answers.
|
|
|
|
|
|
|
|
// Options format:
|
|
|
|
// {
|
|
|
|
// title: "Optional title for question box",
|
|
|
|
// question: "Question text",
|
|
|
|
// answers: [{text: "Answer text", correct: false}, ...],
|
|
|
|
// singleAnswer: true, // or false, will change rendered output slightly.
|
|
|
|
// }
|
|
|
|
window.H5P = window.H5P || {};
|
|
|
|
|
|
|
|
H5P.QuestionSet = function (options) {
|
|
|
|
if ( !(this instanceof H5P.QuestionSet) )
|
|
|
|
return new H5P.QuestionSet(options);
|
|
|
|
|
|
|
|
var texttemplate = '' +
|
2013-01-09 16:26:52 +01:00
|
|
|
'<style type="text/css">' +
|
|
|
|
'.dots-container {' +
|
|
|
|
' text-align: center;' +
|
|
|
|
'}' +
|
|
|
|
'.progress-dot {' +
|
|
|
|
' display: inline-block;' +
|
|
|
|
' width: 10px;' +
|
|
|
|
' height: 10px;' +
|
|
|
|
' border-radius: 50%;' +
|
|
|
|
'}' +
|
|
|
|
'.progress-dot.unanswered {' +
|
|
|
|
' background: darkred;' +
|
|
|
|
'}' +
|
|
|
|
'.progress-dot.answered {' +
|
|
|
|
' background: darkgreen;' +
|
|
|
|
'}' +
|
|
|
|
'.progress-dot.current {' +
|
|
|
|
' box-shadow: 0px 0px 6px firebrick;' +
|
|
|
|
'}' +
|
|
|
|
'</style>' +
|
2013-01-08 13:04:15 +01:00
|
|
|
'<div class="questionset">' +
|
2013-01-08 16:16:19 +01:00
|
|
|
' <div class="title"><%= title %></div>' +
|
2013-01-08 13:04:15 +01:00
|
|
|
' <% for (var i=0; i<questions.length; i++) { %>' +
|
2013-01-09 16:26:52 +01:00
|
|
|
' <div class="question-container" id="q-<%= i %>">' +
|
2013-01-08 16:16:19 +01:00
|
|
|
' <div><%= questions[i].machineName %></div>' +
|
2013-01-08 13:04:15 +01:00
|
|
|
' </div>' +
|
|
|
|
' <% } %>' +
|
|
|
|
' <div class="question-footer">' +
|
|
|
|
' <div class="progress">' +
|
|
|
|
' <% if (progressType == "dots") { %>' +
|
2013-01-09 16:26:52 +01:00
|
|
|
' <div class="dots-container">' +
|
|
|
|
' <% for (var i=0; i<questions.length; i++) { %>' +
|
|
|
|
' <span class="progress-dot unanswered" id="qdot-<%= i %>"></span>' +
|
|
|
|
' <%} %>' +
|
|
|
|
' </div>' +
|
2013-01-08 13:04:15 +01:00
|
|
|
' <% } else if (progressType == "textual") { %>' +
|
2013-01-08 16:16:19 +01:00
|
|
|
' <span class="progress-text"></span>' +
|
2013-01-08 13:04:15 +01:00
|
|
|
' <% } %>' +
|
|
|
|
' </div>' +
|
2013-01-08 16:16:19 +01:00
|
|
|
' <button class="prev button"><%= texts.prevButton %></button>' +
|
|
|
|
' <button class="next button"><%= texts.nextButton %></button>' +
|
|
|
|
' <button class="finish button"><%= texts.finishButton %></button>' +
|
2013-01-08 13:04:15 +01:00
|
|
|
' </div>' +
|
|
|
|
'</div>' +
|
|
|
|
'';
|
|
|
|
|
|
|
|
var defaults = {
|
|
|
|
title: "",
|
|
|
|
randomOrder: false,
|
|
|
|
initialQuestion: 0,
|
|
|
|
progressType: 'textual',
|
2013-01-08 16:16:19 +01:00
|
|
|
questions: [],
|
|
|
|
texts: {
|
|
|
|
prevButton: "Previous",
|
|
|
|
nextButton: "Next",
|
|
|
|
finishButton: "Finish",
|
|
|
|
textualProgress: "Question: @current of @total questions"
|
|
|
|
}
|
2013-01-08 13:04:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
var template = new EJS({text: texttemplate});
|
|
|
|
var params = jQuery.extend({}, defaults, options);
|
|
|
|
|
2013-01-09 16:26:52 +01:00
|
|
|
var currentQuestion = 0;
|
2013-01-08 13:04:15 +01:00
|
|
|
var questionInstances = new Array();
|
|
|
|
var allQuestionsAnswered = false;
|
|
|
|
var myDom;
|
|
|
|
|
|
|
|
if (params.randomOrder) {
|
|
|
|
// TODO: Randomize order of questions
|
|
|
|
console.log("TODO: Randomize order of questions");
|
|
|
|
}
|
|
|
|
|
2013-01-09 16:26:52 +01:00
|
|
|
var _allQuestionsAnswered = function () {
|
|
|
|
var answered = true;
|
|
|
|
for (var i = questionInstances.length - 1; i >= 0; i--) {
|
|
|
|
answered = answered && (questionInstances[i]).getAnswerGiven();
|
|
|
|
}
|
|
|
|
return answered;
|
|
|
|
};
|
|
|
|
|
2013-01-08 13:04:15 +01:00
|
|
|
var _showQuestion = function (questionNumber) {
|
|
|
|
// Sanitize input.
|
|
|
|
if (questionNumber < 0) { questionNumber = 0; }
|
|
|
|
if (questionNumber >= params.questions.length) { questionNumber = params.questions.length - 1; }
|
|
|
|
|
|
|
|
$('.prev.button', myDom).attr({'disabled': (questionNumber === 0)});
|
|
|
|
$('.next.button', myDom).attr({'disabled': (questionNumber == params.questions.length-1)});
|
2013-01-09 16:26:52 +01:00
|
|
|
$('.finish.button', myDom).attr({'disabled': !(_allQuestionsAnswered())});
|
2013-01-08 13:04:15 +01:00
|
|
|
|
|
|
|
// Hide all questions
|
2013-01-09 16:26:52 +01:00
|
|
|
$('.question-container', myDom).hide();
|
2013-01-08 13:04:15 +01:00
|
|
|
|
|
|
|
// Reshow the requested question
|
|
|
|
$('#q-' + questionNumber, myDom).show();
|
|
|
|
|
|
|
|
// Update progress indicator
|
|
|
|
// Test if current has been answered.
|
|
|
|
if (params.progressType == 'textual') {
|
2013-01-08 16:16:19 +01:00
|
|
|
$('.progress-text', myDom).text(params.texts.textualProgress.replace("@current", questionNumber+1).replace("@total", params.questions.length));
|
|
|
|
// $('.progress-current', myDom).text("" + (questionNumber+1));
|
2013-01-08 13:04:15 +01:00
|
|
|
} else {
|
|
|
|
$('.progress-dot.current', myDom).removeClass('current');
|
|
|
|
// Set answered/unanswered for current.
|
|
|
|
$('#qdot-' + questionNumber, myDom).addClass('current');
|
2013-01-09 16:26:52 +01:00
|
|
|
if (questionInstances[currentQuestion].getAnswerGiven()) {
|
|
|
|
$('#qdot-' + currentQuestion, myDom).removeClass('unanswered').addClass('answered');
|
|
|
|
}
|
2013-01-08 13:04:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remember where we are
|
|
|
|
currentQuestion = questionNumber;
|
|
|
|
return currentQuestion;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Function for attaching the multichoice to a DOM element.
|
|
|
|
var attach = function (targetId) {
|
|
|
|
// Render own DOM into target.
|
|
|
|
template.update(targetId, params);
|
|
|
|
myDom = jQuery('#' + targetId);
|
|
|
|
|
|
|
|
// Attach questions
|
|
|
|
for (var i=0; i<params.questions.length; i++) {
|
|
|
|
var quest = params.questions[i];
|
|
|
|
// TODO: Render on init, inject in template.
|
|
|
|
var tmp = new (H5P.classFromName(quest.machineName))(quest.options).attach('q-' + i);
|
|
|
|
questionInstances.push(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set event listeners.
|
2013-01-09 16:26:52 +01:00
|
|
|
$('.next.button', myDom).click(function (ev) {
|
2013-01-08 13:04:15 +01:00
|
|
|
_showQuestion(currentQuestion + 1);
|
|
|
|
});
|
2013-01-09 16:26:52 +01:00
|
|
|
$('.prev.button', myDom).click(function (ev) {
|
2013-01-08 13:04:15 +01:00
|
|
|
_showQuestion(currentQuestion - 1);
|
|
|
|
});
|
2013-01-09 16:26:52 +01:00
|
|
|
$('.finish.button', myDom).click(function (ev) {
|
|
|
|
// get total score.
|
|
|
|
// Display result page.
|
|
|
|
// Display animation if present.
|
|
|
|
// Remove DOM. (delete container)
|
|
|
|
myDom.remove();
|
|
|
|
});
|
2013-01-08 13:04:15 +01:00
|
|
|
|
|
|
|
// Hide all but initial Question.
|
|
|
|
_showQuestion(params.initialQuestion);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
var getScore = function () {
|
|
|
|
var score = 0;
|
|
|
|
for (var i = questionInstances.length - 1; i >= 0; i--) {
|
|
|
|
score += questionInstances[i].getScore();
|
|
|
|
}
|
|
|
|
return score;
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
attach: attach, // Attach to DOM object
|
|
|
|
getQuestions: function () {return questionInstances;},
|
|
|
|
getScore: getScore,
|
|
|
|
defaults: defaults // Provide defaults for inspection
|
|
|
|
};
|
|
|
|
};
|