summaryrefslogtreecommitdiff
path: root/modules/zelfoverhoor/docent.js
blob: 635d5c54ee508012f9f6f30160cf9dc9a06f4ccf (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
window.addEventListener("error",function(err){
	try {
		var str="("+err+") ("+JSON.stringify(err)+") "+err.filename+":"+err.lineno+" "+err.error+": "+err.message;
		var xhr=new XMLHttpRequest();
		xhr.open("POST","/zelfoverhoor/error");
		xhr.responseType="text";
		xhr.setRequestHeader("Content-Type", "text/plain");
		xhr.send(str);
	} catch(e){
		console.log("Error sending error:", e);
	}
});

window.onbeforeunload=function(ev){
	if(editorIsOpen){
		var text="Je bent momenteel een vragenset aan het bewerken; weet je zeker dat je het window wilt sluiten?";
		ev.returnValue=text;
		return text;
	}
	return undefined;
};

var questionsets=null;
var currentlyEditing=null;
var editorIsOpen=false;

function getQuestionSets(){
	var xhr=new XMLHttpRequest();
	xhr.onreadystatechange=function(){
		if(xhr.readyState==4){
			questionsets=JSON.parse(xhr.responseText);
			updateQuestionSetsList();
		}
	};
	xhr.open("GET","/zelfoverhoor/docent/sets");
	xhr.responseType="text";
	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","bewerk");
		input.addEventListener("click",(function(id){return function(){
			doEditQSet(id);
		};})(questionsets[i].id));
		td.appendChild(input);
		tr.appendChild(td);

		td=document.createElement("td");
		var input=document.createElement("input");
		input.setAttribute("type","button");
		input.setAttribute("value","verwijder");
		input.setAttribute("style","color:red");
		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 openBlankSetEditor(){
	document.getElementById("newqsetname").value="";
	document.getElementById("newqsetdescr").value="";
	clearElement(document.getElementById("newquestions"));
	document.getElementById("newqsetform").classList.remove("invisible");
	document.getElementById("newqsetvisible").classList.add("invisible");
	currentlyEditing=null;
	editorIsOpen=true;
}

function doNewQSet(){
	openBlankSetEditor();
	document.getElementById("formheader").innerHTML="Nieuwe vragenset";
	addNewQuestion();
}

function receiveEditQSet(qset){
	openBlankSetEditor();
	document.getElementById("newqsetname").value=qset.name;
	document.getElementById("newqsetdescr").value=qset.description;
	for(var i=0;i<qset.questions.length;i++){
		var div=addNewQuestion();
		var tas=div.getElementsByTagName("textarea");
		tas[0].value=qset.questions[i].q;
		tas[1].value=qset.questions[i].a;
	}
	document.getElementById("formheader").innerHTML="Bewerk vragenset ("+qset.id+")";
	currentlyEditing=qset.id;
}

function doEditQSet(id){
	var xhr=new XMLHttpRequest();
	xhr.onreadystatechange=function(){
		if(xhr.readyState==4){
			if(xhr.status==200){
				var obj;
				try {
					obj=JSON.parse(xhr.responseText);
				} catch(e){
					alert("Server stuurde ongeldige data!");
					return
				}
				receiveEditQSet(obj)
			} else if(xhr.status==404){
				alert("Vragenset lijkt niet te bestaan... Misschien even de pagina herladen?");
			} else {
				alert("Vragenset ophalen lijkt niet te lukken... (error code "+xhr.status+")");
			}
		}
	};
	xhr.open("GET","/zelfoverhoor/qsdata/"+id);
	xhr.send();
}

function closeQSetForm(){
	document.getElementById("newqsetform").classList.add("invisible");
	document.getElementById("newqsetvisible").classList.remove("invisible");
	currentlyEditing=null;
	editorIsOpen=false;
}

function renumberQuestions(){
	var parent=document.getElementById("newquestions");
	var ch=parent.children,nc=ch.length;
	for(var i=0;i<nc;i++){
		ch[i].getElementsByClassName("questionindex")[0].innerHTML=i+1;
	}
}

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

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

	if(number>0){
		var insertbtn=document.createElement("input");
		insertbtn.setAttribute("type","button");
		insertbtn.setAttribute("value","Voeg vraag hier toe");
		insertbtn.setAttribute("style","margin-bottom:5px");
		insertbtn.addEventListener("click",function(){
			addNewQuestion();
			var newdiv=parent.children[parent.children.length-1];
			parent.insertBefore(newdiv,div);
			renumberQuestions();
		});
		div.appendChild(insertbtn);
		div.appendChild(document.createElement("br"));
	}

	var span=document.createElement("span");
	span.setAttribute("style","display:inline-block");
	var b=document.createElement("b");
		b.appendChild(document.createTextNode("Vraag "));
		var span2=document.createElement("span");
		span2.classList.add("questionindex");
		span2.innerHTML=number+1;
		b.appendChild(span2);
		b.appendChild(document.createTextNode(":"));
	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(){
		var index=div.getElementsByClassName("questionindex")[0].innerHTML;
		if(confirm("Weet je zeker dat je vraag "+index+" wilt verwijderen?")){
			parent.removeChild(div);
			renumberQuestions();
		}
	});
	span.appendChild(input);
	div.appendChild(span);

	parent.appendChild(div);
	return 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);
			}
		}
	};
	if(currentlyEditing!=null){
		xhr.open("POST","/zelfoverhoor/docent/editset/"+currentlyEditing);
	} else {
		xhr.open("POST","/zelfoverhoor/docent/addset");
	}
	xhr.send(JSON.stringify(obj));
}

function doMaybeCloseForm(){
	var str;
	if(currentlyEditing)str="Weet je zeker dat je de wijzigingen wilt annuleren?";
	else str="Weet je zeker dat je de nieuwe vragenset wilt weggooien?";
	if(confirm(str)){
		closeQSetForm();
	}
}

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