Skip to content
This repository has been archived by the owner on Sep 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #15 from Typiqally/4-adding-file-export-types
Browse files Browse the repository at this point in the history
  • Loading branch information
Typiqally authored Jun 4, 2022
2 parents dc5628c + 0d40855 commit 98fa5fc
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Epsilon.Canvas.Abstractions/Data/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ public record Module(
{
[JsonIgnore]
public IList<Assignment> Assignments { get; set; } = new Collection<Assignment>();

public bool HasAssignments()
{
return this.Assignments.Count > 0;
}
}
1 change: 1 addition & 0 deletions Epsilon/Epsilon.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="ExcelLibrary" Version="1.2011.7.31" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="6.0.0" />
Expand Down
96 changes: 96 additions & 0 deletions Epsilon/Export/Exporters/ExcelModuleExporter.cs
Original file line number Diff line number Diff line change
@@ -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<ExportOptions> options)
{
_options = options.Value;
}

public IEnumerable<string> Formats { get; } = new[] { "xls" };

public List<Outcome> GetAllOutcomesTypes(Module module)
{
List<Outcome> addedOutcomes = new List<Outcome>();
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<Outcome> 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<Module> modules, string format)
{
Workbook workbook = new Workbook();

foreach (var module in modules)
{
if (module.HasAssignments())
{
List<Outcome> 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}");
}
}
1 change: 1 addition & 0 deletions Epsilon/Extensions/CoreServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ private static IServiceCollection AddExport(this IServiceCollection services, IC

services.AddScoped<ICanvasModuleExporter, ConsoleModuleExporter>();
services.AddScoped<ICanvasModuleExporter, CsvModuleExporter>();
services.AddScoped<ICanvasModuleExporter, ExcelModuleExporter>();

services.AddScoped<IModuleExporterCollection, ModuleExporterCollection>();

Expand Down

0 comments on commit 98fa5fc

Please sign in to comment.