summaryrefslogtreecommitdiff
path: root/modules/zelfoverhoor/docent.js
blob: 7987351323c2fec51c1a6e0ba2664afe981ed5bf (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
var questionsets=null;

function getQuestionSets(){
	var xhr=new XMLHttpRequest();
	xhr.onreadystatechange=function(){
		if(xhr.readyState==4){
			questionsets=JSON.parse(xhr.responseText);
			updateQuestionSetsList();
		}
	};
	xhr.responseType="text";
	xhr.open("GET","/zelfoverhoor/docent/sets");
	xhr.send();
}

function updateQuestionSetsList(){
	var tbody=document.getElementById("qsets");
	clearElement(tbody);
	if(questionsets.length==0){
		var tr=document.createElement("tr");
		var td=document.createElement("td");
		td.appendChild(document.createTextNode("Nog geen vragensets..."));
		td.setAttribute("colspan","4");
		tr.appendChild(td);
		tbody.appendChild(tr);
	}
	for(var i=0;i<questionsets.length;i++){
		var tr=document.createElement("tr");
		td=document.createElement("td");
		var a=document.createElement("a");
		a.setAttribute("href",location.origin+"/zelfoverhoor/qs/"+questionsets[i].id);
		a.setAttribute("target","_blank");
		a.innerHTML=questionsets[i].id;
		td.appendChild(a);
		tr.appendChild(td);
		td=document.createElement("td");
		td.appendChild(document.createTextNode(questionsets[i].name));
		tr.appendChild(td);
		td=document.createElement("td");
		td.appendChild(document.createTextNode(questionsets[i].description));
		tr.appendChild(td);
		td=document.createElement("td");
		td.appendChild(document.createTextNode(questionsets[i].questions.length));
		tr.appendChild(td);
		td=document.createElement("td");
		var input=document.createElement("input");
		input.setAttribute("type","button");
		input.setAttribute("value","verwijder");
		input.addEventListener("click",(function(id,name){return function(){
			if(confirm("Weet je zeker dat je de vragenset '"+name+"' wilt verwijderen?")){
				var xhr=new XMLHttpRequest();
				xhr.onreadystatechange=function(){
					if(xhr.readyState==4){
						if(xhr.status==200){
							getQuestionSets();
						} else if(xhr.status==404){
							alert("Vragenset kon niet worden gevonden... Misschien even de pagina herladen?");
						} else {
							alert("Onbekende error: "+xhr.responseText);
						}
					}
				};
				xhr.open("POST","/zelfoverhoor/docent/deleteset");
				xhr.send(id);
			}
		};})(questionsets[i].id,questionsets[i].name));
		td.appendChild(input);
		tr.appendChild(td);
		tbody.appendChild(tr);
	}
}

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

function doNewQSet(){
	document.getElementById("newqsetname").value="";
	document.getElementById("newqsetdescr").value="";
	clearElement("newquestions");
	document.getElementById("newqsetform").classList.remove("invisible");
	document.getElementById("newqsetvisible").classList.add("invisible");
	addNewQuestion();
}

function closeQSetForm(){
	document.getElementById("newqsetform").classList.add("invisible");
	document.getElementById("newqsetvisible").classList.remove("invisible");
}

function addNewQuestion(){
	var parent=document.getElementById("newquestions");

	var div=document.createElement("div");
	div.classList.add("newquestioncontainer");

	var span=document.createElement("span");
	span.setAttribute("style","display:inline-block");
	var b=document.createElement("b");
	b.appendChild(document.createTextNode("Vraag:"));
	span.appendChild(b);
	span.appendChild(document.createElement("br"));
	var qta=document.createElement("textarea");
	span.appendChild(qta);
	div.appendChild(span);

	span=document.createElement("span");
	span.setAttribute("style","display:inline-block");
	b=document.createElement("b");
	b.appendChild(document.createTextNode("Antwoord:"));
	span.appendChild(b);
	span.appendChild(document.createElement("br"));
	span.appendChild(document.createElement("textarea"));
	div.appendChild(span);

	span=document.createElement("span");
	var input=document.createElement("input");
	input.setAttribute("type","button");
	input.setAttribute("value","verwijder vraag");
	input.setAttribute("style","vertical-align:50px");
	input.addEventListener("click",function(){
		if(confirm("Weet je zeker dat je de volgende vraag wil verwijderen?\n"+qta.value)){
			parent.removeChild(div);
		}
	});
	span.appendChild(input);
	div.appendChild(span);

	parent.appendChild(div);
}

function submitQSet(){
	var name=document.getElementById("newqsetname").value.trim();
	var description=document.getElementById("newqsetdescr").value.trim();
	if(name==""||description==""){
		alert("Naam en beschrijving zijn nodig.");
		return;
	}

	var div=document.getElementById("newquestions");
	var ch=div.children,nq=ch.length;
	var questions=[];
	for(var i=0;i<nq;i++){
		var tas=ch[i].getElementsByTagName("textarea");
		var q={"q": tas[0].value.trim(), "a": tas[1].value.trim()};
		if(q.q==""||q.a==""){
			alert("Een van de vragen of antwoorden is leeg; graag even vullen of de vraag verwijderen.");
			return;
		}
		questions.push(q);
	}

	var obj={"name": name, "description": description, "questions": questions};

	var xhr=new XMLHttpRequest();
	xhr.onreadystatechange=function(){
		if(xhr.readyState==4){
			if(xhr.status==200){
				getQuestionSets();
				closeQSetForm();
			} else {
				alert("Vragenset lijkt niet succesvol toegevoegd te zijn...\n"+xhr.responseText);
			}
		}
	};
	xhr.open("POST","/zelfoverhoor/docent/addset");
	xhr.send(JSON.stringify(obj));
}

window.addEventListener("load",function(){
	getQuestionSets();
});