Punkteeingabe & Korrektur für Klausuren (1.4)
Neuer Dialog ExamGradingDialog, erreichbar über "Punkte eingeben" im Klausuren-Tab (Button + Kontextmenü bei ausgewählter Klausur): - Eingaberaster: Schüler-Zeilen × Aufgaben-Spalten, Summe und Note live über GradingService.CalculateGrade() berechnet. - Tab bewegt sich nativ zur nächsten Zelle; Enter/Pfeil-Hoch/Pfeil-Runter springen zur gleichen Spalte in der Nachbarzeile (auch über noch nicht realisierte, virtualisierte Zeilen hinweg via ScrollIntoView). Komma oder Punkt als Dezimaltrennzeichen werden beide akzeptiert. - Abwesend-Checkbox (Note zeigt dann "abwesend") und Kommentarfeld pro Schüler. - Punkte außerhalb 0..Maximalpunkte werden rot markiert und bewusst nicht gespeichert; der zuletzt gültige Stand bleibt erhalten. - Autosave nach jeder Zelle, kein Speichern-Button nötig. Dabei nebenbei behoben: die Schülerliste berücksichtigt jetzt Enrollment- Zeiträume (H1/H2/Custom) relativ zum Klausurdatum, analog zur bereits bestehenden Logik im Mitarbeit-Tab, statt pauschal alle im Schuljahr eingeschriebenen Schüler zu zeigen. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using LehrerApp.Core.Interfaces;
|
||||
using LehrerApp.Core.Models;
|
||||
using LehrerApp.Core.Services;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Globalization;
|
||||
|
||||
namespace LehrerApp.Desktop.ViewModels.Groups;
|
||||
|
||||
// ── Punkteeingabe & Korrektur (1.4) ──────────────────────────────────────────
|
||||
|
||||
public partial class ExamGradingDialogViewModel : ObservableObject
|
||||
{
|
||||
private readonly IExamResultRepository _results;
|
||||
private readonly GradingService _grading;
|
||||
private readonly Exam _exam;
|
||||
private readonly double _examMaxPoints;
|
||||
|
||||
public string ExamTitle => _exam.Title;
|
||||
public string ExamDateLabel => _exam.Date.ToString("dd.MM.yyyy");
|
||||
public List<ExamTask> Tasks { get; }
|
||||
|
||||
public ObservableCollection<ExamResultRow> Rows { get; } = [];
|
||||
|
||||
public ExamGradingDialogViewModel(IExamResultRepository results, IStudentRepository students,
|
||||
IEnrollmentRepository enrollments, GradingService grading, Exam exam, Guid groupId, string schoolYear)
|
||||
{
|
||||
_results = results; _grading = grading; _exam = exam;
|
||||
Tasks = exam.Tasks.OrderBy(t => t.Nr).ToList();
|
||||
_examMaxPoints = Tasks.Sum(t => t.MaxPoints);
|
||||
|
||||
var enrolled = students.GetByGroup(groupId, schoolYear);
|
||||
var enrollmentList = enrollments.GetByGroupAndYear(groupId, schoolYear);
|
||||
var existing = results.GetByExam(exam.Id).ToDictionary(r => r.StudentId);
|
||||
|
||||
foreach (var s in enrolled.OrderBy(s => s.LastName).ThenBy(s => s.FirstName))
|
||||
{
|
||||
var enrollment = enrollmentList.FirstOrDefault(e => e.StudentId == s.Id);
|
||||
if (enrollment is not null && !IsEnrolledAtDate(enrollment, exam.Date)) continue;
|
||||
|
||||
existing.TryGetValue(s.Id, out var result);
|
||||
var row = new ExamResultRow(s.Id, s.FullName, Tasks, result, _exam.GradingKey, _examMaxPoints, _grading);
|
||||
row.OnChanged = SaveRow;
|
||||
Rows.Add(row);
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveRow(ExamResultRow row) => _results.Save(row.ToModel(_exam.Id));
|
||||
|
||||
private static bool IsEnrolledAtDate(Enrollment e, DateOnly date) => e.Period switch
|
||||
{
|
||||
EnrollmentPeriod.H1Only => date.Month >= 8 || date.Month <= 1,
|
||||
EnrollmentPeriod.H2Only => date.Month >= 2 && date.Month <= 7,
|
||||
EnrollmentPeriod.Custom => (e.JoinedAt is null || date >= e.JoinedAt.Value)
|
||||
&& (e.LeftAt is null || date <= e.LeftAt.Value),
|
||||
_ => true,
|
||||
};
|
||||
}
|
||||
|
||||
// ── Zeile im Punkteraster ──────────────────────────────────────────────────
|
||||
|
||||
public partial class ExamResultRow : ObservableObject
|
||||
{
|
||||
private readonly Guid _resultId;
|
||||
private readonly List<GradingKeyEntry> _gradingKey;
|
||||
private readonly double _examMaxPoints;
|
||||
private readonly GradingService _grading;
|
||||
|
||||
public Guid StudentId { get; }
|
||||
public string Name { get; }
|
||||
|
||||
public ObservableCollection<PointsCell> Cells { get; } = [];
|
||||
|
||||
[ObservableProperty] private bool _absent;
|
||||
[ObservableProperty] private string _comment = "";
|
||||
[ObservableProperty] private double _totalPoints;
|
||||
[ObservableProperty] private string _gradeDisplay = "";
|
||||
|
||||
public string TotalPointsDisplay => TotalPoints.ToString("0.##", CultureInfo.InvariantCulture);
|
||||
|
||||
public Action<ExamResultRow>? OnChanged { get; set; }
|
||||
|
||||
public ExamResultRow(Guid studentId, string name, List<ExamTask> tasks, ExamResult? existing,
|
||||
List<GradingKeyEntry> gradingKey, double examMaxPoints, GradingService grading)
|
||||
{
|
||||
StudentId = studentId; Name = name;
|
||||
_gradingKey = gradingKey; _examMaxPoints = examMaxPoints; _grading = grading;
|
||||
_resultId = existing?.Id ?? Guid.NewGuid();
|
||||
_absent = existing?.Absent ?? false;
|
||||
_comment = existing?.Comment ?? "";
|
||||
|
||||
for (var i = 0; i < tasks.Count; i++)
|
||||
{
|
||||
double? pts = existing is not null && i < existing.Points.Count ? existing.Points[i] : null;
|
||||
var cell = new PointsCell(tasks[i].MaxPoints, pts);
|
||||
cell.OnChanged = () => { RecomputeTotals(); OnChanged?.Invoke(this); };
|
||||
Cells.Add(cell);
|
||||
}
|
||||
RecomputeTotals();
|
||||
}
|
||||
|
||||
partial void OnAbsentChanged(bool value)
|
||||
{
|
||||
RecomputeTotals();
|
||||
OnChanged?.Invoke(this);
|
||||
}
|
||||
|
||||
partial void OnCommentChanged(string value) => OnChanged?.Invoke(this);
|
||||
|
||||
private void RecomputeTotals()
|
||||
{
|
||||
TotalPoints = Cells.Sum(c => c.IsInvalid ? 0 : (c.Value ?? 0));
|
||||
OnPropertyChanged(nameof(TotalPointsDisplay));
|
||||
GradeDisplay = Absent ? "abwesend" : _grading.CalculateGrade(TotalPoints, _examMaxPoints, _gradingKey);
|
||||
}
|
||||
|
||||
public ExamResult ToModel(Guid examId) => new()
|
||||
{
|
||||
Id = _resultId,
|
||||
ExamId = examId,
|
||||
StudentId = StudentId,
|
||||
Points = Cells.Select(c => c.Value ?? 0).ToList(),
|
||||
TotalPoints = TotalPoints,
|
||||
Grade = Absent ? null : GradeDisplay,
|
||||
Absent = Absent,
|
||||
Comment = string.IsNullOrWhiteSpace(Comment) ? null : Comment.Trim(),
|
||||
};
|
||||
}
|
||||
|
||||
// ── Eine Punktezelle ─────────────────────────────────────────────────────────
|
||||
|
||||
public partial class PointsCell : ObservableObject
|
||||
{
|
||||
public double MaxPoints { get; }
|
||||
[ObservableProperty] private double? _value;
|
||||
[ObservableProperty] private bool _isInvalid;
|
||||
|
||||
public Action? OnChanged { get; set; }
|
||||
|
||||
public PointsCell(double maxPoints, double? value)
|
||||
{
|
||||
MaxPoints = maxPoints;
|
||||
_value = value;
|
||||
_isInvalid = IsOutOfRange(value);
|
||||
}
|
||||
|
||||
/// Setzt den Wert; bei Punkten außerhalb 0..Maximalpunkte wird nur rot markiert,
|
||||
/// aber nicht gespeichert (1.4.5) — OnChanged (und damit Autosave) wird dann nicht ausgelöst.
|
||||
public void TrySetValue(double? value)
|
||||
{
|
||||
Value = value;
|
||||
IsInvalid = IsOutOfRange(value);
|
||||
if (!IsInvalid) OnChanged?.Invoke();
|
||||
}
|
||||
|
||||
private bool IsOutOfRange(double? value) =>
|
||||
value.HasValue && (value.Value < 0 || value.Value > MaxPoints + 0.0001);
|
||||
}
|
||||
Reference in New Issue
Block a user