1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
import { GraduationChange, submitGraduationChange } from "./graduation-ENV.ts";
import { fetchStudent, fetchStudents } from "./student-ENV.ts";
import { fetchCourses } from "./moodle-ENV.ts";
import { DB } from "./database-ENV.ts";
import { DATETIME_FMT } from "./utils-common.ts";
let outputNode;
function output(msg) {
if (outputNode.style.display === "none") {
outputNode.style.display = "block";
}
const line = document.createElement("div");
line.innerHTML = msg;
line.classList.add("admin-tools-output-line");
outputNode.appendChild(line);
}
async function coachAssignmentImport() {
const csv = document.getElementById("admin-tools-coach-import").value;
const assignments = {};
for (const line of csv.split("\n")) {
const [studentName, ...coaches] = line.trim().split(";");
if (studentName === "*") {
const students = await fetchStudents();
for (const student of students) {
assignments[student.id] = coaches;
}
} else {
const student = await fetchStudent(undefined, studentName);
if (student === null) {
output(`${studentName} nicht gefunden`);
continue;
}
assignments[student.id] = coaches;
}
}
const assignmentChange = GraduationChange.newCoachAssignmentChange(assignments);
await submitGraduationChange(assignmentChange);
output("Coach Zuweisung importiert");
}
async function updateActivities(course) {
const promises = [];
for (const activityName of ["Cockpit", "Klassenliste", "Coaching", "Admin-Tools"]) {
const input = document.getElementById(`${activityName.toLowerCase()}-file-input`);
if (input.value === "") continue;
const activity = await course.getActivity(activityName);
if (activity === null) {
output(course.title + ": Skipping non existing: " + activityName);
continue;
}
const text = input.files[0].text();
promises.push(activity.update(await text));
}
return Promise.all(promises);
}
async function updateCourses() {
output("loading courses ...");
const courses = await fetchCourses("SOL Graduierung");
output(`updating ${courses.length} courses ... `);
const promises = [];
for (const course of courses) {
promises.push(updateActivities(course));
}
Promise.all(promises)
.then(() => {
output("Updating activities done.");
})
.catch((error) => {
output("Updating activities failed: " + error);
});
}
async function findDuplicates(course) {
const dbActivity = await course.getActivity("Graduation");
const db = new DB(dbActivity.id);
const entries = await db.fetchEntries();
const changes = entries.map((e) => GraduationChange.fromDBEntry(e));
const searched = new Set();
const duplicates = [];
for (const change of changes) {
if (searched.has(change)) continue;
searched.add(change);
const curDuplicates = [change];
for (const other of changes) {
if (other === change) continue;
let same = true;
for (const key in change) {
switch (key) {
case "changeId":
continue;
case "date":
same = same && change.date.getTime() === other.date.getTime();
break;
case "context":
same = same && JSON.stringify(change.context) === JSON.stringify(other.context);
break;
default:
same = same && change[key] === other[key];
}
if (!same) break;
}
if (same) {
curDuplicates.push(other);
searched.add(other);
}
}
if (curDuplicates.length > 1) {
duplicates.push(curDuplicates);
}
}
if (duplicates.length === 0) return;
let msg = `${course.title} Duplikate:<br>`;
for (const d of duplicates) {
const ids = d.map((e) => e.changeId);
const c = d[0];
msg += `- ${DATETIME_FMT.format(c.date)} ${c.coach} ${c.studentId} ${c.change} ${JSON.stringify(c.context)} ids: ${ids}<br>`;
}
output(msg + "\n");
}
async function findAllDuplicates() {
output("loading courses ...");
const courses = await fetchCourses("SOL Graduierung");
output(`searching in ${courses.length} courses ... `);
for (const course of courses) {
findDuplicates(course);
}
}
async function init() {
outputNode = document.getElementById("admin-tools-output");
output("Admin tools ready.");
document
.getElementById("admin-tools-coach-import-submit")
.addEventListener("click", coachAssignmentImport);
document.getElementById("activity-update-submit").addEventListener("click", updateCourses);
document.getElementById("print-ids").addEventListener("click", async function () {
const students = await fetchStudents();
output(JSON.stringify(students.map((s) => s.id)));
});
document.getElementById("find-duplicates").addEventListener("click", findAllDuplicates);
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init);
} else {
init();
}
|