public ActionResult Summary() { if (!WasTestStarted.HasValue) { return(RedirectToAction("StartPage", "Quiz")); } if (WasTestStarted.Value && new QuestionViewModel().RemainingTime.TotalSeconds > 0 && !QuestionViewModel.QuizEnded) { return(RedirectToAction("Question", "Quiz", new { QuestionID = this.QuizUser.CurrentQuestionID })); } var viewModel = new LastPageViewModel(); double questionTotalPoints = 7; double currentScore = 0; var questionsIds = _testQuestionsDal.GetQuestionsIdsByTestId(QuizUser.TestId); viewModel.QuestionsAndAnswers = new List <LastPageViewModel.AnswerStatisticsViewModel>(); foreach (var QuestionID in questionsIds) { var question = _questionDal.GetQuestionById(QuestionID); var numberOfCorrectQuestionAnswers = _answerDal.GetAnswersByQuestionID(question.Id).Where(p => p.Value == true).Count(); var valuePoint = questionTotalPoints / numberOfCorrectQuestionAnswers; var obtainedPoints = 0.0; var userAnswers = _testQuestionsAnswerDal.GetTestQuestionAnswersbyUser(QuizUser.Id, QuizUser.TestId, QuestionID); var questionsAnswers = _answerDal.GetAnswersByQuestionID(QuestionID); if (_questionDal.GetQuestionTypeById(QuestionID) != "radio") { foreach (var answer in userAnswers) { if (questionsAnswers.Where(p => p.Id == answer.AnswerId).First().Value) { obtainedPoints += valuePoint; } else { obtainedPoints -= valuePoint; } } } else { if (questionsAnswers.Where(p => p.Id == userAnswers.FirstOrDefault().AnswerId).FirstOrDefault().Value) { obtainedPoints += valuePoint; } } if (obtainedPoints > 0) { currentScore += Math.Ceiling(obtainedPoints); } var userAnswersIds = userAnswers.Select(p => p.AnswerId); viewModel.QuestionsAndAnswers.Add( new LastPageViewModel.AnswerStatisticsViewModel { questionText = _questionDal.GetAllQuestions().Where(p => p.Id == question.Id).FirstOrDefault().Text_Question, answerCorrectText = _answerDal.GetAllAnswers().Where(p => p.QuestionID == question.Id && p.Value == true).Select(p => p.Text_Answer).ToList(), aswerPosibleText = _answerDal.GetAllAnswers().Where(p => p.QuestionID == question.Id && userAnswersIds.Contains(p.Id)).Select(p => p.Text_Answer).ToList() }); } viewModel.Points = currentScore; viewModel.FinalDateTime = DateTime.Now; viewModel.Percent = viewModel.Points * 100 / 70; viewModel.InitialDateTime = QuestionViewModel.StartTimeAndDate; viewModel.Timer = viewModel.FinalDateTime.TimeOfDay.Subtract(viewModel.InitialDateTime.TimeOfDay); //Send email to admin var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>"; var message = new MailMessage(); var adminEmail = _userDal.GetAllUsers().Where(p => p.IsAdmin).FirstOrDefault().Email; message.To.Add(new MailAddress(adminEmail)); message.From = new MailAddress("*****@*****.**"); message.Subject = "Interview"; message.Body = "Hey Admin the username " + QuizUser.Username + " got the score " + currentScore + " and the procentage " + viewModel.Percent; using (var smtp = new SmtpClient()) { var credential = new NetworkCredential { UserName = "******", // replace with valid value Password = "******" // replace with valid value }; smtp.Credentials = credential; smtp.Host = "smtp.gmail.com"; smtp.Port = 587; smtp.EnableSsl = true; smtp.Send(message); } // Logout(); return(View(viewModel)); }
public LastPage() { InitializeComponent(); lastPageViewModel = new LastPageViewModel(); BindingContext = lastPageViewModel; }