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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
import { GraduationChange, submitGraduationChange } from "./graduation-ENV.ts";
import { fetchStudents } from "./student-ENV.ts";
import { getName } from "./utils-ENV.js";
let name;
function toggle(btn, others) {
if (btn.innerHTML === "X") {
btn.innerHTML = "o";
} else {
btn.innerHTML = "X";
for (const o of others) {
o.innerHTML = "o";
}
}
const submitBtn = document.getElementById("submit-mass-graduation");
submitBtn.style.display = "inline";
submitBtn.disabled = false;
document.getElementById("submit-mass-graduation-state").innerHTML = "";
}
// https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
function chooseMandatory(graduations) {
graduations.sort((g1, g2) => g1.mandatory - g2.mandatory);
// Group the graduations according to the amount of mandatory changes
const mandatoryCandidateGroups = [];
let curGroup = [];
let mandatory = graduations[0].mandatory;
for (const g of graduations) {
if (g.mandatory === mandatory) {
curGroup.push(g);
} else {
// Start a new group
mandatoryCandidateGroups.push(curGroup);
curGroup = [g];
mandatory = g.mandatory;
}
}
// push the last group
mandatoryCandidateGroups.push(curGroup);
// Choose from the group with the smallest amount of mandatory changes
const chosen = [];
for (const candidateGroup of mandatoryCandidateGroups) {
if (chosen.length === 5) break;
// Get a random list of possible indexes
const candidatesIdxs = shuffleArray([...Array(candidateGroup.length).keys()]);
chosen.push(...candidatesIdxs.slice(0, 5 - chosen.length).map((i) => candidateGroup[i]));
}
return chosen;
}
async function loadGroup() {
const students = await fetchStudents();
const graduationPromises = [];
document.getElementById("group-content").style.display = "block";
const mul = document.getElementById("group-mandatory-members");
const ul = document.getElementById("group-members");
mul.innerHTML = "";
ul.innerHTML = "";
const listEntries = [];
for (const student of students) {
const graduationPromise = student.getGraduation();
graduationPromises.push(graduationPromise);
const li = document.createElement("li");
li.student = student;
listEntries.push(li);
const i = document.createElement("img");
i.classList.add("group-member-icon");
graduationPromise.then((graduation) => {
i.src = graduation.getIcon();
});
li.appendChild(i);
}
const graduations = await Promise.all(graduationPromises);
const chosen = chooseMandatory(graduations);
for (const li of listEntries) {
const student = li.student;
const n = document.createElement("span");
n.classList.add("group-member-name");
n.innerHTML = `${student.name}`;
li.appendChild(n);
const btns = [];
for (const col of ["green", "orange", "red"]) {
const b = document.createElement("button");
btns.push(b);
b.classList.add(`group-member-${col}`);
b.innerHTML = "o";
li.appendChild(b);
}
for (const b of btns) {
b.addEventListener("click", () =>
toggle(
b,
btns.filter((o) => o !== b),
),
);
}
if (chosen.includes(student.graduation)) {
li.classList.add("group-mandatory-member");
mul.appendChild(li);
} else {
li.classList.add("group-member");
ul.appendChild(li);
}
}
}
function submitMassGraduation() {
const state = document.getElementById("submit-mass-graduation-state");
state.innerHTML = "";
state.classList.add("submit-mass-graduation-inprogress");
state.style.display = "unset";
// TODO(muhq): await graduation promises
const promises = [];
for (const li of document.querySelectorAll("li.group-member, li.group-mandatory-member")) {
const student = li.student;
const mandatory = li.classList.contains("group-mandatory-member");
if (li.querySelector(".group-member-green").innerHTML === "X") {
promises.push(
submitGraduationChange(
GraduationChange.newGreenPointChange(mandatory, "", name),
student.graduation,
),
);
}
if (li.querySelector(".group-member-orange").innerHTML === "X") {
promises.push(
submitGraduationChange(
GraduationChange.newOrangePointChange(mandatory, "", name),
student.graduation,
),
);
}
if (li.querySelector(".group-member-red").innerHTML === "X") {
promises.push(
submitGraduationChange(
GraduationChange.newRedPointChange(mandatory, "", name),
student.graduation,
),
);
}
}
Promise.all(promises)
.then(() => {
state.style.display = "inline";
state.innerHTML = "✓";
state.style.color = "green";
})
.catch((error) => {
state.style.display = "inline";
state.innerHTML = "X";
state.style.color = "red";
// re-enable submit button
document.getElementById("submit-mass-graduation").disabled = false;
console.log(error);
})
.finally(() => {
state.classList.remove("submit-mass-graduation-inprogress");
});
}
async function init() {
document.getElementById("submit-mass-graduation").addEventListener("click", submitMassGraduation);
name = getName();
loadGroup();
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init);
} else {
init();
}
|