From 7c58eeacf121cebb42818aa059c99c706e2d25e1 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Sat, 13 Jun 2026 13:04:56 +0200 Subject: add groups JS --- js/group.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ js/mock.js | 4 ++++ 2 files changed, 63 insertions(+) create mode 100644 js/group.js diff --git a/js/group.js b/js/group.js new file mode 100644 index 0000000..103f368 --- /dev/null +++ b/js/group.js @@ -0,0 +1,59 @@ +import { getCourseParticipantsUrl } from "./config.js"; +import { fetchStudents } from "./student-ENV.js"; + +/** + * Class to represent a Group of Students. + */ +class Group { + constructor(name, students) { + this.name = name; + if (!students) { + this.students = []; + } + } + + setStudents(students) { + this.students = students; + } + + addStudent(student) { + this.students.push(student); + } + + async getStudents() { + return students; + } +} + +let groups = new Map(); + +function buildGroups(students) { + for (const student of students) { + for (const groupName of student.groups) { + if (!groups.has(groupName)) { + groups.set(groupName, new Group(groupName)); + } + groups.get(groupName).addStudent(student); + + // Create the individual student group + const sGroupName = student.getGroupName(); + groups.set(sGroupName, new Group(sGroupName, [student])); + } + } +} + +/** + * Fetch all Groups and return them as a map. + */ +async function fetchGroups() { + if (groups.size) { + return groups; + } + + // Fetch all students and build the groups from them + const students = fetchStudents(); + buildGroups(students); + return groups; +} + +export { Group, buildGroups, fetchGroups }; diff --git a/js/mock.js b/js/mock.js index a189a11..92bb4c6 100644 --- a/js/mock.js +++ b/js/mock.js @@ -1,5 +1,6 @@ import { Graduation, GraduationChange } from "./graduation-common.js"; import { Student } from "./student-common.js"; +import { buildGroups } from "./group.js"; const students = [ new Student(1, 'foo bar', 'test'), @@ -7,6 +8,9 @@ const students = [ new Student(3, 'b b b', '5a') ]; +// Build the groups from our mock students +buildGroups(students); + function fetchStudentsMock() { return new Promise((resolve) => { resolve(students); }); } -- cgit v1.2.3