public ActionResult TestResult(int testId) { var questions = DataAcess.TestManagment.GetQuestionList(testId); TestResultViewModel testResult = new TestResultViewModel { TestId = testId, TestName = DataAcess.TestManagment.GetTest(testId).TestName, TotalQuestions = questions.Count }; foreach (var questionid in questions) { var questionResult = new QuestionResultViewModel { QuestionId = questionid }; var question = DataAcess.TestManagment.GetQuestion(testId, questionid); int userRightAnswered = 0; foreach (var option in question.UserChoice) { if (!questionResult.UserAnswer.Contains(option)) { questionResult.UserAnswer.Add(option); } foreach (var answer in question.RightAnswer) { if (!questionResult.RightAnswer.Contains(answer)) { questionResult.RightAnswer.Add(answer); } if (option.ToLower().Contains(answer.ToLower())) { userRightAnswered++; } } } if (userRightAnswered == question.RightAnswer.Count) { testResult.UserRightAnswered++; } if (question.UserChoice.Count > 0) { testResult.UserAnswered++; } if (DataAcess.TestManagment.GetUser(User.Identity.Name).Role.Contains(DataAcess.DomainModels.Role.Admin)) { testResult.QuestionInfo.Add(questionResult); } } return(View(testResult)); }
/// <summary> /// Check and display answer method. This method determines whether the game is over based on the question number the user is on. plays a sound if the answer is right /// or plays a sound if the answer is wrong. after the game is over pushes the data into the high scores window. resets the time and the current question number /// shows the high score window dialog. calls the check answer function to see if the answer is right or wrong and increment the appropriate variables. Shows a message /// box if the user made it to the top 10 highest scores or not. removes the bottom user from the list if more 10 or more and a new high score is added /// displays the next question. /// </summary> private void checkAndDisplayAnswer() { PlayGameViewModel pgw = (PlayGameViewModel)this.DataContext; QuestionResultViewModel qrvm = new QuestionResultViewModel(pgw.QuestionResults.Count + 1); this.product.Text = String.Empty; if (pgw.currentQuestionNumber >= pgw.maximumQuestionNumber) { pgw.QuestionResults.Add(qrvm); pgw.checkAnswer(); SoundPlayer answerRight = new SoundPlayer("highScores.wav"); answerRight.Play(); EnterUserDataViewModel eudv = ((MainWindowViewModel)Application.Current.MainWindow.DataContext).EnterUserDataVM; HighScoreWindowViewModel hsvw = ((MainWindowViewModel)Application.Current.MainWindow.DataContext).HighScoreWindowVM; pgw.StopTimer(); HighScoresWindow hsw = new HighScoresWindow(); hsw.DataContext = ((MainWindowViewModel)Application.Current.MainWindow.DataContext).HighScoreWindowVM; if (hsvw.HighScores.Count() >= 10 && pgw.QuestionResults.Count(x => x.IsCorrect == true) <= hsvw.HighScores.ToArray()[9].CorrectAnswers) { MessageBox.Show("I am sorry, but you did not make it to the top 10 highest scores. Please click OK to advance to the High Score Screen."); } else { if (hsvw.HighScores.Count() < 10) { hsvw.HighScoresAdd(eudv.TheCurrentUser.UserName, pgw.QuestionResults.Count(x => x.IsCorrect == true), pgw.QuestionResults.Count(x => x.IsCorrect == false), pgw.gameTime); MessageBox.Show("Congratulations! You made it to the top 10 highest scores! Please click OK to view your score and compare it to the other top 10 players."); } else { hsvw.HighScores.Remove(hsvw.HighScores.ToArray()[9]); hsvw.HighScoresAdd(eudv.TheCurrentUser.UserName, pgw.QuestionResults.Count(x => x.IsCorrect == true), pgw.QuestionResults.Count(x => x.IsCorrect == false), pgw.gameTime); MessageBox.Show("Congratulations! You made it to the top 10 highest scores! Please click OK to view your score and compare it to the other top 10 players."); } } pgw.ResetTimer(); pgw.currentQuestionNumber = 0; pgw.Product = 0; pgw.QuestionResults.Clear(); hsw.ShowDialog(); this.Close(); return; } pgw.QuestionResults.Add(qrvm); pgw.checkAnswer(); pgw.DisplayQuestion(); }
public static TestDetailResultViewModel ConvertToViewModelResult(this IEnumerable <TestDetail> testQuestionAnswers) { TestDetailResultViewModel testQuestionAnswerViewModel = new TestDetailResultViewModel(); DateTime?testMarkAnswer = null; QuestionResultViewModel questionViewModel = new QuestionResultViewModel(); List <AnswerResultViewModel> answerViewModels = new List <AnswerResultViewModel>(); var testQuestionAnswerList = testQuestionAnswers.ToList(); for (int i = 0; i < testQuestionAnswerList.Count(); i++) { if (testQuestionAnswerViewModel.Id == Guid.Empty) { testQuestionAnswerViewModel = new TestDetailResultViewModel { Id = testQuestionAnswerList[i].TestId } } ; if (questionViewModel.QuestionId != Guid.Empty && questionViewModel.QuestionId != testQuestionAnswerList[i].QuestionId) { questionViewModel.MarkOn = testMarkAnswer; questionViewModel.AnswerResultViewModels = answerViewModels; testQuestionAnswerViewModel.QuestionResultViewModels.Add(questionViewModel); answerViewModels = new List <AnswerResultViewModel>(); } answerViewModels.Add(new AnswerResultViewModel { AnswerId = testQuestionAnswerList[i].AnswerId, AnswerText = testQuestionAnswerList[i].AnswerText, MarkAnswer = testQuestionAnswerList[i].MarkAnswer, CorrectAnswer = testQuestionAnswerList[i].CorrectAnswer }); if (testQuestionAnswerList[i].MarkAnswer) { testMarkAnswer = testQuestionAnswerList[i].UpdateOn; } if (testQuestionAnswerList.Count() - 1 == i) { questionViewModel.MarkOn = testMarkAnswer; questionViewModel.AnswerResultViewModels = answerViewModels; testQuestionAnswerViewModel.QuestionResultViewModels.Add(questionViewModel); break; } questionViewModel = new QuestionResultViewModel { QuestionId = testQuestionAnswerList[i].QuestionId, QuestionText = testQuestionAnswerList[i].QuestionText }; } return(testQuestionAnswerViewModel); }
public static QuestionResultViewModel MapQuestionPersistenceToResultViewModel(DAL.Persistence.Question question) { var answers = question.Answers.Select(a => new AnswerViewModel() { Id = a.Id, Body = a.Body, QuestionId = question.Id }); var result = new QuestionResultViewModel() { Id = question.Id, Title = question.Title, Answers = answers }; return(result); }
private void WatchForChanges(QuestionViewModel question, QuestionResultViewModel questionResult) { var answerIds = question.Answers .Select(a => a.Id) .ToArray(); var votesMutable = questionResult.Answers .ToDictionary(a => a.AnswerId, a => a.Votes); var subscription = _notificator.Subscribe( filter: changedAnswerId => answerIds.Contains(changedAnswerId), onNext: changedAnswerId => AnswerResultChanged(changedAnswerId, question, votesMutable)); using (subscription) { Console.ReadKey(true); Console.WriteLine(); } }
public IActionResult Results(int?id) { var question = _context.Questions.FirstOrDefault(m => m.Id == id); if (question == null) { return(RedirectToAction("Index", "Home")); } var votes = _context.Votes.Where(m => m.Question == question).ToList(); var answers = _context.Answers.Where(m => m.Question == question).ToList(); var votecounts = answers.Select(m => votes.Count(v => v.Answer == m)).ToList(); var resVM = new QuestionResultViewModel() { QuestionText = question.Subject, Answers = answers, VoteCounts = votecounts }; return(View(resVM)); }
private static void WriteResultToConsole(QuestionViewModel question, QuestionResultViewModel questionResult) { var allVotes = questionResult.Answers.Sum(ar => ar.Votes); Console.WriteLine(); Console.WriteLine($"{question.QuestionText}"); var index = 1; foreach (var answerResult in questionResult.Answers) { var answerText = question.Answers .Single(a => a.Id == answerResult.AnswerId) .Text; Console.WriteLine($"[{index}]: {answerText} - {GetPercentage(answerResult.Votes, allVotes)} ({answerResult.Votes} votes)"); index++; } Console.WriteLine(); }
public IHttpActionResult GetQuestion(int id) { try { var identity = (ClaimsIdentity)User.Identity; var userId = int.Parse(identity.Claims.Where(c => c.Type == ClaimTypes.NameIdentifier).FirstOrDefault().Value); var question = questionService.GetQuestion(id); if (question == null) { return(BadRequest()); } var retList = questionService.GetForSurvey(question.SurveyId).ToList(); var users = retList.Select(u => u.UserId).ToList(); var profiles = questionService.GetUsers(users).ToList(); var questionResultVM = new QuestionResultViewModel() { Question = question, Users = new List <UserWithEncrptIdViewModel>() }; if (question.Survey.UserId != userId) { return(Unauthorized()); } profiles.ForEach(p => { var answers = new Dictionary <int, bool>(); if (!question.Survey.Anonymous) { var objVM = new UserWithEncrptIdViewModel() { FirstName = p.FirstName, LastName = p.LastName, EncrptId = retList.FirstOrDefault(us => us.UserId == p.Id).EncrptUserId, UserId = p.Id }; if (question.AnswerType != Model.Model.AnswerType.Text) { foreach (var questionAnswer in question.QuestionAnswers) { answers.Add(questionAnswer.Id, question.UserAnswers.Where(ua => ua.AnswerId == questionAnswer.Id).Select(ua => ua.UserId).Contains(p.Id)); } objVM.Answers = answers; } else { var userAnswer = question.UserAnswers.Where(ua => ua.UserId == p.Id).FirstOrDefault(); if (userAnswer == null) { objVM.AnswerText = "Nije odgovorio"; } else { objVM.AnswerText = userAnswer.Answer.AnswerText; } } questionResultVM.Users.Add(objVM); } else { var objVM = new UserWithEncrptIdViewModel() { FirstName = profiles.IndexOf(p).ToString(), LastName = "************", EncrptId = retList.FirstOrDefault(us => us.UserId == p.Id).EncrptUserId, UserId = 0 }; if (question.AnswerType != Model.Model.AnswerType.Text) { foreach (var questionAnswer in question.QuestionAnswers) { answers.Add(questionAnswer.Id, question.UserAnswers.Where(ua => ua.AnswerId == questionAnswer.Id).Select(ua => ua.UserId).Contains(p.Id)); } objVM.Answers = answers; } else { var userAnswer = question.UserAnswers.Where(ua => ua.UserId == p.Id).FirstOrDefault(); if (userAnswer == null) { objVM.AnswerText = "Nije odgovorio"; } else { objVM.AnswerText = userAnswer.Answer.AnswerText; } } questionResultVM.Users.Add(objVM); } }); return(Ok(questionResultVM)); } catch (Exception) { return(BadRequest()); } }