aboutsummaryrefslogtreecommitdiff
path: root/poll
diff options
context:
space:
mode:
Diffstat (limited to 'poll')
-rw-r--r--poll/index.js48
-rw-r--r--poll/public/index.html4
-rw-r--r--poll/public/script.js25
3 files changed, 57 insertions, 20 deletions
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, " "));
+}