aboutsummaryrefslogtreecommitdiff
path: root/poll/public/script.js
blob: 8c56894312b18e87f60c3e1417d59560f05f18f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const type = getParameterByName("type");
const dropdown = document.getElementById("answer");
const question_input = document.getElementById("question");
const question_label = document.getElementById("question_label");

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) => {
        dropdown.insertAdjacentHTML(
            "beforeend",
            `<option value="${elem["id"]}">${elem["name"]} ${elem["middlename"] ? elem["middlename"] + " " : ""}${
                elem["surname"]
            }</option>`,
        );
    });
}

fetch("/auth/api/list" + (type == "teacher" ? "?class=teacher" : ""))
    .then((response) => response.json())
    .then((response) => appendOption(response));

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, " "));
}