public async Task <IActionResult> Buy(int id, int comp) { var date = DateTime.Now; var userid = HttpContext.Session.GetInt32("userid").Value; var usercourse = new UserCourse() { UserId = userid, CourseId = id, DateBegin = date, IsAll = true }; Method.CrtFill(usercourse, userid, date, HttpContext.Connection.RemoteIpAddress.ToString()); if (comp != 0) { usercourse.DateEnd = date.AddDays(comp); } db.Add(usercourse); var lectures = await db.Lectures.Where(a => a.CourseId == id && a.Active).Select(a => new { Id = a.Id, Row = a.Row, lectureGroupId = a.LectureGroupId }).ToListAsync(); foreach (var item in lectures) { var lecturetoadd = new UserCourseDetail() { CourseId = id, UserCourseId = usercourse.Id, LectureGroupId = item.lectureGroupId, UserId = userid, LectureId = item.Id, DateBegin = usercourse.DateBegin, DateEnd = usercourse.DateEnd, Row = item.Row, Done = false }; Method.CrtFill(lecturetoadd, userid, date, HttpContext.Connection.RemoteIpAddress.ToString()); db.Add(lecturetoadd); } await db.SaveChangesAsync(); return(RedirectToAction("Index", "Mycourse")); }
public async Task <IActionResult> Index(int id) { int userid = HttpContext.Session.GetInt32("userid").Value; var usercourseid = id; var resVM = new ResultIndexViewModel(); //-------- UserExamResult oluşturma-------- //sınav sonucuna göre sertifikayı oluşturacak eğer sınav sonucu yok sertifika var ise sertifikayı tekrar oluşturur. //sınav sonucu silinecekse sertifika da silinmeli... var curuserresult = await db.UserExamResults.FirstOrDefaultAsync(a => a.UserId == userid && a.UserCourseId == usercourseid); if (curuserresult == null) { var model = db.UserQuestionChoices.Include(a => a.QuestionChoice).Where(a => a.UserCourseId == id && a.UserId == userid && a.Type == SD.Poll).ToList(); resVM.all = model.Count(); resVM.correct = model.Count(a => a.QuestionChoice.IsCorrect); resVM.UserCourseId = usercourseid; var userresult = new UserExamResult() { UserId = userid, UserCourseId = usercourseid, TotalQuestion = resVM.all, TotalCorrect = resVM.correct, Score = resVM.percentage }; Method.CrtFill(userresult, userid, DateTime.Now, HttpContext.Connection.RemoteIpAddress.ToString()); var uscourse = await db.UserCourses.Include(a => a.Course).FirstOrDefaultAsync(a => a.Id == id); if (resVM.percentage > uscourse.Course.SuccessRate) { userresult.Success = resVM.Success = true; var filepath = env.WebRootPath; var certpath = ""; var certno = usercourseid + "-" + uscourse.CourseId; var user = await repo.GetUserbyId(userid).Select(a => new User { Name = a.Name, Surname = a.Surname, TcNo = a.TcNo }).FirstOrDefaultAsync(); using (Bitmap b = new Bitmap(filepath + uscourse.Course.CertificatePath.Replace('/', '\\'))) //load the image file { using (Graphics g = Graphics.FromImage(b)) { StringFormat nameFormat = new StringFormat(); nameFormat.Alignment = StringAlignment.Near; Font nameFont = new Font("sans serif", 37); Font textFont = new Font("sans serif", 41); g.DrawString("Adı Soyadı", textFont, new SolidBrush(Color.FromArgb(60, 35, 19)), new RectangleF(508, 1448, b.Width - 10, b.Height - 10), nameFormat); g.DrawString(":", textFont, new SolidBrush(Color.FromArgb(60, 35, 19)), new RectangleF(895, 1448, b.Width - 10, b.Height - 10), nameFormat); g.DrawString(user.Name + " " + user.Surname, nameFont, new SolidBrush(Color.FromArgb(60, 35, 19)), new RectangleF(968, 1448, b.Width - 10, b.Height - 10), nameFormat); g.DrawString("Tarih", textFont, new SolidBrush(Color.FromArgb(60, 35, 19)), new RectangleF(508, 1537, b.Width - 10, b.Height - 10), nameFormat); g.DrawString(":", textFont, new SolidBrush(Color.FromArgb(60, 35, 19)), new RectangleF(895, 1537, b.Width - 10, b.Height - 10), nameFormat); g.DrawString((DateTime.Now.ToString("dd.MM.yyyy")), nameFont, new SolidBrush(Color.FromArgb(60, 35, 19)), new RectangleF(968, 1537, b.Width - 10, b.Height - 10), nameFormat); g.DrawString("T.C. Kimlik No ", textFont, new SolidBrush(Color.FromArgb(60, 35, 19)), new RectangleF(508, 1625, b.Width - 10, b.Height - 10), nameFormat); g.DrawString(":", textFont, new SolidBrush(Color.FromArgb(60, 35, 19)), new RectangleF(895, 1625, b.Width - 10, b.Height - 10), nameFormat); g.DrawString(user.TcNo, nameFont, new SolidBrush(Color.FromArgb(60, 35, 19)), new RectangleF(968, 1625, b.Width - 10, b.Height - 10), nameFormat); g.DrawString("Sertifika No", textFont, new SolidBrush(Color.FromArgb(60, 35, 19)), new RectangleF(508, 1713, b.Width - 10, b.Height - 10), nameFormat); g.DrawString(":", textFont, new SolidBrush(Color.FromArgb(60, 35, 19)), new RectangleF(895, 1713, b.Width - 10, b.Height - 10), nameFormat); g.DrawString(certno, nameFont, new SolidBrush(Color.FromArgb(60, 35, 19)), new RectangleF(968, 1713, b.Width - 10, b.Height - 10), nameFormat); //StringFormat stringFormat = new StringFormat(); //stringFormat.Alignment = StringAlignment.Near; //Font stringFont = new Font("sans serif", 20); //g.DrawString("Bu başarı belgesini web sitemizden sorgulayarak bilgileri teyit edebilirsiniz.", stringFont, new SolidBrush(Color.FromArgb(60, 35, 19)), new RectangleF(620, 1905, b.Width - 10, b.Height - 10), stringFormat); //g.DrawString("kaduzem.org", stringFont, new SolidBrush(Color.FromArgb(0, 36, 255)), new RectangleF(620, 1945, b.Width - 10, b.Height - 10), stringFormat); } //usercourseid unique olduğu için certpath = "\\files\\user_certificates\\" + certno + ".png"; using (MemoryStream memory = new MemoryStream()) { var a = filepath + certpath; using (FileStream fs = new FileStream(a, FileMode.Create)) { b.Save(memory, ImageFormat.Png); byte[] bytes = memory.ToArray(); fs.Write(bytes, 0, bytes.Length); } } } var usercert = new UserCertificate() { UserId = userid, UserCourseId = usercourseid, Active = true, Path = certpath }; Method.CrtFill(usercert, userid, DateTime.Now, HttpContext.Connection.RemoteIpAddress.ToString()); curuserresult = userresult; db.Add(usercert); } else { userresult.Success = resVM.Success = false; } db.Add(userresult); await db.SaveChangesAsync(); } else { resVM.all = curuserresult.TotalQuestion; resVM.correct = curuserresult.TotalCorrect; resVM.UserCourseId = usercourseid; resVM.Success = curuserresult.Success; } return(View(resVM)); }