/// <summary> /// Returns GeneratedQuestion /// </summary> /// <param name = "templateId">Template id</param> /// <param name = "seed">Random seed</param> /// <returns></returns> public static GeneratedQuestion Generate(int templateId, int seed) { var result = new GeneratedQuestion(); var rnd = new Random(seed); // Get template from _db Question question = Db.Questions.FirstOrDefault(q => q.ID == templateId); if (question == null) throw new Exception("Template not found"); result.Body = question.Body; result.QuestionType = 1; result.Answers = new List<GeneratedAnswer>(); // Insert definitions to Body int tagsLength = TagStartInsert.Length + TagEndInsert.Length; for (int i = 0; i < result.Body.Length - tagsLength; i++) { if (result.Body.Substring(i, TagStartInsert.Length) != TagStartInsert) continue; // We found start insert tag int tagStartInsertPosition = i; int definitionNamePosition = i + TagStartInsert.Length; for (i = definitionNamePosition; i < result.Body.Length - TagEndInsert.Length; i++) { if (result.Body.Substring(i, TagEndInsert.Length) != TagEndInsert) continue; // We found end insert tag string definitionName = result.Body.Substring(definitionNamePosition, i - definitionNamePosition); if (!string.IsNullOrEmpty(definitionName)) { // Get definition from _db Definition definition = Db.Definitions.FirstOrDefault( d => d.Name == definitionName); if (definition != null) { // Get examples to definition from _db List<Example> examples = definition.Examples.Where(e => !e.IsAntiExample) .ToList(); if (examples.Count == 0) throw new Exception("Definition has no examples"); Example example = examples[rnd.Next(0, examples.Count)]; // Replacing string oldSubstr = TagStartInsert + definitionName + TagEndInsert; result.Body = result.Body.Replace(oldSubstr, example.Value.Trim ()); i = tagStartInsertPosition + example.Value.Length - 1; } } break; } } // Get answers from _db List<Answer> answers = question.Answers.ToList(); if (answers.Count == 0) throw new Exception("Question has no answers"); foreach (Answer answer in answers) { var newAnswer = new GeneratedAnswer { Body = answer.Body, IsCorrect = answer.IsCorrect }; result.Answers.Add(newAnswer); } result.QuestionType = question.QuestionTypeID; return result; }
/// <summary> /// Validate user Answer /// </summary> /// <param name="question">Question that user answered</param> /// <param name="userAnswer">String representation of user answer</param> /// <returns></returns> private bool CheckAnswerIsCorrect(GeneratedQuestion question, string userAnswer) { bool result = false; if (string.IsNullOrWhiteSpace(userAnswer)) return false; if (question.QuestionType == 1) { int rightAnswer = int.Parse(userAnswer); if (question.Answers[rightAnswer].IsCorrect) result = true; } if (question.QuestionType == 2) { result = true; var answerArray = userAnswer.Split(','); for (int i = 0; i < question.Answers.Count; i++) { if (question.Answers[i].IsCorrect != answerArray.Contains(i.ToString())) result = false; } } if (question.QuestionType == 3) { foreach (var ans in question.Answers) { if (ans.Body == userAnswer) result = true; } } return result; }