summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-11-23 22:19:49 +0100
committertomsmeding <tom.smeding@gmail.com>2017-11-23 22:19:49 +0100
commit4d4d8f001db3a8d2025763819ef171a11505a6cf (patch)
treeff913ce9c08196e4bf0ada8e8e8d4659d0c14294
parent7068ba98fb668d7ec32ab3be39b2928cf2601522 (diff)
zelfoverhoor: Editing and various improvements
-rw-r--r--modules/zelfoverhoor/TODO.txt1
-rw-r--r--modules/zelfoverhoor/docent.html4
-rw-r--r--modules/zelfoverhoor/docent.js83
-rw-r--r--modules/zelfoverhoor/zelfoverhoor.js88
4 files changed, 150 insertions, 26 deletions
diff --git a/modules/zelfoverhoor/TODO.txt b/modules/zelfoverhoor/TODO.txt
new file mode 100644
index 0000000..4997eb3
--- /dev/null
+++ b/modules/zelfoverhoor/TODO.txt
@@ -0,0 +1 @@
+- numbering of questions in docent edit view
diff --git a/modules/zelfoverhoor/docent.html b/modules/zelfoverhoor/docent.html
index 224384d..95a5da7 100644
--- a/modules/zelfoverhoor/docent.html
+++ b/modules/zelfoverhoor/docent.html
@@ -43,7 +43,8 @@ td, th {
<input type="button" id="newqsetvisible" onclick="doNewQSet()" value="Nieuwe vragenset maken">
<br>
<div id="newqsetform" class="invisible">
- <h2>Nieuwe vragenset</h2>
+ <h2 id="formheader">Nieuwe vragenset</h2>
+ <p><i>Let op: vergeet niet om op "Vragenset klaar" te klikken als je klaar bent!</i></p>
<b>Naam</b>:<br><input type="text" id="newqsetname" size="40"><br><br>
<b>Beschrijving</b>:<br><textarea id="newqsetdescr" style="width:300px;height:60px"></textarea><br><br>
<div id="newquestions"></div>
@@ -51,6 +52,7 @@ td, th {
<br>
<div style="background-color:#d8d8d8;padding:10px;display:inline-block">
<input type="button" onclick="submitQSet()" value="Vragenset klaar">
+ <input type="button" onclick="doMaybeCloseForm()" value="Annuleren" style="color:red;margin-left:20px">
</div>
</div>
</body>
diff --git a/modules/zelfoverhoor/docent.js b/modules/zelfoverhoor/docent.js
index 7987351..96ec38b 100644
--- a/modules/zelfoverhoor/docent.js
+++ b/modules/zelfoverhoor/docent.js
@@ -1,4 +1,5 @@
var questionsets=null;
+var currentlyEditing=null;
function getQuestionSets(){
var xhr=new XMLHttpRequest();
@@ -26,6 +27,7 @@ function updateQuestionSetsList(){
}
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);
@@ -33,19 +35,34 @@ function updateQuestionSetsList(){
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();
@@ -66,6 +83,7 @@ function updateQuestionSetsList(){
};})(questionsets[i].id,questionsets[i].name));
td.appendChild(input);
tr.appendChild(td);
+
tbody.appendChild(tr);
}
}
@@ -75,18 +93,63 @@ function clearElement(el){
while((c=el.lastChild))el.removeChild(c);
}
-function doNewQSet(){
+function openBlankSetEditor(){
document.getElementById("newqsetname").value="";
document.getElementById("newqsetdescr").value="";
- clearElement("newquestions");
+ clearElement(document.getElementById("newquestions"));
document.getElementById("newqsetform").classList.remove("invisible");
document.getElementById("newqsetvisible").classList.add("invisible");
+ currentlyEditing=null;
+}
+
+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;
}
function addNewQuestion(){
@@ -128,6 +191,7 @@ function addNewQuestion(){
div.appendChild(span);
parent.appendChild(div);
+ return div;
}
function submitQSet(){
@@ -164,10 +228,23 @@ function submitQSet(){
}
}
};
- xhr.open("POST","/zelfoverhoor/docent/addset");
+ 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();
});
diff --git a/modules/zelfoverhoor/zelfoverhoor.js b/modules/zelfoverhoor/zelfoverhoor.js
index bfc9781..8f87f75 100644
--- a/modules/zelfoverhoor/zelfoverhoor.js
+++ b/modules/zelfoverhoor/zelfoverhoor.js
@@ -5,6 +5,8 @@ var moddir;
var accounts=require("./accounts.json");
+var SHUFFLE_QUESTIONS=false;
+
// {<id>: {q: "question", a: "answer"}}
var questiondb={};
// {<id>: {id, name, description, questions: [<question_id>]}}
@@ -55,6 +57,31 @@ function shuffle(a){
}
}
+function checkQsetJson(json,errfunc){
+ var qset;
+ try {
+ qset=JSON.parse(json);
+ } catch(e){
+ errfunc("Invalid json received");
+ return null;
+ }
+ if(!qset.name||typeof qset.name!="string"||
+ !qset.description||typeof qset.description!="string"||
+ !qset.questions||!Array.isArray(qset.questions)){
+ errfunc("Invalid data received");
+ return null;
+ }
+ var i;
+ for(i=0;i<qset.questions.length;i++){
+ if(!qset.questions[i].q||typeof qset.questions[i].q!="string"||
+ !qset.questions[i].a||typeof qset.questions[i].a!="string"){
+ errfunc("Invalid question data received");
+ return null;
+ }
+ }
+ return qset;
+}
+
module.exports=function(app,io,_moddir){
moddir=_moddir;
@@ -85,7 +112,7 @@ module.exports=function(app,io,_moddir){
list.push(questiondb[qset.questions[i]]);
}
var resset={"id": qset.id, "name": qset.name, "description": qset.description, "questions": list};
- // shuffle(resset.questions);
+ if(SHUFFLE_QUESTIONS)shuffle(resset.questions);
fs.readFile(moddir+"/qs.html",function(err,data){
if(err)throw err;
res.send(String(data)
@@ -93,6 +120,19 @@ module.exports=function(app,io,_moddir){
.replace("/*###QUESTIONSET###*/",JSON.stringify(resset)));
});
});
+ app.get("/zelfoverhoor/qsdata/:id",function(req,res){
+ var qset=questionsets[req.params.id];
+ if(qset==null){
+ res.status(404).send("ID not found");
+ return;
+ }
+ var list=[];
+ for(var i=0;i<qset.questions.length;i++){
+ list.push(questiondb[qset.questions[i]]);
+ }
+ var resset={"id": qset.id, "name": qset.name, "description": qset.description, "questions": list};
+ res.send(JSON.stringify(resset));
+ });
app.use(["/zelfoverhoor/docent","/zelfoverhoor/docent/*"],cmn.authgen(accounts));
app.get("/zelfoverhoor/docent",function(req,res){
@@ -114,27 +154,8 @@ module.exports=function(app,io,_moddir){
res.send(JSON.stringify(list));
});
app.post("/zelfoverhoor/docent/addset",function(req,res){
- var qset;
- try {
- qset=JSON.parse(req.body);
- } catch(e){
- res.status(400).send("Invalid json received");
- return;
- }
- if(!qset.name||typeof qset.name!="string"||
- !qset.description||typeof qset.description!="string"||
- !qset.questions||!Array.isArray(qset.questions)){
- res.status(400).send("Invalid data received");
- return;
- }
- var i;
- for(i=0;i<qset.questions.length;i++){
- if(!qset.questions[i].q||typeof qset.questions[i].q!="string"||
- !qset.questions[i].a||typeof qset.questions[i].a!="string"){
- res.status(400).send("Invalid question data received");
- return;
- }
- }
+ var qset=checkQsetJson(req.body,function(err){res.status(400).send(err);});
+ if(qset==null)return;
var ids=[],id;
for(i=0;i<qset.questions.length;i++){
id=uniqidstr(function(s){return questiondb[s]==null;});
@@ -150,6 +171,28 @@ module.exports=function(app,io,_moddir){
persistDB();
res.send(setid);
});
+ app.post("/zelfoverhoor/docent/editset/:id",function(req,res){
+ var setid=req.params.id;
+ if(questionsets[setid]==null){
+ res.status(404).send("ID not found");
+ return;
+ }
+ if(userlists[req.authuser].indexOf(setid)==-1){
+ res.status(403).send("ID does not belong to you");
+ return;
+ }
+ var qset=checkQsetJson(req.body,function(err){res.status(400).send(err);});
+ if(qset==null)return;
+ var ids=[],id;
+ for(i=0;i<qset.questions.length;i++){
+ id=uniqidstr(function(s){return questiondb[s]==null;});
+ questiondb[id]={"q": qset.questions[i].q, "a": qset.questions[i].a};
+ ids.push(id);
+ }
+ questionsets[setid]={"id": setid, "name": qset.name, "description": qset.description, "questions": ids};
+ persistDB();
+ res.send(setid);
+ });
app.post("/zelfoverhoor/docent/deleteset",function(req,res){
var setid=req.body;
if(!setid||typeof setid!="string"){
@@ -172,6 +215,7 @@ module.exports=function(app,io,_moddir){
res.status(404).send("ID not found");
return;
}
+ delete questionsets[setid];
persistDB();
res.send();
});