feat: improve WebUntis sync and competency insights
CI / build-and-test (push) Canceled after 0s

This commit is contained in:
admin committed 2026-09-20 22:17:29 +02:00
1 parent cdb4637c65
commit f0c0479bcb
22 files changed
+1099 -76

No files matched your search

@@ -3,7 +3,8 @@ using LehrerApp.Core.Models;
namespace LehrerApp.Core.Services;
public sealed record CompetencyScore(
double AchievedPoints, double MaxPoints, int EvidenceCount, int ParticipantCount)
double AchievedPoints, double MaxPoints, int EvidenceCount, int ParticipantCount,
int ExamEvidenceCount = 0, int ParticipationEvidenceCount = 0)
{
public double Percent => MaxPoints <= 0 ? 0 : AchievedPoints / MaxPoints * 100.0;
}
@@ -18,6 +19,7 @@ public sealed class CompetencyAnalysisItem
public int TaughtUnitCount { get; init; }
public int PlannedExamCount { get; init; }
public int AssessedExamCount { get; init; }
public int ParticipationSessionCount { get; init; }
public CompetencyScore? GroupScore { get; init; }
public IReadOnlyDictionary<Guid, CompetencyScore> StudentScores { get; init; } =
new Dictionary<Guid, CompetencyScore>();
@@ -29,7 +31,10 @@ public sealed class CompetencyAnalysisService
IReadOnlyList<CompetencyDomain> catalog,
IReadOnlyList<Unit> units,
IReadOnlyList<Exam> exams,
IReadOnlyList<ExamResult> results)
IReadOnlyList<ExamResult> results,
IReadOnlyList<ParticipationSession>? participationSessions = null,
IReadOnlyList<ParticipationEntry>? participationEntries = null,
IReadOnlyList<ParticipationAspect>? participationAspects = null)
{
var builders = new List<ItemBuilder>();
var byId = new Dictionary<Guid, ItemBuilder>();
@@ -105,7 +110,36 @@ public sealed class CompetencyAnalysisService
if (task.MaxPoints <= 0) continue;
var achieved = Math.Clamp(result.Points[taskIndex], 0, task.MaxPoints);
foreach (var reference in NormalizeReferences(task.Competencies))
GetOrAdd(reference).AddScore(result.StudentId, achieved, task.MaxPoints);
GetOrAdd(reference).AddExamScore(result.StudentId, achieved, task.MaxPoints);
}
}
var entriesBySession = (participationEntries ?? [])
.GroupBy(x => x.SessionId)
.ToDictionary(x => x.Key, x => x.ToList());
var aspects = participationAspects ?? [];
foreach (var session in participationSessions ?? [])
{
var references = NormalizeReferences(session.Competencies).ToList();
foreach (var reference in references)
GetOrAdd(reference).ParticipationSessions++;
if (references.Count == 0 || !entriesBySession.TryGetValue(session.Id, out var entries))
continue;
foreach (var entry in entries)
{
var estimated = ParticipationCompetencyEstimate.Estimate(
entry.Ratings, aspects, entry.DayHighlight);
foreach (var reference in references)
{
var explicitRating = entry.CompetencyRatings
.FirstOrDefault(x => x.ItemId == reference.ItemId)?.Value;
var rating = explicitRating ?? estimated;
if (rating is not null)
GetOrAdd(reference).AddParticipationScore(entry.StudentId, rating.Value);
}
}
}
@@ -124,14 +158,23 @@ public sealed class CompetencyAnalysisService
public int TaughtUnits { get; set; }
public int PlannedExams { get; set; }
public int AssessedExams { get; set; }
public int ParticipationSessions { get; set; }
public void AddScore(Guid studentId, double achieved, double maximum)
public void AddExamScore(Guid studentId, double achieved, double maximum) =>
AddScore(studentId, achieved, maximum, isParticipation: false);
public void AddParticipationScore(Guid studentId, int rating) =>
AddScore(studentId, Math.Clamp(rating, -2, 2) + 2, 4, isParticipation: true);
private void AddScore(Guid studentId, double achieved, double maximum, bool isParticipation)
{
if (!_scores.TryGetValue(studentId, out var score))
_scores[studentId] = score = new MutableScore();
score.Achieved += achieved;
score.Maximum += maximum;
score.EvidenceCount++;
if (isParticipation) score.ParticipationEvidenceCount++;
else score.ExamEvidenceCount++;
}
public CompetencyAnalysisItem Build()
@@ -140,6 +183,8 @@ public sealed class CompetencyAnalysisService
var achieved = _scores.Values.Sum(x => x.Achieved);
var maximum = _scores.Values.Sum(x => x.Maximum);
var evidence = _scores.Values.Sum(x => x.EvidenceCount);
var examEvidence = _scores.Values.Sum(x => x.ExamEvidenceCount);
var participationEvidence = _scores.Values.Sum(x => x.ParticipationEvidenceCount);
return new CompetencyAnalysisItem
{
DomainName = domainName,
@@ -150,8 +195,10 @@ public sealed class CompetencyAnalysisService
TaughtUnitCount = TaughtUnits,
PlannedExamCount = PlannedExams,
AssessedExamCount = AssessedExams,
ParticipationSessionCount = ParticipationSessions,
GroupScore = maximum > 0
? new CompetencyScore(achieved, maximum, evidence, _scores.Count)
? new CompetencyScore(achieved, maximum, evidence, _scores.Count,
examEvidence, participationEvidence)
: null,
StudentScores = studentScores,
};
@@ -163,7 +210,10 @@ public sealed class CompetencyAnalysisService
public double Achieved { get; set; }
public double Maximum { get; set; }
public int EvidenceCount { get; set; }
public int ExamEvidenceCount { get; set; }
public int ParticipationEvidenceCount { get; set; }
public CompetencyScore ToScore(int participantCount) =>
new(Achieved, Maximum, EvidenceCount, participantCount);
new(Achieved, Maximum, EvidenceCount, participantCount,
ExamEvidenceCount, ParticipationEvidenceCount);
}
}
@@ -0,0 +1,36 @@
using LehrerApp.Core.Models;
namespace LehrerApp.Core.Services;
/// <summary>
/// Leitet aus den allgemeinen Mitarbeitsaspekten einen Kompetenzwert auf der gemeinsamen
/// Skala von -2 bis +2 ab. Explizite Kompetenzbewertungen werden vom Aufrufer bevorzugt.
/// </summary>
public static class ParticipationCompetencyEstimate
{
public static int? Estimate(IEnumerable<AspectRating> ratings,
IReadOnlyCollection<ParticipationAspect> aspects, DayHighlightKind? dayHighlight)
{
var aspectsByKey = aspects
.GroupBy(x => x.Key, StringComparer.OrdinalIgnoreCase)
.ToDictionary(x => x.Key, x => x.Last(), StringComparer.OrdinalIgnoreCase);
var normalized = ratings
.Where(x => aspectsByKey.ContainsKey(x.Key))
.Select(x =>
{
var aspect = aspectsByKey[x.Key];
return ParticipationRatingScale.Normalize(aspect.ValueType, x.Value, aspect.MaxPoints);
})
.ToList();
if (normalized.Count == 0) return null;
var adjustment = dayHighlight switch
{
DayHighlightKind.Standout => 1.0,
DayHighlightKind.Sleepy or DayHighlightKind.Rough => -1.0,
_ => 0.0,
};
return Math.Clamp((int)Math.Round(normalized.Average() + adjustment,
MidpointRounding.AwayFromZero), -2, 2);
}
}
+33 -19
View File
@@ -72,18 +72,19 @@ public class UntisDiffService
if (string.Equals(evt.Status, "CANCELLED", StringComparison.OrdinalIgnoreCase))
{
substitutions.Add(mapping.Kind == SubstitutionKind.Lesson
? BuildCancelledLesson(evt.Date, mapping, evt.Uid)
: BuildSupervisionNote(evt.Date, mapping, evt.Uid, "Laut WebUntis abgesagt."));
if (mapping.Kind == SubstitutionKind.Lesson)
substitutions.AddRange(BuildCancelledLessons(evt.Date, mapping, evt.Uid));
else
substitutions.Add(BuildSupervisionNote(evt.Date, mapping, evt.Uid, "Laut WebUntis abgesagt."));
continue;
}
if (mapping.Kind == SubstitutionKind.Lesson)
{
if (DeviationReason(evt, mapping) is { } reason)
substitutions.Add(BuildChangedLesson(evt, mapping, reason));
substitutions.AddRange(BuildChangedLessons(evt, mapping, reason));
else
externalIdsToDelete.Add(evt.Uid); // war ggf. vorher fälschlich als Vertretung erkannt
externalIdsToDelete.AddRange(ExternalIdsForPeriods(evt.Uid, mapping)); // ggf. frühere Vertretung entfernen
}
else if (!regularDutyKeys.Contains((mapping.Weekday, mapping.AfterPeriod!.Value)))
{
@@ -143,11 +144,11 @@ public class UntisDiffService
{
// War evtl. an einem früheren Poll (noch) als fehlend gemeldet - jetzt
// wieder im Feed vorhanden, die alte Meldung räumt sich damit selbst ab.
externalIdsToDelete.Add(BuildMissingExternalId(date, mapping));
externalIdsToDelete.AddRange(ExternalIdsForPeriods(BuildMissingExternalId(date, mapping), mapping));
continue;
}
substitutions.Add(BuildCancelledLesson(date, mapping, BuildMissingExternalId(date, mapping)));
substitutions.AddRange(BuildCancelledLessons(date, mapping, BuildMissingExternalId(date, mapping)));
}
}
}
@@ -192,19 +193,32 @@ public class UntisDiffService
private static string RemoveWhitespace(string value) =>
new(value.Where(c => !char.IsWhiteSpace(c)).ToArray());
private static SubstitutionEntry BuildCancelledLesson(DateOnly date, UntisSlotMapping mapping, string externalId) => new()
{
Date = date, Kind = SubstitutionKind.Cancelled, PeriodNumber = mapping.PeriodNumber,
Description = "Automatisch über WebUntis-Abgleich erkannt.", ExternalId = externalId,
};
private static IEnumerable<SubstitutionEntry> BuildCancelledLessons(DateOnly date, UntisSlotMapping mapping,
string externalId) => Periods(mapping).Select((period, index) => new SubstitutionEntry
{
Date = date, Kind = SubstitutionKind.Cancelled, PeriodNumber = period,
Description = "Automatisch über WebUntis-Abgleich erkannt.",
ExternalId = PeriodExternalId(externalId, period, index),
});
private static SubstitutionEntry BuildChangedLesson(UntisIcsEvent evt, UntisSlotMapping mapping, string reason) => new()
{
Date = evt.Date, Kind = SubstitutionKind.Lesson, PeriodNumber = mapping.PeriodNumber,
GroupId = mapping.GroupId, GroupLabel = mapping.ClassToken,
Description = $"WebUntis: {evt.Summary ?? "?"} · {evt.Location} (regulär: {mapping.Summary ?? "?"}) — {reason}",
ExternalId = evt.Uid,
};
private static IEnumerable<SubstitutionEntry> BuildChangedLessons(UntisIcsEvent evt, UntisSlotMapping mapping,
string reason) => Periods(mapping).Select((period, index) => new SubstitutionEntry
{
Date = evt.Date, Kind = SubstitutionKind.Lesson, PeriodNumber = period,
GroupId = mapping.GroupId, GroupLabel = mapping.ClassToken,
Description = $"WebUntis: {evt.Summary ?? "?"} · {evt.Location} (regulär: {mapping.Summary ?? "?"}) — {reason}",
ExternalId = PeriodExternalId(evt.Uid, period, index),
});
private static List<int> Periods(UntisSlotMapping mapping) => mapping.CoveredPeriods.Count > 0
? mapping.CoveredPeriods.Distinct().Order().ToList()
: mapping.PeriodNumber is { } period ? [period] : [];
private static IEnumerable<string> ExternalIdsForPeriods(string externalId, UntisSlotMapping mapping) =>
Periods(mapping).Select((period, index) => PeriodExternalId(externalId, period, index));
private static string PeriodExternalId(string externalId, int period, int index) =>
index == 0 ? externalId : $"{externalId}-period-{period}";
// Aufsichten haben keinen TimetableSlot im Hintergrund (siehe SubstitutionEntry.Kind-Doc) -
// deshalb Kind=Supervision statt Cancelled, auch wenn der Termin verschwunden ist.