aboutsummaryrefslogtreecommitdiff
path: root/src/student-prod.ts
blob: ea258c7f6a24aa0d446525b1d21fb6dfa390708a (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
import type { Graduation } from "./graduation-common.ts";
import { Student } from "./student-common.ts";
import {
	getCourseId,
	CourseLocalStorage,
	getUserIconUrl,
	getCourseParticipantsUrl,
} from "./utils-prod.js";
import { fetchGraduation } from "./graduation-prod.ts";

declare module "./student-common.ts" {
	interface Student {
		getGraduation(): Promise<Graduation>;
		getIconUrl(): Promise<string>;
	}
}
Student.prototype.getGraduation = function () {
	return fetchGraduation(this);
};

Student.prototype.getIconUrl = function () {
	return getUserIconUrl(this.id);
};

// Cache the fetched students.
let students: Array<Student> | null = null;

const idRegex = /id=(\d+)/;
async function _fetchStudents() {
	const res = await fetch(getCourseParticipantsUrl());
	const doc = new DOMParser().parseFromString(await res.text(), "text/html");
	const table = doc.querySelector("tbody")!;
	const fetchedStudents: Array<Student> = [];
	const rows = table.querySelectorAll("tr");
	rows.forEach((row) => {
		if (row.classList.contains("emptyrow")) return;

		const roleCell = row.querySelector(".c4")!;
		// TODO(muhq): Is this locale dependent?
		if (!roleCell.textContent.includes("Schüler")) {
			return;
		}

		const nameCell = row.querySelector<HTMLElement>(".c1")!;
		let name;
		const c = nameCell.firstChild!.firstChild! as HTMLElement;
		if (c.tagName == "SPAN") name = c.title;
		else name = nameCell.innerText;

		const profileUrl = nameCell.querySelector<HTMLAnchorElement>("a.aabtn")!.href;
		const match = idRegex.exec(profileUrl);
		if (match === null) {
			console.error("Profile Regex did not match");
			return;
		}
		const id = match[1];

		fetchedStudents.push(new Student(Number(id), name));
	});
	return fetchedStudents;
}

async function fetchStudents(idFilter?: number, nameFilter?: string) {
	if (students === null) {
		const studentsPromise = _fetchStudents();
		const storedStudents = CourseLocalStorage.getItem(`students-${getCourseId()}`);
		if (storedStudents !== null && storedStudents.length > 0) {
			studentsPromise.then((fetched) =>
				CourseLocalStorage.setItem("students", JSON.stringify(fetched)),
			);
			students = [];
			for (const o of JSON.parse(storedStudents)) {
				students.push(new Student(o.id, o.name));
			}
		} else {
			students = await studentsPromise;
			CourseLocalStorage.setItem("students", JSON.stringify(students));
		}
	}

	return students.filter(
		(s) =>
			(typeof idFilter === "undefined" || s.id === idFilter) &&
			(typeof nameFilter === "undefined" || s.name.includes(nameFilter)),
	);
}

async function fetchStudent(idFilter?: number, nameFilter?: string) {
	const students = await fetchStudents(idFilter, nameFilter);
	return students[0];
}

export { Student, fetchStudents, fetchStudent };