Skip to content

Commit

Permalink
fix: rooms not get seperated correctly like teachers
Browse files Browse the repository at this point in the history
refactor: refactored rooms & teachers generation
  • Loading branch information
SachsenspieltCoding committed Jan 9, 2024
1 parent e28debc commit 2c24d14
Showing 1 changed file with 41 additions and 65 deletions.
106 changes: 41 additions & 65 deletions src/parser/substitutionPlan.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { parse as parseDate } from 'date-fns';
import { de } from 'date-fns/locale';
import { PlannedLesson } from '../interface/plannedLesson.js';
import { Room } from '../interface/room.js';
import { SchoolClass } from '../interface/schoolclass.js';
import { SubstitutionPlan } from '../interface/substitutionPlan.js';
Expand All @@ -9,85 +8,62 @@ import { IndiwareParser } from './generic/parser.js';
import { SchoolClassParser } from './schoolClass.js';

export class SubstitutionPlanParser implements IndiwareParser<SubstitutionPlan> {
private generateRooms(schoolclasses: SchoolClass[]): Room[] {
private generateRoomsAndTeachers(schoolClasses: SchoolClass[]): [Room[], Teacher[]] {
const rooms: Room[] = [];

for (const schoolclass of schoolclasses) {
for (const lesson of schoolclass.plannedLessons) {
if (!lesson.room.value || lesson.room.value == '') continue;

const existingRoomIndex = rooms.findIndex(
(room) => room.name === lesson.room.value,
);

if (existingRoomIndex !== -1) {
rooms[existingRoomIndex].plannedLessons.push(lesson);
continue;
} else {
rooms.push({
name: lesson.room.value,
plannedLessons: [lesson],
});
const teachers: Teacher[] = [];

for (const schoolClass of schoolClasses) {
for (const lesson of schoolClass.plannedLessons) {
if (lesson.room.value && lesson.room.value != '') {
for (const room of lesson.room.value.split(' ')) {
const existingRoomIndex = rooms.findIndex((r) => r.name === room);

if (existingRoomIndex !== -1) {
rooms[existingRoomIndex].plannedLessons.push(lesson);
continue;
} else {
rooms.push({
name: room,
plannedLessons: [lesson],
});
}
}
}
}
}

for (const room of rooms) {
room.plannedLessons.sort((a, b) => {
return a.order - b.order;
});
}

rooms.sort((a, b) => {
return a.name.localeCompare(b.name);
});

return rooms;
}

private generateTeachers(schoolclasses: SchoolClass[]): Teacher[] {
const teachers: Room[] = [];

for (const schoolclass of schoolclasses) {
for (const lesson of schoolclass.plannedLessons) {
if (!lesson.teacher.value || lesson.teacher.value == '') continue;

for (const t of lesson.teacher.value.split(' ')) {
const existingTeacherIndex = teachers.findIndex(
(teacher) => teacher.name === t,
);

if (existingTeacherIndex !== -1) {
teachers[existingTeacherIndex].plannedLessons.push(lesson);
continue;
} else {
teachers.push({
name: t,
plannedLessons: [lesson],
});
if (lesson.teacher.value && lesson.teacher.value != '') {
for (const teacher of lesson.teacher.value.split(' ')) {
const existingTeacherIndex = teachers.findIndex((t) => t.name === teacher);

if (existingTeacherIndex !== -1) {
teachers[existingTeacherIndex].plannedLessons.push(lesson);
continue;
} else {
teachers.push({
name: teacher,
plannedLessons: [lesson],
});
}
}
}
}
}

for (const teacher of teachers) {
teacher.plannedLessons.sort((a: PlannedLesson, b: PlannedLesson) => {
return a.order - b.order;
});
}

teachers.sort((a, b) => {
return a.name.localeCompare(b.name);
rooms.sort((a, b) => a.name.localeCompare(b.name));
teachers.sort((a, b) => a.name.localeCompare(b.name));
teachers.forEach((teacher) => {
teacher.plannedLessons.sort((a, b) => a.order - b.order);
});

return teachers;
return [rooms, teachers];
}

parse(xml: any): SubstitutionPlan {
const schoolClasses = xml.VpMobil.Klassen.Kl.map((schoolClass: any) => {
return new SchoolClassParser().parse(schoolClass);
});

const [rooms, teachers] = this.generateRoomsAndTeachers(schoolClasses);

return new SubstitutionPlan({
date: parseDate(xml.VpMobil.Kopf.DatumPlan, 'EEEE, dd. MMMM y', new Date(), {
locale: de,
Expand All @@ -99,9 +75,9 @@ export class SubstitutionPlanParser implements IndiwareParser<SubstitutionPlan>
return parseDate(holiday.toString(), 'yyMMdd', new Date());
}),
schoolClasses,
rooms: this.generateRooms(schoolClasses),
teachers: this.generateTeachers(schoolClasses),
info: xml.VpMobil.ZusatzInfo ? (xml.VpMobil.ZusatzInfo.ZiZeile as string[]) : [],
rooms,
teachers,
});
}
}

0 comments on commit 2c24d14

Please sign in to comment.