diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2026-05-28 15:20:46 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2026-05-28 15:20:46 +0200 |
| commit | f9a9fa15d6d61eb9730183193ce8af29bdfc17e7 (patch) | |
| tree | c3b96a36623e52f22d281b518d7136795cf82b8a /cockpit/js/student-common.js | |
| download | bycs-sol-f9a9fa15d6d61eb9730183193ce8af29bdfc17e7.tar.gz bycs-sol-f9a9fa15d6d61eb9730183193ce8af29bdfc17e7.zip | |
cockpit: initial commit
Diffstat (limited to 'cockpit/js/student-common.js')
| -rw-r--r-- | cockpit/js/student-common.js | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/cockpit/js/student-common.js b/cockpit/js/student-common.js new file mode 100644 index 0000000..74cc845 --- /dev/null +++ b/cockpit/js/student-common.js @@ -0,0 +1,56 @@ +import { getUserIconUrl, getCourseParticipantsUrl } from "./config.js"; +/** + * Class to represent Students. + */ +class Student { + constructor(id, name, group) { + this.id = id; + this.name = name; + this.group = group; + } + + getIconUrl() { + return getUserIconUrl(this.id); + } +} + +async function fetchStudent(nameFilter, groupFilter) { + return await fetchStudents(nameFilter, groupFilter)[0]; +} + +const idRegex = /id=(\d+)/; +async function fetchStudents(nameFilter, groupFilter) { + const res = await fetch(getCourseParticipantsUrl()); + const doc = new DOMParser().parseFromString(await res.text(), "text/html"); + const table = doc.querySelector("tbody"); + let students = []; + 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"); + const name = nameCell.firstChild.firstChild.title; + 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 ((!nameFilter || name.includes(nameFilter)) && + (!groupFilter || groupFilter === group)) + { + students.push(new Student(id, name, group)); + } + }); + return students; +} + +export { Student, fetchStudents, fetchStudent }; |
