diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2026-06-13 13:04:07 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2026-06-13 13:04:07 +0200 |
| commit | 4882c5ca3a65075e03595588ef3780a36bc74b15 (patch) | |
| tree | 07aa2a276cd87a8e192147440411c1f51d8aa624 /js/student-common.js | |
| parent | 9fbd21ff6bf97b9ce77cb78f420abd951b8aaf27 (diff) | |
| download | bycs-sol-4882c5ca3a65075e03595588ef3780a36bc74b15.tar.gz bycs-sol-4882c5ca3a65075e03595588ef3780a36bc74b15.zip | |
move generic js into root
Diffstat (limited to 'js/student-common.js')
| -rw-r--r-- | js/student-common.js | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/js/student-common.js b/js/student-common.js new file mode 100644 index 0000000..930c373 --- /dev/null +++ b/js/student-common.js @@ -0,0 +1,73 @@ +import { getUserIconUrl, getCourseParticipantsUrl } from "./config.js"; +/** + * Class to represent Students. + */ +class Student { + constructor(id, name, groups) { + this.id = id; + this.name = name; + this.groups = groups; + } + + async getIconUrl() { + return await getUserIconUrl(this.id); + } + + getGroupName() { + return `g${this.id}${this.name.replace(' ', '')}`; + } +} + +async function fetchStudent(idFilter, nameFilter, groupFilter) { + const students = await fetchStudents(idFilter, nameFilter, groupFilter); + return students[0]; +} + +// Cache the fetched students. +let students = []; + +const idRegex = /id=(\d+)/; +async function fetchStudents(idFilter, nameFilter, groupFilter) { + if (students.size) { + return students; + } + + const res = await fetch(getCourseParticipantsUrl()); + const doc = new DOMParser().parseFromString(await res.text(), "text/html"); + const table = doc.querySelector("tbody"); + 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 groupsText = groupCell.querySelector("a.quickeditlink").textContent; + const group = groupsText.trim(); + + if ((!idFilter || id === idFilter) && + (!nameFilter || name.includes(nameFilter)) && + (!groupFilter || groupFilter === group)) + { + students.push(new Student(id, name, group)); + } + }); + return students; +} + +export { Student, fetchStudents, fetchStudent }; |
