aboutsummaryrefslogtreecommitdiff
path: root/src/coaching.js
blob: 2fd2429b8acc3c471c44d67746dd5bf35c1ccb6d (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { fetchStudents } from "./student-ENV.ts";

import { getName, getActivityUrl } from "./utils-ENV.js";

let name;

async function loadGroup() {
	const cockpitUrlPromise = getActivityUrl("Cockpit");

	const students = await fetchStudents();
	const graduationPromises = [];
	document.getElementById("group-content").style.display = "block";

	const ul = document.getElementById("group-members");

	ul.innerHTML = "";

	const listEntries = [];
	for (const student of students) {
		const graduationPromise = student.getGraduation();
		graduationPromises.push(graduationPromise);

		const li = document.createElement("li");
		li.student = student;
		listEntries.push(li);

		const i = document.createElement("img");
		i.classList.add("group-member-icon");
		graduationPromise.then((graduation) => {
			i.src = graduation.getIcon();
		});
		li.appendChild(i);
	}

	const graduations = await Promise.all(graduationPromises);

	for (const li of listEntries) {
		const student = li.student;
		if (!student.graduation.coaches.includes(name)) continue;

		const n = document.createElement("span");
		n.classList.add("group-member-name");
		n.innerHTML = `${student.name}`;
		li.appendChild(n);
		n.addEventListener("click", async function () {
			const cockpitUrl = new URL(await cockpitUrlPromise, window.location.origin);
			cockpitUrl.searchParams.append("userToLoad", student.id);
			window.location = cockpitUrl;
		});

		for (const col of ["green", "orange", "red"]) {
			const s = document.createElement("span");
			s.classList.add(`group-member-${col}`);
			switch (col) {
				case "green":
					s.innerHTML = student.graduation.green;
					break;
				case "orange":
					s.innerHTML = student.graduation.orange;
					break;
				case "red":
					s.innerHTML = student.graduation.red;
					break;
			}
			li.appendChild(s);
		}

		const p = document.createElement("span");
		p.classList.add("group-member-points");
		p.innerHTML = `∑ ${student.graduation.getNettoPoints()}`;
		li.appendChild(p);

		li.classList.add("group-member");
		ul.appendChild(li);
	}
}

async function init() {
	name = getName();
	loadGroup();
}

if (document.readyState === "loading") {
	document.addEventListener("DOMContentLoaded", init);
} else {
	init();
}