aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2026-05-29 14:55:08 +0200
committerFlorian Fischer <florian.fischer@muhq.space>2026-05-29 14:55:08 +0200
commit0d74e04fae3fd118c207de0634b6463bd68f3890 (patch)
tree1eeb92a9532bb9c75e61a36c5f4c7af0425ad316
parent57432354285f2894137c4cc5d80e569410318221 (diff)
downloadbycs-sol-0d74e04fae3fd118c207de0634b6463bd68f3890.tar.gz
bycs-sol-0d74e04fae3fd118c207de0634b6463bd68f3890.zip
cockpit: adapt GraduationChange and implement adding entries to DB
-rw-r--r--cockpit/js/config.js10
-rw-r--r--cockpit/js/database.js33
-rw-r--r--cockpit/js/graduation-common.js41
3 files changed, 45 insertions, 39 deletions
diff --git a/cockpit/js/config.js b/cockpit/js/config.js
index 94dbfc2..545c579 100644
--- a/cockpit/js/config.js
+++ b/cockpit/js/config.js
@@ -1,8 +1,9 @@
-const GRADUATION_DB = "https://lernplattform.bycs.de/mod/data/view.php?d=262744";
+const GRADUATION_DB_ID = 262744;
+const GRADUATION_ACTIVITY_ID = 92663494;
const GRADUATION_DB_PARAMS_MAP = {
id: "f_1133275",
- // name: "f_1130848",
- // group: "f_1130869",
+ name: "f_1130848",
+ group: "f_1130869",
change: "f_1133254",
reason: "f_1130860",
date: (date) => {
@@ -13,7 +14,6 @@ const GRADUATION_DB_PARAMS_MAP = {
f_1130863_z: 1,
};
},
- time: "f_1133248",
coach: "f_1130866",
}
@@ -33,7 +33,7 @@ async function getUserIconUrl(userId) {
}
export {
- GRADUATION_DB, GRADUATION_DB_PARAMS_MAP,
+ GRADUATION_DB_ID, GRADUATION_ACTIVITY_ID, GRADUATION_DB_PARAMS_MAP,
getCourseId,
getUserIconUrl,
getCourseParticipantsUrl
diff --git a/cockpit/js/database.js b/cockpit/js/database.js
index 9c71025..3297a11 100644
--- a/cockpit/js/database.js
+++ b/cockpit/js/database.js
@@ -1,5 +1,5 @@
-async function loadFromDatabase(url, params) {
- let dbUrl = url;
+async function loadFromDatabase(id, params) {
+ let dbUrl = `/mod/data/view.php?d=${id}`;
dbUrl += "&perpage=1000";
// TODO(muhq): Why does this not include entries?
if (params) {
@@ -24,18 +24,25 @@ async function loadFromDatabase(url, params) {
return data;
}
-async function writeToDatabase(url, params) {
- let dbUrl = url;
- dbUrl += "&perpage=1000";
- // TODO(muhq): Why does this not include entries?
- if (params) {
- dbUrl += "&filter=1&advanced=1";
- for (const param in params) {
- dbUrl += `&${param}=${encodeURIComponent(params[param])}`
- }
+async function addEntryToDatabaseActivity(id, params) {
+ const url = `/mod/data/edit.php?id=${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);
+
+ for (const key in params) {
+ const formKey = key.replace("f_", "field_");
+ formData.set(formKey, params[key]);
}
+ formData.set("saveandview", "Speichern");
- // return fetch(encodeURI(dbUrl));
+ return await fetch("/mod/data/edit.php", {
+ method: "POST",
+ // Set the FormData instance as the request body
+ body: formData,
+ });
}
-export { loadFromDatabase, writeToDatabase };
+export { loadFromDatabase, addEntryToDatabaseActivity };
diff --git a/cockpit/js/graduation-common.js b/cockpit/js/graduation-common.js
index 8d0e626..4716e6d 100644
--- a/cockpit/js/graduation-common.js
+++ b/cockpit/js/graduation-common.js
@@ -1,10 +1,9 @@
-import { GRADUATION_DB, GRADUATION_DB_PARAMS_MAP } from "./config.js";
-import { loadFromDatabase, writeToDatabase } from "./database.js"
+import { GRADUATION_DB_ID, GRADUATION_ACTIVITY_ID, GRADUATION_DB_PARAMS_MAP } from "./config.js";
+import { loadFromDatabase, addEntryToDatabaseActivity } from "./database.js"
class GraduationChange {
- constructor(date, time, id, name, group, change, reason, coach, json) {
+ constructor(date, id, name, group, change, reason, coach, json) {
this.date = date;
- this.time = time;
this.id = id;
this.name = name;
this.group = group;
@@ -14,28 +13,27 @@ class GraduationChange {
this.json = json;
}
+ static DB_FIELDS = ["id", "name", "group", "change", "reason", "json"];
+
static newGreenPointChange(student, reason, coach) {
- const now = new Date();
return new GraduationChange(
- now, now.toTimeString(),
+ new Date(),
student.id, student.name, student.group,
"GrĂ¼n", reason, coach, ""
);
}
static newOrangePointChange(student, reason, coach) {
- const now = new Date();
return new GraduationChange(
- now, now.toTimeString(),
+ new Date(),
student.id, student.name, student.group,
"Orange", reason, coach, ""
);
}
static newRedPointChange(student, reason, coach) {
- const now = new Date();
return new GraduationChange(
- now, now.toTimeString(),
+ new Date(),
student.id, student.name, student.group,
"Rot", reason, coach, ""
);
@@ -96,20 +94,14 @@ class Graduation {
async function fetchGraduationChanges(student) {
let params = {};
+ // The id is enough
+ params[GRADUATION_DB_PARAMS_MAP["id"]] = student.id;
- for (const key in student) {
- if (GRADUATION_DB_PARAMS_MAP[key] instanceof Function) {
- const res = GRADUATION_DB_PARAMS_MAP[key](student[key]);
- Object.assign(res, params);
- } else {
- params[GRADUATION_DB_PARAMS_MAP[key]] = student[key];
- }
- }
- const data = await loadFromDatabase(GRADUATION_DB, params);
+ const data = await loadFromDatabase(GRADUATION_DB_ID, params);
let changes = [];
for (const e of data) {
- const gradChange = new GraduationChange(e.date, e.time, e.id, e.name, e.group, e.change, e.reason, e.coach, e.json);
+ const gradChange = new GraduationChange(e.date, e.id, e.name, e.group, e.change, e.reason, e.coach, e.json);
changes.push(gradChange);
}
return changes;
@@ -121,8 +113,15 @@ async function fetchGraduation(student) {
}
async function submitGraduationChange(student, change) {
+ let params = {};
student.graduation.addChange(change);
- writeToDatabase(change);
+ for (const key of GraduationChange.DB_FIELDS) {
+ if (GRADUATION_DB_PARAMS_MAP[key]) {
+ params[GRADUATION_DB_PARAMS_MAP[key]] = change[key];
+ }
+ }
+
+ addEntryToDatabaseActivity(GRADUATION_ACTIVITY_ID, params);
}
export {