Klausur-Deckblätter als Sammel-PDF aus Vorlagen vom Typ exam-cover
Neuer Dialog "Deckblätter…" bei Klausuren, Punktetabelle als Drawing mit festen Spaltenbreiten, FLOWDRAWBOX-Attribut fit=content, Stapel-Rendering mit Seitenzahl je Datensatz, Kursbezeichnung und konfigurierbares Kürzel. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f56caced32
commit
dd92128464
33 files changed
+1158
-28
No files matched your search
@@ -0,0 +1,116 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using LehrerApp.Core.Interfaces;
|
||||
using LehrerApp.Core.Models;
|
||||
using LehrerApp.Core.Services;
|
||||
using LehrerApp.Desktop.Services;
|
||||
using LehrerApp.Desktop.ViewModels.Students;
|
||||
using LehrerApp.Templating;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace LehrerApp.Desktop.ViewModels.Groups;
|
||||
|
||||
/// <summary>Erzeugt zu einer Klausur einen Satz individueller Deckblätter als ein Sammel-PDF.
|
||||
/// Zur Auswahl stehen nur Vorlagen mit <c>report-type=exam-cover</c>, egal in welcher der beiden
|
||||
/// Vorlagenbibliotheken sie importiert wurden. Mit <see cref="FillResults"/> trägt dieselbe
|
||||
/// Vorlage Punkte und Note ein oder bleibt als Blanko-Deckblatt zum Ausfüllen von Hand.</summary>
|
||||
public partial class ExamCoverSheetDialogViewModel : ObservableObject
|
||||
{
|
||||
private readonly Exam _exam;
|
||||
private readonly LearningGroup _group;
|
||||
private readonly string _subjectName;
|
||||
private readonly GradingService _grading;
|
||||
private readonly TemplateStore _loader;
|
||||
private readonly ITemplateBatchRenderer _renderer;
|
||||
|
||||
[ObservableProperty] private LetterTemplateChoice? _selectedTemplate;
|
||||
[ObservableProperty] private bool _fillResults;
|
||||
[ObservableProperty] private string _statusMessage = "";
|
||||
/// <summary>Vorbelegt aus den Einstellungen, hier nur für diesen Druck änderbar (z. B. zwei
|
||||
/// Kürzel bei einem gemeinsam unterrichteten Kurs) — die Einstellung bleibt unberührt.</summary>
|
||||
[ObservableProperty] private string _teacherAbbreviation;
|
||||
|
||||
public string ExamLabel => $"{_exam.Title} · {_group.Name} · {_exam.Date:dd.MM.yyyy}";
|
||||
public ObservableCollection<LetterTemplateChoice> Templates { get; } = [];
|
||||
public ObservableCollection<ExamCoverStudentChoice> Students { get; } = [];
|
||||
public bool HasNoTemplates => Templates.Count == 0;
|
||||
public string SuggestedFileName => SanitizeFileName($"Deckblätter_{_group.Name}_{_exam.Title}.pdf");
|
||||
|
||||
public ExamCoverSheetDialogViewModel(Exam exam, LearningGroup group, string subjectName,
|
||||
IStudentRepository students, IGroupMembershipRepository memberships, IExamResultRepository results,
|
||||
GradingService grading, IReadOnlyList<TemplateStore> templateStores, ITemplateBatchRenderer renderer,
|
||||
string teacherAbbreviation = "")
|
||||
{
|
||||
_teacherAbbreviation = teacherAbbreviation;
|
||||
_exam = exam; _group = group; _subjectName = subjectName; _grading = grading; _renderer = renderer;
|
||||
_loader = templateStores[0];
|
||||
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));
|
||||
SelectedTemplate = Templates.FirstOrDefault();
|
||||
|
||||
var membershipList = memberships.GetByGroup(group.Id);
|
||||
var existing = results.GetByExam(exam.Id).ToDictionary(r => r.StudentId);
|
||||
var wroteOriginal = ExamParticipantService.WroteOriginal(exam, results.GetByExam);
|
||||
foreach (var student in students.GetByGroup(group.Id).OrderBy(s => s.LastName).ThenBy(s => s.FirstName))
|
||||
{
|
||||
var membership = membershipList.FirstOrDefault(m => m.StudentId == student.Id);
|
||||
existing.TryGetValue(student.Id, out var result);
|
||||
if (!ExamParticipantService.Includes(exam, membership, result is not null, wroteOriginal.Contains(student.Id)))
|
||||
continue;
|
||||
Students.Add(new(student, result, membership?.Niveau));
|
||||
}
|
||||
// Vor der Korrektur ist das Blanko-Deckblatt der Normalfall, danach das ausgefüllte.
|
||||
FillResults = Students.Any(s => s.IsCorrected);
|
||||
}
|
||||
|
||||
/// <summary>Schreibt das Sammel-PDF und meldet den Erfolg.</summary>
|
||||
public bool Generate(string outputPath)
|
||||
{
|
||||
StatusMessage = "";
|
||||
if (SelectedTemplate is null) return false;
|
||||
var included = Students.Where(s => s.IsIncluded).ToList();
|
||||
if (included.Count == 0) { StatusMessage = "Kein Schüler ausgewählt."; return false; }
|
||||
try
|
||||
{
|
||||
var loaded = _loader.Load(SelectedTemplate.Model);
|
||||
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))).ToList();
|
||||
var result = _renderer.RenderBatchToPdf(loaded, data);
|
||||
File.WriteAllBytes(outputPath, result.Pdf);
|
||||
|
||||
var message = $"{included.Count} Deckblätter erzeugt.";
|
||||
var blank = FillResults ? included.Count(s => !s.IsCorrected) : 0;
|
||||
if (blank > 0) message += $" {blank} davon ohne Punkte und Note (noch nicht korrigiert).";
|
||||
var overflowing = result.PageCounts.Count(pages => pages > 1);
|
||||
if (overflowing > 0)
|
||||
message += $" Achtung: {overflowing} Deckblätter passen nicht auf eine Seite — die Punktetabelle braucht mehr Platz, als die Vorlage lässt.";
|
||||
StatusMessage = message;
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException or TemplateValidationException)
|
||||
{ StatusMessage = $"Deckblätter konnten nicht erzeugt werden: {ex.Message}"; return false; }
|
||||
}
|
||||
|
||||
private static string SanitizeFileName(string value)
|
||||
{ foreach (var character in Path.GetInvalidFileNameChars()) value = value.Replace(character, '_'); return value; }
|
||||
}
|
||||
|
||||
public partial class ExamCoverStudentChoice : ObservableObject
|
||||
{
|
||||
public Student Model { get; }
|
||||
public ExamResult? Result { get; }
|
||||
public Niveau? Niveau { get; }
|
||||
public string FullName => Model.FullName;
|
||||
public bool IsCorrected => ExamCoverSheetService.IsCorrected(Result);
|
||||
public string Hint => Result is { Absent: true } ? "abwesend" : "";
|
||||
[ObservableProperty] private bool _isIncluded;
|
||||
|
||||
public ExamCoverStudentChoice(Student model, ExamResult? result, Niveau? niveau = null)
|
||||
{
|
||||
Model = model; Result = result; Niveau = niveau;
|
||||
// Wer gefehlt hat, bekommt kein Deckblatt — lässt sich im Dialog wieder anhaken.
|
||||
_isIncluded = result is not { Absent: true };
|
||||
}
|
||||
}
|
||||
@@ -54,25 +54,14 @@ public partial class ExamGradingDialogViewModel : ObservableObject
|
||||
var membershipList = memberships.GetByGroup(groupId);
|
||||
var existing = results.GetByExam(exam.Id).ToDictionary(r => r.StudentId);
|
||||
|
||||
// Nachschreibarbeit: nur Schüler, die die erste Arbeit nicht mitgeschrieben haben. Wer dort
|
||||
// schon eine Note hat, taucht nicht auf — außer es gibt hier bereits ein Ergebnis (nichts
|
||||
// verstecken, was schon eingetragen wurde).
|
||||
var wroteOriginal = new HashSet<Guid>();
|
||||
if (exam.IsMakeUp && exam.RepeatOfExamId is { } originalId)
|
||||
foreach (var r in results.GetByExam(originalId).Where(r => !r.Absent && r.Grade is not null))
|
||||
wroteOriginal.Add(r.StudentId);
|
||||
var wroteOriginal = ExamParticipantService.WroteOriginal(exam, results.GetByExam);
|
||||
|
||||
foreach (var s in enrolled.OrderBy(s => s.LastName).ThenBy(s => s.FirstName))
|
||||
{
|
||||
var membership = membershipList.FirstOrDefault(e => e.StudentId == s.Id);
|
||||
if (membership is not null && !GroupMembershipService.IsActiveOn(membership, exam.Date)) continue;
|
||||
|
||||
// Niveau-Klausur: nur Schüler mit passendem Niveau zeigen. Klausuren ohne
|
||||
// Niveau-Zuordnung gelten weiterhin für die ganze Gruppe.
|
||||
if (exam.Niveau.HasValue && membership?.Niveau != exam.Niveau) continue;
|
||||
|
||||
existing.TryGetValue(s.Id, out var result);
|
||||
if (result is null && wroteOriginal.Contains(s.Id)) continue;
|
||||
if (!ExamParticipantService.Includes(exam, membership, result is not null, wroteOriginal.Contains(s.Id)))
|
||||
continue;
|
||||
var row = new ExamResultRow(s.Id, s.FullName, Tasks, result, _exam.GradingKey,
|
||||
ExamScoringService.Maximum(Tasks, ExamVariant.A), _grading);
|
||||
row.OnChanged = SaveRow;
|
||||
|
||||
@@ -235,6 +235,7 @@ public partial class GroupDetailViewModel : ObservableObject
|
||||
public Func<Exam, Task>? OnGradeExam { get; set; }
|
||||
public Func<Exam, Task>? OnEvaluateExam { get; set; }
|
||||
public Func<Exam, Task>? OnRevealExam { get; set; }
|
||||
public Func<Exam, Task>? OnExamCoverSheets { get; set; }
|
||||
public Func<Task<bool>>? OnConfirmReactivate { get; set; }
|
||||
|
||||
public GroupDetailViewModel(IGroupRepository groups, IStudentRepository students,
|
||||
@@ -421,6 +422,15 @@ public partial class GroupDetailViewModel : ObservableObject
|
||||
await OnRevealExam(exam);
|
||||
}
|
||||
|
||||
[RelayCommand(CanExecute = nameof(CanEditSelectedExam))]
|
||||
private async Task ExamCoverSheets()
|
||||
{
|
||||
if (SelectedExam is null || OnExamCoverSheets is null) return;
|
||||
var exam = _exams.GetById(SelectedExam.Id);
|
||||
if (exam is null) return;
|
||||
await OnExamCoverSheets(exam);
|
||||
}
|
||||
|
||||
[RelayCommand(CanExecute = nameof(CanEditSelectedExam))]
|
||||
private async Task DuplicateExam()
|
||||
{
|
||||
@@ -496,6 +506,7 @@ public partial class GroupDetailViewModel : ObservableObject
|
||||
GradeExamCommand.NotifyCanExecuteChanged();
|
||||
EvaluateExamCommand.NotifyCanExecuteChanged();
|
||||
RevealExamCommand.NotifyCanExecuteChanged();
|
||||
ExamCoverSheetsCommand.NotifyCanExecuteChanged();
|
||||
DuplicateExamCommand.NotifyCanExecuteChanged();
|
||||
DeleteExamCommand.NotifyCanExecuteChanged();
|
||||
AdvanceExamStatusCommand.NotifyCanExecuteChanged();
|
||||
@@ -526,6 +537,7 @@ public partial class GroupDetailViewModel : ObservableObject
|
||||
GradeExamCommand.NotifyCanExecuteChanged();
|
||||
EvaluateExamCommand.NotifyCanExecuteChanged();
|
||||
RevealExamCommand.NotifyCanExecuteChanged();
|
||||
ExamCoverSheetsCommand.NotifyCanExecuteChanged();
|
||||
DuplicateExamCommand.NotifyCanExecuteChanged();
|
||||
DeleteExamCommand.NotifyCanExecuteChanged();
|
||||
AdvanceExamStatusCommand.NotifyCanExecuteChanged();
|
||||
|
||||
@@ -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()) Templates.Add(new(template));
|
||||
foreach (var template in templates.Store.GetTemplates().Where(x => !x.IsExamCover)) Templates.Add(new(template));
|
||||
SelectedTemplate = Templates.FirstOrDefault();
|
||||
foreach (var id in studentIds)
|
||||
if (students.GetById(id) is { } student) Students.Add(new(student));
|
||||
|
||||
Reference in new issue
Block a user