diff --git a/DigitalLearningSolutions.Data/Extensions/ConfigurationExtensions.cs b/DigitalLearningSolutions.Data/Extensions/ConfigurationExtensions.cs index 1af7e9c2db..81e1c34156 100644 --- a/DigitalLearningSolutions.Data/Extensions/ConfigurationExtensions.cs +++ b/DigitalLearningSolutions.Data/Extensions/ConfigurationExtensions.cs @@ -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]; @@ -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]; + } } } diff --git a/DigitalLearningSolutions.Data/Services/CandidateAssessmentDownloadFileService.cs b/DigitalLearningSolutions.Data/Services/CandidateAssessmentDownloadFileService.cs index e1a81c6eb8..f0506185f9 100644 --- a/DigitalLearningSolutions.Data/Services/CandidateAssessmentDownloadFileService.cs +++ b/DigitalLearningSolutions.Data/Services/CandidateAssessmentDownloadFileService.cs @@ -2,7 +2,9 @@ { 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; @@ -10,22 +12,24 @@ 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( @@ -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? dataObjects) + private static void AddSheetToWorkbook(IXLWorkbook workbook, string sheetName, IEnumerable? 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; @@ -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) diff --git a/DigitalLearningSolutions.Web/Controllers/LearningPortalController/SelfAssessment.cs b/DigitalLearningSolutions.Web/Controllers/LearningPortalController/SelfAssessment.cs index 79fcf8bb67..40af7c257c 100644 --- a/DigitalLearningSolutions.Web/Controllers/LearningPortalController/SelfAssessment.cs +++ b/DigitalLearningSolutions.Web/Controllers/LearningPortalController/SelfAssessment.cs @@ -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, diff --git a/DigitalLearningSolutions.Web/appsettings.json b/DigitalLearningSolutions.Web/appsettings.json index a95887004e..c365ec4f76 100644 --- a/DigitalLearningSolutions.Web/appsettings.json +++ b/DigitalLearningSolutions.Web/appsettings.json @@ -27,5 +27,6 @@ "LoginEndpoint": "/login", "LinkingEndpoint": "/create-user", "ClientCode": "DigitalLearningSolutions" - } + }, + "ExcelPassword": "0f8cc6f0-9f21-4f0f-9cb9-365675490458" }