aboutsummaryrefslogtreecommitdiff
path: root/src/admin-tools.js
blob: f598c3538d639aa415c85612262a1ce7ff41c9c5 (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
import { GraduationChange, submitGraduationChange } from "./graduation-ENV.ts";
import { fetchStudent, fetchStudents } from "./student-ENV.ts";

let outputNode;
function output(msg) {
	if (outputNode.style.display === "none") {
		outputNode.style.display = "block";
	}
	const line = document.createElement("div");
	line.innerHTML = msg;
	line.classList.add("admin-tools-output-line");
	outputNode.appendChild(line);
}

async function coachAssignmentImport() {
	const csv = document.getElementById("admin-tools-coach-import").value;
	const assignments = {};

	for (const line of csv.split("\n")) {
		const [studentName, ...coaches] = line.trim().split(";");
		if (studentName === "*") {
			const students = await fetchStudents();
			for (const student of students) {
				assignments[student.id] = coaches;
			}
		} else {
			const student = await fetchStudent(undefined, studentName);
			if (student === null) {
				output(`${studentName} nicht gefunden`);
				continue;
			}
			assignments[student.id] = coaches;
		}
	}

	const assignmentChange = GraduationChange.newCoachAssignmentChange(assignments);
	await submitGraduationChange(assignmentChange);
	output("Coach Zuweisung importiert");
}

async function init() {
	outputNode = document.getElementById("admin-tools-output");
	document
		.getElementById("admin-tools-coach-import-submit")
		.addEventListener("click", coachAssignmentImport);
}

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