public QuizEvaluationResult EvaluateSolution(QuizSolution quizSolution) { var result = new QuizEvaluationResult { ForQuizId = quizSolution.ForQuizId, Title = quizSolution.ForQuiz.Title, CorrectlyAnswered = new List<QuestionResultModel>(), IncorrectlyAnswered = new List<QuestionResultModel>() }; foreach (Answer answer in quizSolution.SelectedAnswers) { var questionResult = new QuestionResultModel { Question = answer.ForQuestion.Title, IsCorrect = answer.IsCorrect, ResultDescription = answer.ForQuestion.ResultDescription, SelectedAnswer = answer.Text, CorrectAnswer = answer.ForQuestion .Answers.First(a => a.IsCorrect).Text }; if (answer.IsCorrect) { result.CorrectlyAnswered.Add(questionResult); } else { result.IncorrectlyAnswered.Add(questionResult); } } return result; }
public QuizEvaluationResult EvaluateSolution(QuizSolution quizSolution) { var result = new QuizEvaluationResult { ForQuizId = quizSolution.ForQuizId, Title = quizSolution.ForQuiz.Title, CorrectlyAnswered = new List <QuestionResultModel>(), IncorrectlyAnswered = new List <QuestionResultModel>() }; foreach (Answer answer in quizSolution.SelectedAnswers) { var questionResult = new QuestionResultModel { Question = answer.ForQuestion.Title, IsCorrect = answer.IsCorrect, ResultDescription = answer.ForQuestion.ResultDescription, SelectedAnswer = answer.Text, CorrectAnswer = answer.ForQuestion .Answers.First(a => a.IsCorrect).Text }; if (answer.IsCorrect) { result.CorrectlyAnswered.Add(questionResult); } else { result.IncorrectlyAnswered.Add(questionResult); } } return(result); }
// TODO: Refactor me public void AddResultToUser(TestKey key, Dictionary <string, ICollection <string> > answers) { var newEntity = TestResultModel.CreateNew(key, answers.Select(c => QuestionResultModel.CreateNew(c.Key, c.Value)).ToList()); try { var existed = _uow.Repository <TestResultModel>().GetQueryable() .Where(c => c.UserModel.Id == CurrentUser.Instance.Id && c.UnitName == key.UnitName && c.LessonName == key.LessonName); if (existed.Any()) { _uow.Repository <TestResultModel>().Delete(existed.ToList()); } newEntity.UserModel = _uow.Repository <UserModel>().GetQueryable().FirstOrDefault(CurrentUser.FindUser); _uow.Repository <TestResultModel>().Add(newEntity); _uow.SaveChanges(); } catch (Exception e) { Console.WriteLine(e); throw; } }
public void InsertQuestionResult(QuestionResultModel passedQuestion) { User currentUser = _repository.GetSingle <User>(u => u.Email == User.Identity.Name); passedQuestion.AnswerTime = DateTime.UtcNow; if (passedQuestion.QuestionType == "Descriptive") { if (passedQuestion.AnswerText == null) { passedQuestion.AnswerText = ""; } var textAnswer = _repository.GetSingle <UserTextAnswer>(a => a.QuizPassQuestion.QuestionId == passedQuestion.QuestionId && a.QuizPassQuestion.QuizPass.UserId == currentUser.Id && a.QuizPassQuestion.QuizPassId == passedQuestion.QuizPassId); if (textAnswer != null) { if (string.IsNullOrEmpty(passedQuestion.AnswerText)) { _repository.Delete <int, UserTextAnswer>("Id", textAnswer.Id); //_repository.Delete<int, QuizPassQuestion>("Id", textAnswer.QuizPassQuestionId); } else { textAnswer.AnswerText = passedQuestion.AnswerText; textAnswer.AnswerTime = passedQuestion.AnswerTime; _repository.Update <UserTextAnswer>(textAnswer); } } else { var quizzPassQuestionToCheck = _repository.GetSingle <QuizPassQuestion>(el => el.QuizPassId == passedQuestion.QuizPassId && el.QuestionId == passedQuestion.QuestionId); var userTextAnswerToInsert = new UserTextAnswer { QuizPassQuestionId = quizzPassQuestionToCheck.Id, AnswerTime = passedQuestion.AnswerTime, AnswerText = passedQuestion.AnswerText }; _repository.Insert <UserTextAnswer>(userTextAnswerToInsert); } } else if (passedQuestion.QuestionType == "Select one") { var radioAnswer = _repository.GetSingle <UserAnswer>(a => a.QuizPassQuestion.QuestionId == passedQuestion.QuestionId && a.QuizPassQuestion.QuizPass.UserId == currentUser.Id && a.QuizPassQuestion.QuizPassId == passedQuestion.QuizPassId); if (radioAnswer != null) { radioAnswer.AnswerId = (int)passedQuestion.AnswerId; radioAnswer.AnswerTime = passedQuestion.AnswerTime; _repository.Update <UserAnswer>(radioAnswer); } else { var quizzPassQuestionToCheck = _repository.GetSingle <QuizPassQuestion>(el => el.QuizPassId == passedQuestion.QuizPassId && el.QuestionId == passedQuestion.QuestionId); if (passedQuestion.AnswerId != null) { var userAnswerToInsert = new UserAnswer { QuizPassQuestionId = quizzPassQuestionToCheck.Id, AnswerTime = passedQuestion.AnswerTime, AnswerId = (int)passedQuestion.AnswerId }; _repository.Insert <UserAnswer>(userAnswerToInsert); } } } else { var checkAnswers = _repository.Get <UserAnswer>(a => a.QuizPassQuestion.QuestionId == passedQuestion.QuestionId && a.QuizPassQuestion.QuizPass.UserId == currentUser.Id && a.QuizPassQuestion.QuizPassId == passedQuestion.QuizPassId); if (checkAnswers != null && checkAnswers.Count != 0) { var allNull = true; foreach (var answer in passedQuestion.Answers) { if (answer != null) { allNull = false; } } if (allNull) { foreach (var existingCheckAnswer in checkAnswers) { _repository.Delete <int, UserAnswer>("Id", existingCheckAnswer.Id); } } else { var quizPassQuestionId = checkAnswers.First().QuizPassQuestionId; foreach (var existingCheckAnswer in checkAnswers) { _repository.Delete <int, UserAnswer>("Id", existingCheckAnswer.Id); } foreach (var newCheckAnswer in passedQuestion.Answers) { if (newCheckAnswer != null) { var userAnswerToInsert = new UserAnswer { QuizPassQuestionId = quizPassQuestionId, AnswerTime = passedQuestion.AnswerTime, AnswerId = (int)newCheckAnswer }; _repository.Insert <UserAnswer>(userAnswerToInsert); } } } } else { var quizzPassQuestionToCheck = _repository.GetSingle <QuizPassQuestion>(el => el.QuizPassId == passedQuestion.QuizPassId && el.QuestionId == passedQuestion.QuestionId); foreach (var checkAnswer in passedQuestion.Answers) { if (checkAnswer != null) { var userAnswerToInsert = new UserAnswer { QuizPassQuestionId = quizzPassQuestionToCheck.Id, AnswerTime = passedQuestion.AnswerTime, AnswerId = (int)checkAnswer }; _repository.Insert <UserAnswer>(userAnswerToInsert); } } } } }