Exemple #1
0
        public async Task <ImportResult> ImportExamsAsync(UntisExportResult result, DateTime?startDate, DateTime?endDate, bool suppressNotifications)
        {
            ConfigureImporter();

            var exams = result.Exams.Where(x => startDate == null || endDate == null || (startDate <= x.Date && x.Date <= endDate))
                        .Select(exam =>
            {
                var period = result.Periods.Reverse <Period>().FirstOrDefault(p => p.Start <= exam.Date);

                if (period == null)
                {
                    return(null);
                }

                var tuitions = exam.Courses.Select(course =>
                {
                    return(ResolveExamTuition(course, result.Tuitions, period));
                });

                var students = new List <string>();

                if (settingsManager.AppSettings.AlwaysIncludeStudents)
                {
                    students.AddRange(exam.Students);

                    if (!string.IsNullOrEmpty(settingsManager.AppSettings.ExcludeRegExp) && Regex.IsMatch(exam.Name, settingsManager.AppSettings.ExcludeRegExp))
                    {
                        students.Clear();
                    }
                }
                else if (!string.IsNullOrEmpty(settingsManager.AppSettings.ExcludeRegExp) && Regex.IsMatch(exam.Name, settingsManager.AppSettings.ExcludeRegExp))
                {
                    students.AddRange(exam.Students);
                }

                return(new ExamData
                {
                    Id = exam.Number.ToString(),
                    Date = exam.Date,
                    LessonStart = exam.LessonStart,
                    LessonEnd = exam.LessonEnd,
                    Description = exam.Text,
                    Tuitions = tuitions.Where(x => x != null).ToList(),
                    Students = students,
                    Supervisions = exam.Supervisions.ToList(),
                    Rooms = exam.Rooms.ToList()
                });
            }).Where(x => x != null).ToList();

            var response = await importer.ImportExamsAsync(exams, suppressNotifications);

            return(HandleResponse(response));
        }
        protected override async Task HandleExamEvent(ExamEvent @event, IIccOutput outputSettings)
        {
            Configure(outputSettings);
            tuitionResolver.Initialize();
            examWritersResolver.Initialize();

            Regex regexUseNameAsId   = null;
            Regex regexNoStudents    = null;
            Regex regexStudentSubset = null;

            var section = new List <Tuple <string, string> >();

            if (!string.IsNullOrEmpty(outputSettings.SetNameAsIdPattern))
            {
                regexUseNameAsId = new Regex(outputSettings.SetNameAsIdPattern);
            }

            if (!string.IsNullOrEmpty(outputSettings.SetNoStudentsPattern))
            {
                regexNoStudents = new Regex(outputSettings.SetNoStudentsPattern);
            }

            if (!string.IsNullOrEmpty(outputSettings.StudentSubsetPattern))
            {
                regexStudentSubset = new Regex(outputSettings.StudentSubsetPattern);
            }

            var examIds = new List <string>();

            var exams = @event.Exams.Select(exam =>
            {
                var tuitions = new List <string>();

                foreach (var course in exam.Courses)
                {
                    foreach (var grade in exam.Grades)
                    {
                        var tuition = tuitionResolver.ResolveTuition(grade, course, null);

                        if (tuition != null)
                        {
                            tuitions.Add(tuition);
                        }
                        else
                        {
                            logger.LogDebug($"Tuition for grade {grade} and course {course} not found.");
                        }
                    }
                }

                var id = GetOrComputeId(exam);

                if (regexUseNameAsId != null && regexUseNameAsId.IsMatch(exam.Name))
                {
                    id = exam.Name;
                }

                var students = new List <string>();

                if (regexNoStudents == null || !regexNoStudents.IsMatch(exam.Name))
                {
                    var examWriters = new List <string>();

                    if (regexStudentSubset != null && regexStudentSubset.IsMatch(exam.Name))
                    {
                        foreach (Match match in regexStudentSubset.Matches(exam.Name))
                        {
                            if (match.Groups.Count < 3)
                            {
                                logger.LogError($"Matching students subset failed as {match.Groups.Count} groups were matched, 3 expected.");
                            }
                            else
                            {
                                var start = match.Groups[1].Value;
                                var end   = match.Groups[2].Value;
                                logger.LogDebug($"Only find students between {start} and {end}.");
                                foreach (var tuition in tuitions)
                                {
                                    examWriters.AddRange(examWritersResolver.ResolveStudents(tuition, exam, start, end));
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (var tuition in tuitions)
                        {
                            examWriters.AddRange(examWritersResolver.ResolveStudents(tuition, exam, null, null));
                        }
                    }

                    examWriters = examWriters.Distinct().ToList();
                    students.AddRange(examWriters);
                }

                var originalId = id;
                var roomAdded  = false;
                var number     = 1;
                while (examIds.Contains(id))
                {
                    if (roomAdded == false && exam.Rooms.Count > 0)
                    {
                        id += exam.Rooms.First();
                    }
                    else
                    {
                        id = originalId + (number++);
                    }
                }

                examIds.Add(id);

                return(new ExamData
                {
                    Id = id,
                    Date = exam.Date,
                    LessonStart = exam.LessonStart,
                    LessonEnd = exam.LessonEnd,
                    Description = exam.Remark,
                    Rooms = exam.Rooms.ToList(),
                    Supervisions = exam.Supervisions.ToList(),
                    Tuitions = tuitions,
                    Students = students
                });
            });



            var response = await iccImporter.ImportExamsAsync(exams.ToList());

            await HandleResponseAsync(response);
        }