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

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 updateActivities(course) {
	const promises = [];
	for (const activityName of ["Cockpit", "Klassenliste", "Coaching", "Admin-Tools"]) {
		const input = document.getElementById(`${activityName.toLowerCase()}-file-input`);
		if (input.value === "") continue;

		const activity = await course.getActivity(activityName);
		if (activity === null) {
			output(course.title + ": Skipping non existing: " + activityName);
			continue;
		}

		const text = input.files[0].text();
		promises.push(activity.update(await text));
	}
	return Promise.all(promises);
}

export async function updateCourses() {
	output("loading courses ...");
	const courses = await fetchCourses("SOL Graduierung");
	output(`updating ${courses.length} courses ... `);
	const promises = [];
	for (const course of courses) {
		promises.push(updateActivities(course));
	}

	Promise.all(promises)
		.then(() => {
			output("Updating activities done.");
		})
		.catch((error) => {
			output("Updating activities failed: " + error);
		});
}

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

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