From 16668963d4dcc95ad281608dc3064677d8643404 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Sun, 27 Sep 2026 18:48:50 +0200 Subject: admin-tools: implement mass course update --- src/moodle-prod.ts | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 src/moodle-prod.ts (limited to 'src/moodle-prod.ts') diff --git a/src/moodle-prod.ts b/src/moodle-prod.ts new file mode 100644 index 0000000..39bb190 --- /dev/null +++ b/src/moodle-prod.ts @@ -0,0 +1,120 @@ +import { getCourseId, getSesskey } from "./utils-ENV.js"; + +export class Activity { + id: number; + title: string; + + constructor(id: number, title: string) { + this.id = id; + this.title = title; + } + + async update(text: string) { + const url = `https://lernplattform.bycs.de/course/modedit.php?update=${this.id}`; + const editRes = await fetch(url); + const editDoc = new DOMParser().parseFromString(await editRes.text(), "text/html"); + const form = editDoc.querySelector('div[role="main"] > form'); + const formData = new FormData(form!); + + formData.set("page[text]", text); + formData.set("submitbutton", "Speichern und anzeigen"); + return await fetch("/course/modedit.php", { + method: "POST", + body: formData, + }); + } +} + +export class Course { + id: Number; + title: string; + activities: Promise>; + + constructor(id: number, title: string, courseDoc?: Document) { + this.id = id; + this.title = title; + this.activities = this.fetchActivities(courseDoc); + } + + async fetchActivities(courseDoc?: Document) { + if (typeof courseDoc === "undefined") { + const courseUrl = `https://lernplattform.bycs.de/course/view.php?id=${this.id}`; + const courseRes = await fetch(courseUrl); + courseDoc = new DOMParser().parseFromString(await courseRes.text(), "text/html"); + } + + const activities = []; + for (const n of courseDoc.querySelectorAll(".activity-name-area")) { + const url = n.querySelector(".aalink")?.href!; + const match = /id=(\d+)/.exec(url); + const a = new Activity(Number(match![1]), n.textContent.trim()); + activities.push(a); + } + + return activities; + } + + async getActivity(name: string) { + const activities = await this.activities; + for (const a of activities) { + if (a.title.includes(name)) { + return a; + } + } + return null; + } +} + +export async function fetchCourse(courseId?: number) { + if (typeof courseId === "undefined") { + courseId = Number(getCourseId()); + } + + const courseUrl = `https://lernplattform.bycs.de/course/view.php?id=${courseId}`; + const courseRes = await fetch(courseUrl); + const courseDoc = new DOMParser().parseFromString(await courseRes.text(), "text/html"); + + return new Course(courseId, courseDoc.querySelector("h1")?.innerText!, courseDoc); +} + +export async function fetchCourses(pattern: string) { + const params = { + index: 0, + methodname: "core_course_get_enrolled_courses_by_timeline_classification", + args: { + offset: 0, + limit: 0, + classification: "search", + sort: "fullname", + customfieldname: "schule", + customfieldvalue: "Gymnasium Messestadt Riem", + searchvalue: pattern, + requiredfields: [ + "id", + "fullname", + "shortname", + "showcoursecategory", + "showshortname", + "visible", + "enddate", + ], + }, + }; + + const coursesRes = await fetch( + `/lib/ajax/service.php?sesskey=${getSesskey()}&info=core_course_get_enrolled_courses_by_timeline_classification`, + { + method: "POST", + body: JSON.stringify([params]), + }, + ); + + const coursesJSON = JSON.parse(await coursesRes.text()); + const courses = []; + for (const o of coursesJSON[0].data.courses) { + const c = new Course(o.id, o.fullname); + courses.push(c); + } + + return courses; +} -- cgit v1.2.3