aboutsummaryrefslogtreecommitdiff
path: root/admin/public/ranking.js
blob: 4281e234eabbefa3c87af494f8ebe50cc8ea53fd (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
fetch("api/questions")
    .then((questions) => questions.json())
    .then((questions) => {
        fetch("api/answers")
            .then((answers) => answers.json())
            .then((answers) => {
                questions.forEach((question) => (question.answers = []));
                answers.forEach((answer) => questions[answer.question_id - 1].answers.push(answer));
                render(questions);
            });
    });

function render(questions) {
    const teacher = document.querySelector("ul#teacher");
    const pupil = document.querySelector("ul#pupil");
    questions.forEach((question) => {
        const list = question.type === "teacher" ? teacher : pupil;
        let answers = "";
        question.answers.forEach((answer) => {
            answers += `<li>${answer.name} ${answer.middlename ? answer.middlename + " " : ""}${answer.surname}: ${
                answer.count
            }</li>`;
        });
        list.insertAdjacentHTML("beforeend", `<li>${question.question}<br><ol>${answers}</ol></li>`);
    });
}