Untis API Integration
This commit is contained in:
@@ -13,6 +13,7 @@ using LehrerApp.Desktop.Views.Shared;
|
||||
using LehrerApp.Desktop.Views.Students;
|
||||
using LehrerApp.Desktop.Views.Workload;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Text;
|
||||
|
||||
namespace LehrerApp.Desktop.Views.Groups;
|
||||
|
||||
@@ -113,18 +114,7 @@ public partial class GroupDetailView : UserControl
|
||||
throw new InvalidDataException("Die Importdatei ist größer als 20 MB.");
|
||||
|
||||
var importFile = new ImportFile(files[0].Name, buffer.ToArray());
|
||||
var preview = await Task.Run(async () =>
|
||||
await service.AnalyzeAsync(importFile, vm.Group.Id).ConfigureAwait(false));
|
||||
var dialogVm = new StudentImportDialogViewModel(service, preview, files[0].Name);
|
||||
var dialog = new StudentImportDialog { DataContext = dialogVm };
|
||||
if (!await dialog.ShowDialog<bool>(owner)) return;
|
||||
|
||||
vm.LoadStudents();
|
||||
vm.ParticipationTab.RefreshCurrentGrid();
|
||||
var result = dialogVm.Result!;
|
||||
App.Services.GetRequiredService<NotificationService>().ShowSuccess(
|
||||
$"Teilnehmerimport abgeschlossen: {result.CreatedStudents} neu, "
|
||||
+ $"{result.UpdatedStudents} ergänzt, {result.CreatedMemberships} zugeordnet.");
|
||||
await ShowStudentImportPreview(owner, vm, importFile, files[0].Name);
|
||||
}
|
||||
catch (Exception ex) when (ex is ImportFormatException
|
||||
or InvalidDataException
|
||||
@@ -135,6 +125,85 @@ public partial class GroupDetailView : UserControl
|
||||
}
|
||||
}
|
||||
|
||||
private async void OnImportParticipantsFromWebUntisClick(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
var owner = TopLevel.GetTopLevel(this) as Window;
|
||||
if (owner is null || DataContext is not GroupDetailViewModel vm || vm.Group is null) return;
|
||||
try
|
||||
{
|
||||
var untis = App.Services.GetRequiredService<WebUntisIntegrationService>();
|
||||
var selectionVm = new WebUntisClassSelectionViewModel(untis);
|
||||
var selection = new WebUntisClassSelectionDialog { DataContext = selectionVm };
|
||||
await selectionVm.InitializeAsync();
|
||||
if (!await selection.ShowDialog<bool>(owner) || selectionVm.SelectedClass is null) return;
|
||||
|
||||
var report = await untis.GetStudentsAsync(selectionVm.SelectedClass.Name);
|
||||
var importFile = BuildWebUntisStudentImport(report);
|
||||
await ShowStudentImportPreview(owner, vm, importFile,
|
||||
$"WebUntis · {selectionVm.SelectedClass.Name}");
|
||||
}
|
||||
catch (WebUntisIntegrationException ex)
|
||||
{
|
||||
App.Services.GetRequiredService<NotificationService>().ShowError(ex.Message);
|
||||
}
|
||||
catch (Exception ex) when (ex is ImportFormatException or InvalidDataException)
|
||||
{
|
||||
App.Services.GetRequiredService<NotificationService>().ShowError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private async void OnCompareWebUntisAbsencesClick(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
var owner = TopLevel.GetTopLevel(this) as Window;
|
||||
if (owner is null || DataContext is not GroupDetailViewModel vm || vm.Group is null) return;
|
||||
var dialogVm = new WebUntisAbsenceComparisonViewModel(vm.Group,
|
||||
App.Services.GetRequiredService<WebUntisIntegrationService>(),
|
||||
App.Services.GetRequiredService<IStudentRepository>(),
|
||||
App.Services.GetRequiredService<IParticipationSessionRepository>(),
|
||||
App.Services.GetRequiredService<IParticipationRepository>());
|
||||
await new WebUntisAbsenceComparisonDialog { DataContext = dialogVm }.ShowDialog(owner);
|
||||
vm.ParticipationTab.RefreshCurrentGrid();
|
||||
}
|
||||
|
||||
private static ImportFile BuildWebUntisStudentImport(UntisStudentReportDto report)
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
builder.AppendLine("longName\tforeName\tgender\tbirthDate\tklasse.name\texternKey\taddress.email\taddress.mobile\taddress.phone\taddress.city\taddress.postCode\taddress.street");
|
||||
foreach (var student in report.Students)
|
||||
{
|
||||
var values = new[]
|
||||
{
|
||||
student.LongName ?? student.Name, student.ForeName, student.Gender,
|
||||
student.BirthDate?.ToString() ?? student.BirthDateRaw, student.ClassName,
|
||||
student.ExternKey.ToString(), student.Address.Email, student.Address.Mobile,
|
||||
student.Address.Phone, student.Address.City, student.Address.PostCode, student.Address.Street,
|
||||
};
|
||||
builder.AppendLine(string.Join('\t', values.Select(SafeTsv)));
|
||||
}
|
||||
return new ImportFile("webuntis-students.csv", Encoding.UTF8.GetBytes(builder.ToString()));
|
||||
}
|
||||
|
||||
private static string SafeTsv(string? value) => (value ?? "").Replace('\t', ' ')
|
||||
.Replace('\r', ' ').Replace('\n', ' ');
|
||||
|
||||
private static async Task ShowStudentImportPreview(Window owner, GroupDetailViewModel vm,
|
||||
ImportFile importFile, string sourceName)
|
||||
{
|
||||
var service = App.Services.GetRequiredService<StudentImportService>();
|
||||
var preview = await Task.Run(async () =>
|
||||
await service.AnalyzeAsync(importFile, vm.Group!.Id).ConfigureAwait(false));
|
||||
var dialogVm = new StudentImportDialogViewModel(service, preview, sourceName);
|
||||
var dialog = new StudentImportDialog { DataContext = dialogVm };
|
||||
if (!await dialog.ShowDialog<bool>(owner)) return;
|
||||
|
||||
vm.LoadStudents();
|
||||
vm.ParticipationTab.RefreshCurrentGrid();
|
||||
var result = dialogVm.Result!;
|
||||
App.Services.GetRequiredService<NotificationService>().ShowSuccess(
|
||||
$"Teilnehmerimport abgeschlossen: {result.CreatedStudents} neu, "
|
||||
+ $"{result.UpdatedStudents} ergänzt, {result.CreatedMemberships} zugeordnet.");
|
||||
}
|
||||
|
||||
private async Task<bool> ShowWithdrawStudentDialog(StudentSummary student)
|
||||
{
|
||||
if (DataContext is not GroupDetailViewModel vm || vm.Group is null) return false;
|
||||
|
||||
Reference in New Issue
Block a user