private TimetableLessonData CreateTimetableLessonData(string tuition, List <string> grades, List <string> teachers, int lessonStart, int lessonEnd, string room, int day, string week, string subject)
        {
            var data = new TimetableLessonData
            {
                Tuition        = tuition,
                Lesson         = lessonStart,
                IsDoubleLesson = lessonEnd == lessonStart + 1,
                Room           = room,
                Day            = day,
                Week           = week,
                Subject        = subject,
                Id             = ComputeTimetableLessonId(day, lessonStart, week, tuition ?? subject, room)
            };

            foreach (var teacher in teachers)
            {
                data.Teachers.Add(teacher);
            }

            foreach (var grade in grades)
            {
                data.Grades.Add(grade);
            }

            return(data);
        }
예제 #2
0
        public async Task <ImportResult> ImportTimetableAsync(UntisExportResult result, Period period)
        {
            ConfigureImporter();

            var lessons = new List <TimetableLessonData>();
            var subjectReplacementMap = settingsManager.AppSettings.SubjectOverrides.ToDictionary(x => x.UntisSubject, x => x.NewSubject);

            var bereit = result.Tuitions.Where(x => x.Periods.Any(y => y.Subject == "Bereit")).ToList();

            var allowedWeeks = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P" }; // Untis only allows a periodicity of 16

            foreach (var tuition in result.Tuitions)
            {
                foreach (var tuitionPeriod in tuition.Periods)
                {
                    if (tuitionPeriod.PeriodNumber != period.Number)
                    {
                        continue;
                    }

                    // Check if time frame is withing current period
                    if (tuitionPeriod.EndDate < period.Start || tuitionPeriod.StartDate > period.End)
                    {
                        continue;
                    }

                    foreach (var timetable in tuitionPeriod.Timetable)
                    {
                        var weeks = !string.IsNullOrEmpty(timetable.Week) ? new string[] { timetable.Week } : tuitionPeriod.TuitionGroups;

                        foreach (var week in weeks)
                        {
                            if (!allowedWeeks.Contains(week) || timetable.Day == 0 || timetable.Lesson == 0 || string.IsNullOrEmpty(tuitionPeriod.Subject))
                            {
                                // ??!?
                                continue;
                            }

                            var lesson = new TimetableLessonData
                            {
                                Day            = timetable.Day,
                                Lesson         = timetable.Lesson,
                                IsDoubleLesson = false,
                                Room           = timetable.Room,
                                Week           = week,
                                Id             = Guid.NewGuid().ToString(),
                                Grades         = tuitionPeriod.Grades,
                                Subject        = tuitionPeriod.Subject
                            };

                            if (!string.IsNullOrEmpty(tuitionPeriod.Teacher))
                            {
                                lesson.Teachers.Add(tuitionPeriod.Teacher);
                            }

                            var last = lessons.LastOrDefault();

                            if (last != null && last.Day == lesson.Day && last.Room == lesson.Room && last.Week == lesson.Week && last.Subject == lesson.Subject && last.IsDoubleLesson == false && last.Lesson == lesson.Lesson - 1)
                            {
                                last.IsDoubleLesson = true;
                                continue;
                            }

                            lessons.Add(lesson);
                        }
                    }
                }
            }

            var splitLessons = new List <TimetableLessonData>();

            foreach (var lesson in lessons)
            {
                if (lesson.Grades.Count == 0)
                {
                    continue;
                }

                if (settingsManager.AppSettings.SplitCourses.Any(x => x.Subject == lesson.Subject && lesson.Grades.Contains(x.Grade)))
                {
                    var firstGrade = lesson.Grades.First();
                    var grades     = lesson.Grades.Skip(1).ToList();
                    lesson.Grades = new List <string> {
                        firstGrade
                    };

                    foreach (var grade in grades)
                    {
                        var newLesson = new TimetableLessonData
                        {
                            Day            = lesson.Day,
                            Lesson         = lesson.Lesson,
                            IsDoubleLesson = lesson.IsDoubleLesson,
                            Room           = lesson.Room,
                            Week           = lesson.Week,
                            Id             = Guid.NewGuid().ToString(),
                            Grades         = new List <string> {
                                grade
                            },
                            Subject  = lesson.Subject,
                            Teachers = lesson.Teachers
                        };

                        splitLessons.Add(newLesson);
                    }
                }
            }

            lessons.AddRange(splitLessons);

            foreach (var lesson in lessons)
            {
                // Replace subject if necessary
                if (subjectReplacementMap.ContainsKey(lesson.Subject))
                {
                    lesson.Subject = subjectReplacementMap[lesson.Subject];
                }
            }

            var response = await importer.ImportTimetableLessonsAsync(period.Name, lessons);

            return(HandleResponse(response));
        }