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

Feat/word export #36

Merged
merged 7 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion Epsilon.Canvas.Abstractions/Model/Outcome.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;

namespace Epsilon.Canvas.Abstractions.Model;

public record Outcome(
[property: JsonPropertyName("id")] int Id,
[property: JsonPropertyName("title")] string Title,
[property: JsonPropertyName("description")] string Description
);
)
{
public string ShortDescription()
{
string description = RemoveHtml();
//Function gives only the short English description back of the outcome.
var startPos = description.IndexOf(" EN ", StringComparison.Ordinal) + " EN ".Length;
var endPos = description.IndexOf(" NL ", StringComparison.Ordinal);

return description.Substring(startPos, endPos - startPos);
}

private string RemoveHtml()
{
var raw = Regex.Replace(Description, "<.*?>", " ");
var trimmed = Regex.Replace(raw, @"\s\s+", " ");

return trimmed;
}
};
21 changes: 19 additions & 2 deletions Epsilon.Canvas.Abstractions/Model/OutcomeResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,24 @@
namespace Epsilon.Canvas.Abstractions.Model;

public record OutcomeResult(
[property: JsonPropertyName("mastery")] bool? Mastery,
[property: JsonPropertyName("mastery")]
bool? Mastery,
[property: JsonPropertyName("score")] double? Score,
[property: JsonPropertyName("links")] OutcomeResultLink Link
);
)
{
public string Grade()
{
switch (Score)
{
default:
return "Unsatisfactory";
case 3:
return "Satisfactory";
case 4:
return "Good";
case 5:
return "Outstanding";
}
}
}
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="DocX" Version="2.3.0" />
<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" />
Expand Down
2 changes: 1 addition & 1 deletion Epsilon/Export/Exporters/ConsoleModuleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private void LogModule(IEnumerable<Module> modules)

foreach (var result in module.Collection.OutcomeResults.Where(o => o.Link.Alignment == alignment.Id))
{
_logger.LogInformation("- {OutcomeName} {Score}", outcomes[result.Link.Outcome].Title, result.Score);
_logger.LogInformation("- {OutcomeName} {Score}", outcomes[result.Link.Outcome].Title, result.Grade());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Epsilon/Export/Exporters/CsvModuleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private static DataTable CreateDataTable(IEnumerable<Module> modules)
alignment.Id,
alignment.Name,
outcome.Title,
result.Score.HasValue ? result.Score : "not achieved",
result.Grade(),
module.Name
);
}
Expand Down
48 changes: 6 additions & 42 deletions Epsilon/Export/Exporters/ExcelModuleExporter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Text;
using System.Text.RegularExpressions;
using Epsilon.Abstractions.Export;
using Epsilon.Canvas.Abstractions.Model;
using ExcelLibrary.SpreadSheet;
Expand Down Expand Up @@ -53,20 +52,22 @@ public void Export(IEnumerable<Module> modules, string format)

if (assignmentIds.Any())
{
worksheet.Cells[index, 0] = new Cell(outcome.Title + " " + ShortDescription(ConvertHtmlToRaw(outcome.Description)));
worksheet.Cells[index, 0] = new Cell($"{outcome.Title} {outcome.ShortDescription()}");

var cellValueBuilder = new StringBuilder();

foreach (var (alignmentId, alignment) in alignments.Where(a => assignmentIds.Contains(a.Key)))
{
cellValueBuilder.AppendLine($"{alignment.Name} {alignment.Url}");
}

worksheet.Cells[index, 1] = new Cell(cellValueBuilder.ToString());

var cellValueOutComeResultsBuilder = new StringBuilder();
foreach (var outcomeResult in module.Collection.OutcomeResults.Where(result => result.Link.Outcome == outcomeId))
foreach (var outcomeResult in module.Collection.OutcomeResults.Where(result =>
result.Link.Outcome == outcomeId))
{
cellValueOutComeResultsBuilder.AppendLine(OutcomeToText(outcomeResult.Score));
cellValueOutComeResultsBuilder.AppendLine(outcomeResult.Grade());
}

worksheet.Cells[index, 2] = new Cell(cellValueOutComeResultsBuilder.ToString());
Expand All @@ -84,41 +85,4 @@ public void Export(IEnumerable<Module> modules, string format)
// We're forced to xls because of the older format
workbook.Save($"{_options.FormattedOutputName}.xls");
}

private static string ShortDescription(string description)
{
//Function gives only the short English description back of the outcome.
var startPos = description.IndexOf(" EN ", StringComparison.Ordinal) + " EN ".Length;
var endPos = description.IndexOf(" NL ", StringComparison.Ordinal);

return description.Substring(startPos, endPos - startPos);
}

private static string ConvertHtmlToRaw(string html)
{
var raw = Regex.Replace(html, "<.*?>", " ");
var trimmed = Regex.Replace(raw, @"\s\s+", " ");

return trimmed;
}

private string OutcomeToText(double? result)
{
switch (result)
{
default:
case 0:
return "Unsatisfactory";
break;
case 3:
return "Satisfactory";
break;
case 4:
return "Good";
break;
case 5:
return "Outstanding";
break;
}
}
}
85 changes: 85 additions & 0 deletions Epsilon/Export/Exporters/WordModuleExporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System.Drawing;
using System.Text;
using Epsilon.Abstractions.Export;
using Epsilon.Canvas.Abstractions.Model;
using Microsoft.Extensions.Options;
using Xceed.Document.NET;
using Xceed.Words.NET;

namespace Epsilon.Export.Exporters;

public class WordModuleExporter : ICanvasModuleExporter
{
private readonly ExportOptions _options;

public WordModuleExporter(IOptions<ExportOptions> options)
{
_options = options.Value;
}

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

public void Export(IEnumerable<Module> modules, string format)
{
using (DocX document = DocX.Create($"{_options.FormattedOutputName}.docx"))
{
document.AddFooters();
var link = document.AddHyperlink("Epsilon", new Uri("https://github.com/Typiqally/epsilon"));

document.Footers.Odd
.InsertParagraph("Created with ")
.AppendHyperlink(link).Color(Color.Blue).UnderlineStyle(UnderlineStyle.singleLine);

foreach (var module in modules.Where(static m => m.Collection.OutcomeResults.Any()))
{
var links = module.Collection.Links;
var alignments = links.AlignmentsDictionary;
var outcomes = links.OutcomesDictionary;

var table = document.AddTable(1, 3);

table.Rows[0].Cells[0].Paragraphs[0].Append("KPI");
table.Rows[0].Cells[1].Paragraphs[0].Append("Assignment(s)");
table.Rows[0].Cells[2].Paragraphs[0].Append("Score");

foreach (var (outcomeId, outcome) in outcomes)
{
var assignmentIds = module.Collection.OutcomeResults
.Where(o => o.Link.Outcome == outcomeId)
.Select(static o => o.Link.Assignment)
.ToArray();

if (assignmentIds.Any())
{
var row = table.InsertRow();
row.Cells[0].Paragraphs[0].Append(outcome.Title + " " + outcome.ShortDescription());

var cellValueBuilder = new StringBuilder();

foreach (var (alignmentId, alignment) in alignments.Where(a => assignmentIds.Contains(a.Key)))
{
cellValueBuilder.AppendLine($"{alignment.Name} {alignment.Url}");
}

row.Cells[1].Paragraphs[0].Append(cellValueBuilder.ToString());

var cellValueOutComeResultsBuilder = new StringBuilder();
foreach (var outcomeResult in module.Collection.OutcomeResults.Where(result =>
result.Link.Outcome == outcomeId))
{
cellValueOutComeResultsBuilder.AppendLine(outcomeResult.Grade());
}

row.Cells[2].Paragraphs[0].Append(cellValueOutComeResultsBuilder.ToString());
}
}

var par = document.InsertParagraph(module.Name);
par.FontSize(24);
par.InsertTableAfterSelf(table).InsertPageBreakAfterSelf();
}

document.Save();
}
}
}
1 change: 1 addition & 0 deletions Epsilon/Extensions/CoreServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ private static IServiceCollection AddExport(this IServiceCollection services, IC
services.AddScoped<ICanvasModuleExporter, ConsoleModuleExporter>();
services.AddScoped<ICanvasModuleExporter, CsvModuleExporter>();
services.AddScoped<ICanvasModuleExporter, ExcelModuleExporter>();
services.AddScoped<ICanvasModuleExporter, WordModuleExporter>();

services.AddScoped<IModuleExporterCollection, ModuleExporterCollection>();

Expand Down