aboutsummaryrefslogtreecommitdiff
path: root/src/mass-graduation.js
blob: 6d53e55f959fe23a2ffe250e46ad451028130a62 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { GraduationChange, submitGraduationChange } from "./graduation-ENV.ts";
import { fetchStudents } from "./student-ENV.ts";

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

let name;

function toggle(btn, others) {
	if (btn.innerHTML === "X") {
		btn.innerHTML = "o";
	} else {
		btn.innerHTML = "X";
		for (const o of others) {
			o.innerHTML = "o";
		}
	}

	const submitBtn = document.getElementById("submit-mass-graduation");
	submitBtn.style.display = "inline";
	submitBtn.disabled = false;
	document.getElementById("submit-mass-graduation-state").innerHTML = "";
}

// https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
function shuffleArray(array) {
	for (let i = array.length - 1; i > 0; i--) {
		const j = Math.floor(Math.random() * (i + 1));
		[array[i], array[j]] = [array[j], array[i]];
	}
	return array;
}

function chooseMandatory(graduations) {
	graduations.sort((g1, g2) => g1.mandatory - g2.mandatory);

	// Get all graduations with smaller mandatory amount than the one with the highest amount
	let mandatoryCandidates = graduations.filter(
		(g) => g.mandatory < graduations[graduations.length - 1].mandatory,
	);
	let rest;

	// All graduations have the same mandatory amount
	if (mandatoryCandidates.length === 0) {
		mandatoryCandidates = graduations;
		rest = [];
	} else {
		rest = graduations.filter((g) => !mandatoryCandidates.includes(g));
	}

	// Get a random list of possible indexes
	const candidatesIdxs = shuffleArray([...Array(mandatoryCandidates.length).keys()]);
	let chosen = candidatesIdxs.slice(0, 5).map((i) => mandatoryCandidates[i]);
	if (chosen.length < 5) {
		const missing = 5 - chosen.length;
		const restIdxs = shuffleArray([...Array(rest.length).keys()]);
		chosen = chosen.concat(restIdxs.slice(0, missing).map((i) => rest[i]));
	}

	return chosen;
}

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

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

	mul.innerHTML = "";
	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);
	const chosen = chooseMandatory(graduations);

	for (const li of listEntries) {
		const student = li.student;

		const n = document.createElement("span");
		n.classList.add("group-member-name");
		n.innerHTML = `${student.name}`;
		li.appendChild(n);

		const btns = [];
		for (const col of ["green", "orange", "red"]) {
			const b = document.createElement("button");
			btns.push(b);
			b.classList.add(`group-member-${col}`);
			b.innerHTML = "o";
			li.appendChild(b);
		}

		for (const b of btns) {
			b.addEventListener("click", () =>
				toggle(
					b,
					btns.filter((o) => o !== b),
				),
			);
		}

		if (chosen.includes(student.graduation)) {
			li.classList.add("group-mandatory-member");
			mul.appendChild(li);
		} else {
			li.classList.add("group-member");
			ul.appendChild(li);
		}
	}
}

function graduationChangeForGroupMember(li) {
	const student = li.student;
	const mandatory = li.classList.contains("group-mandatory-member");
	let change = null;
	if (li.querySelector(".group-member-green").innerHTML === "X") {
		change = GraduationChange.newGreenPointChange(mandatory, "", name);
	} else if (li.querySelector(".group-member-orange").innerHTML === "X") {
		change = GraduationChange.newOrangePointChange(mandatory, "", name);
	} else if (li.querySelector(".group-member-red").innerHTML === "X") {
		change = GraduationChange.newRedPointChange(mandatory, "", name);
	}

	return [change, student];
}

function submitMassGraduation() {
	const state = document.getElementById("submit-mass-graduation-state");
	state.innerHTML = "";
	document.getElementById("submit-mass-graduation").disabled = true;

	// TODO(muhq): await graduation promises

	const mandatoryChanges = [];
	for (const li of document.querySelectorAll("li.group-mandatory-member")) {
		const [ch, s] = graduationChangeForGroupMember(li);
		if (ch !== null) mandatoryChanges.push([ch, s]);
	}

	if (mandatoryChanges.length !== 5) {
		// not all mandatory graduations are selected
		state.style.display = "inline";
		state.innerHTML = "Bitte bewerte alle als erforderlich angezeigten Schülys.";
		return;
	}

	state.classList.add("submit-mass-graduation-inprogress");
	state.style.display = "unset";

	const promises = [];
	for (const [ch, s] of mandatoryChanges) {
		promises.push(submitGraduationChange(ch, s.graduation));
	}

	for (const li of document.querySelectorAll("li.group-member")) {
		const [ch, s] = graduationChangeForGroupMember(li);
		if (ch !== null) promises.push(submitGraduationChange(ch, s.graduation));
	}

	Promise.all(promises)
		.then(() => {
			state.style.display = "inline";
			state.innerHTML = "✓";
			state.style.color = "green";
		})
		.catch((error) => {
			state.style.display = "inline";
			state.innerHTML = "X";
			state.style.color = "red";
			// re-enable submit button
			document.getElementById("submit-mass-graduation").disabled = false;
			console.log(error);
		})
		.finally(() => {
			state.classList.remove("submit-mass-graduation-inprogress");
		});
}

async function init() {
	document.getElementById("submit-mass-graduation").addEventListener("click", submitMassGraduation);
	name = getName();

	loadGroup();
}

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