Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public static class ConfigurationExtensions
private const string JavascriptSearchSortFilterPaginateItemLimitKey =
"JavascriptSearchSortFilterPaginateItemLimit";

private const string ExcelPassword = "ExcelPassword";

public static string GetAppRootPath(this IConfiguration config)
{
return config[AppRootPathName];
Expand Down Expand Up @@ -106,5 +108,10 @@ public static int GetJavascriptSearchSortFilterPaginateItemLimit(this IConfigura
{
return int.Parse(config[JavascriptSearchSortFilterPaginateItemLimitKey]);
}

public static string GetExcelPassword(this IConfiguration config)
{
return config[ExcelPassword];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,34 @@
{
using ClosedXML.Excel;
using DigitalLearningSolutions.Data.DataServices.SelfAssessmentDataService;
using DigitalLearningSolutions.Data.Extensions;
using DigitalLearningSolutions.Data.Models.SelfAssessments.Export;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

public interface ICandidateAssessmentDownloadFileService
{
public byte[] GetCandidateAssessmentDownloadFileForCentre(int candidateAssessmentId, int candidateId);
public byte[] GetCandidateAssessmentDownloadFileForCentre(int candidateAssessmentId, int candidateId, bool isProtected);
}

public class CandidateAssessmentDownloadFileService : ICandidateAssessmentDownloadFileService
{
private readonly ISelfAssessmentDataService selfAssessmentDataService;
private readonly IConfiguration config;

public CandidateAssessmentDownloadFileService(
ISelfAssessmentDataService selfAssessmentDataService
ISelfAssessmentDataService selfAssessmentDataService,
IConfiguration config
)
{
this.selfAssessmentDataService = selfAssessmentDataService;
this.config = config;
}
public byte[] GetCandidateAssessmentDownloadFileForCentre(int candidateAssessmentId, int candidateId)
public byte[] GetCandidateAssessmentDownloadFileForCentre(int candidateAssessmentId, int candidateId, bool isProtected)
{

var summaryData = selfAssessmentDataService.GetCandidateAssessmentExportSummary(candidateAssessmentId, candidateId);
var detailData = selfAssessmentDataService.GetCandidateAssessmentExportDetails(candidateAssessmentId, candidateId);
var details = detailData.Select(
Expand All @@ -47,20 +51,26 @@ public byte[] GetCandidateAssessmentDownloadFileForCentre(int candidateAssessmen
}
);
using var workbook = new XLWorkbook();
AddSummarySheet(workbook, summaryData);
AddSheetToWorkbook(workbook, "Details", details);
var excelPassword = config.GetExcelPassword();
AddSummarySheet(workbook, summaryData, excelPassword, isProtected);
AddSheetToWorkbook(workbook, "Details", details, excelPassword, isProtected);
using var stream = new MemoryStream();
workbook.SaveAs(stream);
return stream.ToArray();
}
private static void AddSheetToWorkbook(IXLWorkbook workbook, string sheetName, IEnumerable<object>? dataObjects)
private static void AddSheetToWorkbook(IXLWorkbook workbook, string sheetName, IEnumerable<object>? dataObjects,string excelPassword, bool isProtected)
{
var sheet = workbook.Worksheets.Add(sheetName);
var table = sheet.Cell(1, 1).InsertTable(dataObjects);
table.Theme = XLTableTheme.TableStyleLight9;
sheet.Columns().AdjustToContents();
if (isProtected)
{
sheet.Protect(excelPassword);
sheet.Columns().Style.Protection.SetLocked(true);
}
}
private static void AddSummarySheet(IXLWorkbook workbook, CandidateAssessmentExportSummary candidateAssessmentExportSummary)
private static void AddSummarySheet(IXLWorkbook workbook, CandidateAssessmentExportSummary candidateAssessmentExportSummary,string excelPassword, bool isProtected)
{
var sheet = workbook.Worksheets.Add("Summary");
var rowNum = 1;
Expand Down Expand Up @@ -99,7 +109,7 @@ private static void AddSummarySheet(IXLWorkbook workbook, CandidateAssessmentExp
sheet.Cell(rowNum, 1).Value = "Questions with no role requirements set";
sheet.Cell(rowNum, 1).Style.Fill.BackgroundColor = XLColor.LightBlue;
sheet.Cell(rowNum, 2).Value = candidateAssessmentExportSummary.NoRequirementsSetCount;
rowNum++;
rowNum++;

}
if (candidateAssessmentExportSummary.MeetingCount > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ public IActionResult SignOffHistory(int selfAssessmentId, string vocabulary)
}
public IActionResult ExportCandidateAssessment(int candidateAssessmentId, string vocabulary)
{
var content = candidateAssessmentDownloadFileService.GetCandidateAssessmentDownloadFileForCentre(candidateAssessmentId, GetCandidateId());
var content = candidateAssessmentDownloadFileService.GetCandidateAssessmentDownloadFileForCentre(candidateAssessmentId, GetCandidateId(), true);
var fileName = $"DLS {vocabulary} Assessment Export {DateTime.Today:yyyy-MM-dd}.xlsx";
return File(
content,
Expand Down
3 changes: 2 additions & 1 deletion DigitalLearningSolutions.Web/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
"LoginEndpoint": "/login",
"LinkingEndpoint": "/create-user",
"ClientCode": "DigitalLearningSolutions"
}
},
"ExcelPassword": "0f8cc6f0-9f21-4f0f-9cb9-365675490458"
}