예제 #1
0
        private string GenerateValidationCode(string username)
        {
            string code = _randomProvider.GenerateRandomCode(ValidationCodeLength);
            User   user = _userService.GetByUsername(username);

            if (user == null)
            {
                throw new UserNotFoundException(username);
            }

            user.ValidationCode = code;
            _users.SaveChanges();
            return(code);
        }
예제 #2
0
        public Certificate GenerateCertificate(string username, int courseId, AssessmentSubmission assessmentSubmission)
        {
            User   user   = _users.GetByUsername(username);
            Course course = _courses.GetById(courseId);

            Certificate certificate = new Certificate
            {
                UserId = user.Id,
                AssesmentSubmissionId = assessmentSubmission.Id,
                CourseId = course.Id,
                Code     = _random.GenerateRandomCode(10)
            };

            CertificateGenerationInfo certificateGenerationInfo = _generationInfoProvider.GetByCourseId(courseId);

            string certificateRelativePath = string.Format(CertificateFilePathFormat, certificate.Code);
            string filePath = Path.Combine(certificateGenerationInfo.BaseFilePath, certificateRelativePath);

            certificate.FilePath = certificateRelativePath;
            _certificates.Add(certificate);
            _certificates.SaveChanges();


            Bitmap        bitmap  = (Bitmap)Image.FromFile(Path.GetFullPath(certificateGenerationInfo.TemplateFilePath));//load the image file
            QRCodeEncoder encoder = new QRCodeEncoder();
            Bitmap        certificateUrlQrCode = encoder.Encode(string.Format(CertificateUrlFormat, certificate.Code));

            using (Graphics certificateTemplate = Graphics.FromImage(bitmap))
            {
                using (Font arialFont = new Font("Arial", 16, FontStyle.Bold))
                {
                    using (Font arialFontLarge = new Font("Arial", 20, FontStyle.Bold))
                    {
                        PlaceholderInfo studentData     = certificateGenerationInfo.StudentName;
                        PlaceholderInfo courseData      = certificateGenerationInfo.CourseName;
                        PlaceholderInfo datePlaceholder = certificateGenerationInfo.IssueDate;
                        PlaceholderInfo qrPlaceholder   = certificateGenerationInfo.QrCode;
                        PlaceholderInfo certificateNumberPlaceholder = certificateGenerationInfo.CertificateNumber;
                        PlaceholderInfo moduleNamesPlaceholder       = certificateGenerationInfo.ModuleNames;
                        PlaceholderInfo numberOfHoursPlaceholder     = certificateGenerationInfo.NumberOfHours;
                        certificateTemplate.DrawString($"{user.FirstName} {user.MiddleName} {user.LastName}", arialFontLarge, new SolidBrush(ColorTranslator.FromHtml(studentData.Color)), new Rectangle(studentData.TopLeftX, studentData.TopLeftY, studentData.Width, studentData.Height));
                        certificateTemplate.DrawString(course.Title, arialFontLarge, new SolidBrush(ColorTranslator.FromHtml(courseData.Color)), new Rectangle(courseData.TopLeftX, courseData.TopLeftY, courseData.Width, courseData.Height));
                        certificateTemplate.DrawString(DateTime.Today.ToString("dd.MM.yyy") + "г.", arialFont, new SolidBrush(ColorTranslator.FromHtml(datePlaceholder.Color)), new Rectangle(datePlaceholder.TopLeftX, datePlaceholder.TopLeftY, datePlaceholder.Width, datePlaceholder.Height));
                        certificateTemplate.DrawImage(certificateUrlQrCode, new Rectangle(qrPlaceholder.TopLeftX, qrPlaceholder.TopLeftY, qrPlaceholder.Width, qrPlaceholder.Height));
                        certificateTemplate.DrawString($"Рег. № {certificate.Id}", arialFontLarge, new SolidBrush(ColorTranslator.FromHtml(certificateNumberPlaceholder.Color)), new Rectangle(certificateNumberPlaceholder.TopLeftX, certificateNumberPlaceholder.TopLeftY, certificateNumberPlaceholder.Width, certificateNumberPlaceholder.Height));
                        certificateTemplate.DrawString(course.ModuleNames, arialFont, new SolidBrush(ColorTranslator.FromHtml(moduleNamesPlaceholder.Color)), new Rectangle(moduleNamesPlaceholder.TopLeftX, moduleNamesPlaceholder.TopLeftY, moduleNamesPlaceholder.Width, moduleNamesPlaceholder.Height));
                        certificateTemplate.DrawString($"{course.NumberOfHours} учебни часа", arialFont, new SolidBrush(ColorTranslator.FromHtml(numberOfHoursPlaceholder.Color)), new Rectangle(numberOfHoursPlaceholder.TopLeftX, numberOfHoursPlaceholder.TopLeftY, numberOfHoursPlaceholder.Width, numberOfHoursPlaceholder.Height));


                        string directoryPath = Path.GetDirectoryName(filePath);
                        Debug.Assert(!string.IsNullOrEmpty(directoryPath));
                        if (!Directory.Exists(directoryPath))
                        {
                            Directory.CreateDirectory(directoryPath);
                        }

                        bitmap.Save(filePath);
                    }
                }
            }
            return(certificate);
        }