diff --git a/Epsilon.Canvas.Abstractions/Data/Module.cs b/Epsilon.Canvas.Abstractions/Data/Module.cs index 874dcafb..5ac2780b 100644 --- a/Epsilon.Canvas.Abstractions/Data/Module.cs +++ b/Epsilon.Canvas.Abstractions/Data/Module.cs @@ -10,4 +10,9 @@ public record Module( { [JsonIgnore] public IList Assignments { get; set; } = new Collection(); + + public bool HasAssignments() + { + return this.Assignments.Count > 0; + } } \ No newline at end of file diff --git a/Epsilon/Epsilon.csproj b/Epsilon/Epsilon.csproj index e6e27c1b..2030f0c7 100644 --- a/Epsilon/Epsilon.csproj +++ b/Epsilon/Epsilon.csproj @@ -12,6 +12,7 @@ + diff --git a/Epsilon/Export/Exporters/ExcelModuleExporter.cs b/Epsilon/Export/Exporters/ExcelModuleExporter.cs new file mode 100644 index 00000000..9f1e67f8 --- /dev/null +++ b/Epsilon/Export/Exporters/ExcelModuleExporter.cs @@ -0,0 +1,96 @@ +using Epsilon.Abstractions.Export; +using Epsilon.Canvas.Abstractions.Data; +using ExcelLibrary.SpreadSheet; +using Microsoft.Extensions.Options; + +namespace Epsilon.Export.Exporters; + +public class ExcelModuleExporter : ICanvasModuleExporter +{ + private readonly ExportOptions _options; + + public ExcelModuleExporter(IOptions options) + { + _options = options.Value; + } + + public IEnumerable Formats { get; } = new[] { "xls" }; + + public List GetAllOutcomesTypes(Module module) + { + List addedOutcomes = new List(); + if (module.HasAssignments()) + { + foreach (var assignment in module.Assignments) + { + foreach (var result in assignment.OutcomeResults) + { + if (result.Outcome != null && !addedOutcomes.Contains(result.Outcome)) + { + addedOutcomes.Add(result.Outcome); + } + } + } + } + + return addedOutcomes.OrderByDescending(o => o.Title.Length).ToList(); + } + + public int GetOutcomeRow(List outcomes, Outcome outcome) + { + var result = outcomes.Find(o => o.Title == outcome.Title); + if (result != null) + { + return outcomes.IndexOf(result); + } + + return 0; + } + + public void Export(IEnumerable modules, string format) + { + Workbook workbook = new Workbook(); + + foreach (var module in modules) + { + if (module.HasAssignments()) + { + List outcomes = GetAllOutcomesTypes(module); + Worksheet worksheet = new Worksheet(module.Name); + //Because reasons @source https://stackoverflow.com/a/8127642 + for(int i = 0;i < 100; i++) + worksheet.Cells[i,0] = new Cell(""); + + //Adding all the outcomes. + for (int index = 0; index < outcomes.Count; index++) + { + worksheet.Cells[index, 0] = new Cell(outcomes[index].Title); + } + + foreach (var assignment in module.Assignments) + { + foreach (var outcomeResult in assignment.OutcomeResults) + { + if (outcomeResult.Outcome != null) + { + int row = GetOutcomeRow(outcomes, outcomeResult.Outcome); + + //Adding assignments to the outcomes + string cellValue = worksheet.Cells[row, 1].StringValue; + cellValue += (cellValue != "" ? "\n": "") + assignment.Name + " " + assignment.Url ; + + worksheet.Cells[row, 1] = new Cell(cellValue); + } + + } + } + + worksheet.Cells.ColumnWidth[0, 0] = 5000; + worksheet.Cells.ColumnWidth[0, 1] = 8000; + workbook.Worksheets.Add(worksheet); + + } + } + workbook.Save($"{_options.FormattedOutputName}.{format}"); + } +} \ No newline at end of file diff --git a/Epsilon/Extensions/CoreServiceCollectionExtensions.cs b/Epsilon/Extensions/CoreServiceCollectionExtensions.cs index 1b5afb09..47e35cef 100644 --- a/Epsilon/Extensions/CoreServiceCollectionExtensions.cs +++ b/Epsilon/Extensions/CoreServiceCollectionExtensions.cs @@ -23,6 +23,7 @@ private static IServiceCollection AddExport(this IServiceCollection services, IC services.AddScoped(); services.AddScoped(); + services.AddScoped(); services.AddScoped();