Skip to content

Commit 564bb1b

Browse files
TD-4702 Resolving the route issue
1 parent 97783c5 commit 564bb1b

File tree

4 files changed

+122
-20
lines changed

4 files changed

+122
-20
lines changed

DigitalLearningSolutions.Web/Controllers/LearningPortalController/SelfAssessment.cs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,29 +1759,19 @@ public IActionResult CompetencySelfAssessmentCertificate(int CandidateAssessment
17591759
return View("SelfAssessments/CompetencySelfAssessmentCertificate", model);
17601760
}
17611761

1762-
[Route("/LearningPortal/selfAssessments/{CandidateAssessmentId:int}/{vocabulary}/DownloadCertificate")]
1763-
public async Task<IActionResult> DownloadCertificate(int candidateAssessmentId, string vocabulary)
1762+
[Route("/LearningPortal/selfAssessments/{CandidateAssessmentId:int}/Proficiencies/DownloadCertificate")]
1763+
public async Task<IActionResult> DownloadCertificate(int candidateAssessmentId)
17641764
{
17651765
PdfReportStatusResponse pdfReportStatusResponse = new PdfReportStatusResponse();
17661766
var delegateId = User.GetCandidateIdKnownNotNull();
1767+
var userId = User.GetUserIdKnownNotNull();
1768+
17671769
var competencymaindata = selfAssessmentService.GetCompetencySelfAssessmentCertificate(candidateAssessmentId);
1768-
if (competencymaindata == null || candidateAssessmentId == 0)
1770+
if (competencymaindata == null || candidateAssessmentId == 0 || userId == 0)
17691771
{
17701772
return RedirectToAction("StatusCode", "LearningSolutions", new { code = 403 });
17711773
}
1772-
if (vocabulary == "Proficiencies")
1773-
{
1774-
var userId = User.GetUserIdKnownNotNull();
17751774
if (userId != competencymaindata.LearnerId) return RedirectToAction("StatusCode", "LearningSolutions", new { code = 403 });
1776-
1777-
}
1778-
if (vocabulary == "ProfileAssessment")
1779-
{
1780-
var adminId = User.GetAdminId();
1781-
var supervisorDelegateDetails = supervisorService.GetSupervisorDelegateDetailsForAdminId(adminId.Value);
1782-
var checkSupervisorDelegate = supervisorDelegateDetails.Where(x => x.DelegateUserID == competencymaindata.LearnerId).FirstOrDefault();
1783-
if (checkSupervisorDelegate == null) return RedirectToAction("StatusCode", "LearningSolutions", new { code = 403 });
1784-
}
17851775
var delegateUserId = competencymaindata.LearnerId;
17861776
var competencycount = selfAssessmentService.GetCompetencyCountSelfAssessmentCertificate(candidateAssessmentId);
17871777
var accessors = selfAssessmentService.GetAccessor(competencymaindata.SelfAssessmentID, competencymaindata.LearnerId);
@@ -1851,7 +1841,7 @@ public async Task<IActionResult> DownloadCertificate(int candidateAssessmentId,
18511841
return View("SelfAssessments/CompetencySelfAssessmentCertificate", model);
18521842
}
18531843

1854-
public static string RenderRazorViewToString(Controller controller, string viewName, object model = null)
1844+
private static string RenderRazorViewToString(Controller controller, string viewName, object model = null)
18551845
{
18561846
controller.ViewData.Model = model;
18571847
using (var sw = new StringWriter())

DigitalLearningSolutions.Web/Controllers/SupervisorController/Supervisor.cs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using DigitalLearningSolutions.Data.Enums;
44
using DigitalLearningSolutions.Data.Helpers;
55
using DigitalLearningSolutions.Data.Models;
6+
using DigitalLearningSolutions.Data.Models.Common;
67
using DigitalLearningSolutions.Data.Models.SearchSortFilterPaginate;
78
using DigitalLearningSolutions.Data.Models.SelfAssessments;
89
using DigitalLearningSolutions.Data.Models.SessionData.Supervisor;
@@ -11,13 +12,19 @@
1112
using DigitalLearningSolutions.Web.Extensions;
1213
using DigitalLearningSolutions.Web.Helpers;
1314
using DigitalLearningSolutions.Web.ServiceFilter;
15+
using DigitalLearningSolutions.Web.Services;
1416
using DigitalLearningSolutions.Web.ViewModels.Common.SearchablePage;
1517
using DigitalLearningSolutions.Web.ViewModels.Supervisor;
1618
using GDS.MultiPageFormData.Enums;
1719
using Microsoft.AspNetCore.Mvc;
20+
using Microsoft.AspNetCore.Mvc.Rendering;
21+
using Microsoft.AspNetCore.Mvc.ViewEngines;
22+
using Microsoft.AspNetCore.Mvc.ViewFeatures;
1823
using System;
1924
using System.Collections.Generic;
25+
using System.IO;
2026
using System.Linq;
27+
using System.Threading.Tasks;
2128

2229
public partial class SupervisorController
2330
{
@@ -1433,5 +1440,109 @@ public IActionResult CompetencySelfAssessmentCertificatesupervisor(int candidate
14331440
var model = new ViewModels.LearningPortal.SelfAssessments.CompetencySelfAssessmentCertificateViewModel(competencymaindata, competencycount, "ProfileAssessment", accessors, activitySummaryCompetencySelfAssesment, sumQuestions, sumVerifiedCount, supervisorDelegateId);
14341441
return View("SelfAssessments/CompetencySelfAssessmentCertificate", model);
14351442
}
1443+
[Route("/Supervisor/Staff/{CandidateAssessmentId:int}/ProfileAssessment/DownloadCertificate")]
1444+
public async Task<IActionResult> DownloadCertificate(int candidateAssessmentId)
1445+
{
1446+
PdfReportStatusResponse pdfReportStatusResponse = new PdfReportStatusResponse();
1447+
var delegateId = User.GetCandidateIdKnownNotNull();
1448+
var adminId = User.GetAdminId();
1449+
var competencymaindata = selfAssessmentService.GetCompetencySelfAssessmentCertificate(candidateAssessmentId);
1450+
if (competencymaindata == null || candidateAssessmentId == 0 || adminId == 0)
1451+
{
1452+
return RedirectToAction("StatusCode", "LearningSolutions", new { code = 403 });
1453+
}
1454+
var supervisorDelegateDetails = supervisorService.GetSupervisorDelegateDetailsForAdminId(adminId.Value);
1455+
var checkSupervisorDelegate = supervisorDelegateDetails.Where(x => x.DelegateUserID == competencymaindata.LearnerId).FirstOrDefault();
1456+
if (checkSupervisorDelegate == null) return RedirectToAction("StatusCode", "LearningSolutions", new { code = 403 });
1457+
var delegateUserId = competencymaindata.LearnerId;
1458+
var competencycount = selfAssessmentService.GetCompetencyCountSelfAssessmentCertificate(candidateAssessmentId);
1459+
var accessors = selfAssessmentService.GetAccessor(competencymaindata.SelfAssessmentID, competencymaindata.LearnerId);
1460+
var activitySummaryCompetencySelfAssesment = selfAssessmentService.GetActivitySummaryCompetencySelfAssesment(competencymaindata.Id);
1461+
var assessment = selfAssessmentService.GetSelfAssessmentForCandidateById(delegateUserId, competencymaindata.SelfAssessmentID);
1462+
var recentResults = selfAssessmentService.GetMostRecentResults(competencymaindata.SelfAssessmentID, competencymaindata.LearnerDelegateAccountId).ToList();
1463+
var competencyIds = recentResults.Select(c => c.Id).ToArray();
1464+
var competencyFlags = frameworkService.GetSelectedCompetencyFlagsByCompetecyIds(competencyIds);
1465+
var competencies = CompetencyFilterHelper.FilterCompetencies(recentResults, competencyFlags, null);
1466+
var supervisorSignOffs = selfAssessmentService.GetSupervisorSignOffsForCandidateAssessment(competencymaindata.SelfAssessmentID, delegateUserId);
1467+
if (!CertificateHelper.CanViewCertificate(recentResults, supervisorSignOffs))
1468+
{
1469+
return RedirectToAction("StatusCode", "LearningSolutions", new { code = 401 });
1470+
}
1471+
foreach (var competency in competencies)
1472+
{
1473+
competency.QuestionLabel = assessment.QuestionLabel;
1474+
foreach (var assessmentQuestion in competency.AssessmentQuestions)
1475+
{
1476+
if (assessmentQuestion.AssessmentQuestionInputTypeID != 2)
1477+
{
1478+
assessmentQuestion.LevelDescriptors = selfAssessmentService
1479+
.GetLevelDescriptorsForAssessmentQuestion(
1480+
assessmentQuestion.Id,
1481+
assessmentQuestion.MinValue,
1482+
assessmentQuestion.MaxValue,
1483+
assessmentQuestion.MinValue == 0
1484+
).ToList();
1485+
}
1486+
}
1487+
}
1488+
1489+
var CompetencyGroups = competencies.GroupBy(competency => competency.CompetencyGroup);
1490+
var competencySummaries = from g in CompetencyGroups
1491+
let questions = g.SelectMany(c => c.AssessmentQuestions).Where(q => q.Required)
1492+
let selfAssessedCount = questions.Count(q => q.Result.HasValue)
1493+
let verifiedCount = questions.Count(q => !((q.Result == null || q.Verified == null || q.SignedOff != true) && q.Required))
1494+
select new
1495+
{
1496+
SelfAssessedCount = selfAssessedCount,
1497+
VerifiedCount = verifiedCount,
1498+
Questions = questions.Count()
1499+
};
1500+
1501+
int sumVerifiedCount = competencySummaries.Sum(item => item.VerifiedCount);
1502+
int sumQuestions = competencySummaries.Sum(item => item.Questions);
1503+
var model = new ViewModels.LearningPortal.SelfAssessments.CompetencySelfAssessmentCertificateViewModel(competencymaindata, competencycount, "Proficiencies", accessors, activitySummaryCompetencySelfAssesment, sumQuestions, sumVerifiedCount, null);
1504+
var renderedViewHTML = RenderRazorViewToString(this, "SelfAssessments/DownloadCompetencySelfAssessmentCertificate", model);
1505+
1506+
var pdfReportResponse = await pdfService.PdfReport(candidateAssessmentId.ToString(), renderedViewHTML, delegateId);
1507+
if (pdfReportResponse != null)
1508+
{
1509+
do
1510+
{
1511+
pdfReportStatusResponse = await pdfService.PdfReportStatus(pdfReportResponse);
1512+
} while (pdfReportStatusResponse.Id == 1);
1513+
1514+
var pdfReportFile = await pdfService.GetPdfReportFile(pdfReportResponse);
1515+
if (pdfReportFile != null)
1516+
{
1517+
var nameTextLength = string.IsNullOrEmpty(model.CompetencySelfAssessmentCertificates.LearnerName) ? 0 : model.CompetencySelfAssessmentCertificates.LearnerName.Length;
1518+
var isPrnExist = !string.IsNullOrEmpty(model.CompetencySelfAssessmentCertificates.LearnerPRN);
1519+
var fileName = $"Competency Certificate - {model.CompetencySelfAssessmentCertificates.LearnerName.Substring(0, nameTextLength >= 15 ? 15 : nameTextLength)}" + (isPrnExist ? $" - {model.CompetencySelfAssessmentCertificates.LearnerPRN}.pdf" : ".pdf");
1520+
return File(pdfReportFile, FileHelper.GetContentTypeFromFileName(fileName), fileName);
1521+
}
1522+
}
1523+
return View("SelfAssessments/CompetencySelfAssessmentCertificate", model);
1524+
}
1525+
private static string RenderRazorViewToString(Controller controller, string viewName, object model = null)
1526+
{
1527+
controller.ViewData.Model = model;
1528+
using (var sw = new StringWriter())
1529+
{
1530+
IViewEngine viewEngine =
1531+
controller.HttpContext.RequestServices.GetService(typeof(ICompositeViewEngine)) as
1532+
ICompositeViewEngine;
1533+
ViewEngineResult viewResult = viewEngine.FindView(controller.ControllerContext, viewName, false);
1534+
1535+
ViewContext viewContext = new ViewContext(
1536+
controller.ControllerContext,
1537+
viewResult.View,
1538+
controller.ViewData,
1539+
controller.TempData,
1540+
sw,
1541+
new HtmlHelperOptions()
1542+
);
1543+
viewResult.View.RenderAsync(viewContext);
1544+
return sw.GetStringBuilder().ToString();
1545+
}
1546+
}
14361547
}
14371548
}

DigitalLearningSolutions.Web/Controllers/SupervisorController/SupervisorController.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public partial class SupervisorController : Controller
2626
private readonly IEmailService emailService;
2727
private readonly ICandidateAssessmentDownloadFileService candidateAssessmentDownloadFileService;
2828
private readonly IClockUtility clockUtility;
29+
private readonly IPdfService pdfService;
2930

3031
public SupervisorController(
3132
ISupervisorService supervisorService,
@@ -45,7 +46,8 @@ public SupervisorController(
4546
IEmailGenerationService emailGenerationService,
4647
IEmailService emailService,
4748
ICandidateAssessmentDownloadFileService candidateAssessmentDownloadFileService,
48-
IClockUtility clockUtility
49+
IClockUtility clockUtility,
50+
IPdfService pdfService
4951
)
5052
{
5153
this.supervisorService = supervisorService;
@@ -62,6 +64,7 @@ IClockUtility clockUtility
6264
this.emailService = emailService;
6365
this.candidateAssessmentDownloadFileService = candidateAssessmentDownloadFileService;
6466
this.clockUtility = clockUtility;
67+
this.pdfService = pdfService;
6568
}
6669

6770
private int GetCentreId()

DigitalLearningSolutions.Web/Views/LearningPortal/SelfAssessments/CompetencySelfAssessmentCertificate.cshtml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@
8181
<a class="nhsuk-button "
8282
asp-controller="LearningPortal"
8383
asp-route-candidateAssessmentId="@Model.CompetencySelfAssessmentCertificates.CandidateAssessmentID"
84-
asp-route-vocabulary="Proficiencies"
8584
asp-action="DownloadCertificate"
8685
role="button">
8786
Download certificate
@@ -90,9 +89,8 @@
9089
@if (Model.Vocabulary == "ProfileAssessment")
9190
{
9291
<a class="nhsuk-button "
93-
asp-controller="LearningPortal"
92+
asp-controller="Supervisor"
9493
asp-route-candidateAssessmentId="@Model.CompetencySelfAssessmentCertificates.CandidateAssessmentID"
95-
asp-route-vocabulary="ProfileAssessment"
9694
asp-action="DownloadCertificate"
9795
role="button">
9896
Download certificate

0 commit comments

Comments
 (0)