aboutsummaryrefslogtreecommitdiff
path: root/js/group.js
blob: 7b3dfd1b4ccb7c79cbc7969194fdc120931e1740 (plain)
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
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 this.students;
	}
}

let groups = new Map();
let bycsGroups = new Map();

function buildGroups(students) {
	for (const student of students) {
		for (const groupName of student.groups) {
			let group = groups.get(groupName);

			if (!group) {
				group = new Group(groupName);
				groups.set(groupName, group);
				if (groupName != student.getGroupName()) {
					bycsGroups.set(groupName, group);
				}
			}

			group.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 = await fetchStudents();
	buildGroups(students);
	return groups;
}

async function fetchBycsGroups() {
	if (bycsGroups.size) {
		return bycsGroups;
	}

	// Fetch all students and build the groups from them
	const students = await fetchStudents();
	buildGroups(students);
	return bycsGroups;
}

export { Group, buildGroups, fetchGroups, fetchBycsGroups };