private void ProcessSequenceAnswer(List <Answer> answers, Question question, AnswerOnTestQuestion answerOntestQuestion) { var isCorrect = true; if (answers.Count() != question.Answers.Count) { throw new InvalidDataException("Последовательность не совпадает с исходной"); } var plainAnswers = question.Answers.ToList(); for (var i = 0; i < answers.Count(); i++) { isCorrect = isCorrect && answers[i].Id == plainAnswers[i].Id; } if (isCorrect) { answerOntestQuestion.Points = question.ComlexityLevel; } using var repositoriesContainer = new LmPlatformRepositoriesContainer(); foreach (var userAnswerId in answers.Select(x => x.Id)) { var answer = GetAnswerById(userAnswerId); answerOntestQuestion.AnswerString += answer.Content; answerOntestQuestion.AnswerString += "\n"; } }
private Tuple <Question, int> GetNextQuestionsFromNotPassedItems(List <AnswerOnTestQuestion> notPassedQuestions, int nextQuestionNumber) { int questionId; if (notPassedQuestions.Any(question => question.Number == nextQuestionNumber)) { questionId = notPassedQuestions .Single(question => question.Number == nextQuestionNumber) .QuestionId; } else { var orderedAnswers = notPassedQuestions.OrderBy(question => question.Number).SkipWhile(question => question.Number < nextQuestionNumber).ToList(); orderedAnswers.AddRange(notPassedQuestions.OrderBy(question => question.Number).TakeWhile(question => question.Number < nextQuestionNumber)); AnswerOnTestQuestion answerOnTestQuestion = orderedAnswers.First(); nextQuestionNumber = answerOnTestQuestion.Number; questionId = answerOnTestQuestion.QuestionId; } Question resultQuestion = GetQuestionById(questionId); if (resultQuestion.QuestionType == QuestionType.TextAnswer) { resultQuestion.Answers = resultQuestion.Answers.Take(1).ToList(); } var random = new Random(); resultQuestion.Answers = resultQuestion.Answers.OrderBy(a => random.Next()).ToList(); return(new Tuple <Question, int>(resultQuestion, nextQuestionNumber)); }
private void SaveAnswerOnTestQuestion(AnswerOnTestQuestion answerOnTestQuestion) { using (var repositoriesContainer = new LmPlatformRepositoriesContainer()) { repositoriesContainer.RepositoryFor <AnswerOnTestQuestion>().Save(answerOnTestQuestion); repositoriesContainer.ApplyChanges(); } }
private PassedQuestionResult GetQuestionStatus(AnswerOnTestQuestion answer) { if (!answer.Time.HasValue) { return(PassedQuestionResult.NotPassed); } return(answer.Points > 0 ? PassedQuestionResult.Success : PassedQuestionResult.Error); }
private void ProcessSequenceAnswer(List <Answer> answers, Question question, AnswerOnTestQuestion answerOntestQuestion) { bool isCorrect = true; if (answers.Count() != question.Answers.Count) { throw new InvalidDataException("Последовательность не совпадает с исходной"); } var plainAnswers = question.Answers.ToList(); for (int i = 0; i < answers.Count(); i++) { isCorrect = isCorrect && answers[i].Id == plainAnswers[i].Id; } if (isCorrect) { answerOntestQuestion.Points = question.ComlexityLevel; } }
private void ProcessOneVariantAnswer(IEnumerable <Answer> userAnswers, Question question, AnswerOnTestQuestion answerOntestQuestion) { if (userAnswers.Count(answer => answer.СorrectnessIndicator > 0) != 1) { throw new InvalidDataException("Пользователь должен указать 1 правильный ответ"); } var correctAnswer = question.Answers.Single(answer => answer.СorrectnessIndicator > 0); if (correctAnswer.Id == userAnswers.Single(answer => answer.СorrectnessIndicator > 0).Id) { answerOntestQuestion.Points = question.ComlexityLevel; } using var repositoriesContainer = new LmPlatformRepositoriesContainer(); { var answer = GetAnswerById(userAnswers.FirstOrDefault(y => y.СorrectnessIndicator > 0).Id); answerOntestQuestion.AnswerString = answer.Content; } }
private void ProcessTextAnswer(IEnumerable <Answer> userAnswers, Question question, AnswerOnTestQuestion answerOntestQuestion) { if (userAnswers.Count() != 1) { throw new InvalidDataException("Пользователь должен указать 1 правильный ответ"); } if (userAnswers.Single().Content == null) { throw new InvalidDataException("Пользователь должен указать ответ"); } if (question.Answers.Select(answer => answer.Content.ToLower()) .Contains(userAnswers.Single().Content.ToLower())) { answerOntestQuestion.Points = question.ComlexityLevel; } answerOntestQuestion.AnswerString = userAnswers.Single().Content.ToLower(); }
public void MakeUserAnswer(IEnumerable <Answer> answers, int userId, int testId, int questionNumber) { var test = GetTest(testId); TestPassResult testPassResult = GetTestPassResult(testId, userId); CheckForTimeEndeed(userId, testId, test, testPassResult); AnswerOnTestQuestion answerOnTestQuestion = GetAnswerOntestQuestion(userId, testId, questionNumber); if (answers == null) { answerOnTestQuestion.Points = 0; } else { Question question = GetQuestionById(answerOnTestQuestion.QuestionId); switch (question.QuestionType) { case QuestionType.HasOneCorrectAnswer: ProcessOneVariantAnswer(answers, question, answerOnTestQuestion); break; case QuestionType.HasManyCorrectAnswers: ProcessManyVariantsAnswer(answers, question, answerOnTestQuestion); break; case QuestionType.TextAnswer: ProcessTextAnswer(answers, question, answerOnTestQuestion); break; case QuestionType.SequenceAnswer: ProcessSequenceAnswer(answers.ToList(), question, answerOnTestQuestion); break; } } answerOnTestQuestion.Time = DateTime.UtcNow; SaveAnswerOnTestQuestion(answerOnTestQuestion); }
private void ProcessManyVariantsAnswer(IEnumerable <Answer> userAnswers, Question question, AnswerOnTestQuestion answerOntestQuestion) { if (userAnswers.Count(answer => answer.СorrectnessIndicator > 0) == 0) { throw new InvalidDataException("Пользователь должен указать хотя бы 1 правильный ответ"); } var correctAnswers = question.Answers.Where(answer => answer.СorrectnessIndicator > 0); var isCorrect = true; foreach (var correctAnswer in correctAnswers) { isCorrect = isCorrect && userAnswers .Where(answer => answer.СorrectnessIndicator > 0) .Any(userAnswer => userAnswer.Id == correctAnswer.Id); } isCorrect = isCorrect && userAnswers.Count(answer => answer.СorrectnessIndicator > 0) == correctAnswers.Count(); if (isCorrect) { answerOntestQuestion.Points = question.ComlexityLevel; } using var repositoriesContainer = new LmPlatformRepositoriesContainer(); foreach (var userAnswerId in userAnswers.Where(answer => answer.СorrectnessIndicator > 0) .Select(x => x.Id)) { var answer = GetAnswerById(userAnswerId); answerOntestQuestion.AnswerString += answer.Content; answerOntestQuestion.AnswerString += "\n"; } }
private void ProcessOneVariantAnswer(IEnumerable <Answer> userAnswers, Question question, AnswerOnTestQuestion answerOntestQuestion) { if (userAnswers.Count(answer => answer.СorrectnessIndicator > 0) != 1) { throw new InvalidDataException("Пользователь должен указать 1 правильный ответ"); } Answer correctAnswer = question.Answers.Single(answer => answer.СorrectnessIndicator > 0); if (correctAnswer.Id == userAnswers.Single(answer => answer.СorrectnessIndicator > 0).Id) { answerOntestQuestion.Points = question.ComlexityLevel; } }
private void ProcessManyVariantsAnswer(IEnumerable <Answer> userAnswers, Question question, AnswerOnTestQuestion answerOntestQuestion) { if (userAnswers.Count(answer => answer.СorrectnessIndicator > 0) == 0) { throw new InvalidDataException("Пользователь должен указать хотя бы 1 правильный ответ"); } IEnumerable <Answer> correctAnswers = question.Answers.Where(answer => answer.СorrectnessIndicator > 0); bool isCorrect = true; foreach (var correctAnswer in correctAnswers) { isCorrect = isCorrect && userAnswers .Where(answer => answer.СorrectnessIndicator > 0) .Any(userAnswer => userAnswer.Id == correctAnswer.Id); } isCorrect = isCorrect && userAnswers.Count(answer => answer.СorrectnessIndicator > 0) == correctAnswers.Count(); if (isCorrect) { answerOntestQuestion.Points = question.ComlexityLevel; } }