using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TimetableDesigner.Core; namespace TimetableDesigner.Export { public class ExporterHTML : Exporter { #region CONSTRUCTORS public ExporterHTML(Project project) : base(project) { } #endregion #region PUBLIC METHODS public override void Export(string path) => Export(path, null); public void Export(string path, string? css) { string content = BuildDocument(css); using (StreamWriter writer = new StreamWriter(path)) { writer.WriteLine(content); } } #endregion #region PRIVATE METHODS private string BuildDocument(string? css) { if (css == null) { StringBuilder cssBuilder = new StringBuilder(); cssBuilder.AppendLine("h1 {text-align: center;}"); cssBuilder.AppendLine("table, td, th { border: 1px solid black; border-collapse: collapse; }"); cssBuilder.AppendLine("td, th { padding: 10px; }"); css = cssBuilder.ToString(); } StringBuilder builder = new StringBuilder(); builder.AppendLine(""); builder.AppendLine(""); builder.AppendLine(""); builder.AppendLine(""); builder.AppendLine(""); if (Groups.Count > 0) { builder.AppendLine("

Groups

"); foreach (Group group in Groups) { builder.AppendLine("

"); builder.AppendLine(group.Name); builder.AppendLine("

"); builder.AppendLine(BuildTable(group)); } } if (Subgroups.Count > 0) { builder.AppendLine("

Subgroups

"); foreach (Subgroup subgroup in Subgroups) { builder.AppendLine("

"); builder.AppendLine(subgroup.Name); builder.AppendLine("

"); builder.AppendLine(BuildTable(subgroup)); } } if (Teachers.Count > 0) { builder.AppendLine("

Teachers

"); foreach (Teacher teacher in Teachers) { builder.AppendLine("

"); builder.AppendLine(teacher.Name); builder.AppendLine("

"); builder.AppendLine(BuildTable(teacher)); } } if (Classrooms.Count > 0) { builder.AppendLine("

Classrooms

"); foreach (Classroom classroom in Classrooms) { builder.AppendLine("

"); builder.AppendLine(classroom.Name); builder.AppendLine("

"); builder.AppendLine(BuildTable(classroom)); } } builder.AppendLine(""); builder.AppendLine(""); return builder.ToString(); } private string BuildTable(Unit unit) { IEnumerable classes = _project.Classes.Where(c => c.Classroom == unit || c.Teacher == unit || c.Group == unit || (unit is Group g && c.Group is Subgroup s && g.AssignedSubgroups.Contains(s))) .Where(c => c.Slot is not null && c.Day is not null); IEnumerable days = _project.TimetableTemplate.Days; IEnumerable slots = _project.TimetableTemplate.Slots; StringBuilder builder = new StringBuilder(); builder.AppendLine(""); builder.AppendLine(""); builder.AppendLine(""); foreach (TimetableDay day in days) { builder.AppendLine($""); } builder.AppendLine(""); foreach (TimetableSpan slot in slots) { builder.AppendLine(""); builder.AppendLine($""); foreach (TimetableDay day in days) { IEnumerable slotClasses = classes.Where(x => x.Day == day && x.Slot == slot); builder.AppendLine(""); } builder.AppendLine(""); } builder.AppendLine("
{day.Name}
{slot}"); foreach (Class slotClass in slotClasses) { string group = slotClass.Group is null ? "none" : slotClass.Group.Name; string teacher = slotClass.Teacher is null ? "none" : slotClass.Teacher.Name; string classroom = slotClass.Classroom is null ? "none" : slotClass.Classroom.Name; builder.AppendLine($"

"); builder.AppendLine(slotClass.Name); builder.AppendLine("

"); } builder.AppendLine("
"); return builder.ToString(); } #endregion } }