diff options
author | LarsVomMars | 2021-01-26 11:57:48 +0100 |
---|---|---|
committer | LarsVomMars | 2021-01-26 11:57:48 +0100 |
commit | 40035bd9cea21c4173f6727ba54d94f4fde44a9c (patch) | |
tree | 88a24abf9be3c223db117cb1402cf38161e01fdc /questions | |
parent | 9fac4d9823f3350c22b156450cba6cc456ed960f (diff) |
Questions update
Diffstat (limited to 'questions')
-rw-r--r-- | questions/index.js | 19 | ||||
-rw-r--r-- | questions/public/index.html | 5 | ||||
-rw-r--r-- | questions/public/script.js | 46 |
3 files changed, 41 insertions, 29 deletions
diff --git a/questions/index.js b/questions/index.js index 1f0f4ce..96bbb8b 100644 --- a/questions/index.js +++ b/questions/index.js @@ -7,15 +7,16 @@ app.use("/", checkUser, express.static(__dirname + "/public")); app.get("/api/question/:id", checkUser, async (req, res) => { try { - const questions = await db.query("SELECT id, question FROM question_questions"); + const questions = await db.query("SELECT id, question FROM question_questions ORDER BY id"); const id = +req.params.id; if (id >= 0 && id < questions.length) { const question = questions[id]; const answers = await db.query( - "SELECT answer FROM question_answers WHERE question_id = ? AND user_id = ?", + "SELECT option_id FROM question_answers WHERE question_id = ? AND user_id = ?", [question.id, req.session.uid], ); - question.answer = answers.length > 0 ? answers[0].answer : undefined; + question.answer = answers.length > 0 ? answers[0].option_id : undefined; + question.options = await db.query("SELECT id, answer_option FROM question_options WHERE question_id = ?", [question.id]); res.json(question); } else { res.json({}); @@ -45,21 +46,27 @@ app.get("/api/questions", checkUser, async (req, res) => { }); app.post("/api/answer", checkUser, async (req, res) => { - return await answer(req, res, "INSERT INTO question_answers (answer, question_id, user_id) VALUE (?,?,?)"); + return await answer(req, res, "INSERT INTO question_answers (option_id, question_id, user_id) VALUE (?,?,?)"); }); app.put("/api/answer", checkUser, async (req, res) => { - return await answer(req, res, "UPDATE question_answers SET answer = ? WHERE question_id = ? AND user_id = ?"); + return await answer(req, res, "UPDATE question_answers SET option_id = ? WHERE question_id = ? AND user_id = ?"); }); async function answer(req, res, qu) { const { question, answer } = req.body; + const fail = { success: false }; try { + const possibleAnswers = await db.query(`SELECT qo.id + FROM question_questions qq + INNER JOIN question_options qo on qq.id = qo.question_id + WHERE qq.id = ?`, [question]); + if (possibleAnswers.find(value => +value.id === +answer) === undefined) return res.json(fail); // Answer not for question await db.query(qu, [answer, question, req.session.uid]); res.json({ success: true }); } catch (e) { console.error(e); - res.json({ success: false }); + res.json(fail); } } diff --git a/questions/public/index.html b/questions/public/index.html index 90244f5..e21db81 100644 --- a/questions/public/index.html +++ b/questions/public/index.html @@ -26,10 +26,7 @@ <div class="bar" id="progress"></div> <label id="question_label" for="question"></label> <input name="question" id="question" hidden /> - <div class="answer-buttons pure-button-group" role="group"> - <button class="pure-button pure-button-primary answer-btn" data-value="1">Ja</button> - <button class="pure-button pure-button-primary answer-btn" data-value="0">Nein</button> - </div> + <div class="answer-buttons pure-button-group" role="group"></div> <br> <div class="back-skip pure-button-group" role="group"> <button id="prev-btn" class="pure-button">Zurück</button> diff --git a/questions/public/script.js b/questions/public/script.js index aaeac00..2a04a02 100644 --- a/questions/public/script.js +++ b/questions/public/script.js @@ -7,7 +7,6 @@ const question_label = document.getElementById("question_label"); const prev = document.getElementById("prev-btn"); const skip = document.getElementById("skip-btn"); const progress = document.getElementById("progress"); -const buttons = document.querySelectorAll(".answer-btn"); skip.addEventListener("click", () => getNext(qid + 1)); prev.addEventListener("click", () => getNext(qid - 1)); @@ -23,10 +22,21 @@ fetch(`api/question/${qid}`) if (!response.empty()) { question_label.innerText = response["question"]; question_input.setAttribute("value", response["id"]); + const div = document.getElementsByClassName("answer-buttons")[0]; + const prop = 100 / response.options.length; + for (const option of response.options) { + const btn = document.createElement("btn"); + btn.classList.add("pure-button", "pure-button-primary", "answer-btn"); + btn.dataset.value = `${option.id}`; + btn.textContent = option.answer_option; + btn.style.width = `${prop}%`; + div.append(btn); + } + addListeners(); if (response.answer !== undefined) { method = "PUT"; + document.querySelector(`.answer-btn[data-value="${response.answer}"]`).style.opacity = "0.5"; } - document.querySelector(`.answer-btn[data-value="${response.answer}"]`).style.opacity = "0.5"; } else getNext(); // Resets }); @@ -63,21 +73,19 @@ NodeList.prototype.on = function (listener, event) { } }; -buttons.on("click", async (e) => { - const body = JSON.stringify({ - question: question_input.value, - answer: e.target.dataset.value === "1", - }); - const resp = await fetch("api/answer", { - method, - headers: { "Content-Type": "application/json" }, - body, +function addListeners() { + const buttons = document.querySelectorAll(".answer-btn"); + buttons.on("click", async (e) => { + const body = JSON.stringify({ + question: question_input.value, + answer: e.target.dataset.value, + }); + const resp = await fetch("api/answer", { + method, + headers: { "Content-Type": "application/json" }, + body, + }); + const res = await resp.json(); + if (res.success) getNext(qid + 1); }); - const res = await resp.json(); - if (res.success) { - method = "PUT"; - getNext(qid + 1); - // document.querySelector(`.answer-btn[data-value="${e.target.dataset.value}"]`).style.opacity = "0.5"; - // document.querySelector(`.answer-btn[data-value="${+!+e.target.dataset.value}"]`).style.opacity = "1"; // Let's not talk about it - } -}); +}
\ No newline at end of file |