aboutsummaryrefslogtreecommitdiff
path: root/src/student-common.js
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2026-08-05 11:51:51 +0200
committerFlorian Fischer <florian.fischer@muhq.space>2026-08-05 11:51:51 +0200
commit582df8885a3573a728394b473b826616f8969316 (patch)
tree9c21db03f4fbfd9956ee398e0d9194ebfa5b3234 /src/student-common.js
parent46ac53e11b49a7f9cdb92c3559b05f41ce90122f (diff)
downloadbycs-sol-582df8885a3573a728394b473b826616f8969316.tar.gz
bycs-sol-582df8885a3573a728394b473b826616f8969316.zip
convert a lot of js to typescript and use a single graduation DB
Diffstat (limited to 'src/student-common.js')
-rw-r--r--src/student-common.js115
1 files changed, 0 insertions, 115 deletions
diff --git a/src/student-common.js b/src/student-common.js
deleted file mode 100644
index 2dfa35d..0000000
--- a/src/student-common.js
+++ /dev/null
@@ -1,115 +0,0 @@
-import { getCourseId, getUserIconUrl, getCourseParticipantsUrl } from "./utils-ENV.js";
-import { fetchGraduation } from "./graduation-common.js";
-/**
- * Class to represent Students.
- */
-class Student {
- graduation = null;
-
- constructor(id, name, group) {
- this.id = id;
- this.name = name;
- this.group = group;
- }
-
- async getIconUrl() {
- return await getUserIconUrl(this.id);
- }
-
- getGroup() {
- return this.group;
- }
-
- async getGraduation() {
- if(!this.graduation) {
- // fetchGraduation(this) sets this.graduation
- await fetchGraduation(this);
- }
- return this.graduation;
- }
-
- static async fetchById(id) {
- let name = "";
- let group = "";
-
- const res = await fetch(`/user/view.php?id=${id}&course=${getCourseId()}`);
- const doc = new DOMParser().parseFromString(await res.text(), "text/html");
-
- name = doc.querySelector("div.page-header-headings > h2").textContent;
-
- const cards = doc.querySelectorAll("div.card-body dl");
- for (const card of cards) {
- if (card.querySelector("dt").textContent !== "Gruppe")
- continue;
- group = card.querySelector("dd > a").textContent;
- break;
- }
-
- return new Student(id, name, group);
- }
-}
-
-// Cache the fetched students.
-let students = [];
-
-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");
- let fetchedStudents = [];
- table.querySelectorAll("tr").forEach(row => {
- if (row.classList.contains("emptyrow"))
- return;
-
- const roleCell = row.querySelector(".c4");
- // TODO(muhq): Is this locale dependent?
- if (roleCell.innerHTML.includes("Lehrer")) {
- return;
- }
-
- const nameCell = row.querySelector(".c1");
- let name;
- if (nameCell.firstChild.firstChild.tagName == "SPAN")
- name = nameCell.firstChild.firstChild.title;
- else
- name = nameCell.innerText;
-
- const profileUrl = nameCell.querySelector("a.aabtn").href;
- const match = idRegex.exec(profileUrl);
- const id = match[1];
-
- const groupCell = row.querySelector(".c5");
- const group = groupCell.querySelector("a.quickeditlink").textContent;
-
- fetchedStudents.push(new Student(id, name, group));
- });
- return fetchedStudents;
-}
-
-async function fetchStudents(idFilter, nameFilter, groupFilter) {
- if (!students.size) {
- const studentsPromise = _fetchStudents()
- const storedStudents = localStorage.getItem("students");
- if (storedStudents) {
- studentsPromise.then((fetched) => localStorage.setItem("students", JSON.stringify(fetched)));
- for (const o of JSON.parse(storedStudents)) {
- students.push(new Student(o.id, o.name, o.group));
- }
- } else {
- students = await studentsPromise;
- localStorage.setItem("students", JSON.stringify(students));
- }
- }
-
- return students.filter((s) => (!idFilter || s.id === idFilter) &&
- (!nameFilter || s.name.includes(nameFilter)) &&
- (!groupFilter || s.group.includes(groupFilter)));
-}
-
-async function fetchStudent(idFilter, nameFilter, groupFilter) {
- const students = await fetchStudents(idFilter, nameFilter, groupFilter);
- return students[0];
-}
-
-export { Student, fetchStudents, fetchStudent };