diff options
author | Marvin Borner | 2020-10-10 12:55:47 +0200 |
---|---|---|
committer | Marvin Borner | 2020-10-10 12:55:47 +0200 |
commit | f56c3cad0cf123dafa3cb6978f4e5ca3c2c623e6 (patch) | |
tree | 6313b34f87650a90b9488a35e0b5155e2fd1f73f | |
parent | 2432d4f5f79638d62e663c7abebdfa3f1405acb5 (diff) |
Added working teacher polls
-rw-r--r-- | .prettierrc.js | 4 | ||||
-rw-r--r-- | auth/index.js | 23 | ||||
-rw-r--r-- | db.js | 10 | ||||
-rw-r--r-- | overview/public/index.html | 7 | ||||
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | poll/index.js | 48 | ||||
-rw-r--r-- | poll/public/index.html | 4 | ||||
-rw-r--r-- | poll/public/script.js | 25 |
8 files changed, 88 insertions, 35 deletions
diff --git a/.prettierrc.js b/.prettierrc.js index 222cb77..621af3a 100644 --- a/.prettierrc.js +++ b/.prettierrc.js @@ -5,5 +5,5 @@ module.exports = { printWidth: 120, tabWidth: 4, useTabs: false, - endOfLine: "auto" -} + endOfLine: "auto", +}; diff --git a/auth/index.js b/auth/index.js index d80e673..1ea6290 100644 --- a/auth/index.js +++ b/auth/index.js @@ -58,13 +58,22 @@ app.post("/api/password", checkUser, async (req, res) => { app.get("/api/list", checkUser, async (req, res) => { let users; - if (req.query.class === "all") { - users = await db.query("SELECT id, name, middlename, surname, class_id FROM users ORDER BY class_id, name"); - } else { - users = await db.query( - "SELECT id, name, middlename, surname, class_id FROM users WHERE class_id = (SELECT class_id FROM users WHERE id = ?) AND id != ? ORDER BY name", - [req.session.uid, req.session.uid], - ); + try { + if (req.query.class === "all") { + users = await db.query("SELECT id, name, middlename, surname, class_id FROM users ORDER BY class_id, name"); + } else if (req.query.class === "teacher") { + users = await db.query( + "SELECT id, name, middlename, surname, class_id FROM users WHERE type_id = 2 ORDER BY class_id, name", + ); + } else { + users = await db.query( + "SELECT id, name, middlename, surname, class_id FROM users WHERE class_id = (SELECT class_id FROM users WHERE id = ?) AND id != ? ORDER BY name", + [req.session.uid, req.session.uid], + ); + } + } catch (e) { + console.error(e); + return res.send("error"); } res.json(users); @@ -88,7 +88,15 @@ class DB { userPasswords[classIndex].push({ username, pwd }); await this.query( "INSERT INTO users (username, name, middlename, surname, password, class_id, type_id) VALUE (?,?,?,?,?,?,?)", - [username, names[0].replace("\r", ""), middlename, surname, password, classIndex + 1, 2], + [ + username, + names[0].replace("\r", ""), + middlename, + surname, + password, + classIndex + 1, + classIndex + 1 === 6 ? 2 : 1, + ], ); } } diff --git a/overview/public/index.html b/overview/public/index.html index 27d5292..c97b83f 100644 --- a/overview/public/index.html +++ b/overview/public/index.html @@ -32,11 +32,10 @@ <!-- <li><a href="/motto">Motto Wähler</a></li> --> <li><a href="/mottovote">Motto-Voting</a></li> <li><a href="/quotes">Zitate</a></li> - <li><a href="/poll">Schüler-Ranking</a></li> + <li><a href="/poll?type=pupil">Schüler-Ranking</a></li> + <li><a href="/poll?type=teacher">Lehrer-Ranking</a></li> </ul> - <p> - Bitte benutzt diese Tools nur ernsthaft - es ist nun mal unsere gemeinsame Zeitung :) - </p> + <p>Bitte benutzt diese Tools nur ernsthaft - es ist nun mal unsere gemeinsame Zeitung :)</p> <p> Falls ihr uns bei der Entwicklung helfen wollt, Fehler gefunden habt oder Fragen an uns habt, könnt ihr gerne mit dem Entwicklungs-Team Kontakt aufnehmen: diff --git a/package.json b/package.json index 5e5a835..1775768 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "redis": "^3.0.2" }, "scripts": { - "format": "prettier --write \"*.{html,css,js}\"", + "format": "prettier --write \"**/*.{html,css,js}\"", "lint": "eslint .", "run": "node app.js", "dev": "nodemon app.js" diff --git a/poll/index.js b/poll/index.js index 809ab44..84af052 100644 --- a/poll/index.js +++ b/poll/index.js @@ -6,35 +6,53 @@ 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"); + if (!req.body.answer || !req.body.question || !req.query.type) return res.send("error"); if (req.body.answer == req.session.uid) return res.send("error"); try { - const user_class = (await db.query("SELECT class_id FROM users WHERE id = ?", [req.session.uid]))[0].class_id; - const answer_class = (await db.query("SELECT class_id FROM users WHERE id = ?", [parseInt(req.body.answer)]))[0] - .class_id; - if (user_class != answer_class) return res.send("error"); + if (req.query.type == "pupil") { + const user_class = (await db.query("SELECT class_id FROM users WHERE id = ?", [req.session.uid]))[0] + .class_id; + const answer_class = ( + await db.query("SELECT class_id FROM users WHERE id = ?", [parseInt(req.body.answer)]) + )[0].class_id; + if (user_class != answer_class) return res.send("error"); + } else if (req.query.type == "teacher") { + const answer_type = ( + await db.query( + "SELECT t.name FROM users AS u INNER JOIN types AS t ON u.type_id = t.id WHERE u.id = ?", + [parseInt(req.body.answer)], + ) + )[0].name; + if (answer_type != "teacher") return res.send("error"); + } else { + return res.send("error"); + } 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"); + res.redirect("/poll?type=" + req.query.type); } catch (e) { console.error(e); - res.json("error"); + res.send("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); + try { + 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 = ? LIMIT 1", + [req.session.uid, req.query.type], + ) + )[0]; + res.json(question); + } catch (e) { + console.error(e); + res.send("error"); + } }); module.exports = app; diff --git a/poll/public/index.html b/poll/public/index.html index dd66c96..59e5939 100644 --- a/poll/public/index.html +++ b/poll/public/index.html @@ -26,8 +26,8 @@ <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/> + <input name="question" id="question" hidden /> + <br /> <label for="answer">Antwort</label> <select name="answer" id="answer" required></select> <button type="submit" class="pure-button pure-button-primary">Antworten</button> diff --git a/poll/public/script.js b/poll/public/script.js index a1911fa..8c56894 100644 --- a/poll/public/script.js +++ b/poll/public/script.js @@ -1,8 +1,17 @@ +const type = getParameterByName("type"); 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>'); +if (!["teacher", "pupil"].includes(type)) window.location.href = "/"; + +dropdown.insertAdjacentHTML( + "beforeend", + '<option selected="true" disabled>' + (type == "teacher" ? "Lehrer" : "Schüler") + "/in auswählen...</option>", +); +document.querySelector("legend").innerText = type == "teacher" ? "Lehrer-Ranking" : "Schüler-Ranking"; +document.querySelector("p").innerText = "Welche/r " + (type == "teacher" ? "Lehrer/in" : "Schüler/in") + "..."; +document.querySelector("form").setAttribute("action", "api/answer?type=" + type); function appendOption(response) { response.forEach((elem) => { @@ -15,13 +24,23 @@ function appendOption(response) { }); } -fetch("/auth/api/list") +fetch("/auth/api/list" + (type == "teacher" ? "?class=teacher" : "")) .then((response) => response.json()) .then((response) => appendOption(response)); -fetch("/poll/api/get") +fetch("/poll/api/get?type=" + type) .then((response) => response.json()) .then((response) => { question_label.innerText = response["question"]; question_input.setAttribute("value", response["id"]); }); + +function getParameterByName(name, url) { + if (!url) url = window.location.href; + name = name.replace(/[\[\]]/g, "\\$&"); + var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), + results = regex.exec(url); + if (!results) return null; + if (!results[2]) return ""; + return decodeURIComponent(results[2].replace(/\+/g, " ")); +} |