|  | 
| 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 |     { | 
| @@ -1401,5 +1408,109 @@ public IActionResult CompetencySelfAssessmentCertificatesupervisor(int candidate | 
| 1401 | 1408 |             var model = new ViewModels.LearningPortal.SelfAssessments.CompetencySelfAssessmentCertificateViewModel(competencymaindata, competencycount, "ProfileAssessment", accessors, activitySummaryCompetencySelfAssesment, sumQuestions, sumVerifiedCount, supervisorDelegateId); | 
| 1402 | 1409 |             return View("SelfAssessments/CompetencySelfAssessmentCertificate", model); | 
| 1403 | 1410 |         } | 
|  | 1411 | +        [Route("/Supervisor/Staff/{CandidateAssessmentId:int}/ProfileAssessment/DownloadCertificate")] | 
|  | 1412 | +        public async Task<IActionResult> DownloadCertificate(int candidateAssessmentId) | 
|  | 1413 | +        { | 
|  | 1414 | +            PdfReportStatusResponse pdfReportStatusResponse = new PdfReportStatusResponse(); | 
|  | 1415 | +            var delegateId = User.GetCandidateIdKnownNotNull(); | 
|  | 1416 | +            var adminId = User.GetAdminId(); | 
|  | 1417 | +            var competencymaindata = selfAssessmentService.GetCompetencySelfAssessmentCertificate(candidateAssessmentId); | 
|  | 1418 | +            if (competencymaindata == null || candidateAssessmentId == 0 || adminId == 0) | 
|  | 1419 | +            { | 
|  | 1420 | +                return RedirectToAction("StatusCode", "LearningSolutions", new { code = 403 }); | 
|  | 1421 | +            } | 
|  | 1422 | +                var supervisorDelegateDetails = supervisorService.GetSupervisorDelegateDetailsForAdminId(adminId.Value); | 
|  | 1423 | +                var checkSupervisorDelegate = supervisorDelegateDetails.Where(x => x.DelegateUserID == competencymaindata.LearnerId).FirstOrDefault(); | 
|  | 1424 | +                if (checkSupervisorDelegate == null) return RedirectToAction("StatusCode", "LearningSolutions", new { code = 403 }); | 
|  | 1425 | +            var delegateUserId = competencymaindata.LearnerId; | 
|  | 1426 | +            var competencycount = selfAssessmentService.GetCompetencyCountSelfAssessmentCertificate(candidateAssessmentId); | 
|  | 1427 | +            var accessors = selfAssessmentService.GetAccessor(competencymaindata.SelfAssessmentID, competencymaindata.LearnerId); | 
|  | 1428 | +            var activitySummaryCompetencySelfAssesment = selfAssessmentService.GetActivitySummaryCompetencySelfAssesment(competencymaindata.Id); | 
|  | 1429 | +            var assessment = selfAssessmentService.GetSelfAssessmentForCandidateById(delegateUserId, competencymaindata.SelfAssessmentID); | 
|  | 1430 | +            var recentResults = selfAssessmentService.GetMostRecentResults(competencymaindata.SelfAssessmentID, competencymaindata.LearnerDelegateAccountId).ToList(); | 
|  | 1431 | +            var competencyIds = recentResults.Select(c => c.Id).ToArray(); | 
|  | 1432 | +            var competencyFlags = frameworkService.GetSelectedCompetencyFlagsByCompetecyIds(competencyIds); | 
|  | 1433 | +            var competencies = CompetencyFilterHelper.FilterCompetencies(recentResults, competencyFlags, null); | 
|  | 1434 | +            var supervisorSignOffs = selfAssessmentService.GetSupervisorSignOffsForCandidateAssessment(competencymaindata.SelfAssessmentID, delegateUserId); | 
|  | 1435 | +            if (!CertificateHelper.CanViewCertificate(recentResults, supervisorSignOffs)) | 
|  | 1436 | +            { | 
|  | 1437 | +                return RedirectToAction("StatusCode", "LearningSolutions", new { code = 401 }); | 
|  | 1438 | +            } | 
|  | 1439 | +            foreach (var competency in competencies) | 
|  | 1440 | +            { | 
|  | 1441 | +                competency.QuestionLabel = assessment.QuestionLabel; | 
|  | 1442 | +                foreach (var assessmentQuestion in competency.AssessmentQuestions) | 
|  | 1443 | +                { | 
|  | 1444 | +                    if (assessmentQuestion.AssessmentQuestionInputTypeID != 2) | 
|  | 1445 | +                    { | 
|  | 1446 | +                        assessmentQuestion.LevelDescriptors = selfAssessmentService | 
|  | 1447 | +                            .GetLevelDescriptorsForAssessmentQuestion( | 
|  | 1448 | +                                assessmentQuestion.Id, | 
|  | 1449 | +                                assessmentQuestion.MinValue, | 
|  | 1450 | +                                assessmentQuestion.MaxValue, | 
|  | 1451 | +                                assessmentQuestion.MinValue == 0 | 
|  | 1452 | +                            ).ToList(); | 
|  | 1453 | +                    } | 
|  | 1454 | +                } | 
|  | 1455 | +            } | 
|  | 1456 | + | 
|  | 1457 | +            var CompetencyGroups = competencies.GroupBy(competency => competency.CompetencyGroup); | 
|  | 1458 | +            var competencySummaries = from g in CompetencyGroups | 
|  | 1459 | +                                      let questions = g.SelectMany(c => c.AssessmentQuestions).Where(q => q.Required) | 
|  | 1460 | +                                      let selfAssessedCount = questions.Count(q => q.Result.HasValue) | 
|  | 1461 | +                                      let verifiedCount = questions.Count(q => !((q.Result == null || q.Verified == null || q.SignedOff != true) && q.Required)) | 
|  | 1462 | +                                      select new | 
|  | 1463 | +                                      { | 
|  | 1464 | +                                          SelfAssessedCount = selfAssessedCount, | 
|  | 1465 | +                                          VerifiedCount = verifiedCount, | 
|  | 1466 | +                                          Questions = questions.Count() | 
|  | 1467 | +                                      }; | 
|  | 1468 | + | 
|  | 1469 | +            int sumVerifiedCount = competencySummaries.Sum(item => item.VerifiedCount); | 
|  | 1470 | +            int sumQuestions = competencySummaries.Sum(item => item.Questions); | 
|  | 1471 | +            var model = new ViewModels.LearningPortal.SelfAssessments.CompetencySelfAssessmentCertificateViewModel(competencymaindata, competencycount, "Proficiencies", accessors, activitySummaryCompetencySelfAssesment, sumQuestions, sumVerifiedCount, null); | 
|  | 1472 | +            var renderedViewHTML = RenderRazorViewToString(this, "SelfAssessments/DownloadCompetencySelfAssessmentCertificate", model); | 
|  | 1473 | + | 
|  | 1474 | +            var pdfReportResponse = await pdfService.PdfReport(candidateAssessmentId.ToString(), renderedViewHTML, delegateId); | 
|  | 1475 | +            if (pdfReportResponse != null) | 
|  | 1476 | +            { | 
|  | 1477 | +                do | 
|  | 1478 | +                { | 
|  | 1479 | +                    pdfReportStatusResponse = await pdfService.PdfReportStatus(pdfReportResponse); | 
|  | 1480 | +                } while (pdfReportStatusResponse.Id == 1); | 
|  | 1481 | + | 
|  | 1482 | +                var pdfReportFile = await pdfService.GetPdfReportFile(pdfReportResponse); | 
|  | 1483 | +                if (pdfReportFile != null) | 
|  | 1484 | +                { | 
|  | 1485 | +                    var nameTextLength = string.IsNullOrEmpty(model.CompetencySelfAssessmentCertificates.LearnerName) ? 0 : model.CompetencySelfAssessmentCertificates.LearnerName.Length; | 
|  | 1486 | +                    var isPrnExist = !string.IsNullOrEmpty(model.CompetencySelfAssessmentCertificates.LearnerPRN); | 
|  | 1487 | +                    var fileName = $"Competency Certificate - {model.CompetencySelfAssessmentCertificates.LearnerName.Substring(0, nameTextLength >= 15 ? 15 : nameTextLength)}" + (isPrnExist ? $" - {model.CompetencySelfAssessmentCertificates.LearnerPRN}.pdf" : ".pdf"); | 
|  | 1488 | +                    return File(pdfReportFile, FileHelper.GetContentTypeFromFileName(fileName), fileName); | 
|  | 1489 | +                } | 
|  | 1490 | +            } | 
|  | 1491 | +            return View("SelfAssessments/CompetencySelfAssessmentCertificate", model); | 
|  | 1492 | +        } | 
|  | 1493 | +        private static string RenderRazorViewToString(Controller controller, string viewName, object model = null) | 
|  | 1494 | +        { | 
|  | 1495 | +            controller.ViewData.Model = model; | 
|  | 1496 | +            using (var sw = new StringWriter()) | 
|  | 1497 | +            { | 
|  | 1498 | +                IViewEngine viewEngine = | 
|  | 1499 | +                    controller.HttpContext.RequestServices.GetService(typeof(ICompositeViewEngine)) as | 
|  | 1500 | +                        ICompositeViewEngine; | 
|  | 1501 | +                ViewEngineResult viewResult = viewEngine.FindView(controller.ControllerContext, viewName, false); | 
|  | 1502 | + | 
|  | 1503 | +                ViewContext viewContext = new ViewContext( | 
|  | 1504 | +                    controller.ControllerContext, | 
|  | 1505 | +                    viewResult.View, | 
|  | 1506 | +                    controller.ViewData, | 
|  | 1507 | +                    controller.TempData, | 
|  | 1508 | +                    sw, | 
|  | 1509 | +                    new HtmlHelperOptions() | 
|  | 1510 | +                ); | 
|  | 1511 | +                viewResult.View.RenderAsync(viewContext); | 
|  | 1512 | +                return sw.GetStringBuilder().ToString(); | 
|  | 1513 | +            } | 
|  | 1514 | +        } | 
| 1404 | 1515 |     } | 
| 1405 | 1516 | } | 
0 commit comments