aboutsummaryrefslogtreecommitdiff
path: root/src/graduation-common.ts
blob: 61600d3d60fc0d43f56cd32bfb904c41cdcf7b3a (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import { getGraduationDb, DatabaseEntry } from "./database-ENV.ts"
import type { Student } from "./student-common.ts"

import NeulingPng from "../assets/Neuling.png";
import EntdeckerPng from "../assets/Entdecker.png";
import NavigatorPng from "../assets/Navigator.png";
import PioneerPng from "../assets/Pioneer.png";

const LEVEL_NAMES = ["Neuling", "Entdecker", "Navigator", "Pionieer"];

const LEVEL_IMAGES = [
	new URL(NeulingPng),
	new URL(EntdeckerPng),
	new URL(NavigatorPng),
	new URL(PioneerPng),
];

class GraduationChange {
	date: Date;
	studentId: number;
	change: string;
	reason: string;
	coach: string;
	context: any;

	constructor(date: Date, studentId: number, change: string, reason: string, coach: string, context: any) {
		this.date = date;
		this.studentId = studentId;
		this.change = change;
		this.reason = reason;
		this.coach = coach;
		this.context = context;
	}

	dbFields () {
		return {
			"studentId": this.studentId,
			"change": this.change,
			"reason": this.reason,
			"context": this.context,
		};
	}

	static fromDBEntry(e: DatabaseEntry) {
		const {date, coach, json} = e;
		const change = JSON.parse(json);
		return new GraduationChange(new Date(date), change.change, change.student, change.reason, coach, change.context);
	}

	static newObservationRequest() {
		return new GraduationChange(new Date(), -1, "ObservationRequest", "", "", "");
	}

	static newObservationStart() {
		return new GraduationChange(new Date(), -1, "ObservationStart", "", "", "");
	}

	static newObservationEnd(reason: string, success: boolean) {
		return new GraduationChange(new Date(), -1, "ObservationEnd", reason, "", JSON.stringify(success));
	}

	static newObservationChange(observation: Observation) {
		return new GraduationChange(observation.date, -1, "Observation", "", "", JSON.stringify(observation.status));
	}

	static newGreenPointChange(reason: string, coach: string) {
		return new GraduationChange(new Date(), -1, "Grün", reason, coach, "");
	}

	static newOrangePointChange(reason: string, coach: string) {
		return new GraduationChange(new Date(), -1, "Orange", reason, coach, "");
	}

	static newRedPointChange(reason: string, coach: string) {
		return new GraduationChange(new Date(), -1, "Rot", reason, coach, "");
	}
}

class Graduation {
	student: Student;
	green: number;
	orange: number;
	red: number;
	level: number;
	observationRequested: Date | null;
	observation: ObservationPeriod | null;
	pastObservations: Array<ObservationPeriod>;
	changes: Array<GraduationChange>;

	constructor(student: Student, green: number, orange: number, red: number, level: number,  observationRequested: Date | null, observation: ObservationPeriod | null, pastObservations: Array<ObservationPeriod>, changes: Array<GraduationChange>) {
		this.student = student;
		this.green = green;
		this.orange = orange;
		this.red = red;
		this.level = level;
		this.observationRequested = observationRequested;
		this.observation = observation;
		this.pastObservations = pastObservations;
		this.changes = changes;

		student.graduation = this;
	}

	static from(student: Student, changes: Array<GraduationChange>) {
		let green = 0;
		let orange = 0;
		let red = 0;
		let level = 1;
		let openObservationRequest = null;
		let observationPeriod = null;
		let pastObservations = [];

		// The parsing logic assumes that changes are ordered chronologically
		changes.sort((a, b) => a.date.valueOf() - b.date.valueOf());

		for (const change of changes) {
			switch (change.change) {
			case "Grün":
				green++;
				break;

			case "Orange":
				orange++;
				break;

			case "Rot":
				red++;
				break;

			case "ObservationRequest":
				openObservationRequest = change.date;
				break;

			case "ObservationStart":
				console.assert(openObservationRequest === null,
					"No request for observation start");

				observationPeriod = new ObservationPeriod(change.date);
				openObservationRequest = null;
				break;

			case "ObservationEnd":
				if (observationPeriod === null) {
					console.error("No active observation period");
					continue;
				}
				observationPeriod.finishPeriod(change.context.success);
				if (change.context.success) {
					level++;
				}
				pastObservations.push(observationPeriod);
				observationPeriod = null;
				break;

			case "Observation":
				if (observationPeriod === null) {
					console.error("No active observation period");
					continue;
				}
				const observation = Observation.fromChange(change);
				observationPeriod.addObservation(observation);
				break;

			case "LevelChange":
				level = change.context.level;
				break;

			default:
				throw new Error(`Unexpected change: ${change}`);
			}
		}
		return new Graduation(student, green, orange, red, level,
			openObservationRequest, observationPeriod, pastObservations,
			changes);
	}

	addChange(change: GraduationChange) {
		change.studentId = this.student.id;
		// TODO(muhq): Implement incremental addition
		let changes = this.changes;
		changes.push(change);
		// This updates the student.graduation reference as well
		Graduation.from(this.student, changes);
	}

	/**
	 * Request observation
	 */
	async requestObservation() {
		const request = GraduationChange.newObservationRequest();
		this.observationRequested = request.date;
		const db = await getGraduationDb();
		await db.saveEntry(JSON.stringify(request.dbFields()));
	}

	/**
	 * Start an observation
	 */
	async startObservation() {
		if (this.observationRequested === null) {
			console.error("No observation requested");
			return;
		}
		this.observationRequested = null;
		this.observation = new ObservationPeriod(new Date());
		return submitGraduationChange(this, GraduationChange.newObservationStart());
	}

	/**
	 * End an observation
	 */
	async endObservation(reason: string, success: boolean) {
		if (this.observation === null) {
			console.error("No active observation");
			return;
		}
		this.observation.end = new Date();
		this.pastObservations.push(this.observation);
		this.observation = null;
		return submitGraduationChange(this, GraduationChange.newObservationEnd(reason, success));
	}

	/**
	 * Return if the student is in observation.
	 */
	isObserved() {
		return Boolean(this.observation);
	}

	/**
	 * Return the URI of the icon representing the graduation level.
	 */
	getIcon() {
		return LEVEL_IMAGES[this.level - 1];
	}

	/**
	 * Return the name of the current graduation level.
	 */
	getLevelName() {
		return LEVEL_NAMES[this.level - 1];
	}
}

class ObservationPeriod {
	start: Date;
	end: Date | null;
	observations: Array<Observation>;
	success = false;

	constructor(start: Date) {
		this.start = start;
		this.end = null;
		this.observations = [];
	}

	finishPeriod(success: boolean) {
		this.end = new Date();
		this.success = success;
	}

	ended() {
		return Boolean(this.end);
	}

	ready() {
		if (this.observations.length < 10)
			return false;

		// at least 10 observation on different days
		let days = new Map();
		for (const observation of this.observations) {
			const d = observation.date;
			days.set(`${d.getFullYear()}${d.getMonth()}${d.getDate()}`, true);
		}
		return days.size >= 10;
	}

	getObservationsSums() {
		let sums = Array(OBSERVATION_TYPES.length).fill(null).map(() => ({"yes":0, "no":0}));
		for (const observation of this.observations) {
			for (let i = 0; i < sums.length; i++) {
				if (observation.status[i]) {
					if (observation.status[i] === "yes")
						sums[i].yes++;
					else
						sums[i].no++;
				}
			}
		}
		return sums;
	}

	addObservation(observation: Observation) {
		this.observations.push(observation);
	}
}

const OBSERVATION_TYPES = [
	"Arbeitet konzentriert an Aufgaben",
	"Arbeitet zunehmend selbstständig",
	"Führt das Logbuch zuverlässig",
	"Bringt Material mit",
	"Hält Regeln ein",
	"Geht verantwortungsvoll mit Materialien und LernRaum um",
	"Nimmt Rückmeldungen an und setzt sie um",
];

class Observation {
	date: Date;
	status: Array<string>;

	constructor(date: Date, status: Array<string>) {
		console.assert(status.length == OBSERVATION_TYPES.length, "invalid observation status");
		this.date = date;
		this.status = status;
	}

	static fromChange(change: GraduationChange) {
		return new Observation(change.date, JSON.parse(change.context))
	}
}

async function submitGraduationChange(graduation: Graduation, change: GraduationChange) {
	graduation.addChange(change);
	const db = await getGraduationDb();
	return db.saveEntry(JSON.stringify(change.dbFields()));
}

async function submitObservation(graduation: Graduation, observation: Observation) {
	if(graduation.observation === null) throw new Error("no active observation");
	graduation.observation.addObservation(observation);
	await submitGraduationChange(graduation, GraduationChange.newObservationChange(observation));
}

export {
	LEVEL_NAMES,
	LEVEL_IMAGES,
	GraduationChange,
	Graduation,
	submitGraduationChange,
	submitObservation,

	OBSERVATION_TYPES,
	Observation,
	ObservationPeriod,
};