Files
LehrerApp/LehrerApp.Desktop/ViewModels/ClassTeacher/VorgangViewModels.cs
T
adminandClaude Opus 5.5 aa326cf8b6
CI / build-and-test (push) Canceled after 0s
Add warning detail popup and quick blocks for Vorgänge
Dashboard warnings (attendance, homework, lateness) get a "Details" flyout
with key figures, a list of dates and the compact month calendar from the
class teacher overview, now extracted as a shared control. The calendar
now aligns days under their weekday (Mo-So header, blank leading cells).

Creating Vorgänge and documentation is supported by quick blocks with the
current figures, dated standard phrases (e.g. "Klassenleitung informiert")
and documentation templates (status snapshot, conversation, informing the
class/subject teachers, parent call, parent letter). The class teacher
Vorgänge tab uses WebUntis figures (absences across all subjects, class
register) for the same blocks and templates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 11:16:13 +02:00

296 lines
13 KiB
C#

using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using LehrerApp.Core.Models;
using LehrerApp.Desktop.ViewModels.Students;
namespace LehrerApp.Desktop.ViewModels.ClassTeacher;
// ── Anzeige-Helfer ────────────────────────────────────────────────────────
public static class VorgangTagDisplay
{
// Vom Nutzer selbst genannte Schlagwörter — eigene Labels bleiben trotzdem frei möglich
// (AutoCompleteBox, gleiches Muster wie DocumentationTagDisplay.Suggestions).
public static string[] Suggestions { get; } =
["Absentismus", "Hausaufgaben", "Verspätungen", "Konflikte", "Mitarbeit", "Elternkontakt", "Eskalation"];
}
public static class VorgangStatusDisplay
{
public static string Label(VorgangStatus status) => status == VorgangStatus.Closed ? "Geschlossen" : "Offen";
}
/// Ergebnis des "→ Vorgang"-Auswahldialogs beim Anheften einer Klassenbuchzeile: entweder ein
/// bestehender, offener Vorgang oder der Titel für einen neu anzulegenden.
public sealed record PinToVorgangChoice(Guid? ExistingVorgangId, string? NewVorgangTitle);
/// Eingefrorene WebUntis-Klassenbuchzeile mit deutscher Anzeige-Aufbereitung — das Core-Modell
/// <see cref="VorgangClassRegisterEntry"/> bleibt bewusst framework-frei ohne Formatierungslogik.
public sealed record VorgangClassRegisterEntryRow(VorgangClassRegisterEntry Model)
{
public string DateLabel => Model.Date.ToString("dd.MM.yyyy");
public string StudentName => Model.StudentName;
public string SummaryLabel => string.Join(" · ", new[] { Model.Subject, Model.CategoryName, Model.Text }
.Where(v => !string.IsNullOrWhiteSpace(v)));
}
// ── Mehrfachauswahl Schüler*innen (Anlegen/Bearbeiten-Dialog) ────────────────
public partial class VorgangStudentOption(Guid studentId, string name) : ObservableObject
{
public Guid StudentId { get; } = studentId;
public string Name { get; } = name;
[ObservableProperty] private bool _isSelected;
}
// ── Listen-Eintrag mit aufklappbarer Detailansicht ───────────────────────────
/// <summary>
/// Ein Vorgang in der Liste des "Vorgänge"-Tabs. Verknüpfte Dokumentation/Klassenbucheinträge
/// werden von <see cref="ClassTeacherCasesViewModel"/> beim Laden befüllt; Link/Unlink-Aktionen
/// laufen über die hier übergebenen Callbacks zurück ins ViewModel (statt eigenem Repository-
/// Zugriff hier), damit dieser Wrapper ein reiner Anzeige-Baustein bleibt — analog
/// <see cref="DocumentationItem"/>.
/// </summary>
public partial class VorgangItem : ObservableObject
{
private readonly Func<VorgangItem, DocumentationItem, Task> _onLink;
private readonly Func<VorgangItem, DocumentationItem, Task> _onUnlink;
private readonly Func<VorgangItem, VorgangClassRegisterEntryRow, Task> _onRemoveClassRegisterEntry;
private readonly Func<DocumentationItem, Task> _onEditDocumentation;
public Vorgang Model { get; }
public string StudentNames { get; }
public List<TagChip> TagChips { get; }
public string StatusLabel => VorgangStatusDisplay.Label(Model.Status);
public bool IsOpen => Model.Status == VorgangStatus.Open;
public string CreatedLabel => $"Angelegt {Model.CreatedAt:dd.MM.yyyy}";
public ObservableCollection<DocumentationItem> LinkedDocumentation { get; } = [];
public ObservableCollection<DocumentationItem> AvailableDocumentation { get; } = [];
public ObservableCollection<VorgangClassRegisterEntryRow> ClassRegisterRows { get; } = [];
public bool HasLinkedDocumentation => LinkedDocumentation.Count > 0;
public bool HasAvailableDocumentation => AvailableDocumentation.Count > 0;
public bool HasClassRegisterRows => ClassRegisterRows.Count > 0;
[ObservableProperty] private bool _isRevealed;
public VorgangItem(Vorgang model, string studentNames,
Func<VorgangItem, DocumentationItem, Task> onLink,
Func<VorgangItem, DocumentationItem, Task> onUnlink,
Func<VorgangItem, VorgangClassRegisterEntryRow, Task> onRemoveClassRegisterEntry,
Func<DocumentationItem, Task> onEditDocumentation)
{
Model = model;
StudentNames = studentNames;
TagChips = model.Tags.Select(t => new TagChip(t)).ToList();
_onLink = onLink;
_onUnlink = onUnlink;
_onRemoveClassRegisterEntry = onRemoveClassRegisterEntry;
_onEditDocumentation = onEditDocumentation;
}
[RelayCommand] private void Reveal() => IsRevealed = !IsRevealed;
/// Nutzer-Feedback: standardisierte Dokumentationseinträge statt alles von Hand — nur im
/// schülerbezogenen Vorgänge-Tab gesetzt (StudentVorgaengeViewModel), der Parameter ist ein
/// Schlüssel aus VorgangDocumentationTemplates. Eigener Command je Item statt $parent-Bindung,
/// weil die Auswahl in einem MenuFlyout-Popup liegt, das nicht im normalen visuellen Baum hängt.
public Func<VorgangItem, string, Task>? OnCreateFromTemplate { get; set; }
public bool HasDocumentationTemplates => OnCreateFromTemplate is not null;
[RelayCommand]
private async Task CreateFromTemplate(string? key)
{
if (key is not null && OnCreateFromTemplate is not null) await OnCreateFromTemplate(this, key);
}
/// Öffnet den vollen Dokumentations-Dialog zum Nachlesen/Bearbeiten, ohne den Vorgänge-Tab zu
/// verlassen (Nutzer-Feedback: bisher musste man dafür in den Schülerdatensatz wechseln).
[RelayCommand]
private async Task EditDocumentation(DocumentationItem? doc)
{
if (doc is not null) await _onEditDocumentation(doc);
}
[RelayCommand]
private async Task Link(DocumentationItem? doc)
{
if (doc is not null) await _onLink(this, doc);
}
[RelayCommand]
private async Task Unlink(DocumentationItem? doc)
{
if (doc is not null) await _onUnlink(this, doc);
}
[RelayCommand]
private async Task RemoveClassRegisterEntry(VorgangClassRegisterEntryRow? row)
{
if (row is not null) await _onRemoveClassRegisterEntry(this, row);
}
}
// ── Dialog: Vorgang anlegen/bearbeiten ────────────────────────────────────
public partial class VorgangDialogViewModel : ObservableObject
{
private readonly Vorgang? _editing;
public ObservableCollection<VorgangStudentOption> StudentOptions { get; }
[ObservableProperty] private string _title = "";
[ObservableProperty] private string _description = "";
[ObservableProperty] private string _newTag = "";
public ObservableCollection<string> Tags { get; } = [];
public string[] TagSuggestions => VorgangTagDisplay.Suggestions;
[ObservableProperty] private string _titleError = "";
[ObservableProperty] private string _studentsError = "";
private readonly bool _isNewDraft;
public string DialogTitle => _editing is null || _isNewDraft ? "Vorgang anlegen" : "Vorgang bearbeiten";
public Vorgang? Result { get; private set; }
/// Per Klick in die Beschreibung einfügbare Textbausteine mit dem aktuellen Stand (Fehlquote,
/// Fehltermine, WebUntis-Fehlzeiten, …). Setzt sich aus festen Bausteinen (Schülerdetail) und —
/// im Klassenlehrer-Tab mit Mehrfachauswahl — aus Bausteinen je ausgewählter/m Schüler*in
/// zusammen, die bei jeder Änderung der Auswahl neu berechnet werden.
public ObservableCollection<VorgangQuickBlock> QuickBlocks { get; } = [];
public bool HasQuickBlocks => QuickBlocks.Count > 0;
/// Datumsgestempelte Standardformulierungen ("Klassenleitung informiert", …), siehe VorgangPhraseBlocks.
public IReadOnlyList<VorgangQuickBlock> PhraseBlocks { get; }
public bool HasPhraseBlocks => PhraseBlocks.Count > 0;
private readonly IReadOnlyList<VorgangQuickBlock> _fixedQuickBlocks;
private readonly Func<IReadOnlyList<Guid>, IReadOnlyList<VorgangQuickBlock>>? _studentQuickBlocks;
/// <param name="isNewDraft">editing ist ein noch nicht gespeicherter, vorbefüllter Entwurf (z.B.
/// aus einer Dashboard-Warnung) — Titel "anlegen" statt "bearbeiten".</param>
public VorgangDialogViewModel(List<StudentOption> rosterStudents, Vorgang? editing,
IReadOnlyList<VorgangQuickBlock>? quickBlocks = null, bool isNewDraft = false,
IReadOnlyList<VorgangQuickBlock>? phraseBlocks = null,
Func<IReadOnlyList<Guid>, IReadOnlyList<VorgangQuickBlock>>? studentQuickBlocks = null)
{
_editing = editing;
_isNewDraft = isNewDraft;
_fixedQuickBlocks = quickBlocks ?? [];
_studentQuickBlocks = studentQuickBlocks;
PhraseBlocks = phraseBlocks ?? [];
// Bei genau einem Kandidaten (z.B. der schülerbezogene Vorgänge-Tab im Schülerdetail) und
// einem neuen Vorgang ist ohnehin nur eine Auswahl möglich — Nutzer-Feedback: der sonst
// nötige Extra-Klick auf die einzige Checkbox ist unnötige Reibung.
StudentOptions = new ObservableCollection<VorgangStudentOption>(rosterStudents.Select(s =>
new VorgangStudentOption(s.Id, s.Name)
{
IsSelected = editing?.StudentIds.Contains(s.Id) == true
|| (editing is null && rosterStudents.Count == 1),
}));
if (_studentQuickBlocks is not null)
foreach (var option in StudentOptions)
option.PropertyChanged += (_, e) =>
{
if (e.PropertyName == nameof(VorgangStudentOption.IsSelected)) RebuildQuickBlocks();
};
RebuildQuickBlocks();
if (editing is null) return;
Title = editing.Title;
Description = editing.Description;
foreach (var tag in editing.Tags) Tags.Add(tag);
}
[RelayCommand]
private void AddTag()
{
if (string.IsNullOrWhiteSpace(NewTag)) return;
var tag = NewTag.Trim();
if (!Tags.Contains(tag)) Tags.Add(tag);
NewTag = "";
}
[RelayCommand] private void RemoveTag(string? tag) { if (tag is not null) Tags.Remove(tag); }
/// Hängt den Baustein als eigenen Absatz an — nie überschreiben, damit bereits Getipptes bleibt.
[RelayCommand]
private void InsertQuickBlock(VorgangQuickBlock? block)
{
if (block is null) return;
var current = (Description ?? "").TrimEnd();
Description = current.Length == 0 ? block.Text : current + "\n\n" + block.Text;
}
/// Standardformulierungen sind Verlaufsnotizen — eine Zeile, kein eigener Absatz.
[RelayCommand]
private void InsertPhrase(VorgangQuickBlock? block)
{
if (block is null) return;
var current = (Description ?? "").TrimEnd();
Description = current.Length == 0 ? block.Text : current + "\n" + block.Text;
}
private void RebuildQuickBlocks()
{
QuickBlocks.Clear();
foreach (var block in _fixedQuickBlocks) QuickBlocks.Add(block);
if (_studentQuickBlocks is not null)
{
var selected = StudentOptions.Where(o => o.IsSelected).Select(o => o.StudentId).ToList();
foreach (var block in _studentQuickBlocks(selected)) QuickBlocks.Add(block);
}
OnPropertyChanged(nameof(HasQuickBlocks));
}
[RelayCommand]
private void Save()
{
TitleError = ""; StudentsError = "";
var valid = true;
if (string.IsNullOrWhiteSpace(Title)) { TitleError = "Titel erforderlich."; valid = false; }
var selectedIds = StudentOptions.Where(s => s.IsSelected).Select(s => s.StudentId).ToList();
if (selectedIds.Count == 0) { StudentsError = "Mindestens eine/n Schüler*in auswählen."; valid = false; }
if (!valid) return;
Result = _editing ?? new Vorgang();
Result.Title = Title.Trim();
Result.Description = (Description ?? "").Trim();
Result.StudentIds = selectedIds;
Result.Tags = Tags.ToList();
}
}
// ── Dialog: Klassenbuchzeile an (bestehenden oder neuen) Vorgang anheften ────
public partial class PinToVorgangDialogViewModel : ObservableObject
{
public string RowSummary { get; }
public List<VorgangItem> MatchingOpenCases { get; }
public bool HasMatchingOpenCases => MatchingOpenCases.Count > 0;
[ObservableProperty] private VorgangItem? _selectedCase;
[ObservableProperty] private string _newTitle = "";
[ObservableProperty] private string _error = "";
public PinToVorgangChoice? Result { get; private set; }
public PinToVorgangDialogViewModel(string rowSummary, List<VorgangItem> matchingOpenCases)
{
RowSummary = rowSummary;
MatchingOpenCases = matchingOpenCases;
}
[RelayCommand]
private void Confirm()
{
Error = "";
if (SelectedCase is not null) { Result = new PinToVorgangChoice(SelectedCase.Model.Id, null); return; }
if (!string.IsNullOrWhiteSpace(NewTitle)) { Result = new PinToVorgangChoice(null, NewTitle.Trim()); return; }
Error = "Bestehenden Vorgang wählen oder Titel für einen neuen Vorgang eingeben.";
}
}