aboutsummaryrefslogtreecommitdiff
path: root/cockpit/js/cockpit.js
blob: de0f3edbcd38efd44e312f1039e33531f12f3f49 (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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import {
	fetchSubjectsMock as fetchSubjects,
	fetchProgressMock as fetchProgress,
} from "./mock.js";

import {
	GraduationChange,
	fetchGraduation,
	submitGraduationChange
} from "./graduation-ENV.js";

import { Student, fetchStudents } from "./student-ENV.js";
import { isTeacher as getIsTeacher } from "../../utils-ENV.js";

let isTeacher;
let firstname;
let lastname;
let student;

function initCurrentName() {
	const autoNameDiv = document.getElementById("auto-name");
	firstname = autoNameDiv.children[0].innerHTML;
	lastname = autoNameDiv.children[1].innerHTML;
	autoNameDiv.remove();
}

async function loadNewStudent(name, group) {
	student = new Student(0, name, group);
	await loadStudent(student);
}

async function loadStudent(studentToLoad) {
	student = studentToLoad;
	fetchAndUpdateStudentInfo();
	updateStudentCalendar();
	updateStudentProgress();
	updateStudentGraduation();
}

async function initStudentSelection() {
	const sel = document.getElementById("student-selection");
	const list = document.getElementById('student-selection-list');
	const noResults = document.getElementById('student-selection-noresults');
	const clearBtn = document.getElementById('student-selection-clear');

	const students = await fetchStudents();

	let items = [];

	let currentFocus = -1;
	let visibleItems = [];
	function setActive() {
		visibleItems.forEach(item => item.classList.remove("active"));
		if (currentFocus >= 0 && visibleItems[currentFocus]) {
			visibleItems[currentFocus].classList.add("active");
		}
	}

	for (const student of students) {
		let item = document.createElement("li");
		item.student = student;
		item.innerHTML = `${student.name} ${student.group}`;
		item.style.display = 'none';

		item.addEventListener('click', function() {
			sel.value = "";
			sel.dispatchEvent(new Event('keyup'));
			loadStudent(student);
		});

		list.appendChild(item);
		items.push(item);
	}

	sel.addEventListener('keyup', function(e) {
		if ((["ArrowDown", "ArrowUp", "Enter"]).includes(e.key))
			return;

		const filter = sel.value.toLowerCase();
		visibleItems = [];
		for (const item of items) {
			const itemText = item.textContent.trim().toLowerCase();

			if (itemText.indexOf(filter) > -1 && filter !== "") {
				item.style.display = "";
				visibleItems.push(item);

				// Highlight matched text
				const regex = new RegExp(`(${filter})`, 'gi');
				item.innerHTML = item.textContent.replace(
					regex,
					"<u class='student-selection-list-highlight'>$1</u>"
				);
			} else {
				item.style.display = "none";
			}
		}

		// Show the clear button if there is an input
		clearBtn.style.display = sel.value ? "block" : "none";

		// Select the first or none if there are no items visible
		currentFocus = visibleItems.length ? 0 : -1;
		setActive(visibleItems);
		noResults.style.display = visibleItems.length || filter === "" ? "none" : "block";
	});

	clearBtn.addEventListener('click', function() {
		sel.value = "";
		sel.dispatchEvent(new Event('keyup')); // reset list
		sel.focus();
	});

	sel.addEventListener('keydown', function(e) {
		if (e.key === "ArrowDown" || e.key === "ArrowUp") {
			if(e.key === "ArrowDown")
				currentFocus = (currentFocus + 1) % visibleItems.length;
			else
				currentFocus = (currentFocus - 1 + visibleItems.length) % visibleItems.length;
			setActive();
			e.preventDefault();
		} else if (e.key === "Enter") {
			if (currentFocus > -1 && visibleItems[currentFocus]) {
				
				const visibleItem = visibleItems[currentFocus];
				sel.value = "";
				sel.dispatchEvent(new Event('keyup'));
				loadStudent(visibleItem.student);
			}
			e.preventDefault();
		}
	});
}

function pointClickHandler(color) {
	const textareaDiv = document.getElementById("student-info-point-reason-container");
	const textarea = document.getElementById("student-info-point-reason");
	const submitBtn = document.getElementById("student-info-point-reason-submit");

	// TODO(muhq): Prefill the textarea with common reasons
	switch (color) {
	case "green":
		textarea.style.background = "rgb(from green r g b / 20%)"
		textarea.value = "";
		break;
	case "orange":
		textarea.style.background = "rgb(from orange r g b / 20%)"
		textarea.value = "";
		break;
	case "red":
		textarea.style.background = "rgb(from red r g b / 20%)"
		textarea.value = "";
		break;
	}
	textareaDiv.style.display = "block";
	textarea.focus();

	// Can not use string interpolation because of Moodle's firstname filter.
	const coach = firstname + " " + lastname;

	let submit = function() {
		let change;
		switch (color) {
		case "green":
			change = GraduationChange.newGreenPointChange(student, textarea.value, coach);
			break;
		case "orange":
			change = GraduationChange.newOrangePointChange(student, textarea.value, coach);
			break;
		case "red":
			change = GraduationChange.newRedPointChange(student, textarea.value, coach);
			break;
		default:
			alert("Invalid color");
			break;
		}
		submitGraduationChange(student, change);
		textareaDiv.style.display = "none";
		updateStudentInfo();
	}
	// Use onEVENT to override any previous handler
	textarea.onkeydown = function(e) {
		if (e.shiftKey && e.key === "Enter") {
			submit();
		}
		if (e.key === "Escape") {
			textareaDiv.style.display = "none";
		}
	};
	submitBtn.onclick = submit;
}

async function fetchAndUpdateStudentInfo() {
	student.graduation = await fetchGraduation(student);
	updateStudentInfo();
}

let DEFAULT_STUDENT_ICON;

function updateStudentInfo() {
	// Reset the student icon
	document.getElementById("student-info-icon").src = DEFAULT_STUDENT_ICON;

	// Try to replace the generic icon with the user's icon
	var img = document.createElement('img');
	img.onload = function() {
		document.getElementById("student-info-icon").src = img.src;
		img = img.onload = null;
	};
	student.getIconUrl().then( (url) => img.src = url );

	document.getElementById("student-info-name").innerHTML = `${student.name}`;

	if (student.graduation) {
		const tag = document.getElementById("student-info-tag");
		tag.innerHTML = student.graduation.level;
		tag.style.display = "block";

		document.getElementById("student-info-green").addEventListener('click', () => pointClickHandler("green"));
		document.getElementById("student-info-points-green").innerHTML = student.graduation.green;

		document.getElementById("student-info-orange").addEventListener('click', () => pointClickHandler("orange"));
		document.getElementById("student-info-points-orange").innerHTML = student.graduation.orange;

		document.getElementById("student-info-red").addEventListener('click', () => pointClickHandler("red"));
		document.getElementById("student-info-points-red").innerHTML = student.graduation.red;

		renderGraduationLevel(student.graduation, document.getElementById("student-info-level"));

		renderGraduationStickers(student.graduation, document.getElementById("student-info-stickers"));
		renderGraduationBonus(student.graduation, document.getElementById("student-info-stickers"));
	}

	document.getElementById("student-content").style.display = "block";
}

function renderGraduationLevel(graduation, node) {
	node.innerHTML = graduation.level;
}

function renderGraduationStickers(graduation, node) {
}

function renderGraduationBonus(graduation, node) {
}

function newProgressBar(subject) {
	let bar = document.createElement("div");
	fetchProgress(subject).then((progress) => {
		bar.progress = progress;
	});

	let s1 = document.createElement("div");
	s1.onhover

	bar.appendChild(s1);
	let s2 = document.createElement("div");
	bar.appendChild(s2);
	let s3 = document.createElement("div");
	bar.appendChild(s3);
	return bar;
}

let selectedSubjects;
async function updateStudentProgress() {
	selectedSubjects = await fetchSubjects(student);

	let prog = document.getElementById("progress");
	for(const subj of selectedSubjects) {
		let subjDiv = document.createElement("div");
		subjDiv.innerHTML = subj;
		subjDiv.appendChild(newProgressBar(subj))
		prog.appendChild(subjDiv);
	}
}

async function updateStudentCalendar() {
	let cal = document.getElementById("calendar");
}

async function updateStudentGraduation() {
	let graduation = document.getElementById("graduation");
}

async function initCockpit() {
	if (isTeacher) {
		await initStudentSelection();
	} else {
		loadNewStudent(`${firstname} ${lastname}`);
	}
	return;
}

window.onload = function() {
	isTeacher = getIsTeacher();
	DEFAULT_STUDENT_ICON = document.getElementById("student-info-icon").src;
	initCurrentName();
	initCockpit(isTeacher);
}