/// <summary> /// Answers specified quiz question. /// </summary> /// <param name="question">The question to answer.</param> /// <param name="answerTemplateId">The answer template identifier.</param> public void AnswerQuestion(Question question, int answerTemplateId) { ThrowIf.Null(question, nameof(question)); ThrowIf.Completed(question.Quiz); if (question.IsAnswered) { throw new QuizFlowException( QuizFlowErrorCodes.QuestionAlreadyAnswered, "Question already answered."); } QuestionTemplate questionTemplate = this.Uow.QuestionTemplateRepository .GetByID(question.TemplateId); ThrowIf.NotFound(questionTemplate, question.TemplateId); AnswerTemplate answerTemplate = questionTemplate.GetAnswer(answerTemplateId); ThrowIf.NotFound(answerTemplate, answerTemplateId); var answer = new Answer() { TemplateId = answerTemplateId, IsCorrect = answerTemplate.IsCorrect }; question.Answers.Add(answer); question.DateEnd = DateTime.Now; this.Uow.Save(); }
public void AnswerQuestion_WhenAnswerTemplateFound_SetQuestionAnswered() { // Arrange var questionStub = new Question(); var answerStub = new AnswerTemplate() { Id = 1, IsCorrect = true }; var questionTemplateStub = new QuestionTemplate(); questionTemplateStub.Answers.Add(answerStub); var mockQuizRepo = new Mock <IQuizRepository>(); var mockQuestionTemplateRepo = new Mock <IQuestionTemplateRepository>(); mockQuestionTemplateRepo.Setup(repo => repo.GetByID(It.IsAny <int>())) .Returns(value: questionTemplateStub); var mockUow = new Mock <IUnitOfWork>(); mockUow.Setup(repo => repo.QuestionTemplateRepository) .Returns(mockQuestionTemplateRepo.Object); var manager = new QuizFlowManager(mockUow.Object, null); // Act manager.AnswerQuestion(questionStub, 1); // Assert Assert.True(questionStub.IsAnswered); Assert.Single(questionStub.Answers); Assert.True(questionStub.Answers.First().IsCorrect); }
public static List <AnswerTemplate> GetAnswerTemplateDetails(string htmlContent) { MatchCollection matches; Regex obRegEx = new Regex(@"<a\shref='index\.php\?page=answer&action=answer&id=\d+?&link=(?<Link>\d+?)&value=.{33}'>(?<AnswerTitle>.*?)</a>"); matches = obRegEx.Matches(htmlContent); List <AnswerTemplate> answerTemplateList = new List <AnswerTemplate>(); foreach (Match match in matches) { AnswerTemplate template = new AnswerTemplate(); GroupCollection groups = match.Groups; template.Link = groups["Link"].Value.Trim(); template.Title = groups["AnswerTitle"].Value.Trim(); answerTemplateList.Add(template); } return(answerTemplateList); }
private Response GetRangeResponse(Month[] calendar, AnswerTemplate templates, int year, int monthNumber, ICollection <DayType> dayTypes) { var month = calendar[monthNumber]; var monthName = month.Name; var yearFormat = string.Empty; if (year != DateTime.Now.Year) { yearFormat = string.Format(templates.YearFormat, year); monthName = $"{monthName}{yearFormat}"; } var stringBuilder = new StringBuilder(); stringBuilder.Append(string.Format(templates.Introduction ?? "{0}", monthName)); stringBuilder.AppendLine(); foreach (var dayType in dayTypes) { var template = templates[dayType]; var ranges = calendar[monthNumber][dayType].ToList(); var rangesString = FormatRanges(ranges, template); var joinedRanges = string.Join(template?.EnumerationSeparator, rangesString); stringBuilder.Append(string.Format(template?.MainFormat ?? "{0}", joinedRanges)); stringBuilder.AppendLine(); } var text = stringBuilder.ToString(); var image = GetImage(year, monthNumber, templates?.ImageTitleFormat, text); return(new Response { Text = text, Image = image }); }
private Response GetClosestDaysResponse(Month[] calendar, AnswerTemplate templates, ICollection <DayType> dayTypes) { var stringBuilder = new StringBuilder(); var monthNumber = DateTime.Now.Month; var nowDate = DateTime.Now.Date; var introduction = templates.ClosestIntroduction ?? "{0}"; foreach (var dayType in dayTypes) { var template = templates[dayType]; var mainFormat = string.Format(introduction, template.MainFormat.ToLower()); var ranges = calendar.Where(c => c.Number >= monthNumber).SelectMany(c => c[dayType]).Where(r => r.StartDate >= DateTime.Now).Take(1).ToList(); var rangesString = FormatRanges(ranges, template); //The name of the month in the case. Date formatting gives you the correct month name. Convenient, no need to go to other services var monthNameInCase = ranges.FirstOrDefault()?.StartDate.ToString("d MMMM").Split(' ').LastOrDefault(); var joinedRanges = string.Join(template?.EnumerationSeparator, rangesString); var rangeWithMonthName = $"{joinedRanges} {monthNameInCase}"; stringBuilder.Append(string.Format(mainFormat ?? "{0}", rangeWithMonthName)); stringBuilder.AppendLine(); introduction = "{0}"; } var text = stringBuilder.ToString(); return(new Response { Text = text }); }
private static void InitializeData(ApplicationDatabaseContext context) { if (context.QuizTemplates.Any()) { return; } var question1 = new QuestionTemplate() { QuestionType = QuestionType.SingleRight, Text = "Who is the author of `Alice in Wonderland` ?" }; var answerQ1A1 = new AnswerTemplate() { IsCorrect = true, Text = "Charles Lutwidge Dodgson" }; var answerQ1A2 = new AnswerTemplate() { IsCorrect = false, Text = "Charles John Huffam Dickens" }; var answerQ1A3 = new AnswerTemplate() { IsCorrect = false, Text = "Agatha Mary Clarissa Christie" }; var answerQ1A4 = new AnswerTemplate() { IsCorrect = false, Text = "No author. It's a folklore." }; question1.Answers.AddRange(new[] { answerQ1A1, answerQ1A2, answerQ1A3, answerQ1A4 }); var question2 = new QuestionTemplate() { QuestionType = QuestionType.SingleRight, Text = "Which of these novels written by Lev Nikolayevich Tolstoy are the most latest ?" }; var answerQ2A1 = new AnswerTemplate() { IsCorrect = true, Text = "Hadji Murat" }; var answerQ2A2 = new AnswerTemplate() { IsCorrect = false, Text = "The Cossacks" }; var answerQ2A3 = new AnswerTemplate() { IsCorrect = false, Text = "War and Peace" }; var answerQ2A4 = new AnswerTemplate() { IsCorrect = false, Text = "Childhood" }; question2.Answers.AddRange(new[] { answerQ2A1, answerQ2A2, answerQ2A3, answerQ2A4 }); var quizTemplate = new QuizTemplate() { Title = "Simple literature quiz", Description = "Short demo quiz about literature.", }; var quizQuestion1 = new QuizQuestionTemplate(quizTemplate, question1, 1) { Enabled = true }; var quizQuestion2 = new QuizQuestionTemplate(quizTemplate, question2, 2) { Enabled = true }; context.QuizTemplates.Add(quizTemplate); context.QuizQuestionTemplates.AddRange(new[] { quizQuestion1, quizQuestion2 }); }
public void logAnswer(double ans, bool correct) { AnswerTemplate answer = new AnswerTemplate(ans, correct); FirebaseConnect.post("/answers/" + PlayerPrefs.GetString("User", "") + "/" + questionID, answer); }
public IHttpActionResult Edit(AnswerTemplate answerTemplate) { _parameterBusiness.UpdateAnswerTemplate(answerTemplate); return(Ok()); }
public void UpdateAnswerTemplate(AnswerTemplate answerTemplate) { _unitOfWork.AnswerTemplateRepository.Update(answerTemplate); _unitOfWork.Save(); }
public void CreateAnswerTemplate(AnswerTemplate answerTemplate) { _unitOfWork.AnswerTemplateRepository.Add(answerTemplate); _unitOfWork.Save(); }