Example #1
0
        public CertificateDTO GetCourseCertificate(int courseId)
        {
            try
            {
                var entity = CertificateRepository.GetMany(x => x.CourseId == courseId).FirstOrDefault();

                if (entity == null)
                {
                    var courseEntity = CourseRepository.GetById(courseId);

                    return(courseEntity != null ? new CertificateDTO(courseId, courseEntity.CourseName, this.GetCurrentUserName()) : new CertificateDTO {
                        IsValid = false, Message = "Course not found"
                    });
                }

                var token = entity.Entity2CertificateDto(StudentCertificatesRepository.IsAny(x => x.CertificateId == entity.CertificateId), CourseQuizzesRepository.IsAny(x => x.CourseId == entity.CourseId && x.AttachCertificate));

                return(token);
            }
            catch (Exception ex)
            {
                Logger.Error("get certificate token", courseId, ex, CommonEnums.LoggerObjectTypes.Certificate);
                return(new CertificateDTO
                {
                    IsValid = false
                    , Message = FormatError(ex)
                });
            }
        }
Example #2
0
        public bool DeleteCertificate(int certId, out string error)
        {
            try
            {
                if (StudentCertificatesRepository.IsAny(x => x.CertificateId == certId))
                {
                    error = "Certificate already sent to students and can't be deleted. You could disable it.";
                    return(false);
                }

                CertificateRepository.Delete(x => x.CertificateId == certId);
                return(CertificateRepository.UnitOfWork.CommitAndRefreshChanges(out error));
            }
            catch (Exception ex)
            {
                Logger.Error("delete certificate", certId, ex, CommonEnums.LoggerObjectTypes.Certificate);
                error = FormatError(ex);
                return(false);
            }
        }
Example #3
0
        public StudentCertificateDTO GetStudentCertificate(int courseId, int userId)
        {
            try
            {
                var studentCertificate = StudentCertificatesViewRepository.Get(x => x.CourseId == courseId && x.StudentUserId == userId);

                if (studentCertificate != null)
                {
                    return(studentCertificate.Entity2StudentCertificateDto());
                }

                var certificateEntity = CertificateRepository.GetMany(x => x.CourseId == courseId).First();

                if (certificateEntity == null)
                {
                    Logger.Warn("GetStudentCertificate for course " + courseId + ":: certificate entity not found", CommonEnums.LoggerObjectTypes.Quiz);
                    return(new StudentCertificateDTO {
                        IsValid = false, Message = "certificate entity not found"
                    });
                }

                var studentCertificateEntity = certificateEntity.CertificateEntity2StudentCertificateEntity(userId);

                StudentCertificatesRepository.Add(studentCertificateEntity);

                string error;

                return(StudentCertificatesRepository.UnitOfWork.CommitAndRefreshChanges(out error) ?
                       StudentCertificatesViewRepository.Get(x => x.CourseId == courseId && x.StudentUserId == userId).Entity2StudentCertificateDto() :
                       new StudentCertificateDTO {
                    IsValid = false, Message = error
                });
            }
            catch (Exception ex)
            {
                Logger.Error("GetStudentCertificate for course " + courseId, userId, ex, CommonEnums.LoggerObjectTypes.Certificate);
                return(new StudentCertificateDTO {
                    IsValid = false, Message = FormatError(ex)
                });
            }
        }
Example #4
0
        public bool SendStudentCertificate(StudentCertificateDTO token, string certificateBody, out string error)
        {
            try
            {
                var strKey = string.Format("{0}{3}{1}{3}{2}", token.StudentCertificateId, token.StudentInfo.UserId, token.CertificateId, KEY_SEPARATOR);
                var key    = _encryptionServices.EncryptText(strKey);
                token.Key = Uri.EscapeDataString(key);

                token.OnlineCertificateUrl = $"{"https:"}//{new Uri(Utils.GetKeyValue("baseUrl")).Authority}/Widget/{"User"}/{"Certificate"}?key={Uri.EscapeDataString(key)}";


                long emailId;
                _emailServices.SaveStudentCertificateMessage(token, certificateBody, out emailId, out error);

                if (emailId < 0)
                {
                    return(false);
                }

                var pdfConverter = new StandardPdfRenderer();

                // var fileName = $"Certificate_{token.StudentInfo.UserId}_{ShortGuid.NewGuid()}.pdf";
                //				var virtualUrl = "~/Certificates/" + fileName;
                //				var path       = HttpContext.Current.Server.MapPath(virtualUrl);
                //
                //				//var bytes = pdfConverter.Html2Pdf(certificateBody);
                //				//File.WriteAllBytes(path, bytes);
                //
                //                if (File.Exists(path)) TryDeleteFile(path);
                //
                //                var pdfDoc = pdfConverter.Html2PdfDoc(certificateBody, PdfPageSize.A4);
                //
                //				pdfDoc.WriteToFile(path);
                //
                //                pdfDoc.Close();
                //
                //                pdfDoc.ReleaseSourceDoc();

                //                var sended = _amazonEmailWrapper.SendEmailWithAttachment(emailId, path, out error);

                var outstream = pdfConverter.Html2PdfStream(certificateBody);
//
//                var filePath = $"{FileEnums.eFileOwners.Student}/Certificates/{token.StudentInfo.UserId}/{fileName}";
//
//                _s3Wrapper.Upload(filePath, "application/octet-stream", outstream, out error);
//
//                // var fullCertificatePath = $"{S3_ROOT_URL}{S3_BUCKET_NAME}/{filePath}";
//
//                outstream = pdfConverter.Html2PdfStream(certificateBody);

                var sended = _amazonEmailWrapper.SendEmailWithAttachment(emailId, outstream, out error);

                if (sended)
                {
                    var entity = StudentCertificatesRepository.GetById(token.StudentCertificateId);

                    entity.UpdateCertificateSendDate();

                    StudentCertificatesRepository.UnitOfWork.CommitAndRefreshChanges();
                }
                outstream.Close();
                outstream.Dispose();

                return(sended);
            }
            catch (Exception ex)
            {
                Logger.Error("Send Student Certificate for course " + token.CourseId, CurrentUserId, ex, CommonEnums.LoggerObjectTypes.Certificate);
                error = FormatError(ex);
                return(false);
            }
        }