2016-02-14 15:02:26 +01:00
|
|
|
var QueryString = function () {
|
|
|
|
// This function is anonymous, is executed immediately and
|
|
|
|
// the return value is assigned to QueryString!
|
|
|
|
var query_string = {};
|
|
|
|
var query = window.location.search.substring(1);
|
|
|
|
var vars = query.split("&");
|
|
|
|
for (var i=0;i<vars.length;i++) {
|
|
|
|
var pair = vars[i].split("=");
|
|
|
|
// If first entry with this name
|
|
|
|
if (typeof query_string[pair[0]] === "undefined") {
|
|
|
|
query_string[pair[0]] = decodeURIComponent(pair[1]);
|
|
|
|
// If second entry with this name
|
|
|
|
} else if (typeof query_string[pair[0]] === "string") {
|
|
|
|
var arr = [ query_string[pair[0]],decodeURIComponent(pair[1]) ];
|
|
|
|
query_string[pair[0]] = arr;
|
|
|
|
// If third or later entry with this name
|
|
|
|
} else {
|
|
|
|
query_string[pair[0]].push(decodeURIComponent(pair[1]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return query_string;
|
|
|
|
}();
|
|
|
|
|
|
|
|
window.onload = init();
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
enable_disable_question();
|
|
|
|
}
|
|
|
|
|
|
|
|
function enable_disable_question() {
|
|
|
|
var elements = document.querySelectorAll("p[id*='enable-']");
|
|
|
|
|
|
|
|
for(var i = 0; i < elements.length; i++) {
|
|
|
|
var element = elements[i];
|
|
|
|
var questionId = element.getAttribute("data-questionId");
|
|
|
|
|
|
|
|
if(element.getAttribute("data-value") == "False") {
|
|
|
|
element.className = element.className.replace("hide", "");
|
|
|
|
//document.getElementById("showSolution_" + questionId).disabled = true;
|
|
|
|
//get_answer(element.getAttribute("data-questionId"));
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-31 21:18:13 +01:00
|
|
|
function toggle_solution(id)
|
|
|
|
{
|
2016-02-14 15:02:26 +01:00
|
|
|
var element = document.getElementById("explanation_" + id);
|
2016-01-31 21:18:13 +01:00
|
|
|
if (element.className == "show") {
|
|
|
|
element.className = "hide";
|
|
|
|
} else {
|
|
|
|
element.className = "show";
|
|
|
|
}
|
2016-02-09 19:51:05 +01:00
|
|
|
}
|
|
|
|
|
2016-02-09 21:35:46 +01:00
|
|
|
function show_solution(id)
|
|
|
|
{
|
2016-02-14 15:02:26 +01:00
|
|
|
var element = document.getElementById("explanation_" + id);
|
2016-02-21 22:41:24 +01:00
|
|
|
if (element.className.indexOf("hide") > -1) {
|
|
|
|
element.className = element.className.replace("hide", "show");
|
2016-02-09 21:35:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-09 19:51:05 +01:00
|
|
|
function get_answer(id) {
|
|
|
|
|
2016-02-14 15:02:26 +01:00
|
|
|
var showSolutionButton = document.getElementById("showSolution_" + id);
|
2016-02-09 21:35:46 +01:00
|
|
|
showSolutionButton.disabled = true;
|
|
|
|
|
|
|
|
show_solution(id);
|
|
|
|
|
2016-02-09 19:51:05 +01:00
|
|
|
var answerRequest=new ajaxRequest()
|
|
|
|
answerRequest.onreadystatechange=function(){
|
|
|
|
if (answerRequest.readyState==4){
|
|
|
|
if (answerRequest.status==200 || window.location.href.indexOf("http")==-1){
|
2016-02-09 21:35:46 +01:00
|
|
|
parseResponse(id, answerRequest.responseText);
|
2016-02-21 22:41:24 +01:00
|
|
|
setButtonToNext(id);
|
2016-02-09 19:51:05 +01:00
|
|
|
}
|
|
|
|
else{
|
2016-02-21 22:41:24 +01:00
|
|
|
alert("Sorry, sending your answer was not successful!");
|
2016-02-09 19:51:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-09 21:35:46 +01:00
|
|
|
|
2016-02-14 15:02:26 +01:00
|
|
|
var questionId=encodeURIComponent(id);
|
|
|
|
var token=encodeURIComponent(QueryString.token);
|
2016-02-14 17:07:34 +01:00
|
|
|
var answers=encodeURIComponent(JSON.stringify(getCheckboxAnswers(id)));
|
2016-02-09 21:35:46 +01:00
|
|
|
|
2016-02-14 15:02:26 +01:00
|
|
|
answerRequest.open("GET", "/api?id="+questionId+"&token="+token+"&answers="+answers, true)
|
2016-02-09 19:51:05 +01:00
|
|
|
answerRequest.send(null)
|
|
|
|
}
|
|
|
|
|
2016-02-21 22:41:24 +01:00
|
|
|
function setButtonToNext(id) {
|
|
|
|
button = document.getElementById('showSolution_' + id);
|
|
|
|
button.className = button.className.replace("show", "hide");
|
|
|
|
|
|
|
|
if(getNextQuestion(id)) {
|
|
|
|
button = document.getElementById('next_' + id);
|
|
|
|
button.className = button.className.replace("hide", "show");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function scrollToNextQuestion(id) {
|
|
|
|
var nextQuestion = getNextQuestion(id);
|
|
|
|
|
|
|
|
if(nextQuestion) {
|
|
|
|
location.href = "#" + nextQuestion.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNextQuestion(id) {
|
|
|
|
var questions = document.getElementsByName("question");
|
|
|
|
|
|
|
|
for(var i = 0; i < questions.length; i++) {
|
|
|
|
if(questions[i].id == "question_" + id) {
|
|
|
|
return questions[i+1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-02-09 19:51:05 +01:00
|
|
|
function getCheckboxAnswers(id) {
|
|
|
|
|
2016-02-14 17:07:34 +01:00
|
|
|
var answers = [];
|
2016-02-09 19:51:05 +01:00
|
|
|
|
|
|
|
var elements = document.getElementsByName("checkbox_" + id);
|
|
|
|
for(var i = 0; i < elements.length; i++) {
|
|
|
|
if(elements[i].checked) {
|
2016-02-14 17:07:34 +01:00
|
|
|
answers.push(parseInt(elements[i].value));
|
2016-02-09 19:51:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return answers;
|
|
|
|
}
|
|
|
|
|
2016-02-09 21:35:46 +01:00
|
|
|
function parseResponse(id, responseText) {
|
|
|
|
|
2016-02-21 22:41:24 +01:00
|
|
|
var correctOptions = JSON.parse(responseText).options_id;
|
|
|
|
var progress = JSON.parse(responseText).progress;
|
2016-02-09 21:35:46 +01:00
|
|
|
|
2016-02-21 22:41:24 +01:00
|
|
|
setCorrectOptions(correctOptions, id)
|
|
|
|
setProgress(progress)
|
|
|
|
}
|
2016-02-09 21:35:46 +01:00
|
|
|
|
2016-02-21 22:41:24 +01:00
|
|
|
function setCorrectOptions(correctOptions, id) {
|
|
|
|
var elements = document.getElementsByName("checkbox_" + id);
|
|
|
|
var holders = document.getElementsByName("checkbox_div_" + id);
|
|
|
|
|
|
|
|
for(var i = 0; i < elements.length; i++) {
|
|
|
|
var element = elements[i];
|
|
|
|
var holder = holders[i];
|
|
|
|
|
|
|
|
var correct = isCorrectOption(element.value);
|
|
|
|
if(correct) {
|
|
|
|
holder.className = holder.className + " list-group-item-success";
|
|
|
|
} else {
|
|
|
|
if(element.checked) {
|
|
|
|
holder.className = holder.className + " list-group-item-danger";;
|
|
|
|
}
|
2016-02-09 21:35:46 +01:00
|
|
|
}
|
2016-02-21 22:41:24 +01:00
|
|
|
element.disabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isCorrectOption(option) {
|
|
|
|
for(var i = 0; i < correctOptions.length; i++) {
|
|
|
|
if(correctOptions[i] == option) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-02-09 21:35:46 +01:00
|
|
|
}
|
2016-02-21 22:41:24 +01:00
|
|
|
return false;
|
2016-02-09 21:35:46 +01:00
|
|
|
}
|
2016-02-21 22:41:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function setProgress(progress) {
|
|
|
|
progressbar = document.getElementById("progressbar");
|
|
|
|
progressbar.style.width = progress + "%";
|
2016-02-09 21:35:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function ajaxRequest() {
|
2016-02-09 19:51:05 +01:00
|
|
|
|
|
|
|
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
|
|
|
|
if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
|
|
|
|
for (var i=0; i<activexmodes.length; i++){
|
|
|
|
try{
|
|
|
|
return new ActiveXObject(activexmodes[i])
|
|
|
|
}
|
|
|
|
catch(e){
|
|
|
|
//suppress error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (window.XMLHttpRequest) // if Mozilla, Safari etc
|
|
|
|
return new XMLHttpRequest()
|
|
|
|
else
|
|
|
|
return false
|
2016-02-14 15:02:26 +01:00
|
|
|
}
|
|
|
|
|