summaryrefslogtreecommitdiff
path: root/modules/zelfoverhoor/qs.js
blob: e9aedb5d11ce90de8d60ec065458b486e204a436 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
window.addEventListener("error",function(err){
	var str=err.filename+":"+err.lineno+" "+err.error+": "+err.message;
	var xhr=new XMLHttpRequest();
	xhr.open("POST","/zelfoverhoor/error");
	xhr.send(str);
});

var setname=questionset.name;
var setdescription=questionset.description;
var questions=questionset.questions;

var currentidx=0;
var numcorrect=0;

function clearElement(el){
	while(el.lastChild)el.removeChild(el.lastChild);
}

function startQuiz(){
	currentidx=0;
	numcorrect=0;
	showCurrent();
}

function simpleFormat(text){
	return text
		.replace(/&/g,"&")
		.replace(/</g,"&lt;")
		.replace(/>/g,"&gt;")
		.replace(/\n/g,"<br>");
}

function pluralVraag(n){
	if(n==1)return "vraag";
	else return "vragen";
}

function showCurrent(){
	document.getElementById("qcontainer").classList.remove("invisible");
	document.getElementById("rescontainer").classList.add("invisible");
	document.getElementById("answercontainer").classList.add("invisible");
	document.getElementById("showAnswerButton").classList.remove("invisible");
	document.getElementById("progress").innerHTML=(currentidx+1)+"/"+questions.length;

	var qdiv=document.getElementById("question");
	clearElement(qdiv);
	qdiv.innerHTML=simpleFormat(questions[currentidx].q);
}

function showAnswer(){
	document.getElementById("answercontainer").classList.remove("invisible");
	document.getElementById("showAnswerButton").classList.add("invisible");

	var adiv=document.getElementById("answer");
	clearElement(adiv);
	adiv.innerHTML=simpleFormat(questions[currentidx].a);
}

function finishQuiz(){
	document.getElementById("qcontainer").classList.add("invisible");
	document.getElementById("rescontainer").classList.remove("invisible");
	document.getElementById("numcorrect").innerHTML=numcorrect;
	document.getElementById("numcorrectvraag").innerHTML=pluralVraag(numcorrect);
	document.getElementById("numtotal").innerHTML=questions.length;
	document.getElementById("numtotalvraag").innerHTML=pluralVraag(questions.length);
	if(numcorrect==questions.length){
		document.getElementById("allcorrectp").classList.remove("invisible");
	} else {
		document.getElementById("allcorrectp").classList.add("invisible");
	}
	if(numcorrect==0){
		document.getElementById("allwrongp").classList.remove("invisible");
	} else {
		document.getElementById("allwrongp").classList.add("invisible");
	}
}

function advance(corr){
	if(corr)numcorrect++;
	currentidx++;
	if(currentidx<questions.length){
		showCurrent();
	} else {
		finishQuiz();
	}
}

window.addEventListener("load",function(){
	document.getElementById("qsetname").appendChild(document.createTextNode(setname));
	document.getElementById("qsetdescr").appendChild(document.createTextNode(setdescription));
	if(questions.length==0){
		alert("Deze lijst heeft geen vragen, dus er kan weinig overhoord worden, eerlijk gezegd...");
		return;
	}
	startQuiz();
});