コード例 #1
0
        public QuestionSet GenererateTest(int maxCount, bool isRandomize)
        {
            byte[] rndBuf = new byte[sizeof(int)];
            RNGCryptoServiceProvider rndGen    = new RNGCryptoServiceProvider();
            QuestionSet           qs           = new QuestionSet(name + ".Test");
            List <int>            rndKeys      = new List <int>();
            Dictionary <int, int> questionPool = new Dictionary <int, int>();

            //Dictionary<int, int> questionIndex = new Dictionary<int, int>();

            rndGen.GetBytes(rndBuf);
            foreach (string s in selectedCategories)
            {
                if (categoryIndex.ContainsKey(s))
                {
                    foreach (int qID in categoryIndex[s])
                    {
                        if (!questionPool.ContainsValue(qID))
                        {
                            if (selectedSources.Contains(questions[qID].Source))
                            {
                                rndGen.GetBytes(rndBuf);
                                int rndKey = isRandomize ? BitConverter.ToInt32(rndBuf, 0) : qID;
                                questionPool.Add(rndKey, qID);
                                rndKeys.Add(rndKey);
                            }
                        }
                    }
                }
            }
            rndKeys.Sort();
            if (maxCount > rndKeys.Count)
            {
                maxCount = rndKeys.Count;
            }
            for (int i = 0; i < maxCount; i++)
            {
                Question q = this.questions[questionPool[rndKeys[i]]].Copy(isRandomize);
                if (q.Categories.Count > 1)
                {
                    string cat = "";
                    do
                    {
                        rndGen.GetBytes(rndBuf);
                        int rndKey = Math.Abs(BitConverter.ToInt32(rndBuf, 0)) % q.Categories.Count;
                        cat = q.Categories[rndKey];
                    } while (!IsCategorySelected(cat));
                    q.Categories.Clear();
                    q.Categories.Add(cat);
                }
                qs[i] = q;
            }
            return(qs);
        }
コード例 #2
0
        public static QuestionSet LoadFromXML(string fileName)
        {
            QuestionSet qs  = null;
            Stream      zip =
                new DeflateStream(new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read),
                                  CompressionMode.Decompress, false);
            XmlReader xReader = null;

            try
            {
                xReader = XmlReader.Create(zip);
            }
            catch
            {
                xReader = XmlReader.Create(fileName);
            }

            using (xReader)
            {
                Question q = null;
                xReader.Read();
                while (!xReader.EOF)
                {
                    switch (xReader.NodeType)
                    {
                    case XmlNodeType.Element:
                        switch (xReader.Name)
                        {
                        case "Questions":
                            qs = new QuestionSet(xReader["Name"]);
                            xReader.Read();
                            break;

                        case "Question":
                            if (q != null)
                            {
                                if (qs != null)
                                {
                                    qs[q.ID] = q;
                                }
                            }
                            q = new Question(Convert.ToInt32(xReader["ID"]));
                            xReader.Read();
                            break;

                        case "Category":
                            if (q != null)
                            {
                                q.Categories.Add(xReader.ReadElementContentAsString());
                            }
                            break;

                        case "Source":
                            if (q != null)
                            {
                                q.Source = xReader.ReadElementContentAsString();
                            }
                            break;

                        case "Text":
                            if (q != null)
                            {
                                Question.TextSectionFormat fmt = Question.TextSectionFormat.None;
                                if (xReader.HasAttributes)
                                {
                                    fmt = (Question.TextSectionFormat)
                                          Enum.Parse(typeof(Question.TextSectionFormat), xReader["Format"]);
                                }
                                q.AppendTextSection(fmt, xReader.ReadElementContentAsString().Replace("\n", "\r\n"));
                            }
                            else
                            {
                                xReader.Read();
                            }
                            break;

                        case "Answer":
                            if (q != null)
                            {
                                bool valid = Convert.ToBoolean(xReader["Valid"]);
                                q.Answer.Variants.Add(new Variant(xReader.ReadElementContentAsString().Replace("\n", "\r\n"), valid));
                            }
                            else
                            {
                                xReader.Read();
                            }
                            break;

                        default:
                            xReader.Read();
                            break;
                        }
                        break;

                    default:
                        xReader.Read();
                        break;
                    }
                }
                if (q != null)
                {
                    if (qs != null)
                    {
                        qs[q.ID] = q;
                    }
                }
            }
            qs.fileName   = fileName;
            qs.isModified = false;
            return(qs);
        }