Manuellen WebUntis-Abgleich für Unterrichtsnummern und Stundenplan ergänzen
CI / build-and-test (push) Canceled after 0s
CI / build-and-test (push) Canceled after 0s
This commit is contained in:
1 parent
d9eac0d1d2
commit
f56caced32
10 files changed
+273
-24
No files matched your search
@@ -7,7 +7,7 @@ using LehrerApp.Desktop.Services;
|
||||
|
||||
namespace LehrerApp.Desktop.ViewModels.Planning;
|
||||
|
||||
public sealed record WebUntisGroupOption(Guid Id, string DisplayName);
|
||||
public sealed record WebUntisGroupOption(Guid Id, string DisplayName, int? LessonId = null);
|
||||
|
||||
public partial class WebUntisTimetableRow : ObservableObject
|
||||
{
|
||||
@@ -18,8 +18,14 @@ public partial class WebUntisTimetableRow : ObservableObject
|
||||
public required string SuggestedGroupName { get; init; }
|
||||
public string? SubjectName { get; init; }
|
||||
public string? Room { get; init; }
|
||||
public int? LessonId { get; init; }
|
||||
public string LessonIdLabel => LessonId is { } id ? $"Unterrichtsnummer: {id}" : "Unterrichtsnummer nicht gefunden";
|
||||
[ObservableProperty] private bool _applyLessonId = true;
|
||||
[ObservableProperty] private bool _include = true;
|
||||
public ObservableCollection<WebUntisGroupOption> GroupOptions { get; } = [];
|
||||
[ObservableProperty] private WebUntisGroupOption? _selectedGroup;
|
||||
public string ExistingLessonIdLabel => SelectedGroup?.LessonId is { } id ? $"Bisher: {id}" : "Bisher: keine Unterrichtsnummer";
|
||||
partial void OnSelectedGroupChanged(WebUntisGroupOption? value) => OnPropertyChanged(nameof(ExistingLessonIdLabel));
|
||||
public string WeekdayLabel => Weekday switch
|
||||
{
|
||||
DayOfWeek.Monday => "Mo", DayOfWeek.Tuesday => "Di", DayOfWeek.Wednesday => "Mi",
|
||||
@@ -40,6 +46,11 @@ public partial class WebUntisTimetableImportViewModel : ObservableObject
|
||||
[ObservableProperty] private DateTimeOffset? _weekDate = DateTimeOffset.Now;
|
||||
[ObservableProperty] private string _status = "Lehrkraft auswählen und Untis-Woche laden.";
|
||||
[ObservableProperty] private bool _busy;
|
||||
[ObservableProperty] private bool _updateLessonIds = true;
|
||||
[ObservableProperty] private bool _updateTimetable;
|
||||
[ObservableProperty] private bool _replaceTimetable;
|
||||
private bool _loaded;
|
||||
private int? _loadedTeacherId;
|
||||
public bool Saved { get; private set; }
|
||||
public Func<WebUntisTimetableRow, Task<LearningGroup?>>? OnCreateGroup { get; set; }
|
||||
|
||||
@@ -68,18 +79,21 @@ public partial class WebUntisTimetableImportViewModel : ObservableObject
|
||||
private async Task Load()
|
||||
{
|
||||
if (SelectedTeacher is null) { Status = "Bitte eine Lehrkraft auswählen."; return; }
|
||||
Busy = true; Rows.Clear();
|
||||
var teacherId = SelectedTeacher.UntisId;
|
||||
Busy = true; Rows.Clear(); _loaded = false; Saved = false;
|
||||
try
|
||||
{
|
||||
var selected = DateOnly.FromDateTime((WeekDate ?? DateTimeOffset.Now).LocalDateTime);
|
||||
var monday = selected.AddDays(-(((int)selected.DayOfWeek + 6) % 7));
|
||||
var periods = await _untis.GetTimetableAsync(SelectedTeacher.UntisId, monday, monday.AddDays(6));
|
||||
var periods = await _untis.GetTimetableAsync(teacherId, monday, monday.AddDays(6));
|
||||
var lessonIds = await _untis.GetWeeklyLessonIdsAsync(teacherId, monday);
|
||||
var grid = await _untis.GetTimeGridAsync();
|
||||
var groupOptions = BuildGroupOptions();
|
||||
var localSlots = _slots.GetAll().ToDictionary(x => (x.Weekday, x.PeriodNumber));
|
||||
|
||||
foreach (var period in periods.Where(x => string.IsNullOrWhiteSpace(x.Code) || x.Code != "cancelled")
|
||||
.GroupBy(x => (x.Date, x.StartTime, x.EndTime, x.StudentGroup,
|
||||
LessonId: lessonIds.TryGetValue(x.Id, out var id) ? (int?)id : null,
|
||||
Class: string.Join("/", x.Classes.Select(c => c.Name)),
|
||||
Subject: string.Join("/", x.Subjects.Select(s => s.Name)),
|
||||
Room: string.Join("/", x.Rooms.Select(r => r.Name))))
|
||||
@@ -101,6 +115,7 @@ public partial class WebUntisTimetableImportViewModel : ObservableObject
|
||||
.Where(x => !string.IsNullOrWhiteSpace(x))),
|
||||
SuggestedGroupName = suggested, SubjectName = subject,
|
||||
Room = period.Rooms.FirstOrDefault()?.Name,
|
||||
LessonId = lessonIds.TryGetValue(period.Id, out var lessonId) ? lessonId : null,
|
||||
};
|
||||
foreach (var option in groupOptions) row.GroupOptions.Add(option);
|
||||
if (localSlots.TryGetValue((row.Weekday, row.PeriodNumber), out var existing))
|
||||
@@ -108,9 +123,12 @@ public partial class WebUntisTimetableImportViewModel : ObservableObject
|
||||
row.SelectedGroup ??= BestMatch(groupOptions, suggested, className, subject);
|
||||
Rows.Add(row);
|
||||
}
|
||||
_settings.SetTeacherUntisId(SelectedTeacher.UntisId);
|
||||
_loaded = true;
|
||||
_loadedTeacherId = teacherId;
|
||||
Status = Rows.Count == 0 ? "In dieser Woche wurde kein Unterricht gefunden."
|
||||
: $"{Rows.Count} regelmäßige Termine gefunden. Zuordnung prüfen und übernehmen.";
|
||||
if (Rows.Any(row => row.LessonId is null))
|
||||
Status += " Für einzelne Termine wurde keine Unterrichtsnummer gefunden; deren bisherige Nummer bleibt erhalten.";
|
||||
}
|
||||
catch (WebUntisIntegrationException ex) { Status = ex.Message; }
|
||||
finally { Busy = false; }
|
||||
@@ -133,8 +151,44 @@ public partial class WebUntisTimetableImportViewModel : ObservableObject
|
||||
[RelayCommand]
|
||||
private void Save()
|
||||
{
|
||||
var selected = Rows.Where(x => x.SelectedGroup is not null).ToList();
|
||||
foreach (var row in selected)
|
||||
Saved = false;
|
||||
if (Busy || !_loaded || Rows.Count == 0) { Status = "Bitte zuerst eine Unterrichtswoche laden."; return; }
|
||||
if (!UpdateLessonIds && !UpdateTimetable) { Status = "Bitte auswählen, was übernommen werden soll."; return; }
|
||||
var selected = Rows.Where(x => x.Include && x.SelectedGroup is not null).ToList();
|
||||
if (selected.Count == 0) { Status = "Bitte mindestens eine Lerngruppe zuordnen."; return; }
|
||||
var idRows = selected.Where(x => x.ApplyLessonId && x.LessonId is > 0).ToList();
|
||||
if (UpdateLessonIds)
|
||||
{
|
||||
var conflict = idRows.GroupBy(x => x.SelectedGroup!.Id)
|
||||
.FirstOrDefault(g => g.Select(x => x.LessonId).Distinct().Count() > 1);
|
||||
if (conflict is not null)
|
||||
{
|
||||
Status = $"Mehrere Unterrichtsnummern für {conflict.First().SelectedGroup!.DisplayName}. " +
|
||||
"Zuordnung korrigieren oder bei den betreffenden Zeilen die ID-Übernahme abwählen.";
|
||||
return;
|
||||
}
|
||||
if (!UpdateTimetable && idRows.Count == 0) { Status = "Keine Unterrichtsnummern zur Übernahme ausgewählt."; return; }
|
||||
}
|
||||
if (UpdateTimetable && selected.GroupBy(x => (x.Weekday, x.PeriodNumber)).Any(g => g.Count() > 1))
|
||||
{
|
||||
Status = "Mehrere Einträge für dieselbe Stunde. Bitte jeweils nur einen zuordnen.";
|
||||
return;
|
||||
}
|
||||
var groupChanges = 0;
|
||||
if (UpdateLessonIds)
|
||||
foreach (var assignment in idRows.GroupBy(x => x.SelectedGroup!.Id))
|
||||
{
|
||||
var group = _groups.GetById(assignment.Key);
|
||||
if (group is null || group.WebUntisLessonId == assignment.First().LessonId) continue;
|
||||
group.WebUntisLessonId = assignment.First().LessonId;
|
||||
_groups.Save(group);
|
||||
groupChanges++;
|
||||
}
|
||||
if (UpdateTimetable && ReplaceTimetable)
|
||||
foreach (var slot in _slots.GetAll().Where(slot =>
|
||||
!selected.Any(row => row.Weekday == slot.Weekday && row.PeriodNumber == slot.PeriodNumber)))
|
||||
_slots.Delete(slot.Id);
|
||||
foreach (var row in selected.Where(_ => UpdateTimetable))
|
||||
{
|
||||
var existing = _slots.GetAll().FirstOrDefault(x => x.Weekday == row.Weekday && x.PeriodNumber == row.PeriodNumber);
|
||||
var slot = existing ?? new TimetableSlot { Weekday = row.Weekday, PeriodNumber = row.PeriodNumber };
|
||||
@@ -143,11 +197,13 @@ public partial class WebUntisTimetableImportViewModel : ObservableObject
|
||||
_slots.Save(slot);
|
||||
}
|
||||
Saved = true;
|
||||
Status = $"{selected.Count} Stundenplan-Einträge übernommen.";
|
||||
_settings.SetTeacherUntisId(_loadedTeacherId);
|
||||
Status = $"{groupChanges} Unterrichtsnummern aktualisiert, " +
|
||||
$"{(UpdateTimetable ? selected.Count : 0)} Stundenplan-Einträge übernommen.";
|
||||
}
|
||||
|
||||
private List<WebUntisGroupOption> BuildGroupOptions() => _groups.GetAll().OrderBy(x => x.Name)
|
||||
.Select(x => new WebUntisGroupOption(x.Id, x.Name)).ToList();
|
||||
.Where(x => x.IsActive).Select(x => new WebUntisGroupOption(x.Id, x.Name, x.WebUntisLessonId)).ToList();
|
||||
|
||||
private static WebUntisGroupOption? BestMatch(IEnumerable<WebUntisGroupOption> options, params string?[] terms) =>
|
||||
options.FirstOrDefault(x => terms.Any(term => !string.IsNullOrWhiteSpace(term) &&
|
||||
|
||||
Reference in new issue
Block a user