diff options
Diffstat (limited to 'poll')
-rw-r--r-- | poll/index.js | 34 | ||||
-rw-r--r-- | poll/public/index.html | 35 | ||||
-rw-r--r-- | poll/public/script.js | 40 | ||||
-rw-r--r-- | poll/public/style.css | 35 |
4 files changed, 144 insertions, 0 deletions
diff --git a/poll/index.js b/poll/index.js new file mode 100644 index 0000000..ab9ee6f --- /dev/null +++ b/poll/index.js @@ -0,0 +1,34 @@ +const express = require("express"); +const db = require("../db"); +const app = express.Router(); +const { checkUser } = require("../auth"); + +app.use("/", checkUser, express.static(__dirname + "/public")); + +app.post("/api/answer", checkUser, async (req, res) => { + if (!req.body.answer || !req.body.question) return res.send("error"); + try { + await db.query("INSERT INTO ranking_answers (question_id, user_id, answer_id) VALUE (?,?,?)", [ + parseInt(req.body.question), + req.session.uid, + parseInt(req.body.answer), + ]); + res.redirect("/poll"); + } catch (e) { + console.error(e); + res.json("error"); + } +}); + +app.get("/api/get", checkUser, async (req, res) => { + // TODO: Add teacher questions + const question = ( + await db.query( + "SELECT q.id, q.question, t.name FROM ranking_questions AS q INNER JOIN types AS t ON type_id = t.id WHERE q.id NOT IN (SELECT question_id FROM ranking_answers WHERE user_id = ?) AND t.name = 'pupil' LIMIT 1", + [req.session.uid] + ) + )[0]; + res.json(question); +}); + +module.exports = app; diff --git a/poll/public/index.html b/poll/public/index.html new file mode 100644 index 0000000..8530b76 --- /dev/null +++ b/poll/public/index.html @@ -0,0 +1,35 @@ +<!DOCTYPE html> +<html> + <head> + <meta charset="UTF-8" /> + <meta name="viewport" content="width=device-width, initial-scale=1" /> + <link + rel="stylesheet" + href="https://unpkg.com/purecss@2.0.3/build/pure-min.css" + integrity="sha384-cg6SkqEOCV1NbJoCu11+bm0NvBRc8IYLRGXkmNrqUBfTjmMYwNKPWBTIKyw9mHNJ" + crossorigin="anonymous" + /> + <link rel="stylesheet" href="style.css" type="text/css" media="all" /> + + <title>Schüler-Ranking</title> + </head> + <body> + <div> + <form class="pure-form pure-form-stacked" action="api/answer" method="post"> + <fieldset> + <!-- TODO: Add progress --> + <legend>Schüler-Ranking</legend> + <p>Welche/r Schüler/in...</p> + <label id="question_label" for="question"></label> + <input name="question" id="question" hidden></input> + <br/> + <label for="answer">Antwort</label> + <select name="answer" id="answer" required></select> + <button type="submit" class="pure-button pure-button-primary">Antworten</button> + </fieldset> + </form> + </div> + + <script src="script.js" charset="utf-8"></script> + </body> +</html> diff --git a/poll/public/script.js b/poll/public/script.js new file mode 100644 index 0000000..a42777f --- /dev/null +++ b/poll/public/script.js @@ -0,0 +1,40 @@ +const dropdown = document.getElementById("answer"); +const question_input = document.getElementById("question"); +const question_label = document.getElementById("question_label"); + +dropdown.insertAdjacentHTML("beforeend", '<option selected="true" disabled>Schüler/in auswählen...</option>'); + +function appendOption(response) { + response.forEach((elem) => { + dropdown.insertAdjacentHTML( + "beforeend", + `<option value="${elem["id"]}">${elem["name"]} ${elem["middlename"] ? elem["middlename"] : " "}${ + elem["surname"] + }</option>` + ); + }); +} + +function appendQuote(response) { + response.forEach((elem) => { + document + .getElementById(elem["class"]) + .insertAdjacentHTML( + "beforeend", + `<li>${elem["name"]} ${elem["middlename"] ? elem["middlename"] : " "}${elem["surname"]}: ${ + elem["quote"] + }</li>` + ); + }); +} + +fetch("/auth/api/list") + .then((response) => response.json()) + .then((response) => appendOption(response)); + +fetch("/poll/api/get") + .then((response) => response.json()) + .then((response) => { + question_label.innerText = response["question"]; + question_input.setAttribute("value", response["id"]); + }); diff --git a/poll/public/style.css b/poll/public/style.css new file mode 100644 index 0000000..2c9c695 --- /dev/null +++ b/poll/public/style.css @@ -0,0 +1,35 @@ +html, +body { + padding: 0; + margin: 0; + height: 100%; + width: 100%; + background-color: #eec0c6; + background-image: linear-gradient(315deg, #eec0c6 0%, #7ee8fa 74%); +} + +div { + position: absolute; + max-height: 80%; + overflow-y: scroll; + width: 30%; + left: 50%; + top: 50%; + -webkit-transform: translate(-50%, -50%); + transform: translate(-50%, -50%); + padding: 20px; + border-radius: 10px; + background: white; +} + +input, +button, +select { + width: 100%; +} + +@media only screen and (max-width: 600px) { + div { + width: calc(100% - 50px); + } +} |