aboutsummaryrefslogtreecommitdiff
path: root/js/graduation-common.js
blob: 4479becd588c0c75e661da63aed9f3abda1275e3 (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
import { GRADUATION_DB_ID, GRADUATION_ACTIVITY_ID, GRADUATION_DB_PARAMS_MAP } from "./config.js";
import { loadFromDatabase, addEntryToDatabaseActivity } from "./database.js"

const LEVEL_NAMES = ["Beginner", "Entdecker"];

class GraduationChange {
	constructor(date, id, name, group, change, reason, coach, json) {
		this.date = date;
		this.id = id;
		this.name = name;
		this.group = group;
		this.change = change;
		this.reason = reason;
		this.coach = coach;
		this.json = json;
	}

	static DB_FIELDS = ["id", "name", "group", "change", "reason", "json"];

	static newGreenPointChange(student, reason, coach) {
		return new GraduationChange(
			new Date(),
			student.id, student.name, student.group,
			"Grün", reason, coach, ""
		);
	}

	static newOrangePointChange(student, reason, coach) {
		return new GraduationChange(
			new Date(),
			student.id, student.name, student.group,
			"Orange", reason, coach, ""
		);
	}

	static newRedPointChange(student, reason, coach) {
		return new GraduationChange(
			new Date(),
			student.id, student.name,
			"Rot", reason, coach, ""
		);
	}
}

class Graduation {
	constructor(student, green, orange, red, level, stickers, observed, changes) {
		this.student = student;
		this.green = green;
		this.orange = orange;
		this.red = red;
		this.level = level;
		this.stickers = stickers;
		this.observed = observed;
		this.changes = changes;

		student.graduation = this;
	}

	static from(student, changes) {
		let green = 0;
		let orange = 0;
		let red = 0;
		let level = 1;
		let observed = false;
		let stickers = [];
		for (const change of changes) {
			switch (change.change) {
			case "Grün":
				green++;
				break;
			case "Orange":
				orange++;
				break;
			case "Rot":
				red++;
				break;
			case "Gespräch":
				orange = 0;
				break;
			case "Änderung":
				let ctx = JSON.parse(change.json)
				switch (ctx.type) {
				case "ObservationStart":
					observed = true;
					break;
				default:
					alert(`Unexpected ctx type: ${ctx.type}`);
				}
				break;
			default:
				alert(`Unexpected change: ${change}`);
			}
		}
		return new Graduation(student, green, orange, red, level, stickers, observed, changes);
	}

	addChange(change) {
		// 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() {
		// TODO(muhq): Implement observation mark
		this.observed = true;
	}

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

	/**
	 * Return the URI of the icon representing the graduation level.
	 */
	getIcon() {
		return "";
	}

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

async function fetchGraduationChanges(student) {
	let params = {};
	// The id is enough
	params[GRADUATION_DB_PARAMS_MAP["id"]] = student.id;

	const data = await loadFromDatabase(GRADUATION_DB_ID, params);

	let changes = [];
	for (const e of data) {
		const gradChange = new GraduationChange(e.date, e.id, e.name, e.group, e.change, e.reason, e.coach, e.json);
		changes.push(gradChange);
	}
	return changes;
}

async function fetchGraduation(student) {
	const changes = await fetchGraduationChanges(student);
	return Graduation.from(student, changes);
}

async function submitGraduationChange(student, change) {
	let params = {};
	student.graduation.addChange(change);
	for (const key of GraduationChange.DB_FIELDS) {
		if (GRADUATION_DB_PARAMS_MAP[key]) {
			params[GRADUATION_DB_PARAMS_MAP[key]] = change[key];
		}
	}

	addEntryToDatabaseActivity(GRADUATION_ACTIVITY_ID, params);
}

export {
	LEVEL_NAMES,
	GraduationChange,
	Graduation,
	fetchGraduation,
	fetchGraduationChanges,
	submitGraduationChange
};