|
3 | 3 | using DigitalLearningSolutions.Data.Enums; |
4 | 4 | using DigitalLearningSolutions.Data.Helpers; |
5 | 5 | using DigitalLearningSolutions.Data.Models; |
| 6 | + using DigitalLearningSolutions.Data.Models.Common; |
6 | 7 | using DigitalLearningSolutions.Data.Models.SearchSortFilterPaginate; |
7 | 8 | using DigitalLearningSolutions.Data.Models.SelfAssessments; |
8 | 9 | using DigitalLearningSolutions.Data.Models.SessionData.Supervisor; |
|
11 | 12 | using DigitalLearningSolutions.Web.Extensions; |
12 | 13 | using DigitalLearningSolutions.Web.Helpers; |
13 | 14 | using DigitalLearningSolutions.Web.ServiceFilter; |
| 15 | + using DigitalLearningSolutions.Web.Services; |
14 | 16 | using DigitalLearningSolutions.Web.ViewModels.Common.SearchablePage; |
15 | 17 | using DigitalLearningSolutions.Web.ViewModels.Supervisor; |
16 | 18 | using GDS.MultiPageFormData.Enums; |
17 | 19 | using Microsoft.AspNetCore.Mvc; |
| 20 | + using Microsoft.AspNetCore.Mvc.Rendering; |
| 21 | + using Microsoft.AspNetCore.Mvc.ViewEngines; |
| 22 | + using Microsoft.AspNetCore.Mvc.ViewFeatures; |
18 | 23 | using System; |
19 | 24 | using System.Collections.Generic; |
| 25 | + using System.IO; |
20 | 26 | using System.Linq; |
| 27 | + using System.Threading.Tasks; |
21 | 28 |
|
22 | 29 | public partial class SupervisorController |
23 | 30 | { |
@@ -1433,5 +1440,109 @@ public IActionResult CompetencySelfAssessmentCertificatesupervisor(int candidate |
1433 | 1440 | var model = new ViewModels.LearningPortal.SelfAssessments.CompetencySelfAssessmentCertificateViewModel(competencymaindata, competencycount, "ProfileAssessment", accessors, activitySummaryCompetencySelfAssesment, sumQuestions, sumVerifiedCount, supervisorDelegateId); |
1434 | 1441 | return View("SelfAssessments/CompetencySelfAssessmentCertificate", model); |
1435 | 1442 | } |
| 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 | + } |
1436 | 1547 | } |
1437 | 1548 | } |
0 commit comments