Schülerberichte und erweiterte Deckblatt-Bausteine ergänzen
CI / build-and-test (push) Canceled after 0s

This commit is contained in:
admin committed 2026-10-22 11:24:43 +02:00
1 parent 336869e75f
commit e2f019667a
31 files changed
+961 -14

No files matched your search

@@ -21,6 +21,17 @@ public partial class ExamCoverSheetDialogViewModel : ObservableObject
private readonly GradingService _grading;
private readonly TemplateStore _loader;
private readonly ITemplateBatchRenderer _renderer;
private readonly Exam? _original;
private readonly Dictionary<Guid, ExamResult> _originalResults;
[ObservableProperty] private ReportInterimRunChoice? _selectedInterimRun;
[ObservableProperty] private bool _usesInterimGrades;
public ObservableCollection<ReportInterimRunChoice> InterimRuns { get; } = [];
partial void OnSelectedTemplateChanged(LetterTemplateChoice? value)
{
UsesInterimGrades = value is not null &&
DrawingBoxLocator.Find(_loader.Load(value.Model), ReportTextDrawing.InterimGrades) is not null;
}
[ObservableProperty] private LetterTemplateChoice? _selectedTemplate;
[ObservableProperty] private bool _fillResults;
@@ -50,12 +61,19 @@ public partial class ExamCoverSheetDialogViewModel : ObservableObject
public ExamCoverSheetDialogViewModel(Exam exam, LearningGroup group, string subjectName,
IStudentRepository students, IGroupMembershipRepository memberships, IExamResultRepository results,
GradingService grading, IReadOnlyList<TemplateStore> templateStores, ITemplateBatchRenderer renderer,
string teacherAbbreviation = "")
string teacherAbbreviation = "", IExamRepository? exams = null, IInterimGradeRepository? interims = null)
{
_teacherAbbreviation = teacherAbbreviation;
_coverTitle = exam.Title;
_exam = exam; _group = group; _subjectName = subjectName; _grading = grading; _renderer = renderer;
_loader = templateStores[0];
_original = exam.RepeatOfExamId is { } originalId ? exams?.GetById(originalId) : null;
_originalResults = exam.RepeatOfExamId is { } firstId
? results.GetByExam(firstId).ToDictionary(r => r.StudentId) : [];
foreach (var run in (interims?.GetByGroup(group.Id) ?? [])
.GroupBy(g => (g.Date, g.Occasion)).OrderByDescending(g => g.Key.Date).ThenBy(g => g.Key.Occasion))
InterimRuns.Add(new(run.Key.Date, run.Key.Occasion, run.ToList()));
// Bewusste Auswahl statt stillschweigend den neuesten Zwischenstand zu drucken.
foreach (var template in templateStores.SelectMany(x => x.GetTemplates()).Where(x => x.IsExamCover)
.DistinctBy(x => x.Id).OrderBy(x => x.Name, StringComparer.CurrentCultureIgnoreCase))
Templates.Add(new(template));
@@ -87,10 +105,20 @@ public partial class ExamCoverSheetDialogViewModel : ObservableObject
try
{
var loaded = _loader.Load(SelectedTemplate.Model);
if (UsesInterimGrades && SelectedInterimRun is null)
{ StatusMessage = "Bitte einen gespeicherten Notenzwischenstand auswählen."; return false; }
if (UsesInterimGrades)
{
var missing = included.Where(s => !SelectedInterimRun!.Grades.Any(g => g.StudentId == s.Model.Id)).ToList();
if (missing.Count > 0)
{ StatusMessage = "Im gewählten Zwischenstand fehlen: " + string.Join(", ", missing.Select(s => s.FullName)) + ". Bitte Schüler abwählen oder einen anderen Durchgang wählen."; return false; }
}
var today = DateOnly.FromDateTime(DateTime.Now);
var data = included.Select(s => (ITemplateDataProvider)new LetterDataProvider(
ExamCoverPlaceholderBuilder.Build(_exam, _group, _subjectName, s.Model, s.Result, FillResults,
_grading, loaded, today, s.Niveau, TeacherAbbreviation, CoverTitle))).ToList();
_grading, loaded, today, s.Niveau, TeacherAbbreviation, CoverTitle,
_original, _originalResults.GetValueOrDefault(s.Model.Id),
UsesInterimGrades ? SelectedInterimRun!.Grades.Single(g => g.StudentId == s.Model.Id) : null))).ToList();
ReportProgress(0, included.Count);
IsGenerating = true;
var progress = new Progress<int>(done => ReportProgress(done, included.Count));
@@ -110,7 +138,7 @@ public partial class ExamCoverSheetDialogViewModel : ObservableObject
StatusMessage = message;
return true;
}
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException or TemplateValidationException)
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException or TemplateValidationException or QuestPDF.Drawing.Exceptions.DocumentLayoutException)
{ StatusMessage = $"Deckblätter konnten nicht erzeugt werden: {ex.Message}"; return false; }
finally { IsGenerating = false; }
}
@@ -146,3 +174,8 @@ public partial class ExamCoverStudentChoice : ObservableObject
_isIncluded = result is not { Absent: true };
}
}
public sealed record ReportInterimRunChoice(DateOnly Date, string Occasion, IReadOnlyList<InterimGrade> Grades)
{
public string Label => $"{Date:dd.MM.yyyy} · {Occasion}";
}
@@ -33,7 +33,7 @@ public partial class PersonalizeWorksheetDialogViewModel : ObservableObject
IStudentRepository students, WorksheetTemplateStore templates, ITemplateRenderer renderer)
{
_group = group; _templates = templates; _renderer = renderer;
foreach (var template in templates.Store.GetTemplates().Where(x => !x.IsExamCover)) Templates.Add(new(template));
foreach (var template in templates.Store.GetTemplates().Where(x => !x.IsExamCover && !x.IsStudentReport)) Templates.Add(new(template));
SelectedTemplate = Templates.FirstOrDefault();
foreach (var id in studentIds)
if (students.GetById(id) is { } student) Students.Add(new(student));
@@ -60,7 +60,7 @@ public partial class CreateLetterDialogViewModel : ObservableObject
_attendanceCalendarFactory = attendanceCalendarFactory;
_absenceDayListFactory = absenceDayListFactory;
_attendanceDataRefresher = attendanceDataRefresher;
foreach (var template in templates.GetTemplates().Where(x => !x.IsExamCover)) Templates.Add(new(template));
foreach (var template in templates.GetTemplates().Where(x => !x.IsExamCover && !x.IsStudentReport)) Templates.Add(new(template));
foreach (var contact in student.Contacts.Where(c => !c.InvalidSince.HasValue).OrderBy(c => c.Name)) Contacts.Add(new(contact));
foreach (var membership in memberships.GetByStudent(student.Id))
if (groups.GetById(membership.GroupId) is { } group) Groups.Add(new(group));
@@ -0,0 +1,106 @@
using System.Collections.ObjectModel;
using System.Globalization;
using CommunityToolkit.Mvvm.ComponentModel;
using LehrerApp.Core.Interfaces;
using LehrerApp.Core.Models;
using LehrerApp.Core.Services;
using LehrerApp.Desktop.Services;
using LehrerApp.Templating;
namespace LehrerApp.Desktop.ViewModels.Students;
public partial class StudentReportDialogViewModel : ObservableObject
{
private readonly Student _student;
private readonly StudentReportService _reports;
private readonly ITemplateRenderer _renderer;
private readonly IInterimGradeRepository _interims;
private readonly SchoolYearService _schoolYears;
private readonly TemplateStore _loader;
public string StudentName => _student.FullName;
public string SuggestedFileName => string.Concat($"Schülerbericht_{_student.LastName}_{_student.FirstName}.pdf"
.Select(c => Path.GetInvalidFileNameChars().Contains(c) ? '_' : c));
public ObservableCollection<LetterGroupChoice> Groups { get; } = [];
public ObservableCollection<StudentReportTemplateChoice> Templates { get; } = [];
public ObservableCollection<StudentReportInterimChoice> Interims { get; } = [];
[ObservableProperty] private LetterGroupChoice? _selectedGroup;
[ObservableProperty] private StudentReportTemplateChoice? _selectedTemplate;
[ObservableProperty] private StudentReportInterimChoice? _selectedInterim;
[ObservableProperty] private string _from = "";
[ObservableProperty] private string _to = DateTime.Today.ToString("dd.MM.yyyy");
[ObservableProperty] private bool _includeInterim = true;
[ObservableProperty] private bool _includeParticipation = true;
[ObservableProperty] private bool _includeCompetencies = true;
[ObservableProperty] private bool _includeHomework = true;
[ObservableProperty] private bool _includeDocumentation = true;
[ObservableProperty] private bool _includeConfidential;
[ObservableProperty] private string _comment = "";
[ObservableProperty] private string _statusMessage = "";
[ObservableProperty, NotifyPropertyChangedFor(nameof(CanGenerate))] private bool _isGenerating;
public bool CanGenerate => !IsGenerating && SelectedGroup is not null && SelectedTemplate is not null;
public StudentReportDialogViewModel(Student student, IGroupMembershipRepository memberships,
IGroupRepository groups, IInterimGradeRepository interims, StudentReportService reports,
ITemplateRenderer renderer, SchoolYearService schoolYears, IReadOnlyList<TemplateStore> stores)
{
_student = student; _interims = interims; _reports = reports; _renderer = renderer;
_schoolYears = schoolYears; _loader = stores[0];
Templates.Add(new(null));
foreach (var template in stores.SelectMany(s => s.GetTemplates()).Where(t => t.IsStudentReport)
.DistinctBy(t => t.PackagePath).OrderBy(t => t.Name)) Templates.Add(new(template));
foreach (var membership in memberships.GetByStudent(student.Id))
if (groups.GetById(membership.GroupId) is { } group && !Groups.Any(g => g.Model.Id == group.Id))
Groups.Add(new(group));
SelectedTemplate = Templates[0];
SelectedGroup = Groups.OrderByDescending(g => g.Model.SchoolYear).FirstOrDefault();
}
partial void OnSelectedGroupChanged(LetterGroupChoice? value)
{
Interims.Clear(); SelectedInterim = null;
if (value is not null)
{
From = _schoolYears.SchoolYearStart(value.Model.SchoolYear).ToString("dd.MM.yyyy");
To = _schoolYears.SchoolYearEnd(value.Model.SchoolYear).ToString("dd.MM.yyyy");
var today = DateOnly.FromDateTime(DateTime.Today);
if (_schoolYears.SchoolYearEnd(value.Model.SchoolYear) > today) To = today.ToString("dd.MM.yyyy");
foreach (var grade in _interims.GetByStudentAndGroup(_student.Id, value.Model.Id)
.OrderByDescending(g => g.Date).ThenBy(g => g.Occasion)) Interims.Add(new(grade));
}
OnPropertyChanged(nameof(CanGenerate));
}
partial void OnSelectedTemplateChanged(StudentReportTemplateChoice? value) => OnPropertyChanged(nameof(CanGenerate));
public async Task<bool> GenerateAsync(string outputPath)
{
if (!CanGenerate) return false;
StatusMessage = "";
if (!DateOnly.TryParseExact(From, "dd.MM.yyyy", CultureInfo.GetCultureInfo("de-DE"), DateTimeStyles.None, out var from)
|| !DateOnly.TryParseExact(To, "dd.MM.yyyy", CultureInfo.GetCultureInfo("de-DE"), DateTimeStyles.None, out var to))
{ StatusMessage = "Bitte Beginn und Ende als TT.MM.JJJJ eingeben."; return false; }
try
{
var template = SelectedTemplate!.Model is { } installed ? _loader.Load(installed) : StudentReportTemplate.Create();
// Datenbankzugriffe auf dem UI-Thread; nur das Setzen und Schreiben läuft im Hintergrund.
var values = _reports.Build(_student, SelectedGroup!.Model, template,
new(from, to, SelectedInterim?.Model, IncludeInterim, IncludeParticipation, IncludeCompetencies,
IncludeHomework, IncludeDocumentation, IncludeConfidential, Comment));
IsGenerating = true;
await Task.Run(() => File.WriteAllBytes(outputPath, _renderer.RenderToPdf(template, new LetterDataProvider(values))));
StatusMessage = "Schülerbericht erzeugt.";
return true;
}
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException or TemplateValidationException or QuestPDF.Drawing.Exceptions.DocumentLayoutException)
{ StatusMessage = $"Der Schülerbericht konnte nicht erzeugt werden: {ex.Message}"; return false; }
finally { IsGenerating = false; }
}
}
public sealed record StudentReportTemplateChoice(InstalledTemplate? Model)
{
public string Name => Model?.Name ?? "Schülerbericht Standard";
}
public sealed record StudentReportInterimChoice(InterimGrade Model)
{
public string Label => $"{Model.Date:dd.MM.yyyy} · {Model.Occasion}";
}
@@ -276,6 +276,7 @@ public partial class StudentDetailViewModel : ObservableObject
public Func<Documentation, string, Task<Documentation?>>? OnConductParentCall { get; set; }
public Func<Student, Task<StudentManagementResult>>? OnManageStudent { get; set; }
public Func<Task>? OnCreateLetter { get; set; }
public Func<Task>? OnCreateStudentReport { get; set; }
public Action? OnReturnToStudentList { get; set; }
partial void OnIsStudentActiveChanged(bool value) => OnPropertyChanged(nameof(StudentStatus));
@@ -455,6 +456,12 @@ public partial class StudentDetailViewModel : ObservableObject
[RelayCommand] private void StartEdit() => IsEditing = true;
[RelayCommand]
private async Task CreateStudentReport()
{
if (Student is not null && OnCreateStudentReport is not null) await OnCreateStudentReport();
}
[RelayCommand]
private async Task CreateLetter()
{