21 Commits

Author SHA1 Message Date
fierke ab133cd267 [file:] better image 2026-06-12 22:20:28 +02:00
fierke f31e6d2813 [file:] better image 2026-06-12 22:11:51 +02:00
fierke 08db1eb681 [fix:] first semester priority 2026-06-07 20:31:30 +02:00
fierke 9be546d25f [feat:] overview of generated courses (pdf) 2026-06-07 20:19:54 +02:00
fierke 2910b1aeda Merge remote-tracking branch 'origin/main' 2026-06-07 12:51:51 +02:00
fierke 7a0e392ba8 [chore:] removed comments 2026-06-07 12:51:45 +02:00
fierke 91c6ea1269 [feat:] reordering courses if they are of the same sport and are not weighted equally 2026-06-06 14:24:25 +02:00
fierke c0da656331 [fix:] pdfsharp integration 2026-06-05 07:48:19 +02:00
fierke eea7b9f628 [fix:] sentence was too long 2026-06-04 19:15:03 +02:00
fierke a784e598de [chore:] forgot to 'git add' these... 2026-06-04 19:12:52 +02:00
fierke 66596b530b [feat:] im- and export (pdf/json) 2026-06-04 18:59:01 +02:00
fierke 5ef41b21b0 [gui:] buttons for im- and exporting 2026-06-04 18:58:32 +02:00
fierke c291ed1788 [file:] included fonts 2026-06-04 18:58:03 +02:00
fierke 78d25e6231 [fix:] Sport ids 2026-06-01 21:28:07 +02:00
fierke 73af2039ba [file:] added fonts 2026-06-01 21:27:29 +02:00
fierke c5be9d2c6e [feat:] frontend to edit courses (ai-slop) 2026-06-01 17:00:55 +02:00
fierke c46417c56d [chore:] single-semester-side limiting of courses 2026-06-01 17:00:39 +02:00
fierke 895c55a52f [chore:] cleaned up some rubbish 2026-06-01 13:14:03 +02:00
fierke e54e2e7840 [chore:] logo integration 2026-06-01 12:45:35 +02:00
fierke 0a710bea8b [chore:] window icon integration 2026-06-01 12:28:59 +02:00
fierke d4de543d71 [file:] added logo.ico 2026-06-01 12:25:33 +02:00
18 changed files with 1476 additions and 180 deletions
+77
View File
@@ -0,0 +1,77 @@
using System;
using System.IO;
using PdfSharp.Drawing;
using PdfSharp.Fonts;
namespace spplus;
/// <summary>
/// Custom font resolver for PdfSharp that loads fonts from the res/fonts directory.
/// </summary>
public class CustomFontResolver : IFontResolver
{
private static string? _fontPath;
public CustomFontResolver()
{
// Try to locate the fonts directory
var basePath = AppDomain.CurrentDomain.BaseDirectory;
var possiblePaths = new[]
{
Path.Combine(basePath, "res", "fonts"),
Path.Combine(basePath, "fonts"),
Path.Combine(Directory.GetCurrentDirectory(), "res", "fonts"),
Path.Combine(Directory.GetCurrentDirectory(), "fonts"),
};
foreach (var path in possiblePaths)
{
if (Directory.Exists(path))
{
_fontPath = path;
break;
}
}
if (_fontPath == null)
{
throw new DirectoryNotFoundException(
$"Font directory not found. Searched: {string.Join(", ", possiblePaths)}");
}
}
public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
{
// Map family name to font file
var fileName = familyName switch
{
"Cantarell" => isBold
? isItalic ? "Cantarell-BoldItalic.ttf" : "Cantarell-Bold.ttf"
: isItalic ? "Cantarell-Italic.ttf" : "Cantarell-Regular.ttf",
_ => isBold
? isItalic ? "Cantarell-BoldItalic.ttf" : "Cantarell-Bold.ttf"
: isItalic ? "Cantarell-Italic.ttf" : "Cantarell-Regular.ttf"
};
var fontPath = Path.Combine(_fontPath, fileName);
if (!File.Exists(fontPath))
{
throw new FileNotFoundException($"Font file not found: {fontPath}");
}
// Return a FontResolverInfo with the font path
return new FontResolverInfo(fontPath);
}
public byte[]? GetFont(string faceName)
{
// faceName is the path returned by ResolveTypeface
if (File.Exists(faceName))
{
return File.ReadAllBytes(faceName);
}
return null;
}
}
+56 -24
View File
@@ -3,7 +3,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="spplus.MainWindow" WindowState="Maximized"
x:Class="spplus.MainWindow" WindowState="Maximized" Icon="res/logo.ico"
Title="spplus">
<Border>
<Grid RowDefinitions="30,*">
@@ -196,8 +196,20 @@
</Grid>
<Line />
<Grid ColumnDefinitions="*,3*">
<Label Content="Maximale Sportkursanzahl pro Semester"></Label>
<NumericUpDown Grid.Column="1" x:Name="NudSportMaxPerSemester" ValueChanged="NudSportMaxPerSemester_OnValueChanged"></NumericUpDown>
<Label Content="Maximale Sportkursanzahl Semester 1"></Label>
<NumericUpDown Grid.Column="1" x:Name="NudSportMaxPerSemester1" ValueChanged="NudSportMaxPerSemester1_OnValueChanged"></NumericUpDown>
</Grid>
<Grid ColumnDefinitions="*,3*">
<Label Content="Maximale Sportkursanzahl Semester 2"></Label>
<NumericUpDown Grid.Column="1" x:Name="NudSportMaxPerSemester2" ValueChanged="NudSportMaxPerSemester2_OnValueChanged"></NumericUpDown>
</Grid>
<Grid ColumnDefinitions="*,3*">
<Label Content="Maximale Sportkursanzahl Semester 3"></Label>
<NumericUpDown Grid.Column="1" x:Name="NudSportMaxPerSemester3" ValueChanged="NudSportMaxPerSemester3_OnValueChanged"></NumericUpDown>
</Grid>
<Grid ColumnDefinitions="*,3*">
<Label Content="Maximale Sportkursanzahl Semester 4"></Label>
<NumericUpDown Grid.Column="1" x:Name="NudSportMaxPerSemester4" ValueChanged="NudSportMaxPerSemester4_OnValueChanged"></NumericUpDown>
</Grid>
</StackPanel>
</Grid>
@@ -210,27 +222,47 @@
<Label FontSize="20" Content="Ergebnisse" VerticalContentAlignment="Center" />
</StackPanel>
</TabItem.Header>
<Grid ColumnDefinitions="*,*" RowDefinitions="50,2*,*">
<ListBox Grid.RowSpan="2" x:Name="LbResult" Margin="0,10,10,10"></ListBox>
<Grid Grid.Row="0" Grid.Column="1" Grid.ColumnDefinitions="*,*">
<Button Grid.Column="0" Margin="0,10,0,0" x:Name="BtnExportCoursePDF" VerticalAlignment="Top" Height="35" HorizontalAlignment="Stretch" Click="BtnExportCoursePDF_OnClick" HorizontalContentAlignment="Center">
<StackPanel Orientation="Horizontal">
<LucideIcon Kind="FileText" Width="24" Height="24" />
<Label Content="Export (PDF)..." VerticalContentAlignment="Center" FontSize="12"
FontWeight="Bold" />
</StackPanel>
</Button>
<Button Grid.Column="1" Margin="5,10,0,0" x:Name="BtnExportCourseCSV" VerticalAlignment="Top" Height="35" HorizontalAlignment="Stretch" Click="BtnExportCourseCSV_OnClick" HorizontalContentAlignment="Center">
<StackPanel Orientation="Horizontal">
<LucideIcon Kind="FileJson" Width="24" Height="24" />
<Label Content="Export (CSV)..." VerticalContentAlignment="Center" FontSize="12"
FontWeight="Bold" />
</StackPanel>
</Button>
</Grid>
<ScrollViewer Grid.Row="1" Grid.Column="1" Margin="0,5,0,10" Background="#44CCCCCC">
<TextBlock x:Name="TbResultStatistics"></TextBlock>
</ScrollViewer>
<Grid ColumnDefinitions="*,*" RowDefinitions="100,2*,*">
<ListBox Grid.RowSpan="2" x:Name="LbResult" Margin="0,10,10,10" PointerPressed="LbResult_OnPointerPressed">
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem Header="Ändern" x:Name="MnuChange" />
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>
<Grid Grid.Row="0" Grid.Column="1" RowDefinitions="*,*" ColumnDefinitions="*,*">
<Button Grid.Row="0" Grid.Column="0" Margin="0,10,5,0" x:Name="BtnExportCoursePDF" VerticalAlignment="Top" Height="35" HorizontalAlignment="Stretch" Click="BtnExportCoursePDF_OnClick" HorizontalContentAlignment="Center">
<StackPanel Orientation="Horizontal">
<LucideIcon Kind="FileText" Width="24" Height="24" />
<Label Content="Export (PDF)..." VerticalContentAlignment="Center" FontSize="12"
FontWeight="Bold" />
</StackPanel>
</Button>
<Button Grid.Row="0" Grid.Column="1" Margin="5,10,0,0" x:Name="BtnExportCourseCSV" VerticalAlignment="Top" Height="35" HorizontalAlignment="Stretch" Click="BtnExportCourseCSV_OnClick" HorizontalContentAlignment="Center">
<StackPanel Orientation="Horizontal">
<LucideIcon Kind="FileJson" Width="24" Height="24" />
<Label Content="Export (CSV)..." VerticalContentAlignment="Center" FontSize="12"
FontWeight="Bold" />
</StackPanel>
</Button>
<Button Grid.Row="1" Grid.Column="0" Margin="0,5,5,0" x:Name="BtnExportConfiguration" VerticalAlignment="Top" Height="35" HorizontalAlignment="Stretch" Click="MnuExpSettings_OnClick" HorizontalContentAlignment="Center">
<StackPanel Orientation="Horizontal">
<LucideIcon Kind="FileJson" Width="24" Height="24" />
<Label Content="Konfiguration exportieren..." VerticalContentAlignment="Center" FontSize="12"
FontWeight="Bold" />
</StackPanel>
</Button>
<Button Grid.Row="1" Grid.Column="1" Margin="5,5,0,0" x:Name="BtnImportResults" VerticalAlignment="Top" Height="35" HorizontalAlignment="Stretch" Click="MnuImpResult_OnClick" HorizontalContentAlignment="Center">
<StackPanel Orientation="Horizontal">
<LucideIcon Kind="Import" Width="24" Height="24" />
<Label Content="Ergebnisse importieren..." VerticalContentAlignment="Center" FontSize="12"
FontWeight="Bold" />
</StackPanel>
</Button>
</Grid>
<ScrollViewer Grid.Row="1" Grid.Column="1" Margin="0,5,0,10" Background="#44CCCCCC">
<TextBlock x:Name="TbResultStatistics"></TextBlock>
</ScrollViewer>
<ScrollViewer Grid.Row="2" Grid.Column="1" Margin="0,0,0,10" Background="#44CCCCCC">
<TextBlock x:Name="TbResultTextout" FontFamily="Consolas"></TextBlock>
</ScrollViewer>
+311 -17
View File
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
@@ -12,6 +14,26 @@ public partial class MainWindow : Window
{
public static MainWindow Instance { get; set; }
public static string ApplicationVersion = "v1.2.24";
private sealed class ResultEntry
{
public Student Student { get; }
public int Semester { get; }
public string CourseName { get; }
public ResultEntry(Student student, int semester, string? courseName)
{
Student = student;
Semester = semester;
CourseName = courseName ?? string.Empty;
}
public override string ToString()
{
return $"{Student.Name} ({Student.ID}) - {Semester}. Semester: {CourseName}";
}
}
public MainWindow()
{
InitializeComponent();
@@ -20,13 +42,199 @@ public partial class MainWindow : Window
RefreshCoursesList();
try
{
NudSportMaxPerSemester.Value = Settings.Instance.NumCoursesPerSemester;
NudSportMaxPerSemester1.Value = Settings.Instance.NumCoursesPerSemester[0];
NudSportMaxPerSemester2.Value = Settings.Instance.NumCoursesPerSemester[1];
NudSportMaxPerSemester3.Value = Settings.Instance.NumCoursesPerSemester[2];
NudSportMaxPerSemester4.Value = Settings.Instance.NumCoursesPerSemester[3];
} catch {}
}
private void MnuExpSettings_OnClick(object? sender, RoutedEventArgs e)
private void RegenerateContextMenu()
{
var res = MessageBox.Show(this, "Dieses Feature ist noch nicht implementiert", "Fehlend");
MnuChange.Items.Clear();
foreach (var sport in Settings.Instance.Sports)
{
var item = new MenuItem { Header = sport.Name };
item.Click += (_, _) => ChangeStudentCourse(sport);
MnuChange.Items.Add(item);
}
}
private async void ChangeStudentCourse(Sport targetSport)
{
if (LbResult.SelectedItem is not ResultEntry selectedEntry)
return;
try
{
if (await ApplyStudentCourseChange(selectedEntry.Student, selectedEntry.Semester, targetSport))
{
CourseCrafter.ReloadResult();
RefreshResultView();
RegenerateContextMenu();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
}
}
private async Task<bool> ApplyStudentCourseChange(Student student, int semester, Sport targetSport)
{
if (semester < 1 || semester > 4)
return false;
var semesterCourses = CourseCrafter.GeneratedCourses
.Where(course => course.Semester == semester)
.ToList();
var currentCourses = semesterCourses
.Where(course => course.Instance.Students.Contains(student.ID))
.ToList();
string? oldSportName = currentCourses
.Select(course => course.Instance.Sport.Name)
.FirstOrDefault();
var existingTargetCourses = semesterCourses
.Where(course => course.Instance.Sport.Name == targetSport.Name)
.ToList();
CourseCrafter.CourseInstance? targetCourse = existingTargetCourses
.Where(course => !course.Instance.Students.Contains(student.ID) &&
course.Instance.Students.Count < course.Instance.Sport.MaxStudents)
.OrderBy(course => course.Instance.Students.Count)
.Select(course => course.Instance)
.FirstOrDefault();
if (targetCourse == null && existingTargetCourses.Any())
{
targetCourse = existingTargetCourses
.OrderBy(course => course.Instance.Students.Count)
.Select(course => course.Instance)
.FirstOrDefault();
}
bool changed = false;
foreach (var course in currentCourses)
{
if (targetCourse != null && ReferenceEquals(course.Instance, targetCourse))
continue;
if (course.Instance.Students.Remove(student.ID))
changed = true;
}
int removedEmptyCourses = CourseCrafter.GeneratedCourses.RemoveAll(course =>
course.Semester == semester &&
course.Instance.Students.Count == 0 &&
!ReferenceEquals(course.Instance, targetCourse));
if (removedEmptyCourses > 0)
changed = true;
if (targetCourse == null)
{
var newCourse = new CourseCrafter.CourseInstance
{
Sport = targetSport,
Students = new List<string> { student.ID }
};
CourseCrafter.GeneratedCourses.Add((semester, newCourse));
changed = true;
}
else if (!targetCourse.Students.Contains(student.ID))
{
targetCourse.Students.Add(student.ID);
changed = true;
}
if (changed && !string.IsNullOrEmpty(oldSportName) &&
IsRebalanceNeededForSportSemester(oldSportName, semester))
{
var result = await MessageBox.Show(this,
$"Der Kursplan für {oldSportName} im {semester}. Semester ist nach der Änderung unausgeglichen. Soll die alte Sportart umverteilt werden?",
"Umverteilung der alten Sportart", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
BalanceSportSemester(oldSportName, semester);
}
return changed;
}
private bool IsRebalanceNeededForSportSemester(string sportName, int semester)
{
var courses = CourseCrafter.GeneratedCourses
.Where(course => course.Semester == semester && course.Instance.Sport.Name == sportName)
.ToList();
if (courses.Count <= 1)
return false;
var ordered = courses
.OrderBy(course => course.Instance.Students.Count)
.ToList();
int minCount = ordered.First().Instance.Students.Count;
int maxCount = ordered.Last().Instance.Students.Count;
if (maxCount - minCount <= 1)
return false;
if (ordered.Last().Instance.Students.Count - 1 < GetEffectiveMinStudents(ordered.Last().Instance.Sport, semester))
return false;
if (ordered.First().Instance.Students.Count >= ordered.First().Instance.Sport.MaxStudents)
return false;
return true;
}
private void BalanceSportSemester(string sportName, int semester)
{
bool changed;
do
{
changed = false;
var courses = CourseCrafter.GeneratedCourses
.Where(course => course.Semester == semester && course.Instance.Sport.Name == sportName)
.OrderBy(course => course.Instance.Students.Count)
.ToList();
if (courses.Count <= 1)
break;
var target = courses.First();
var source = courses.Last();
if (source.Instance.Students.Count <= target.Instance.Students.Count + 1)
break;
if (source.Instance.Students.Count - 1 < GetEffectiveMinStudents(source.Instance.Sport, semester))
break;
if (target.Instance.Students.Count >= target.Instance.Sport.MaxStudents)
break;
var studentId = source.Instance.Students[^1];
source.Instance.Students.RemoveAt(source.Instance.Students.Count - 1);
target.Instance.Students.Add(studentId);
changed = true;
} while (changed);
}
private int GetEffectiveMinStudents(Sport sport, int semester)
{
int reduction = (semester >= 3) ? 2 : 0;
return Math.Max(1, sport.MinStudents - reduction);
}
private async void MnuExpSettings_OnClick(object? sender, RoutedEventArgs e)
{
await ExportConfigurationAsync();
}
private void MnuExit_OnClick(object? sender, RoutedEventArgs e)
@@ -159,6 +367,21 @@ public partial class MainWindow : Window
//TbiResults.Focus();
}
private void LbResult_OnPointerPressed(object? sender, PointerPressedEventArgs e)
{
if (!e.GetCurrentPoint(LbResult).Properties.IsRightButtonPressed)
return;
if (e.Source is Control control && control.DataContext is ResultEntry entry)
{
LbResult.SelectedItem = entry;
}
else
{
LbResult.SelectedItem = null;
}
}
private void RefreshResultView()
{
LbResult.Items.Clear();
@@ -168,7 +391,7 @@ public partial class MainWindow : Window
{
for(int i = 0; i<s.Result.Length;i++)
{
LbResult.Items.Add($"{s.Name} ({s.ID}) - {i+1}. Semester: {s.Result[i]}");
LbResult.Items.Add(new ResultEntry(s, i + 1, s.Result[i]));
}
}
catch (Exception ex)
@@ -262,6 +485,7 @@ public partial class MainWindow : Window
{
LbSportCourses.Items.Add(sp);
}
RegenerateContextMenu();
}
@@ -298,6 +522,7 @@ public partial class MainWindow : Window
try
{
((Sport)LbSportCourses.SelectedItem).Name = TbSportName.Text;
RegenerateContextMenu();
} catch {}
}
@@ -411,16 +636,53 @@ public partial class MainWindow : Window
} catch (Exception ex){}
}
private void BtnExportCoursePDF_OnClick(object? sender, RoutedEventArgs e)
private async void BtnExportCoursePDF_OnClick(object? sender, RoutedEventArgs e)
{
// Export as PDF
var topLevel = GetTopLevel(this);
var file = await topLevel!.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
{
Title = "PDF-Datei speichern",
SuggestedFileName = "spplus_kurse.pdf",
SuggestedFileType = new FilePickerFileType(".pdf-Datei")
{
Patterns = new[] { "*.pdf" }
}
});
if (file == null) return;
PdfExportUtility.ExportGeneratedCourses(file.Path.LocalPath);
}
private void NudSportMaxPerSemester_OnValueChanged(object? sender, NumericUpDownValueChangedEventArgs e)
private void NudSportMaxPerSemester1_OnValueChanged(object? sender, NumericUpDownValueChangedEventArgs e)
{
try
{
Settings.Instance.NumCoursesPerSemester = Convert.ToInt32(NudSportMaxPerSemester.Value);
Settings.Instance.NumCoursesPerSemester[0] = Convert.ToInt32(NudSportMaxPerSemester1.Value);
} catch {}
}
private void NudSportMaxPerSemester2_OnValueChanged(object? sender, NumericUpDownValueChangedEventArgs e)
{
try
{
Settings.Instance.NumCoursesPerSemester[1] = Convert.ToInt32(NudSportMaxPerSemester2.Value);
} catch {}
}
private void NudSportMaxPerSemester3_OnValueChanged(object? sender, NumericUpDownValueChangedEventArgs e)
{
try
{
Settings.Instance.NumCoursesPerSemester[2] = Convert.ToInt32(NudSportMaxPerSemester3.Value);
} catch {}
}
private void NudSportMaxPerSemester4_OnValueChanged(object? sender, NumericUpDownValueChangedEventArgs e)
{
try
{
Settings.Instance.NumCoursesPerSemester[3] = Convert.ToInt32(NudSportMaxPerSemester4.Value);
} catch {}
}
@@ -430,33 +692,65 @@ public partial class MainWindow : Window
var file = await topLevel!.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
{
Title = "CSV-Datei speichern",
SuggestedFileType = new FilePickerFileType(".csv-Datei")
SuggestedFileName = "spplus_ergebnisse.json",
SuggestedFileType = new FilePickerFileType(".json-Datei")
{
Patterns = new[] { "*.csv" }
Patterns = new[] { "*.json" }
}
});
if (file == null) return;
ExportUtility.ExportToCSV(file.Path.AbsolutePath);
ExportUtility.ExportResultsToJson(file.Path.LocalPath);
}
private async void MnuImpResult_OnClick(object? sender, RoutedEventArgs e)
{
// Hier importieren
var topLevel = GetTopLevel(this);
var file = await topLevel!.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
{
Title = "CSV-Datei laden",
SuggestedFileType = new FilePickerFileType(".csv-Datei")
Title = "Ergebnis-CSV laden",
AllowMultiple = false,
FileTypeFilter = new[]
{
Patterns = new[] { "*.csv" }
new FilePickerFileType(".json-Datei")
{
Patterns = new[] { "*.json" }
}
}
});
if (file == null || file.Count == 0) return;
var imported = import.ImportResultFromJson(file[0].Path.LocalPath.ToString());
if (imported != null && imported.Count > 0)
{
CourseCrafter.GeneratedCourses = imported
.OrderBy(c => c.Semester)
.ThenBy(c => c.Instance.Sport.Name)
.ToList();
CourseCrafter.ReloadResult();
RefreshResultView();
}
}
private async System.Threading.Tasks.Task ExportConfigurationAsync()
{
var topLevel = GetTopLevel(this);
var file = await topLevel!.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
{
Title = "Konfiguration speichern",
SuggestedFileName = "spplus_konfiguration.json",
SuggestedFileType = new FilePickerFileType(".json-Datei")
{
Patterns = new[] { "*.json" }
}
});
if (file == null) return;
ExportUtility.ExportConfigurationToJson(file.Path.LocalPath);
}
}
}
+1 -1
View File
@@ -2,7 +2,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" SizeToContent="WidthAndHeight"
mc:Ignorable="d" SizeToContent="WidthAndHeight" Icon="res/logo.ico"
x:Class="spplus.MessageBox" WindowStartupLocation="CenterScreen"
Title="MessageBox">
<StackPanel>
+621
View File
@@ -0,0 +1,621 @@
using System;
using System.Collections.Generic;
using System.Linq;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
namespace spplus;
public static class PdfExportUtility
{
private const string FontFamily = "Cantarell";
private const double Margin = 36.0;
private const double FooterHeight = 28.0;
private static readonly XBrush FooterBrush = XBrushes.Gray;
public static void ExportGeneratedCourses(string filepath)
{
var document = new PdfDocument
{
Info =
{
Title = "spplus - generierte Kurse",
Author = "spplus",
Creator = "spplus"
}
};
var courses = CourseCrafter.GeneratedCourses
.OrderBy(course => course.Semester)
.ThenBy(course => course.Instance.Sport.Name)
.ToList();
if (courses.Count == 0)
{
RenderEmptyDocument(document);
document.Save(filepath);
return;
}
RenderCoursesOverview(document, courses);
var studentsById = Settings.Instance.Students
.GroupBy(student => student.ID, StringComparer.OrdinalIgnoreCase)
.ToDictionary(group => group.Key, group => group.First(), StringComparer.OrdinalIgnoreCase);
foreach (var course in courses)
{
RenderCourse(document, course, studentsById);
}
RenderMissingStudentsReport(document, studentsById, courses);
document.Save(filepath);
}
private static void RenderMissingStudentsReport(
PdfDocument document,
IReadOnlyDictionary<string, Student> studentsById,
IReadOnlyList<(int Semester, CourseCrafter.CourseInstance Instance)> courses)
{
var assignedSemesters = studentsById.Values
.ToDictionary(student => student.ID, _ => new bool[4], StringComparer.OrdinalIgnoreCase);
foreach (var course in courses)
{
if (course.Semester < 1 || course.Semester > 4)
continue;
foreach (var studentId in course.Instance.Students)
{
if (assignedSemesters.TryGetValue(studentId, out var assignment))
assignment[course.Semester - 1] = true;
}
}
var missingStudents = studentsById.Values
.Where(student => assignedSemesters.TryGetValue(student.ID, out var assignment) && assignment.Any(a => !a))
.OrderBy(student => student.Name)
.ThenBy(student => student.ID)
.ToList();
var columns = new double[] { 30.0, 90.0, 190.0, 40.0, 40.0, 40.0, 40.0 };
var headerRow = new[] { "Nr.", "ID", "Name", "Sem1", "Sem2", "Sem3", "Sem4" };
int rowIndex = 0;
int pageIndex = 0;
while (rowIndex < missingStudents.Count || pageIndex == 0)
{
var page = document.AddPage();
ConfigurePage(page);
using var gfx = XGraphics.FromPdfPage(page);
var titleFont = new XFont(FontFamily, 18, XFontStyleEx.Bold);
var subtitleFont = new XFont(FontFamily, 10, XFontStyleEx.Regular);
var tableHeaderFont = new XFont(FontFamily, 9, XFontStyleEx.Bold);
var tableBodyFont = new XFont(FontFamily, 8.5, XFontStyleEx.Regular);
double pageWidth = page.Width.Point;
double pageHeight = page.Height.Point;
double contentWidth = pageWidth - (Margin * 2);
double contentBottom = pageHeight - Margin - FooterHeight;
double y = Margin;
var headerTitle = pageIndex == 0
? "Nicht zugeordnete Schülerinnen und Schüler"
: "Fortsetzung: Nicht zugeordnete Schülerinnen und Schüler";
gfx.DrawString(headerTitle, titleFont, XBrushes.Black,
new XRect(Margin, y, contentWidth, 24), XStringFormats.TopLeft);
y += 26;
if (pageIndex == 0)
{
gfx.DrawString("Es werden nur Schülerinnen und Schüler aufgeführt, die in mindestens einem Semester keine Zuordnung erhalten haben.",
subtitleFont, XBrushes.Gray, new XRect(Margin, y, contentWidth, 20), XStringFormats.TopLeft);
y += 24;
}
if (missingStudents.Count == 0)
{
var bodyFont = new XFont(FontFamily, 11, XFontStyleEx.Regular);
gfx.DrawString("Alle Schülerinnen und Schüler sind in allen vier Semestern zugeordnet.", bodyFont, XBrushes.Black,
new XRect(Margin, y, contentWidth, 20), XStringFormats.TopLeft);
DrawFooter(gfx, page);
break;
}
y += DrawTableRow(gfx, y, columns, headerRow, tableHeaderFont, fillHeader: true);
while (rowIndex < missingStudents.Count)
{
var student = missingStudents[rowIndex];
var assignment = assignedSemesters[student.ID];
var row = BuildMissingStudentRow(rowIndex + 1, student, assignment);
double rowHeight = MeasureRowHeight(gfx, columns, row, tableBodyFont);
if (y + rowHeight > contentBottom)
break;
y += DrawTableRow(gfx, y, columns, row, tableBodyFont, fillHeader: false);
rowIndex++;
}
DrawFooter(gfx, page);
pageIndex++;
if (rowIndex >= missingStudents.Count)
break;
}
}
private static string[] BuildMissingStudentRow(int rowNumber, Student student, bool[] assignment)
{
return new[]
{
rowNumber.ToString(),
student.ID,
student.Name,
assignment[0] ? string.Empty : "X",
assignment[1] ? string.Empty : "X",
assignment[2] ? string.Empty : "X",
assignment[3] ? string.Empty : "X",
};
}
private static void RenderEmptyDocument(PdfDocument document)
{
var page = document.AddPage();
ConfigurePage(page);
using var gfx = XGraphics.FromPdfPage(page);
var titleFont = new XFont(FontFamily, 18, XFontStyleEx.Bold);
var bodyFont = new XFont(FontFamily, 11, XFontStyleEx.Regular);
gfx.DrawString("spplus", titleFont, XBrushes.Black,
new XRect(Margin, Margin, page.Width.Point - Margin * 2, 24), XStringFormats.TopLeft);
gfx.DrawString("Es wurden keine generierten Kurse gefunden.", bodyFont, XBrushes.Black,
new XRect(Margin, Margin + 34, page.Width.Point - Margin * 2, 24), XStringFormats.TopLeft);
DrawFooter(gfx, page);
}
private static void RenderCoursesOverview(
PdfDocument document,
IReadOnlyList<(int Semester, CourseCrafter.CourseInstance Instance)> courses)
{
var overviewFont = new XFont(FontFamily, 18, XFontStyleEx.Bold);
var subtitleFont = new XFont(FontFamily, 10, XFontStyleEx.Regular);
var tableHeaderFont = new XFont(FontFamily, 9, XFontStyleEx.Bold);
var tableBodyFont = new XFont(FontFamily, 8.5, XFontStyleEx.Regular);
int count = 0;
var coursesPerSemester = courses
.GroupBy(course => course.Semester)
.ToDictionary(group => group.Key, group => group.Count());
var courseIndexBySemester = new Dictionary<int, int>();
var rows = courses.Select(course =>
{
count++;
var key = course.Semester;
courseIndexBySemester.TryGetValue(key, out var index);
index++;
courseIndexBySemester[key] = index;
return new[]
{
count.ToString(),
course.Semester.ToString(),
course.Instance.Sport.Name,
$"{index}/{coursesPerSemester[key]}",
course.Instance.Students.Count.ToString()
};
}).ToList();
var columns = new[] { 50.0, 70.0, 150.0, 150.0, 100.0 };
var headerRow = new[]
{
"Nr.",
"Semester",
"Sportart",
"Kurs (Semester)",
"Anzahl SuS"
};
int rowIndex = 0;
int pageIndex = 0;
while (rowIndex < rows.Count || pageIndex == 0)
{
var page = document.AddPage();
ConfigurePage(page);
using var gfx = XGraphics.FromPdfPage(page);
double pageWidth = page.Width.Point;
double pageHeight = page.Height.Point;
double contentWidth = pageWidth - (Margin * 2);
double contentBottom = pageHeight - Margin - FooterHeight;
double y = Margin;
var headerTitle = pageIndex == 0
? "Übersicht generierter Kurse"
: "Fortsetzung: Übersicht generierter Kurse";
gfx.DrawString(headerTitle, overviewFont, XBrushes.Black,
new XRect(Margin, y, contentWidth, 24), XStringFormats.TopLeft);
y += 26;
if (pageIndex == 0)
{
gfx.DrawString("Tabellarische Übersicht aller generierten Kurse nach Semester und Sportart.",
subtitleFont, XBrushes.Gray, new XRect(Margin, y, contentWidth, 20), XStringFormats.TopLeft);
y += 24;
}
y += DrawTableRow(gfx, y, columns, headerRow, tableHeaderFont, fillHeader: true);
while (rowIndex < rows.Count)
{
var row = rows[rowIndex];
double rowHeight = MeasureRowHeight(gfx, columns, row, tableBodyFont);
if (y + rowHeight > contentBottom)
break;
y += DrawTableRow(gfx, y, columns, row, tableBodyFont, fillHeader: false);
rowIndex++;
}
DrawFooter(gfx, page);
pageIndex++;
if (rowIndex >= rows.Count)
break;
}
}
private static void RenderCourse(
PdfDocument document,
(int Semester, CourseCrafter.CourseInstance Instance) course,
IReadOnlyDictionary<string, Student> studentsById)
{
var studentIds = course.Instance.Students.ToList();
int index = 0;
int pageIndex = 0;
do
{
var page = document.AddPage();
ConfigurePage(page);
using var gfx = XGraphics.FromPdfPage(page);
var titleFont = new XFont(FontFamily, 18, XFontStyleEx.Bold);
var subtitleFont = new XFont(FontFamily, 10, XFontStyleEx.Regular);
var labelFont = new XFont(FontFamily, 9, XFontStyleEx.Bold);
var valueFont = new XFont(FontFamily, 9, XFontStyleEx.Regular);
var tableHeaderFont = new XFont(FontFamily, 9, XFontStyleEx.Bold);
var tableBodyFont = new XFont(FontFamily, 8.5, XFontStyleEx.Regular);
double pageWidth = page.Width.Point;
double pageHeight = page.Height.Point;
double contentWidth = pageWidth - (Margin * 2);
double contentBottom = pageHeight - Margin - FooterHeight;
double y = Margin;
var headerTitle = $"Sem. {course.Semester} - {course.Instance.Sport.Name}";
if (pageIndex > 0)
headerTitle += " (Fortsetzung)";
gfx.DrawString(headerTitle, titleFont, XBrushes.Black,
new XRect(Margin, y, contentWidth, 24), XStringFormats.TopLeft);
y += 26;
gfx.DrawString($"{course.Instance.Students.Count} Schülerinnen und Schüler", subtitleFont,
XBrushes.Gray, new XRect(Margin, y, contentWidth, 18), XStringFormats.TopLeft);
y += 24;
y = DrawInfoRows(gfx, y, contentWidth, labelFont, valueFont, course);
y += 10;
var columns = GetTableColumns(contentWidth);
var headerRow = new[]
{
"Nr.",
"ID",
"Name",
};
y += DrawTableRow(gfx, y, columns, headerRow, tableHeaderFont, fillHeader: true);
if (studentIds.Count == 0)
{
var emptyFont = new XFont(FontFamily, 9, XFontStyleEx.Italic);
gfx.DrawString("Keine Schülerinnen und Schüler zugeordnet.", emptyFont, XBrushes.Gray,
new XRect(Margin + 6, y + 8, contentWidth - 12, 20), XStringFormats.TopLeft);
}
else
{
while (index < studentIds.Count)
{
if (!studentsById.TryGetValue(studentIds[index], out var student))
student = new Student { ID = studentIds[index] };
var row = BuildStudentRow(index + 1, student);
double rowHeight = MeasureRowHeight(gfx, columns, row, tableBodyFont);
if (y + rowHeight > contentBottom)
break;
y += DrawTableRow(gfx, y, columns, row, tableBodyFont, fillHeader: false);
index++;
}
}
DrawFooter(gfx, page);
pageIndex++;
} while (index < studentIds.Count);
}
private static double DrawInfoRows(
XGraphics gfx,
double startY,
double contentWidth,
XFont labelFont,
XFont valueFont,
(int Semester, CourseCrafter.CourseInstance Instance) course)
{
double y = startY;
double labelWidth = 160.0;
double valueWidth = contentWidth - labelWidth;
double rowPadding = 4.0;
var rows = new List<(string Label, string Value)>
{
("Kurs-ID", course.Instance.Sport.ID.ToString()),
("Kursname", course.Instance.Sport.Name),
("Semester", course.Semester.ToString()),
("Anzahl SuS", course.Instance.Students.Count.ToString()),
("Min / Max", $"{course.Instance.Sport.MinStudents} / {course.Instance.Sport.MaxStudents}"),
("Semesterangebote", string.Join(", ", course.Instance.Sport.Semester.Select((count, idx) => $"S{idx + 1}:{count}"))),
("Alternativnamen", course.Instance.Sport.AlternativeNames.Count == 0
? "-"
: string.Join(", ", course.Instance.Sport.AlternativeNames))
};
foreach (var row in rows)
{
double rowHeight = MeasureInfoRowHeight(gfx, labelFont, valueFont, labelWidth, valueWidth, row.Label, row.Value);
gfx.DrawRectangle(XPens.LightGray, Margin, y, labelWidth, rowHeight);
gfx.DrawRectangle(XPens.LightGray, Margin + labelWidth, y, valueWidth, rowHeight);
DrawWrappedText(gfx, row.Label, labelFont, XBrushes.Black, Margin + 4, y + rowPadding, labelWidth - 8);
DrawWrappedText(gfx, row.Value, valueFont, XBrushes.Black, Margin + labelWidth + 4, y + rowPadding, valueWidth - 8);
y += rowHeight;
}
return y;
}
private static double MeasureInfoRowHeight(
XGraphics gfx,
XFont labelFont,
XFont valueFont,
double labelWidth,
double valueWidth,
string label,
string value)
{
double labelHeight = MeasureWrappedTextHeight(gfx, label, labelFont, labelWidth - 8);
double valueHeight = MeasureWrappedTextHeight(gfx, value, valueFont, valueWidth - 8);
return Math.Max(labelHeight, valueHeight) + 8.0;
}
private static string[] BuildStudentRow(int rowNumber, Student student)
{
return new[]
{
rowNumber.ToString(),
student.ID,
student.Name,
};
}
private static string GetCourseName(Student student, int index)
{
if (index < 0 || index >= student.SelectedCourseNames.Count)
return string.Empty;
return student.SelectedCourseNames[index];
}
private static double[] GetTableColumns(double contentWidth)
{
return new[]
{
30.0,
75.0,
145.0
};
}
private static double DrawTableRow(
XGraphics gfx,
double startY,
double[] columns,
IReadOnlyList<string> values,
XFont font,
bool fillHeader)
{
const double padding = 4.0;
double rowHeight = MeasureRowHeight(gfx, columns, values, font);
double x = Margin;
for (int i = 0; i < columns.Length; i++)
{
double width = columns[i];
var rect = new XRect(x, startY, width, rowHeight);
gfx.DrawRectangle(XPens.LightGray, rect);
if (fillHeader)
gfx.DrawRectangle(new XSolidBrush(XColor.FromArgb(235, 235, 235)), rect);
gfx.DrawRectangle(XPens.LightGray, rect);
var lines = WrapText(gfx, values[i], font, width - (padding * 2));
double lineHeight = font.GetHeight();
double textY = startY + padding;
foreach (var line in lines)
{
gfx.DrawString(line, font, XBrushes.Black,
new XRect(x + padding, textY, width - (padding * 2), lineHeight), XStringFormats.TopLeft);
textY += lineHeight;
}
x += width;
}
return rowHeight;
}
private static double MeasureRowHeight(
XGraphics gfx,
double[] columns,
IReadOnlyList<string> values,
XFont font)
{
const double padding = 4.0;
double maxHeight = font.GetHeight() + (padding * 2);
for (int i = 0; i < columns.Length; i++)
{
double height = MeasureWrappedTextHeight(gfx, values[i], font, columns[i] - (padding * 2)) + (padding * 2);
if (height > maxHeight)
maxHeight = height;
}
return maxHeight;
}
private static double MeasureWrappedTextHeight(XGraphics gfx, string text, XFont font, double width)
{
var lines = WrapText(gfx, text, font, width);
return lines.Count * font.GetHeight();
}
private static List<string> WrapText(XGraphics gfx, string? text, XFont font, double maxWidth)
{
var result = new List<string>();
if (string.IsNullOrWhiteSpace(text))
{
result.Add(string.Empty);
return result;
}
foreach (var paragraph in text.Replace("\r", string.Empty).Split('\n'))
{
if (string.IsNullOrWhiteSpace(paragraph))
{
result.Add(string.Empty);
continue;
}
string current = string.Empty;
foreach (var token in paragraph.Split(' ', StringSplitOptions.RemoveEmptyEntries))
{
foreach (var wordPart in SplitWordIfNeeded(gfx, token, font, maxWidth))
{
var candidate = string.IsNullOrEmpty(current) ? wordPart : $"{current} {wordPart}";
if (gfx.MeasureString(candidate, font).Width <= maxWidth)
{
current = candidate;
}
else
{
if (!string.IsNullOrEmpty(current))
result.Add(current);
current = wordPart;
}
}
}
if (!string.IsNullOrEmpty(current))
result.Add(current);
}
return result.Count == 0 ? new List<string> { string.Empty } : result;
}
private static IEnumerable<string> SplitWordIfNeeded(XGraphics gfx, string word, XFont font, double maxWidth)
{
if (gfx.MeasureString(word, font).Width <= maxWidth)
{
yield return word;
yield break;
}
string chunk = string.Empty;
foreach (char c in word)
{
string candidate = chunk + c;
if (chunk.Length > 0 && gfx.MeasureString(candidate, font).Width > maxWidth)
{
yield return chunk;
chunk = c.ToString();
}
else
{
chunk = candidate;
}
}
if (!string.IsNullOrEmpty(chunk))
yield return chunk;
}
private static void DrawWrappedText(
XGraphics gfx,
string? text,
XFont font,
XBrush brush,
double x,
double y,
double width)
{
double lineHeight = font.GetHeight();
foreach (var line in WrapText(gfx, text, font, width))
{
gfx.DrawString(line, font, brush, new XRect(x, y, width, lineHeight), XStringFormats.TopLeft);
y += lineHeight;
}
}
private static void DrawFooter(XGraphics gfx, PdfPage page)
{
var footerFont = new XFont(FontFamily, 7, XFontStyleEx.Regular);
double width = page.Width.Point - (Margin * 2);
double x = Margin;
double baseY = page.Height.Point - Margin + 2;
gfx.DrawString("generated by spplus", footerFont, FooterBrush,
new XRect(x, baseY, width, 9), XStringFormats.Center);
gfx.DrawString("(c) 2026 MyPapertown", footerFont, FooterBrush,
new XRect(x, baseY + 8, width, 9), XStringFormats.Center);
gfx.DrawString("www.mypapercloud.de/spplus", footerFont, FooterBrush,
new XRect(x, baseY + 16, width, 9), XStringFormats.Center);
}
private static void ConfigurePage(PdfPage page)
{
page.Size = PageSize.A4;
page.Orientation = PageOrientation.Portrait;
}
}
+10 -2
View File
@@ -1,5 +1,7 @@
using Avalonia;
using System;
using PdfSharp;
using PdfSharp.Fonts;
namespace spplus;
@@ -9,8 +11,14 @@ class Program
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
[STAThread]
public static void Main(string[] args) => BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);
public static void Main(string[] args)
{
// Initialize PdfSharp font resolver before any PDF operations
GlobalFontSettings.FontResolver = new CustomFontResolver();
BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);
}
// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
+225 -70
View File
@@ -15,6 +15,66 @@ public class CourseCrafter
public static List<(int Semester, CourseInstance Instance)> GeneratedCourses
= new();
private static Sport? ResolveSportFromCourseName(string courseName)
{
if (string.IsNullOrWhiteSpace(courseName) ||
string.Equals(courseName, "null", StringComparison.OrdinalIgnoreCase))
{
return null;
}
return Settings.Instance.Sports.FirstOrDefault(sport =>
string.Equals(sport.Name, courseName, StringComparison.OrdinalIgnoreCase) ||
sport.AlternativeNames.Any(alt => string.Equals(alt, courseName, StringComparison.OrdinalIgnoreCase)));
}
public static void RebuildGeneratedCoursesFromResults(IEnumerable<Student> importedResults)
{
var importedById = importedResults
.Where(student => !string.IsNullOrWhiteSpace(student.ID))
.GroupBy(student => student.ID, StringComparer.OrdinalIgnoreCase)
.ToDictionary(group => group.Key, group => group.First(), StringComparer.OrdinalIgnoreCase);
var generatedCourses = new Dictionary<(int Semester, string SportName), CourseInstance>();
foreach (var student in Settings.Instance.Students)
{
if (!importedById.TryGetValue(student.ID, out var importedStudent))
continue;
for (int semesterIndex = 0; semesterIndex < 4; semesterIndex++)
{
if (importedStudent.Result.Length <= semesterIndex)
continue;
var resultName = importedStudent.Result[semesterIndex];
var sport = ResolveSportFromCourseName(resultName);
if (sport == null)
continue;
var key = (semesterIndex + 1, sport.Name);
if (!generatedCourses.TryGetValue(key, out var course))
{
course = new CourseInstance
{
Sport = sport,
Students = new List<string>()
};
generatedCourses[key] = course;
}
if (!course.Students.Contains(student.ID))
course.Students.Add(student.ID);
}
}
GeneratedCourses = generatedCourses
.Select(entry => (Semester: entry.Key.Semester, Instance: entry.Value))
.OrderBy(course => course.Semester)
.ThenBy(course => course.Instance.Sport.Name)
.ToList();
}
public static void Craft()
{
GeneratedCourses = new();
@@ -148,7 +208,7 @@ public class CourseCrafter
for (int semester = 1; semester <= 4; semester++)
{
int cancel = 0;
while (GeneratedCourses.Count(c => c.Semester == semester) < Settings.Instance.NumCoursesPerSemester)
while (GeneratedCourses.Count(c => c.Semester == semester) < Settings.Instance.NumCoursesPerSemester[semester-1])
{
cancel++;
if (cancel >= 20) break;
@@ -323,6 +383,8 @@ public class CourseCrafter
}
}
BalanceCoursesBetweenSameSportAndSemester();
// ---------------------------------------------------------------------------
// Lokale Hilfsfunktionen
// ---------------------------------------------------------------------------
@@ -346,10 +408,60 @@ public class CourseCrafter
return true;
}
void BalanceCoursesBetweenSameSportAndSemester()
{
bool changed;
do
{
changed = false;
var groups = GeneratedCourses
.GroupBy(c => (c.Semester, c.Instance.Sport.Name))
.Where(g => g.Count() > 1);
foreach (var group in groups)
{
var courses = group
.OrderBy(c => c.Instance.Students.Count)
.ToList();
for (int sourceIndex = courses.Count - 1; sourceIndex > 0; sourceIndex--)
{
var sourceCourse = courses[sourceIndex];
for (int targetIndex = 0; targetIndex < sourceIndex; targetIndex++)
{
var targetCourse = courses[targetIndex];
if (sourceCourse.Instance.Students.Count <= targetCourse.Instance.Students.Count + 1)
break;
if (targetCourse.Instance.Students.Count >= targetCourse.Instance.Sport.MaxStudents)
continue;
if (sourceCourse.Instance.Students.Count - 1 < getEffectiveMinStudents(sourceCourse.Instance.Sport, sourceCourse.Semester))
continue;
var studentId = sourceCourse.Instance.Students[^1];
sourceCourse.Instance.Students.RemoveAt(sourceCourse.Instance.Students.Count - 1);
targetCourse.Instance.Students.Add(studentId);
changed = true;
break;
}
if (changed)
break;
}
if (changed)
break;
}
} while (changed);
}
bool requestExit()
{
globalCount++;
if (GeneratedCourses.Count >= Settings.Instance.NumCoursesPerSemester * 4) return true;
int sum = Settings.Instance.NumCoursesPerSemester[0] + Settings.Instance.NumCoursesPerSemester[1] +
Settings.Instance.NumCoursesPerSemester[2] + Settings.Instance.NumCoursesPerSemester[3];
if (GeneratedCourses.Count >= sum) return true;
int low = 0;
foreach (var item in initial_sportlist)
@@ -440,7 +552,7 @@ public class CourseCrafter
if (sportCoursesInSemester >= sport.Semester[semesterIndex])
continue;
if (GeneratedCourses.Count(c => c.Semester == semester) >= Settings.Instance.NumCoursesPerSemester)
if (GeneratedCourses.Count(c => c.Semester == semester) >= Settings.Instance.NumCoursesPerSemester[semester-1])
continue;
var directlyPlaceable = remainingStudents
@@ -632,6 +744,87 @@ public class CourseCrafter
return true;
}
bool EnsureFirstSemesterCoverage()
{
bool changed = false;
foreach (var student in Settings.Instance.Students)
{
if (student.SelectedCourseNames.Count == 0)
continue;
var assignments = GetAssignmentsBySemester(student.ID);
if (assignments[0] != null)
continue;
if (TryAssignStudentToFirstSemester(student))
changed = true;
}
return changed;
}
bool TryAssignStudentToFirstSemester(Student student)
{
var desiredSports = student.SelectedCourseNames
.Select(ResolveSportFromSelection)
.Where(sp => sp != null && sp.Semester[0] != 0)
.DistinctBy(sp => sp!.Name)
.Select(sp => sp!)
.ToList();
foreach (var sport in desiredSports)
{
// Direkt in bestehendes Kursangebot eintragen
var existingCourse = GeneratedCourses
.FirstOrDefault(c => c.Semester == 1 && c.Instance.Sport.Name == sport.Name &&
c.Instance.Students.Count < c.Instance.Sport.MaxStudents);
if (existingCourse.Instance != null)
{
existingCourse.Instance.Students.Add(student.ID);
students_in_semester[0].Add(student.ID);
return true;
}
// Versuche einen Teilnehmer aus einem bestehenden Sem.1-Kurs umzudisponieren
var firstSemesterCourses = GeneratedCourses
.Where(c => c.Semester == 1 && c.Instance.Sport.Name == sport.Name)
.ToList();
foreach (var course in firstSemesterCourses)
{
if (course.Instance.Students.Count <= getEffectiveMinStudents(sport, 1))
continue;
foreach (var otherStudent in course.Instance.Students.ToList())
{
if (otherStudent == student.ID)
continue;
var targetCourse = GeneratedCourses
.FirstOrDefault(c => c.Semester != 1 && c.Instance.Sport.Name == sport.Name &&
c.Instance.Students.Count < c.Instance.Sport.MaxStudents &&
!students_in_semester[c.Semester - 1].Contains(otherStudent));
if (targetCourse.Instance == null)
continue;
course.Instance.Students.Remove(otherStudent);
students_in_semester[0].Remove(otherStudent);
targetCourse.Instance.Students.Add(otherStudent);
students_in_semester[targetCourse.Semester - 1].Add(otherStudent);
course.Instance.Students.Add(student.ID);
students_in_semester[0].Add(student.ID);
return true;
}
}
}
return false;
}
(int Semester, CourseInstance Instance)?[] GetAssignmentsBySemester(string studentId)
{
var assignments = new (int Semester, CourseInstance Instance)?[4];
@@ -649,52 +842,7 @@ public class CourseCrafter
Sport? ResolveSportFromSelection(string selectedCourseName)
{
if (string.IsNullOrWhiteSpace(selectedCourseName) ||
string.Equals(selectedCourseName, "null", StringComparison.OrdinalIgnoreCase))
{
return null;
}
return Settings.Instance.Sports
.FirstOrDefault(sport => sport.AlternativeNames.Contains(selectedCourseName));
}
int getSemesterForSport2(Sport sp, List<string> interestedStudents)
{
int[] semcount = new int[4];
foreach (var inst in GeneratedCourses)
semcount[inst.Semester - 1]++;
int bestSem = 0;
int minCourses = int.MaxValue;
for (int i = 0; i < 4; i++)
{
if (sp.Semester[i] == 0) continue;
int sportCoursesInSemester = GeneratedCourses
.Count(g => g.Semester == i + 1 && g.Instance.Sport.Name == sp.Name);
if (sportCoursesInSemester >= sp.Semester[i])
continue;
int freeInterestedStudents = interestedStudents
.Distinct()
.Count(studentId => !students_in_semester[i].Contains(studentId));
if (freeInterestedStudents < getEffectiveMinStudents(sp, i + 1))
continue;
if (semcount[i] < Settings.Instance.NumCoursesPerSemester &&
semcount[i] < minCourses)
{
minCourses = semcount[i];
bestSem = i + 1;
}
}
return bestSem;
return ResolveSportFromCourseName(selectedCourseName);
}
int getSemesterForSport(Sport sp, List<string> interestedStudents)
@@ -721,7 +869,7 @@ public class CourseCrafter
if (sportCoursesInThisSemester >= sp.Semester[i])
continue;
if (totalCoursesPerSemester[i] >= Settings.Instance.NumCoursesPerSemester)
if (totalCoursesPerSemester[i] >= Settings.Instance.NumCoursesPerSemester[i])
continue;
int freeInterestedStudents = interestedStudents
@@ -733,7 +881,8 @@ public class CourseCrafter
if (freeInterestedStudents > maxFreeInterestedStudents ||
(freeInterestedStudents == maxFreeInterestedStudents &&
totalCoursesPerSemester[i] < minCourses))
(totalCoursesPerSemester[i] < minCourses ||
(totalCoursesPerSemester[i] == minCourses && semesterNumber < bestSem))))
{
maxFreeInterestedStudents = freeInterestedStudents;
minCourses = totalCoursesPerSemester[i];
@@ -744,35 +893,41 @@ public class CourseCrafter
return bestSem;
}
var errors = ValidateCourses(GeneratedCourses);
EnsureFirstSemesterCoverage();
ReloadResult();
}
if (errors.Count == 0)
public static void ReloadResult()
{
MainWindow.Instance.TbResultLog.Text = "--- Alle generierten Kursen erfüllen die gegebenen Voraussetzungen ---";
}
else
{
MainWindow.Instance.TbResultLog.Text = "--- Bei der Generierung sind folgende Fehler aufgetreten: ---\n\n";
foreach (var e in errors)
MainWindow.Instance.TbResultLog.Text += e + "\n";
}
var errors = ValidateCourses(GeneratedCourses);
foreach (var student in Settings.Instance.Students)
{
student.Result = new string[4];
}
if (errors.Count == 0)
{
MainWindow.Instance.TbResultLog.Text = "--- Alle generierten Kursen erfüllen die gegebenen Voraussetzungen ---";
}
else
{
MainWindow.Instance.TbResultLog.Text = "--- Bei der Generierung sind folgende Fehler aufgetreten: ---\n\n";
foreach (var e in errors)
MainWindow.Instance.TbResultLog.Text += e + "\n";
}
foreach (var course in GeneratedCourses)
{
foreach (var student in Settings.Instance.Students)
{
if (course.Instance.Students.Contains(student.ID))
student.Result = new string[4];
}
foreach (var course in GeneratedCourses)
{
foreach (var student in Settings.Instance.Students)
{
student.Result[course.Semester - 1] = course.Instance.Sport.Name;
if (course.Instance.Students.Contains(student.ID))
{
student.Result[course.Semester - 1] = course.Instance.Sport.Name;
}
}
}
}
}
public static string GenerateStatistics()
{
GeneratedCourses.Sort((x,y) => x.Semester.CompareTo(y.Semester) );
+49 -1
View File
@@ -1,9 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text.Json;
namespace spplus;
public static class ExportUtility
{
private sealed class ConfigurationExport
{
public List<Sport> Sports { get; set; } = [];
public int[] NumCoursesPerSemester { get; set; } = [];
}
public static void ExportToCSV(string filepath)
{
char separator = ',';
@@ -16,4 +26,42 @@ public static class ExportUtility
File.WriteAllText(filepath, output);
}
}
private sealed class ResultExportEntry
{
public int Semester { get; set; }
public string SportName { get; set; } = string.Empty;
public List<string> Students { get; set; } = new();
}
public static void ExportResultsToJson(string filepath)
{
var list = CourseCrafter.GeneratedCourses
.Select(g => new ResultExportEntry
{
Semester = g.Semester,
SportName = g.Instance.Sport.Name,
Students = g.Instance.Students.ToList()
})
.ToList();
var json = JsonSerializer.Serialize(list, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(filepath, json);
}
public static void ExportConfigurationToJson(string filepath)
{
var export = new ConfigurationExport
{
Sports = Settings.Instance.Sports,
NumCoursesPerSemester = Settings.Instance.NumCoursesPerSemester
};
var json = JsonSerializer.Serialize(export, new JsonSerializerOptions
{
WriteIndented = true
});
File.WriteAllText(filepath, json);
}
}
Binary file not shown.

Before

Width:  |  Height:  |  Size: 82 KiB

After

Width:  |  Height:  |  Size: 141 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 194 KiB

After

Width:  |  Height:  |  Size: 207 KiB

+65 -28
View File
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Linq;
namespace spplus;
@@ -50,42 +51,78 @@ public static class import
public static List<Student> ImportResultFromFile(string path)
{
var dict = new Dictionary<string, (string Name, List<string> Courses)>();
var dict = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase);
foreach (var line in File.ReadLines(path).Skip(1)) // Header überspringen
{
if (string.IsNullOrWhiteSpace(line))
continue;
var parts = line.Split(',');
if (parts.Length < 3)
var parts = line.Split(',', StringSplitOptions.None);
if (parts.Length < 5)
continue;
string nameWithId = parts[0].Trim();
string course = parts[2].Replace("(2)", "").Replace("(3)", "").Replace("(4)", "").Trim();
int open = nameWithId.LastIndexOf('(');
int close = nameWithId.LastIndexOf(')');
if (open < 0 || close < 0 || close <= open)
continue;
string name = nameWithId[..open].Trim();
string id = nameWithId[(open + 1)..close].Trim();
if (!dict.ContainsKey(id))
dict[id] = (name, new List<string>());
dict[id].Courses.Add(course);
var studentId = parts[0].Trim();
dict[studentId] = new[]
{
parts[1].Trim(),
parts[2].Trim(),
parts[3].Trim(),
parts[4].Trim()
};
}
var result = new List<Student>();
foreach (var (id, data) in dict)
{
var student = new Student(id, data.Name, data.Courses);
result.Add(student);
}
return result;
return dict
.Select(entry => new Student(entry.Key, string.Empty, new List<string>())
{
Result = entry.Value
})
.ToList();
}
}
private sealed class ResultImportEntry
{
public int Semester { get; set; }
public string SportName { get; set; } = string.Empty;
public List<string> Students { get; set; } = new();
}
public static List<(int Semester, CourseCrafter.CourseInstance Instance)> ImportResultFromJson(string path)
{
try
{
var json = File.ReadAllText(path);
var entries = JsonSerializer.Deserialize<List<ResultImportEntry>>(json);
if (entries == null) return new();
var result = new List<(int Semester, CourseCrafter.CourseInstance Instance)>();
foreach (var e in entries)
{
if (string.IsNullOrWhiteSpace(e.SportName))
continue;
var sport = Settings.Instance.Sports.FirstOrDefault(s =>
string.Equals(s.Name, e.SportName, StringComparison.OrdinalIgnoreCase) ||
s.AlternativeNames.Any(a => string.Equals(a, e.SportName, StringComparison.OrdinalIgnoreCase)));
if (sport == null)
continue;
var ci = new CourseCrafter.CourseInstance
{
Sport = sport,
Students = e.Students.Distinct().ToList()
};
result.Add((e.Semester, ci));
}
return result;
}
catch
{
return new();
}
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 106 KiB

+43 -22
View File
@@ -1,22 +1,43 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ApplicationManifest>app.manifest</ApplicationManifest>
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="11.3.11" />
<PackageReference Include="Avalonia.Desktop" Version="11.3.11" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.3.11" />
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.3.11" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Include="Avalonia.Diagnostics" Version="11.3.11">
<IncludeAssets Condition="'$(Configuration)' != 'Debug'">None</IncludeAssets>
<PrivateAssets Condition="'$(Configuration)' != 'Debug'">All</PrivateAssets>
</PackageReference>
<PackageReference Include="Lucide.Avalonia" Version="0.1.35"/>
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ApplicationManifest>app.manifest</ApplicationManifest>
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
<ApplicationIcon>res/logo.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="11.3.11" />
<PackageReference Include="Avalonia.Desktop" Version="11.3.11" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.3.11" />
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.3.11" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Include="Avalonia.Diagnostics" Version="11.3.11">
<IncludeAssets Condition="'$(Configuration)' != 'Debug'">None</IncludeAssets>
<PrivateAssets Condition="'$(Configuration)' != 'Debug'">All</PrivateAssets>
</PackageReference>
<PackageReference Include="Lucide.Avalonia" Version="0.1.35"/>
<PackageReference Include="PdfSharp" Version="6.2.4"/>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.8"/>
</ItemGroup>
<ItemGroup>
<None Remove="res\logo.ico"/>
<AvaloniaResource Include="res\logo.ico">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</AvaloniaResource>
<None Remove="res\logo.png"/>
<AvaloniaResource Include="res\logo.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</AvaloniaResource>
</ItemGroup>
<ItemGroup>
<None Include="res/fonts/*.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
+18 -15
View File
@@ -85,7 +85,7 @@ public class Settings
public List<Student> Students { get; set; } = [];
public List<Sport> Sports { get; set; } = [];
public int NumCoursesPerSemester { get; set; } = 10; // Exact Amount of courses, not a maximum
public int[] NumCoursesPerSemester { get; set; } = [10,10,11,11]; // Exact Amount of courses, not a maximum
public Settings()
{
@@ -99,19 +99,22 @@ public class Settings
public static void ImportInitial()
{
Instance.Sports.Add(new Sport("Tischtennis"){ AlternativeNames = {"Sport_TT"}});
Instance.Sports.Add(new Sport("Badminton"){ AlternativeNames = {"Sport_BM"}});
Instance.Sports.Add(new Sport("Gymnastik/Tanz"){ AlternativeNames = {"Sport_Gym"}});
Instance.Sports.Add(new Sport("Schwimmen"){ AlternativeNames = {"Sport_SW"}, Semester = [1, 1, 1, 1], MaxStudents = 18});
Instance.Sports.Add(new Sport("Bouldern"){ AlternativeNames = {"Sport_BO"}, Semester = [1, 1, 1, 1]});
Instance.Sports.Add(new Sport("Basketball"){ AlternativeNames = {"Sport_BS"}});
Instance.Sports.Add(new Sport("Fitness"){ AlternativeNames = {"Sport_Fit"}});
Instance.Sports.Add(new Sport("Fußball"){ AlternativeNames = {"Sport_Fuß"}, Semester = [1, 0, 1, 0]});
Instance.Sports.Add(new Sport("Handball"){ AlternativeNames = {"Sport_HB"}});
Instance.Sports.Add(new Sport("Leichtathletik"){ AlternativeNames = {"Sport_LA"}, Semester = [1, 0, 1, 0], MaxStudents = 18});
Instance.Sports.Add(new Sport("Tennis"){ AlternativeNames = {"Sport_Te"}});
Instance.Sports.Add(new Sport("Turnen"){ AlternativeNames = {"Sport_Tur"}});
Instance.Sports.Add(new Sport("Volleyball"){ AlternativeNames = {"Sport_VB"}});
Instance.Sports.Clear();
int id = 1;
Instance.Sports.Add(new Sport("Tischtennis"){ ID = id++, AlternativeNames = {"Sport_TT"}});
Instance.Sports.Add(new Sport("Badminton"){ ID = id++, AlternativeNames = {"Sport_BM"}});
Instance.Sports.Add(new Sport("Gymnastik/Tanz"){ ID = id++, AlternativeNames = {"Sport_Gym"}});
Instance.Sports.Add(new Sport("Schwimmen"){ ID = id++, AlternativeNames = {"Sport_SW"}, Semester = [1, 1, 1, 1], MaxStudents = 18});
Instance.Sports.Add(new Sport("Bouldern"){ ID = id++, AlternativeNames = {"Sport_BO"}, Semester = [1, 1, 1, 1]});
Instance.Sports.Add(new Sport("Basketball"){ ID = id++, AlternativeNames = {"Sport_BS"}});
Instance.Sports.Add(new Sport("Fitness"){ ID = id++, AlternativeNames = {"Sport_Fit"}});
Instance.Sports.Add(new Sport("Fußball"){ ID = id++, AlternativeNames = {"Sport_Fuß"}, Semester = [1, 0, 1, 0]});
Instance.Sports.Add(new Sport("Handball"){ ID = id++, AlternativeNames = {"Sport_HB"}});
Instance.Sports.Add(new Sport("Leichtathletik"){ ID = id++, AlternativeNames = {"Sport_LA"}, Semester = [1, 0, 1, 0], MaxStudents = 18});
Instance.Sports.Add(new Sport("Tennis"){ ID = id++, AlternativeNames = {"Sport_Te"}});
Instance.Sports.Add(new Sport("Turnen"){ ID = id++, AlternativeNames = {"Sport_Tur"}});
Instance.Sports.Add(new Sport("Volleyball"){ ID = id++, AlternativeNames = {"Sport_VB"}});
}
public static string? GetSportNameFromID(int id)
@@ -126,4 +129,4 @@ public class Settings
return null;
}
}
}