Esempio n. 1
0
        internal static IEnumerable <QuestionOption> CreateFractionOptions(int optionCount,
                                                                           int minValue,
                                                                           int maxValue,
                                                                           bool isMR,
                                                                           Func <ArithmeticFractionValuePart, bool> validationFun,
                                                                           ArithmeticFractionValuePart correctFraction = null)
        {
            Random rand = new Random((int)DateTime.Now.Ticks);

            List <QuestionOption> optionList = new List <QuestionOption>();
            List <ArithmeticFractionValuePart> optionValueList = new List <ArithmeticFractionValuePart>();
            ArithmeticFractionValuePart        correctValue    = null;

            if (correctFraction != null)
            {
                correctValue = correctFraction;
            }
            for (int j = 0; j < optionCount - 1; j++)
            {
                while (true)
                {
                    ArithmeticFractionValuePart fractionValue = null;
                    foreach (ArithmeticFractionValuePart value in CreateFractionValue(minValue, maxValue, minValue == 0 ? 1: minValue, maxValue))
                    {
                        fractionValue = value;
                    }

                    if (!isMR && validationFun(fractionValue))
                    {
                        correctValue = fractionValue;
                        Thread.Sleep(10);
                        continue;
                    }

                    IEnumerable <ArithmeticFractionValuePart> queryResult = optionValueList.Where <ArithmeticFractionValuePart>(c => c.Equals(fractionValue));
                    if (queryResult.Count <ArithmeticFractionValuePart>() > 0)
                    {
                        Thread.Sleep(10);
                        continue;
                    }

                    QuestionOption option = new QuestionOption();
                    option.OptionContent.Content     = fractionValue.PlaceHolder;
                    option.OptionContent.ContentType = ContentType.Text;
                    option.OptionContent.QuestionPartCollection.Add(fractionValue);
                    if (isMR)
                    {
                        option.IsCorrect = validationFun(fractionValue);
                    }

                    optionList.Add(option);

                    optionValueList.Add(fractionValue);
                    break;
                }
            }

            if (correctValue == null)
            {
                while (true)
                {
                    ArithmeticFractionValuePart fractionValue = null;
                    foreach (ArithmeticFractionValuePart value in CreateFractionValue(minValue, maxValue, minValue == 0 ? 1 : minValue, maxValue))
                    {
                        fractionValue = value;
                    }

                    if (validationFun(fractionValue))
                    {
                        correctValue = fractionValue;
                        break;
                    }
                    Thread.Sleep(10);
                }
            }

            QuestionOption correctOption = new QuestionOption();

            correctOption.OptionContent.Content = correctValue.PlaceHolder;
            correctOption.OptionContent.QuestionPartCollection.Add(correctValue);
            correctOption.IsCorrect = true;
            int correctOptionIndex = rand.Next(0, optionCount * 8) % optionCount;

            if (correctOptionIndex == optionCount - 1)
            {
                optionList.Add(correctOption);
            }
            else
            {
                optionList.Insert(correctOptionIndex, correctOption);
            }

            Thread.Sleep(50);

            return(optionList);
        }