public void AddAswerToQuestionTest() { QuestionManagement _QuestionManagement = new QuestionManagement(); IAnswerManagement _AnswerManagement = AnswerManagement.Instance(); DAO.Question _Question = _QuestionManagement.GetQuestion(StaticQuestions.Q1); _QuestionManagement.AddAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A1)); _QuestionManagement.AddAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A2)); _QuestionManagement.AddAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A3)); _Question = _QuestionManagement.GetQuestion(StaticQuestions.Q2); _QuestionManagement.AddAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A4)); _QuestionManagement.AddAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A5)); _QuestionManagement.AddAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A6)); _Question = _QuestionManagement.GetQuestion(StaticQuestions.Q3); _QuestionManagement.AddAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A7)); _QuestionManagement.AddAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A8)); _QuestionManagement.AddAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A9)); _Question = _QuestionManagement.GetQuestion(StaticQuestions.Q4); _QuestionManagement.AddAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A10)); _QuestionManagement.AddAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A11)); _QuestionManagement.AddAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A12)); _Question = _QuestionManagement.GetQuestion(StaticQuestions.Q5); _QuestionManagement.AddAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A13)); _QuestionManagement.AddAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A14)); _QuestionManagement.AddAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A15)); _DbContext.SaveChanges(); Assert.True(_Question.Answers.Count() == 3); Assert.True(_Question.Answers.Where(x => x.AnswerId == _AnswerManagement.GetAnswer(StaticAnswers.A14).Id).Count() == 1); }
public string AnswerTheQuestion(DTO.InterviweeTest DtoInterviweeTest) { DTO.InterviweeTest _DtoInterviweeTest = DtoInterviweeTest; // Прохождение теста. DAO.InterviweeTests _DaoInterviweeTest = GetInterviweeTestByDtoId(ref _DtoInterviweeTest); // Текущий вопрос. DAO.Question _CurrentQuestion = m_QuestionManagement.GetQuestion(_DtoInterviweeTest.CurrentQuestion.Id); // Ответ на вопрос. DAO.Answer _Answer = m_AnswerManagement.GetAnswer(_DtoInterviweeTest.CurrentQuestion.SelectedAnswerId); m_Testing.AnswerToQuestion(_DaoInterviweeTest, _CurrentQuestion, _Answer); // Определим завершенность теста. m_Testing.DetermineStatusComplete(_DaoInterviweeTest); // Прохождение теста - обновление после ответа. _DaoInterviweeTest = GetInterviweeTestByDtoId(ref _DtoInterviweeTest); // Вопрос. _DtoInterviweeTest = GetNextQuestion(_DtoInterviweeTest, _DaoInterviweeTest, _CurrentQuestion); string _Json = Utils.JsonSerialize(_DtoInterviweeTest); return(_Json); }
public void SetCorrectAnswerTest() { using (var _Transaction = _DbContext.Database.BeginTransaction()) { // Создаем вопрос. DAO.Question _Question = new DAO.Question() { Text = "SetCorrectAnswerTest?" }; _QuestionManagement.CreateQuestion(_Question); _DbContext.SaveChanges(); // Получаем сущность из БД - если не получить, то EF подумает, что надо создать новую. _Question = _DbContext.Questions.Include("Answers").Single(x => x.Text == _Question.Text); // Выбираем ответы для заполнения. DAO.Answer[] _Answers = new DAO.Answer[] { _DbContext.Answers.Single(x => x.Text == "Венера"), _DbContext.Answers.Single(x => x.Text == "Меркурий"), _DbContext.Answers.Single(x => x.Text == "Земля") }; _QuestionManagement.AddAnswer(_Question, _Answers); _DbContext.SaveChanges(); Assert.DoesNotThrow(() => _QuestionManagement.SetCorrectAnswer(_Question, _DbContext.Answers.Single(x => x.Text == "Венера"))); var _UnCorrectAnswer = _DbContext.Answers.Single(x => x.Text == "101010"); Assert.Throws <Exception>(() => _QuestionManagement.SetCorrectAnswer(_Question, _UnCorrectAnswer)); _Transaction.Rollback(); } }
/// <summary> /// Получить следующий вопрос для прохождения теста. /// </summary> /// <param name="InterviweeTests">Прохождение теста.</param> /// <returns></returns> public DAO.Question GetNextQuestion(DAO.InterviweeTests InterviweeTest, DAO.Question ExcludeQuestion = null) { // Если прохождение теста завершено. if (InterviweeTest.IsComplete) { return(null); } // Получим массив вопросов с ответами. DAO.Question[] _QuestionResults = _DbContext.TestingResults .Where(x => x.InterviweeTestsId == InterviweeTest.Id) .Select(x => x.Question) .ToArray(); // Получим массив вопросов теста. DAO.Question[] _QuestionsTest = _DbContext.TestQuestions .Where(x => x.TestId == InterviweeTest.TestId) .Select(x => x.Question) .ToArray(); // Получим массив незаданных вопросов. DAO.Question[] _QuestionsNotAnswer = _QuestionsTest.Except(_QuestionResults).ToArray(); // Случайный вопрос. DAO.Question _Question = RandomQuestion(_QuestionsNotAnswer, ExcludeQuestion); return(_Question); }
public void CreateQuestionAnswersTest() { using (var _Transaction = _DbContext.Database.BeginTransaction()) { // Создаем вопрос. DAO.Question _Question = new DAO.Question() { Text = "CreateQuestionAnswersTest?" }; _QuestionManagement.CreateQuestion(_Question); _DbContext.SaveChanges(); // Получаем сущность из БД - если не получить, то EF подумает, что надо создать новую. _Question = _DbContext.Questions.Include("Answers").Single(x => x.Text == _Question.Text); // Выбираем ответы для заполнения. DAO.Answer[] _Answers = new DAO.Answer[] { _DbContext.Answers.Single(x => x.Text == "Венера"), _DbContext.Answers.Single(x => x.Text == "Меркурий"), _DbContext.Answers.Single(x => x.Text == "Земля") }; _QuestionManagement.AddAnswer(_Question, _Answers); _DbContext.SaveChanges(); Assert.True(_Question.Answers.Count() == 3); _Transaction.Rollback(); } }
/// <summary> /// Выбрать случайный вопрос. /// </summary> /// <param name="Questions">Вопросы</param> /// <param name="ExcludeQuestion">Вопрос для исключения из выборки. Не сработает если этот вопрос последний.</param> /// <returns></returns> public DAO.Question RandomQuestion(DAO.Question[] Questions, DAO.Question ExcludeQuestion = null) { DAO.Question[] _Questions = null; // Исключим из выборки указанный вопрос. if (Questions.Count() > 1 && ExcludeQuestion != null) { _Questions = Questions.Where(x => x.Id != ExcludeQuestion.Id).ToArray(); } else { _Questions = Questions; } // Получим следующий случайный вопрос. if (_Questions.Length > 0) { Random _Random = new Random(Guid.NewGuid().GetHashCode()); int _MaxRangeNext = _Questions.Count() - 1; int _NextQuestion = _Random.Next(0, _MaxRangeNext); return(_Questions[_NextQuestion]); } return(null); }
/// <summary> /// Ответить на вопрос. /// </summary> /// <param name="InterviweeTestId">Прохождение теста.</param> /// <param name="QuestionId">Вопрос.</param> /// <param name="AnswerId">Ответ.</param> public void AnswerToQuestion(DAO.InterviweeTests InterviweeTest, DAO.Question Question, DAO.Answer Answer) { // Если ответ на такой вопрос уже был - выходим. var ExistAnswer = _DbContext.TestingResults.Where(x => x.InterviweeTestsId == InterviweeTest.Id & x.QuestionId == Question.Id).Count(); if (ExistAnswer > 0) { return; } // Определить ответ. DAO.TestingResult _TestingResult = new DAO.TestingResult() { InterviweeTestsId = InterviweeTest.Id, QuestionId = Question.Id, AnswerId = Answer.Id, IsCorrect = _DbContext.QuestionAnswers.Where(x => x.QuestionId == Question.Id & x.IsCorrect).FirstOrDefault()?.AnswerId == Answer.Id }; // Добавить в результаты теста. _DbContext.TestingResults.Add(_TestingResult); _DbContext.SaveChanges(); }
public string EditQuestion(DTO.Question DtoQuestion) { DAO.Question _DaoQuestion = m_QuestionManagement.GetQuestion(DtoQuestion.Id); _DaoQuestion.Text = DtoQuestion.Text; m_QuestionManagement.SaveQuestion(_DaoQuestion); return(string.Empty); }
/// <summary> /// Добавить вариант ответа в вопрос. /// </summary> /// <param name="Question"></param> /// <param name="Answer"></param> public void AddAnswer(DAO.Question Question, params DAO.Answer[] Answer) { foreach (var item in Answer) { AddAnswer(Question, item); } ; }
public string AddAnswerToQuestion(DTO.QuestionAnswers QuestionAnswer) { DAO.Question _Question = m_QuestionManagement.GetQuestion(QuestionAnswer.QuestionId); DAO.Answer _Answer = m_AnswerManagement.GetAnswer(QuestionAnswer.AnswerId); m_QuestionManagement.AddAnswer(_Question, _Answer); return(string.Empty); }
public string SetCorrectAnswer(DTO.QuestionAnswers CorrectAnswer) { DAO.Question _Question = m_QuestionManagement.GetQuestion(CorrectAnswer.QuestionId); DAO.Answer _Answer = m_AnswerManagement.GetAnswer(CorrectAnswer.AnswerId); m_QuestionManagement.SetCorrectAnswer(_Question, _Answer); return(string.Empty); }
/// <summary> /// Сохранить изменение вопроса. /// </summary> /// <param name="Question">Вопрос.</param> public void SaveQuestion(DAO.Question Question) { if (_DbContext.Entry(Question).State == EntityState.Detached) { _DbContext.Entry(Question).State = EntityState.Modified; } _DbContext.SaveChanges(); }
/// <summary> /// Найти вопрос. /// </summary> /// <param name="Question"></param> /// <returns></returns> private bool IsExist(DAO.Question Question) { DAO.Question _FinKey = GetQuestion(Question.Id); DAO.Question _FindText = GetQuestion(Question.Text); bool _IsExist = _FinKey != null || _FindText != null; return(_IsExist); }
/// <summary> /// Создать вопрос. /// </summary> public void CreateQuestion(DAO.Question Question) { if (IsExist(Question)) { return; } _DbContext.Questions.Add(Question); _DbContext.SaveChanges(); }
public string CreateQuestion(DTO.Question DtoQuestion) { DAO.Question _DaoQuestion = new DAO.Question() { Text = DtoQuestion.Text }; m_QuestionManagement.CreateQuestion(_DaoQuestion); return(string.Empty); }
public string AddQuestionToTest(DTO.TestQuestions DtoTestQuestion) { DAO.Test _Test = m_TestManagement.GetTest(DtoTestQuestion.TestId); DAO.Question _Question = m_QuestionManagement.GetQuestion(DtoTestQuestion.QuestionId); if (_Test != null && _Question != null) { m_TestManagement.AddQuestion(_Test, _Question); } return(string.Empty); }
public void TestingTest() { TestManagement _TestManagement = new TestManagement(); InterviweeManagement _InterviweeManagement = new InterviweeManagement(); KnowledgeTesting.BL.Testing _Testing = new KnowledgeTesting.BL.Testing(); DAO.Interviwee _Interviwee = _InterviweeManagement.GetInterviwee(StaticInterviwee.LasName, StaticInterviwee.FirstName, StaticInterviwee.SecondName); DAO.Test _Test = _TestManagement.GetTest(StaticTests.T1); int _CountCompleteTeststBefore = _Interviwee.Tests.Count(); DAO.InterviweeTests _InterviweeTests = _Testing.GetTesting(_Interviwee, _Test); // Количество отвеченных вопросов // (в конце на 1 больше чем вопрсов из-за последнего прохода цикла для определения статуса завершения). int _CountQuestions = 0; while (!_InterviweeTests.IsComplete) { // Определить статус завершения. _InterviweeTests.IsComplete = _Testing.DetermineStatusComplete(_InterviweeTests); if (!_InterviweeTests.IsComplete) { // Получить следующий вопрос. DAO.Question _Question = _Testing.GetNextQuestion(_InterviweeTests); // Отвтеить на вопрос. if (_Question != null) { _Testing.AnswerToQuestion(_InterviweeTests, _Question, _Question.Answers.First().Answer); } } // Сохранить изменения - БЕЗ этого не возможно определить статус завершения теста. _DbContext.SaveChanges(); _CountQuestions++; Assert.True(_CountQuestions <= 10); } int _CountCompleteTeststAfter = _Interviwee.Tests.Count(); Assert.True(_DbContext.InterviweeTests.Where(x => x.Id == _InterviweeTests.Id).First().IsComplete); Assert.True(_CountCompleteTeststAfter > _CountCompleteTeststBefore); Assert.True(_InterviweeTests.TestingResults.Count() > 0); }
public void CreateQuestionNotAnswersTest() { using (var _Transaction = _DbContext.Database.BeginTransaction()) { DAO.Question _Question = new DAO.Question() { Text = "wtf?" }; Assert.DoesNotThrow(() => _QuestionManagement.CreateQuestion(_Question)); _DbContext.SaveChanges(); Assert.True(_DbContext.Questions.Where(x => x.Text == _Question.Text).Count() == 1); _Transaction.Rollback(); } }
/// <summary> /// Установить правильный ответ на вопрос. /// </summary> public void SetCorrectAnswer(DAO.Question Question, DAO.Answer Answer) { DAO.QuestionAnswers _QuestionAnswer = _DbContext.QuestionAnswers.Find(Question.Id, Answer.Id); if (_QuestionAnswer == null) { throw new Exception("Правильный ответ должен содержаться в вопросе."); } DAO.QuestionAnswers _CurrentCorrectAnswer = _DbContext.QuestionAnswers.SingleOrDefault(x => x.QuestionId == Question.Id & x.IsCorrect); if (_CurrentCorrectAnswer != null) { _CurrentCorrectAnswer.IsCorrect = false; } _QuestionAnswer.IsCorrect = true; _DbContext.SaveChanges(); }
public string CreateAnswerToQuestion(DTO.NewAnswerToQuestion NewAnswerToQuestion) { DAO.Answer _Answer = new DAO.Answer() { Text = NewAnswerToQuestion.AnswerText }; DAO.Question _Question = m_QuestionManagement.GetQuestion(NewAnswerToQuestion.QuestionId); m_AnswerManagement.CreateAnswer(_Answer); _Answer = m_AnswerManagement.GetAnswer(NewAnswerToQuestion.AnswerText); if (_Answer != null) { m_QuestionManagement.AddAnswer(_Question, _Answer); } return(string.Empty); }
public string GetNextQuestion(DTO.InterviweeTest DtoInterviweeTest) { DTO.InterviweeTest _DtoInterviweeTest = DtoInterviweeTest; // Прохождение теста. DAO.InterviweeTests _DaoInterviweeTest = GetInterviweeTestByDtoId(ref _DtoInterviweeTest); // Текущий вопрос. DAO.Question _CurrentQuestion = null; if (DtoInterviweeTest.CurrentQuestion != null) { _CurrentQuestion = m_QuestionManagement.GetQuestion(DtoInterviweeTest.CurrentQuestion.Id); } // Вопрос. _DtoInterviweeTest = GetNextQuestion(_DtoInterviweeTest, _DaoInterviweeTest, _CurrentQuestion); string _Json = Utils.JsonSerialize(_DtoInterviweeTest); return(_Json); }
/// <summary> /// Добавить вариант ответа в вопрос. /// </summary> /// <param name="Question"></param> /// <param name="Answer"></param> public void AddAnswer(DAO.Question Question, DAO.Answer Answer) { if (Question.Answers.Where(x => x.AnswerId == Answer.Id).Count() == 1) { return; } if ((Question.Answers.Count() >= 3)) { throw new Exception("Вопрос может содержать не более 3 вариантов ответа."); } DAO.QuestionAnswers _QuestionAnswers = new DAO.QuestionAnswers() { AnswerId = Answer.Id, QuestionId = Question.Id, IsCorrect = false }; _DbContext.QuestionAnswers.Add(_QuestionAnswers); _DbContext.SaveChanges(); }
/// <summary> /// Получить следующий вопрос. /// </summary> /// <returns></returns> private DTO.InterviweeTest GetNextQuestion( DTO.InterviweeTest DtoInterviweeTest, DAO.InterviweeTests DaoInterviweeTest, DAO.Question CurrentQuestion) { DTO.InterviweeTest _DtoInterviweeTest = DtoInterviweeTest; // Вопрос. DAO.Question _DaoQuestion = m_Testing.GetNextQuestion(DaoInterviweeTest, CurrentQuestion); if (_DaoQuestion == null) { _DtoInterviweeTest.CurrentQuestion = new DTO.Question(); return(_DtoInterviweeTest); } if (_DaoQuestion != null) { _DtoInterviweeTest.CurrentQuestion = Utils.ConverObjectByJson <DTO.Question>(_DaoQuestion); // Ответы на вопрос. DAO.QuestionAnswers[] _DaoQuestionAnswers = _DaoQuestion.Answers.ToArray(); List <DTO.QuestionAnswers> _ListAnswers = new List <DTO.QuestionAnswers>(); foreach (var _DaoQuestionAnswer in _DaoQuestionAnswers) { _ListAnswers.Add(new DTO.QuestionAnswers() { AnswerId = _DaoQuestionAnswer.Answer.Id, AnswerText = _DaoQuestionAnswer.Answer.Text, IsCorrect = false, QuestionId = _DaoQuestionAnswer.QuestionId }); } _DtoInterviweeTest.CurrentQuestion.Answers = _ListAnswers; } return(_DtoInterviweeTest); }
public void SetCorrectAnswerToQuestionTest() { QuestionManagement _QuestionManagement = new QuestionManagement(); AnswerManagement _AnswerManagement = new AnswerManagement(); DAO.Question _Question = _QuestionManagement.GetQuestion(StaticQuestions.Q1); _QuestionManagement.SetCorrectAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A1)); _Question = _QuestionManagement.GetQuestion(StaticQuestions.Q2); _QuestionManagement.SetCorrectAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A6)); _Question = _QuestionManagement.GetQuestion(StaticQuestions.Q3); _QuestionManagement.SetCorrectAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A7)); _Question = _QuestionManagement.GetQuestion(StaticQuestions.Q4); _QuestionManagement.SetCorrectAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A11)); _Question = _QuestionManagement.GetQuestion(StaticQuestions.Q5); _QuestionManagement.SetCorrectAnswer(_Question, _AnswerManagement.GetAnswer(StaticAnswers.A13)); _DbContext.SaveChanges(); Assert.True(_Question.Answers.Where(x => x.AnswerId == _AnswerManagement.GetAnswer(StaticAnswers.A13).Id & x.IsCorrect).Count() == 1); }
/// <summary> /// Добавить вопрос в тест. /// </summary> /// <param name="Test"></param> /// <param name="Question"></param> public void AddQuestion(DAO.Test Test, DAO.Question Question) { if (Test.Questions.Where(x => x.QuestionId == Question.Id).Count() == 1) { return; } if (Test.Questions.Count() >= 10) { throw new Exception("В тесте максимум 10 вопрсов."); } if (Question.Answers.Where(x => x.IsCorrect).Count() == 0) { throw new Exception("В вопросе не указан правильный ответ."); } DAO.TestQuestions _TestQuestion = new DAO.TestQuestions() { TestId = Test.Id, QuestionId = Question.Id }; _DbContext.TestQuestions.Add(_TestQuestion); _DbContext.SaveChanges(); }
/// <summary> /// Получить вопрос по коду. /// </summary> /// <param name="QuestionId">Код вопроса.</param> /// <returns></returns> public Question GetQuestion(int QuestionId) { DAO.Question _FinKey = _DbContext.Questions.Where(x => x.Id == QuestionId).FirstOrDefault(); return(_FinKey); }