"use strict"; export const DATE_FMT = new Intl.DateTimeFormat("de-DE", { month: "numeric", day: "numeric", year: "numeric", }); export const DATETIME_FMT = new Intl.DateTimeFormat("de-DE", { month: "numeric", day: "numeric", year: "numeric", hour: "numeric", minute: "numeric", }); export function dateStringNow() { return DATE_FMT.format(new Date()); } export function datetimeStringNow() { return DATETIME_FMT.format(new Date()); } declare global { var M: { cfg: { userId: number; courseId: number } }; } export function getUserId(): number { // M is exported by Moodle return globalThis.M.cfg.userId; } export function getCourseId(): number { // M is exported by Moodle return globalThis.M.cfg.courseId; } // TODO(muhq): Cache the retrieved URL export async function getUserIconUrl(userId: number): Promise { const userRes = await fetch(`https://lernplattform.bycs.de/user/profile.php?id=${userId}`); const userDoc = new DOMParser().parseFromString(await userRes.text(), "text/html"); const userpicture = userDoc.querySelector(".userpicture"); if (userpicture === null) { return ""; } return (userpicture as HTMLImageElement).src ?? ""; } // SplitMix32 implementation taken from: // https://github.com/bryc/code/blob/master/jshash/PRNGs.md // We only need a seeded PRNG to choose students to observe. export function splitmix32(a: number) { return function () { a |= 0; a = (a + 0x9e3779b9) | 0; let t = a ^ (a >>> 16); t = Math.imul(t, 0x21f0aaad); t = t ^ (t >>> 15); t = Math.imul(t, 0x735a2d97); return ((t = t ^ (t >>> 15)) >>> 0) / 4294967296; }; }