aboutsummaryrefslogtreecommitdiff
path: root/src/mass-graduation.js
blob: 7da1c0fb1c94e7096d5b3185e0cfbc63298bd5b3 (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
import { GraduationChange, submitGraduationChange } from "./graduation-ENV.ts";

import { fetchGroups } from "./group-fetch.ts";
import { initSelection } from "./selection.js";
import { getName } from "./utils-ENV.js";

let group;
let name;

function toggle(btn) {
	if (btn.innerHTML === "X") {
		btn.innerHTML = "o";
	} else {
		btn.innerHTML = "X";
	}

	document.getElementById("submit-mass-graduation").style.display = "inline";
	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(selected) {
	group = selected;
	const students = await group.getStudents();
	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);

		for (const col of ["green", "orange", "red"]) {
			const b = document.createElement("button");
			b.classList.add(`group-member-${col}`);
			b.innerHTML = "o";
			b.addEventListener("click", () => toggle(b));
			li.appendChild(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 submitMassGraduation() {
	const state = document.getElementById("submit-mass-graduation-state");
	state.innerHTML = "";
	state.classList.add("submit-mass-graduation-inprogress");
	state.style.display = "unset";

	// TODO(muhq): await graduation promises

	const promises = [];
	for (const li of document.querySelectorAll("li.group-member, li.group-mandatory-member")) {
		const student = li.student;
		const mandatory = li.classList.contains("group-mandatory-member");
		if (li.querySelector(".group-member-green").innerHTML === "X") {
			promises.push(
				submitGraduationChange(
					student.graduation,
					GraduationChange.newGreenPointChange(mandatory, "", name),
				),
			);
		}
		if (li.querySelector(".group-member-orange").innerHTML === "X") {
			promises.push(
				submitGraduationChange(
					student.graduation,
					GraduationChange.newOrangePointChange(mandatory, "", name),
				),
			);
		}
		if (li.querySelector(".group-member-red").innerHTML === "X") {
			promises.push(
				submitGraduationChange(
					student.graduation,
					GraduationChange.newRedPointChange(mandatory, "", name),
				),
			);
		}
	}

	Promise.all(promises)
		.then(() => {
			state.style.display = "inline";
			state.innerHTML = "✓";
			state.style.color = "green";
			for (const li of document.querySelectorAll("li.group-member")) {
				if (!li.observationDiv) continue;
				li.observationDiv.remove();
				li.observationDiv = null;
			}
		})
		.catch((error) => {
			state.style.display = "inline";
			state.innerHTML = "X";
			state.style.color = "red";
			console.log(error);
		})
		.finally(() => {
			state.classList.remove("submit-mass-graduation-inprogress");
		});
}

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

	initSelection(
		"group-selection",
		async function () {
			return (await fetchGroups()).values();
		},
		(group) => `${group.name}`,
		loadGroup,
	);
}

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