Exemplo n.º 1
0
        /// <summary>
        /// Creates a multiple choice question.
        /// </summary>
        /// <param name="number">Question number</param>
        /// <param name="question">Question</param>
        /// <param name="answers">Answers to question (as a string)</param>
        /// <param name="correct">Correct answers (as a string)</param>
        /// <returns>Created <seealso cref="MultipleChoiceQuestion"/> instance</returns>
        public static MultipleChoiceQuestion Build(int number, string question, IEnumerable <string> answers, IEnumerable <string> correct)
        {
            var multipleChoice = new MultipleChoiceQuestion(number, question.Trim())
            {
                Id = number
            };

            var i = 0;

            foreach (var answer in answers)
            {
                var choice = new Choice(answer);

                multipleChoice.AddChoice(choice);

                if (correct.Contains(answer))
                {
                    multipleChoice.AddAnswer(choice);
                }

                i++;
            }

            if (multipleChoice.Answer == null)
            {
                Logger.Log(Logger.Type.Error, $"Could not find answer to '{question}'");
            }

            return(multipleChoice);
        }
Exemplo n.º 2
0
        public IQuestion Build(string contents)
        {
            var questionMatch = _questionRegex.Match(contents);

            var num     = int.Parse(questionMatch.Groups[1].Value);
            var text    = questionMatch.Groups[2].Value.Trim();
            var answers = questionMatch.Groups[3].Value;
            var correct = questionMatch.Groups[5].Value.Trim()[0];

            var question = new MultipleChoiceQuestion(num, text);

            _questionExtraFactory.InsertExtras(contents, question);

            foreach (Match answerMatch in _answersRegex.Matches(answers))
            {
                var option     = answerMatch.Groups[1].Value.Trim()[0];
                var answerText = answerMatch.Groups[2].Value.Trim();

                var choice = new Choice(option, answerText);

                question.AddChoice(choice);

                if (correct == option)
                {
                    question.Answer = choice;
                }
            }

            return(question);
        }
        /// <summary>
        /// Creates a multiple choice question.
        /// </summary>
        /// <param name="number"></param>
        /// <param name="question"></param>
        /// <param name="answers"></param>
        /// <param name="correct"></param>
        /// <returns></returns>
        public static IQuestion Build(int number, string question, IList <string> answers, int correct)
        {
            // Is it a true/false question?
            if (answers.Count == 2 && (
                    answers[0].Equals("true", System.StringComparison.InvariantCultureIgnoreCase) &&
                    answers[1].Equals("false", System.StringComparison.InvariantCultureIgnoreCase)
                    ))
            {
                return(TrueFalseQuestionFactory.Build(number, question, correct == 0));
            }

            var multipleChoice = new MultipleChoiceQuestion(number, question.Trim())
            {
                Id = number
            };

            var i = 0;

            foreach (var answer in answers)
            {
                var choice = new Choice(answer.Trim());

                multipleChoice.AddChoice(choice);

                if (i == correct)
                {
                    multipleChoice.Answer = choice;
                }

                i++;
            }

            if (multipleChoice.Answer == null)
            {
                Logger.Log(Logger.Type.Error, $"Could not find answer to '{question}'");
            }

            return(multipleChoice);
        }