private void ValidateQuestion(Question question) { if (string.IsNullOrEmpty(question.Title)) { throw new InvalidDataException("Название вопроса не должно быть пустым"); } if (question.ComlexityLevel <= 0) { throw new InvalidDataException("Сложность теста должна быть боьше нуля"); } if (question.Answers.Any(answer => string.IsNullOrEmpty(answer.Content))) { throw new InvalidDataException("Вопрос содержит пустые варианты ответа"); } switch (question.QuestionType) { case QuestionType.HasOneCorrectAnswer: ValidateOneCorrectVariantQuestion(question); break; case QuestionType.HasManyCorrectAnswers: ValidateManyCorrectVariantsQuestion(question); break; case QuestionType.TextAnswer: ValidateTextAnswerQuestion(question); break; case QuestionType.SequenceAnswer: ValidateSequenceAnswerQuestion(question); break; } }
public static QuestionItemListViewModel FromQuestion(Question question, string htmlString) { var model = FromQuestion(question); model.Action = new HtmlString(htmlString); return model; }
public Question SaveQuestion(Question question) { CheckForTestIsNotLocked(question.TestId); ValidateQuestion(question); using (var repositoriesContainer = new LmPlatformRepositoriesContainer()) { repositoriesContainer.QuestionsRepository.Save(question); Question existingQuestion = GetQuestion(question.Id); var answersToDelete = existingQuestion.Answers.Where(a => question.Answers.All(answer => answer.Id != a.Id)); // TODO: Resolve problem (items are saved only first time) foreach (Answer answer in question.Answers) { answer.QuestionId = question.Id; } repositoriesContainer.RepositoryFor<Answer>().Save(question.Answers); repositoriesContainer.RepositoryFor<Answer>().Delete(answersToDelete); repositoriesContainer.ApplyChanges(); return question; } }
public static QuestionItemListViewModel FromQuestion(Question question) { return new QuestionItemListViewModel { Id = question.Id, Title = question.Title }; }
public static QuestionViewModel FromQuestion(Question question) { return new QuestionViewModel { Id = question.Id, TestId = question.TestId, Title = question.Title, Description = question.Description, ComplexityLevel = question.ComlexityLevel, QuestionType = question.QuestionType, Answers = question.Answers != null ? question.Answers.Select(AnswerViewModel.FromAnswer) : null }; }
private void ValidateSequenceAnswerQuestion(Question question) { if (question.Answers.Count < 2) { throw new InvalidDataException("Последовательность должна состоять хотя бы из 2 элементов"); } }
private void ValidateOneCorrectVariantQuestion(Question question) { if (question.Answers.Count < 2) { throw new InvalidDataException("Вопрос должен иметь хотя бы 2 варианта"); } int correctAnswersCount = question.Answers.Count(answer => answer.СorrectnessIndicator > 0); if (correctAnswersCount == 0) { throw new InvalidDataException("Вопрос должен иметь хотя бы один правильный ответ"); } if (correctAnswersCount > 1) { throw new InvalidDataException("Вопрос должен иметь только один правильный ответ"); } }
private void ValidateManyCorrectVariantsQuestion(Question question) { if (question.Answers.Count < 3) { throw new InvalidDataException("Вопрос должен иметь хотя бы 3 варианта"); } int correctAnswersCount = question.Answers.Count(answer => answer.СorrectnessIndicator > 0); if (correctAnswersCount <= 1) { throw new InvalidDataException("Вопрос должен иметь хотя бы 2 правильных ответа"); } }
private void ValidateTextAnswerQuestion(Question question) { if (question.Answers.Count < 1) { throw new InvalidDataException("Вопрос должен иметь хотя бы 1 правильный ответ"); } }
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; } }
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; } }
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; } }