feat: Anhänge je Stunde, Klausur-Sitzplan mischen, Gefährdungsbeurteilungs-Assistent

Lesson bekommt dieselbe Anhang-Infrastruktur wie Documentation (Material,
Arbeitsblätter, Experimentunterlagen), samt Fix einer Sync-Lücke, die Anhang-
Dateibytes bisher nur für Documentation statt generisch übertragen hat
(IHasAttachments). Sitzplan-Tab bekommt einen "Plätze mischen"-Button für
Klausursitzpläne. Neu: mehrschrittiger Gefährdungsbeurteilungs-Assistent mit
optionalem KI-Entwurf (ai-backend/gbu.php) und PDF-Export, Format bewusst als
JSON-Anhang statt eigener Datenbank-Entität. Details und Architekturentscheidungen
in TODO.md (4.2, 7.1.5, 10.1.8).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-20 17:13:14 +02:00
co-authored by Claude Sonnet 5
parent 331033db5c
commit 038337997f
29 changed files with 1886 additions and 22 deletions
@@ -1,9 +1,11 @@
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Platform.Storage;
using LehrerApp.Core.Interfaces;
using LehrerApp.Core.Models;
using LehrerApp.Desktop.Services;
using LehrerApp.Desktop.ViewModels.Groups;
using LehrerApp.Desktop.ViewModels.Students;
using Microsoft.Extensions.DependencyInjection;
namespace LehrerApp.Desktop.Views.Groups;
@@ -38,7 +40,136 @@ public partial class LessonDialog : Window
}
}
private void OnCancel(object? s, RoutedEventArgs e) => Close(false);
private void OnCancel(object? s, RoutedEventArgs e)
{
if (DataContext is LessonDialogViewModel vm) vm.DiscardUnsavedAttachments();
Close(false);
}
private async void OnAddAttachment(object? sender, RoutedEventArgs e)
{
if (DataContext is not LessonDialogViewModel vm) return;
var files = await StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
{
Title = "Anhang auswählen", AllowMultiple = false,
});
if (files.Count == 0) return;
await using var stream = await files[0].OpenReadAsync();
vm.AddAttachment(files[0].Name, stream);
}
private async void OnOpenAttachment(object? sender, RoutedEventArgs e)
{
if (DataContext is not LessonDialogViewModel vm) return;
if (sender is not Button { Tag: AttachmentItem item }) return;
using var source = vm.OpenAttachment(item);
if (source is null) return;
var file = await StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
{
Title = "Anhang speichern unter", SuggestedFileName = item.FileName,
});
if (file is null) return;
await using var target = await file.OpenWriteAsync();
await source.CopyToAsync(target);
}
private async void OnCreateHazardAssessment(object? sender, RoutedEventArgs e)
{
if (DataContext is not LessonDialogViewModel vm) return;
var (unit, contextLesson, groupName) = BuildHazardAssessmentContext(vm);
var dialogVm = new HazardAssessmentWizardViewModel(null, groupName,
App.Services.GetRequiredService<AiPlanningService>(), App.Services.GetRequiredService<AiSettingsService>(),
unit, contextLesson);
var dialog = new HazardAssessmentWizardDialog { DataContext = dialogVm };
var ok = await dialog.ShowDialog<bool>(this);
if (!ok || dialogVm.Result is null) return;
UploadHazardAssessment(vm, dialogVm.Result);
}
private async void OnOpenHazardAssessment(object? sender, RoutedEventArgs e)
{
if (DataContext is not LessonDialogViewModel vm) return;
if (sender is not Button { Tag: AttachmentItem item }) return;
var editing = await ReadHazardAssessmentAsync(vm, item);
if (editing is null) return;
var (unit, contextLesson, _) = BuildHazardAssessmentContext(vm);
var dialogVm = new HazardAssessmentWizardViewModel(editing, editing.GroupLabel,
App.Services.GetRequiredService<AiPlanningService>(), App.Services.GetRequiredService<AiSettingsService>(),
unit, contextLesson);
var dialog = new HazardAssessmentWizardDialog { DataContext = dialogVm };
var ok = await dialog.ShowDialog<bool>(this);
if (!ok || dialogVm.Result is null) return;
vm.RemoveAttachmentCommand.Execute(item);
UploadHazardAssessment(vm, dialogVm.Result);
}
private async void OnPrintHazardAssessment(object? sender, RoutedEventArgs e)
{
var topLevel = TopLevel.GetTopLevel(this);
if (topLevel is null || DataContext is not LessonDialogViewModel vm) return;
if (sender is not Button { Tag: AttachmentItem item }) return;
var assessment = await ReadHazardAssessmentAsync(vm, item);
if (assessment is null) return;
var data = new HazardAssessmentPrintData(
assessment.Title, assessment.GroupLabel, assessment.Date?.ToString("dd.MM.yyyy") ?? "",
ExperimentKindDisplay.Label(assessment.Kind), assessment.Procedure,
assessment.Substances.Select(s => new HazardSubstancePrintRow(
s.Name, s.Amount, string.Join(", ", s.GhsPictograms.Select(GhsPictogramDisplay.Code)),
s.HStatements, s.PStatements)).ToList(),
assessment.Hazards, assessment.ProtectiveMeasures, assessment.FirstAid, assessment.Disposal,
assessment.Notes, assessment.IsAiAssisted);
var pdf = App.Services.GetRequiredService<PdfExportService>().BuildHazardAssessmentPdf(data);
await App.Services.GetRequiredService<ExportService>().SaveAsync(topLevel.StorageProvider,
ExportFile.Pdf("Gefährdungsbeurteilung als PDF speichern", $"GBU_{assessment.Title}", pdf));
}
/// Baut den Kontext für den Gefährdungsbeurteilungs-Assistenten (KI-Anfrage + Vorbelegung) aus
/// dem gerade im Dialog bearbeiteten, ggf. noch nicht gespeicherten Stand — funktioniert damit
/// auch beim Neuanlegen einer Stunde, bevor überhaupt gespeichert wurde.
private (Unit? Unit, Lesson ContextLesson, string GroupName) BuildHazardAssessmentContext(LessonDialogViewModel vm)
{
var unit = App.Services.GetRequiredService<IUnitRepository>().GetById(vm.UnitId);
var groupName = unit is null ? "" : App.Services.GetRequiredService<IGroupRepository>().GetById(unit.GroupId)?.Name ?? "";
var contextLesson = new Lesson { Topic = vm.Topic, Phases = vm.Phases.Select(p => p.ToModel()).ToList() };
return (unit, contextLesson, groupName);
}
private static async Task<HazardAssessment?> ReadHazardAssessmentAsync(LessonDialogViewModel vm, AttachmentItem item)
{
using var source = vm.OpenAttachment(item);
if (source is null) return null;
using var reader = new StreamReader(source);
var json = await reader.ReadToEndAsync();
return System.Text.Json.JsonSerializer.Deserialize<HazardAssessment>(json);
}
private static void UploadHazardAssessment(LessonDialogViewModel vm, HazardAssessment result)
{
var json = System.Text.Json.JsonSerializer.Serialize(result);
using var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json));
vm.AddAttachment(HazardAssessmentFileName(result.Title), stream);
}
private static string HazardAssessmentFileName(string title)
{
var invalid = Path.GetInvalidFileNameChars();
var safe = new string(title.Select(c => invalid.Contains(c) ? '_' : c).ToArray()).Trim();
if (safe.Length == 0) safe = "Gefaehrdungsbeurteilung";
return safe + AttachmentItem.HazardAssessmentSuffix;
}
/// KI-Unterstützung mit Fokus auf genau diese (bereits gespeicherte) Stunde, statt den Umweg
/// über die Einheiten-Übersicht nehmen zu müssen (4.5.22, Nutzer-Feedback). Die Übernahme im