diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2026-06-13 13:04:07 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2026-06-13 13:04:07 +0200 |
| commit | 4882c5ca3a65075e03595588ef3780a36bc74b15 (patch) | |
| tree | 07aa2a276cd87a8e192147440411c1f51d8aa624 /js/graduation-common.js | |
| parent | 9fbd21ff6bf97b9ce77cb78f420abd951b8aaf27 (diff) | |
| download | bycs-sol-4882c5ca3a65075e03595588ef3780a36bc74b15.tar.gz bycs-sol-4882c5ca3a65075e03595588ef3780a36bc74b15.zip | |
move generic js into root
Diffstat (limited to 'js/graduation-common.js')
| -rw-r--r-- | js/graduation-common.js | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/js/graduation-common.js b/js/graduation-common.js new file mode 100644 index 0000000..4716e6d --- /dev/null +++ b/js/graduation-common.js @@ -0,0 +1,133 @@ +import { GRADUATION_DB_ID, GRADUATION_ACTIVITY_ID, GRADUATION_DB_PARAMS_MAP } from "./config.js"; +import { loadFromDatabase, addEntryToDatabaseActivity } from "./database.js" + +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, student.group, + "Rot", reason, coach, "" + ); + } +} + +class Graduation { + constructor(student, green, orange, red, level, stickers, changes) { + this.student = student; + this.green = green; + this.orange = orange; + this.red = red; + this.level = level; + this.stickers = stickers; + this.changes = changes; + + student.graduation = this; + } + + static from(student, changes) { + let green = 0; + let orange = 0; + let red = 0; + let level = "Beginner"; + 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": + // TODO: + break; + default: + alert(`Unexpected change: ${change}`); + } + } + return new Graduation(student, green, orange, red, level, stickers, 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); + } +} + +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 { + GraduationChange, + Graduation, + fetchGraduation, + fetchGraduationChanges, + submitGraduationChange +};
\ No newline at end of file |
