Add student name formatting utilities and refactor team student name handling

This commit introduces two new utility classes: StudentNameFormatter and TeamStudentNameFormatter, which provide methods for formatting student names with options for indicating absence and overlaps. The Team class has been updated to remove the StudentsFirstNames property, and various components across the WebApp have been refactored to utilize the new formatting utilities. This enhances the maintainability and readability of the code while improving the presentation of student names in the UI.
This commit is contained in:
2026-01-11 21:43:00 -05:00
parent 6cd4418142
commit f8c22690d4
13 changed files with 504 additions and 121 deletions
@@ -4,6 +4,7 @@
@using Core.Calculation
@using Microsoft.EntityFrameworkCore
@using WebApp.Components.Shared.Components
@using Core.Utility
@inject IConfiguration Configuration
@inject AppDbContext Context
@inject ClipboardService ClipboardService
@@ -380,21 +381,44 @@
private string FormatStudentList(Team team, TeamScheduleTimeSlot timeslot)
{
return string.Join(", ",
team.Students
.OrderBy(e => e == team.Captain)
.ThenBy(e => e.FirstName)
.Select(e => FormatStudentName(e, timeslot)));
return TeamStudentNameFormatter.FormatStudentList(
team,
new TeamStudentNameFormatter.FormatOptions
{
Ordering = TeamStudentNameFormatter.OrderingStyle.CaptainFirst,
MarkOverlaps = true,
HasOverlaps = timeslot.StudentHasOverlaps,
MarkAbsent = true,
AbsentStudents = _absentStudents.ToList()
});
}
private string FormatStudentName(Student student, TeamScheduleTimeSlot timeslot)
{
var name = student.FirstName;
if (timeslot.StudentHasOverlaps(student))
name += "*";
if (_absentStudents.Contains(student))
name += " (absent)";
return name;
// Find the team this student belongs to for formatting context
var team = _teams.FirstOrDefault(t => t.Students.Contains(student));
if (team == null)
{
// No team context, use StudentNameFormatter directly
return StudentNameFormatter.FormatStudentName(
student,
new StudentNameFormatter.FormatOptions
{
HasOverlap = timeslot.StudentHasOverlaps(student),
IsAbsent = _absentStudents.Contains(student)
});
}
return TeamStudentNameFormatter.FormatStudentName(
student,
team,
new TeamStudentNameFormatter.FormatOptions
{
MarkOverlaps = true,
HasOverlaps = timeslot.StudentHasOverlaps,
MarkAbsent = true,
AbsentStudents = _absentStudents.ToList()
});
}
private void AppendUnscheduledStudents(StringBuilder sb, TeamScheduleTimeSlot timeslot)
@@ -407,9 +431,12 @@
foreach (var student in timeslot.UnscheduledStudents)
{
var studentName = student.FirstName;
if (_absentStudents.Contains(student))
studentName += " (absent)";
var studentName = StudentNameFormatter.FormatStudentName(
student,
new StudentNameFormatter.FormatOptions
{
IsAbsent = _absentStudents.Contains(student)
});
var unassignedTeams = _solution.StudentUnassignedTeams(student);
var teamsList = string.Join(", ", unassignedTeams.Select(e => e.ToString()));
@@ -1,5 +1,6 @@
@using Core.Calculation
@using WebApp.Models
@using Core.Utility
<MudStack>
<MudText Typo="Typo.h6">@TimeSlotName</MudText>
@@ -8,31 +9,54 @@
var scheduledTeamIds = ScheduledTeams.Select(t => t.Id).ToHashSet();
var removed = !scheduledTeamIds.Contains(team.Id);
<MudLink Typo="Typo.body1"
Class="d-flex align-center"
Color="Color.Default"
OnClick="@(() => OnToggleTeam.InvokeAsync(team))">
<MudIcon Icon="@Icons.Material.Filled.Clear"
Size="Size.Small"
Class="@(removed ? "" : "d-none")">
</MudIcon>
@team -
@foreach (var student in team.Students)
{
var overlap = StudentHasOverlaps(student);
var isAbsent = AbsentStudents.Contains(student);
var color = overlap ? Color.Warning : Color.Default;
var suffix = GetStudentSuffix(overlap, isAbsent);
if (student != team.Students.First())
{
<MudText>, </MudText>
<MudStack Row="true" Spacing="2" AlignItems="AlignItems.Center" Class="d-flex align-center">
<MudStack Row="true" Spacing="1" AlignItems="AlignItems.Center">
<MudIcon Icon="@Icons.Material.Filled.Clear"
Size="Size.Small"
Class="@(removed ? "" : "d-none")"
OnClick="@(() => OnToggleTeam.InvokeAsync(team))"
Style="cursor: pointer;">
</MudIcon>
@{
var teamMembers = TeamStudentNameFormatter.FormatStudentList(
team,
new TeamStudentNameFormatter.FormatOptions
{
Ordering = TeamStudentNameFormatter.OrderingStyle.None
});
}
<MudText Typo="Typo.body2" Color="@color">
&nbsp;@student.FirstName@suffix
</MudText>
}
</MudLink>
<MudTooltip Text="@teamMembers">
<div @onclick="@(() => OnToggleTeam.InvokeAsync(team))" style="cursor: pointer; display: inline-block;">
<MudChip T="string"
Size="Size.Small"
Color="Color.Default">
@team
</MudChip>
</div>
</MudTooltip>
</MudStack>
<MudStack Row="true" Spacing="1" Wrap="Wrap.Wrap" AlignItems="AlignItems.Center">
@foreach (var student in team.Students)
{
var overlap = StudentHasOverlaps(student);
var chipColor = overlap ? Color.Warning : Color.Default;
var formattedName = TeamStudentNameFormatter.FormatStudentName(
student,
team,
new TeamStudentNameFormatter.FormatOptions
{
MarkOverlaps = true,
HasOverlaps = StudentHasOverlaps,
MarkAbsent = true,
AbsentStudents = AbsentStudents.ToList()
});
<MudChip T="string" Size="Size.Small" Color="@chipColor" Variant="Variant.Outlined">
@formattedName
</MudChip>
}
</MudStack>
</MudStack>
}
</MudStack>
@@ -54,11 +78,4 @@
[Parameter]
public EventCallback<Team> OnToggleTeam { get; set; }
private string GetStudentSuffix(bool overlap, bool isAbsent)
{
var suffix = overlap ? "*" : "";
suffix += isAbsent ? " (absent)" : "";
return suffix;
}
}
@@ -1,4 +1,5 @@
@using Core.Calculation
@using Core.Utility
@if (UnscheduledStudents.Any())
{
@@ -6,32 +7,51 @@
<MudStack>
@foreach (var student in UnscheduledStudents)
{
var isAbsent = AbsentStudents.Contains(student);
<MudItem>
<MudText Typo="Typo.body1" HtmlTag="i">
@student.FirstName@(isAbsent ? " (absent)" : "")&nbsp;
</MudText>
@foreach (var unassignedTeam in UnassignedTeams(student))
{
var isPossibleAddition = PossibleAdditions.Contains(unassignedTeam, new TeamIdComparer());
var scheduledTeamIds = ScheduledTeams.Select(t => t.Id).ToHashSet();
var isScheduled = scheduledTeamIds.Contains(unassignedTeam.Id);
var color = isPossibleAddition ? Color.Success : Color.Default;
if (unassignedTeam != UnassignedTeams(student).First())
{
<span>, </span>
}
<MudLink Typo="Typo.body2"
Color="@color"
OnClick="@(() => OnToggleTeam.InvokeAsync(unassignedTeam))">
<MudIcon Icon="@Icons.Material.Filled.Check"
Size="Size.Small"
Class="@(isScheduled ? "" : "d-none")">
</MudIcon>
@unassignedTeam
</MudLink>
@{
var formattedName = StudentNameFormatter.FormatStudentName(
student,
new StudentNameFormatter.FormatOptions
{
IsAbsent = AbsentStudents.Contains(student)
});
}
<MudStack Row="true" Spacing="2" AlignItems="AlignItems.Center">
<MudStack Row="true" Spacing="1" AlignItems="AlignItems.Center">
<MudChip T="string" Size="Size.Small" Variant="Variant.Outlined" Class="font-style-italic">
@formattedName
</MudChip>
</MudStack>
<MudStack Row="true" Spacing="1" Wrap="Wrap.Wrap" AlignItems="AlignItems.Center">
@foreach (var unassignedTeam in UnassignedTeams(student))
{
var isPossibleAddition = PossibleAdditions.Contains(unassignedTeam, new TeamIdComparer());
var scheduledTeamIds = ScheduledTeams.Select(t => t.Id).ToHashSet();
var isScheduled = scheduledTeamIds.Contains(unassignedTeam.Id);
var chipColor = isPossibleAddition ? Color.Success : Color.Default;
var teamMembers = TeamStudentNameFormatter.FormatStudentList(
unassignedTeam,
new TeamStudentNameFormatter.FormatOptions
{
Ordering = TeamStudentNameFormatter.OrderingStyle.None
});
<MudTooltip Text="@teamMembers">
<div @onclick="@(() => OnToggleTeam.InvokeAsync(unassignedTeam))" style="cursor: pointer; display: inline-block;">
<MudChip T="string"
Size="Size.Small"
Color="@chipColor">
@if (isScheduled)
{
<MudIcon Icon="@Icons.Material.Filled.Check" Size="Size.Small" Style="margin-right: 4px;" />
}
@unassignedTeam
</MudChip>
</div>
</MudTooltip>
}
</MudStack>
</MudStack>
</MudItem>
}
</MudStack>