コード例 #1
0
ファイル: GIFTUtilities.cs プロジェクト: ngocnt1801/QBCS
        public List <QuestionTmpModel> StripTagsCharArray(StreamReader reader, bool checkCate, bool checkHTML)
        {
            List <QuestionTmpModel> list = new List <QuestionTmpModel>();

            try
            {
                string line = null;
                List <OptionViewModel> options = new List <OptionViewModel>();
                bool isStartQuestion           = false;
                //string destination = "[html]";
                int           countCode     = 0;
                int           inLine        = 0;
                bool          isPlain       = false;
                StringProcess stringProcess = new StringProcess();
                while ((line = reader.ReadLine()) != null)
                {
                    string id       = null;
                    int    colon    = 0;
                    string question = null;
                    string right    = null;
                    string wrong    = null;
                    string result   = null;
                    string mark     = null;
                    //string flagCategory = "";
                    int countRight = 0;
                    int countWrong = 0;
                    //int countCate = 0;
                    //int countStartCate = 0;
                    int  countElement = 1;
                    bool isBlock      = false;
                    //bool isStartCate = false;
                    bool isInLine = false;
                    bool isStart  = false;
                    bool isEnd    = false;
                    //bool keeping = false;

                    //bool isComma = false;
                    bool isMultipleChoice = false;
                    if (line.Contains("$CATEGORY"))
                    {
                        string[] temp = line.Split('/');
                        for (int i = 0; i < temp.Count(); i++)
                        {
                            if (i == 1)
                            {
                                category = temp[i];
                            }
                            if (i == 2)
                            {
                                learningOutcome = temp[i];
                            }
                            if (i == 3)
                            {
                                level = temp[i];
                            }
                        }
                    }
                    if (!line.StartsWith("//"))
                    {
                        line = stringProcess.RemoveHtmlBrTag(line);

                        string resultTmp = "";

                        if (line.Contains("[plain]"))
                        {
                            isPlain = true;
                        }
                        if (checkHTML == false && isPlain == false)
                        {
                            HtmlDocument htmlDoc = new HtmlDocument();
                            htmlDoc.LoadHtml(line);
                            resultTmp = htmlDoc.DocumentNode.InnerText;
                        }
                        if (!resultTmp.Equals(""))
                        {
                            result = WebUtility.HtmlDecode(resultTmp);
                        }
                        else
                        {
                            result = line;
                        }

                        result = stringProcess.RemoveHtmlTagGIFT(result);
                        for (int i = 0; i < result.Length; i++)
                        {
                            char let = result[i];
                            #region start count to track the position
                            //if (let == '$' && !isStartQuestion)
                            //{
                            //    category = "";
                            //    level = "";
                            //    learningOutcome = "";
                            //    countStartCate++;
                            //    continue;
                            //}
                            //if (let == '$')
                            //{
                            //    category = "";
                            //    level = "";
                            //    learningOutcome = "";
                            //    flagCategory += let;


                            //}
                            //if (flagCategory.Equals("$CATEGORY"))
                            //{
                            //    countStartCate++;
                            //    continue;
                            //}

                            //if (let == '/' && !isStartQuestion)
                            //{

                            //    isStartCate = true;
                            //    countCate++;
                            //    continue;
                            //}
                            if (let == ':' && !isStartQuestion)
                            {
                                countCode++;
                                colon    = colon + 1;
                                isStart  = true;
                                isInLine = true;
                                continue;
                            }
                            if (inLine == 1)
                            {
                                char tab = (char)9;

                                if (let == '=' || let == '~' || let == tab)
                                {
                                    inLine          = 2; //{ is end question
                                    countCode       = 0;
                                    isStartQuestion = true;
                                    // continue;
                                }
                                else
                                {
                                    inLine = 3; // { in question
                                }
                            }
                            if (let == '=' && !isBlock && isStartQuestion && isInLine == false && inLine == 2)
                            {
                                countRight++;
                                countWrong = 0;
                                isBlock    = true;
                                isInLine   = true;
                                continue;
                            }
                            if (let == '~' && !isBlock && isStartQuestion && isInLine == false && inLine == 2)
                            {
                                countWrong++;
                                countRight = 0;
                                isBlock    = true;
                                isInLine   = true;
                                continue;
                            }
                            if (let == '}' && isStartQuestion && isInLine == false)
                            {
                                isEnd           = true;
                                isStartQuestion = false;
                                continue;
                            }

                            //if (let == '?')
                            //{
                            //    isComma = true;

                            //}
                            if (let == '{' && !isBlock)
                            {
                                //countCode = 0;
                                //isStartQuestion = true;
                                inLine = 1; // not sure to end question
                            }


                            #endregion

                            #region add character to variables
                            //if (countStartCate == 3 && isStartCate)
                            //{

                            //    if (countCate == 1)
                            //    {
                            //        category += let;
                            //        continue;
                            //    }
                            //    if (countCate == 2)
                            //    {
                            //        learningOutcome += let;
                            //        continue;
                            //    }
                            //    if (countCate == 3)
                            //    {
                            //        level += let;
                            //        continue;
                            //    }


                            //}
                            // if (countCode <= 3 && countStartCate == 0 && isStart)
                            if (countCode <= 3 && isStart)
                            {
                                id += let;
                                continue;
                            }
                            if (countCode >= 4)
                            {
                                // { char is in question
                                if (inLine == 3)
                                {
                                    question += '{';
                                    if (colon >= 5)
                                    {
                                        question += ':';
                                        colon     = 4;
                                    }
                                    question += let;
                                    inLine    = 0;
                                    isStart   = false;
                                    continue;
                                }
                                if (inLine == 0)
                                {
                                    if (colon >= 5)
                                    {
                                        question += ':';
                                        colon     = 4;
                                    }
                                    question += let;
                                    inLine    = 0;
                                    isStart   = false;
                                    continue;
                                }
                            }
                            if (countRight >= 1 && !isEnd)
                            {
                                right += let;
                                continue;
                            }
                            if (isMultipleChoice == true)
                            {
                                if (let != '%')
                                {
                                    mark += let;
                                    continue;
                                }
                            }
                            if (mark != null)
                            {
                                if (!mark.Equals(""))
                                {
                                    if (int.Parse(mark) < 0)
                                    {
                                        countWrong++;
                                        countRight       = 0;
                                        isBlock          = true;
                                        isMultipleChoice = false;
                                        mark             = null;
                                        continue;
                                    }
                                    if (int.Parse(mark) > 0)
                                    {
                                        countRight++;
                                        countWrong       = 0;
                                        isBlock          = true;
                                        isMultipleChoice = false;
                                        mark             = null;
                                        continue;
                                    }
                                }
                            }

                            if (countWrong >= 1 && !isEnd)
                            {
                                if (let == '%' && countElement == 1)
                                {
                                    isMultipleChoice = true;
                                    continue;
                                }
                                else
                                {
                                    wrong += let;
                                    countElement++;
                                    continue;
                                }
                            }
                            #endregion
                        }
                    }
                    if (category != null /*&& learningOutcome != null && level != null*/ && checkCate == true)
                    {
                        if (checkCate == false)
                        {
                            category        = "";
                            learningOutcome = "";
                            level           = "";
                            //countStartCate = 0;
                            //isStartCate = false;
                        }
                        else
                        {
                            quesModel.Category        = category;
                            quesModel.LearningOutcome = learningOutcome;
                            quesModel.Level           = level;
                            //countStartCate = 0;
                            //isStartCate = false;
                        }
                    }
                    if (id != null)
                    {
                        quesModel.Code = id;
                        id             = null;
                    }
                    if (question != null)
                    {
                        if (question.Equals(""))
                        {
                            quesModel.QuestionContentError = "Question content is empty";
                        }

                        if (quesModel.QuestionContent != null)
                        {
                            quesModel.QuestionContent += "<br>" + question;
                        }
                        else
                        {
                            question = stringProcess.UpperCaseKeyWord(question);
                            if (question.Contains("[html]"))
                            {
                                quesModel.QuestionContent = question;
                            }
                            else
                            {
                                quesModel.QuestionContent = "[html]" + question;
                            }
                        }
                        question = null;
                    }

                    if (right != null)
                    {
                        optionModel = new OptionViewModel();
                        optionModel.OptionContent = "[html]" + right;
                        optionModel.IsCorrect     = true;
                        options.Add(optionModel);
                    }
                    if (wrong != null)
                    {
                        optionModel = new OptionViewModel();
                        optionModel.OptionContent = "[html]" + wrong;
                        optionModel.IsCorrect     = false;
                        options.Add(optionModel);
                    }

                    if (quesModel.QuestionContent != null && isEnd && quesModel.Code != null)
                    {
                        int  countMatching  = 0;
                        bool isMatchingQues = false;
                        if (options.Count < 4)
                        {
                            quesModel.Error = "Number of option " + options.Count.ToString();
                        }

                        quesModel.Options = options;
                        foreach (var item in quesModel.Options)
                        {
                            if (item.IsCorrect == true)
                            {
                                countMatching  = countMatching + 1;
                                isMatchingQues = true;
                            }
                            if (item.IsCorrect == false)
                            {
                                countMatching = 0;
                            }
                            if (countMatching >= 4 && isMatchingQues)
                            {
                                quesModel.Status = (int)Enum.StatusEnum.Invalid;
                            }
                        }
                        list.Add(quesModel);
                        quesModel        = new QuestionTmpModel();
                        options          = new List <OptionViewModel>();
                        countCode        = 0;
                        inLine           = 0;
                        isMultipleChoice = false;
                        isPlain          = false;
                    }
                }
            }
            catch (Exception ex)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                quesModel.OtherError = "Other Error " + ex.Message.ToString();
            }

            return(list);
        }
コード例 #2
0
        private static void IterateTable(WTable table)
        {
            List <DocViewModel> optionCheckList = new List <DocViewModel>();

            foreach (WTableRow row in table.Rows)
            {
                #region get key

                IEntity titleEntity = row.Cells[0].ChildEntities[0];
                string  key         = (titleEntity as WParagraph).Text;

                #endregion

                #region question content
                if (key.Contains("QN=") || key.Contains("QN ="))
                {
                    quesModel.Code = key.Replace("QN=", "").Replace("QN =", "").Trim();
                    //for (int i = 0; i < row.Cells[1].ChildEntities.Count; i++)
                    //{
                    //    IEntity bodyItemEntity = row.Cells[1].ChildEntities[i];
                    //    WParagraph wParagraph = bodyItemEntity as WParagraph;
                    //    if (wParagraph.ChildEntities.Count != 0)
                    //    {
                    //        ParagraphItem pItem = wParagraph.ChildEntities[0] as ParagraphItem;
                    //        switch (pItem.EntityType)
                    //        {
                    //            default:
                    //                if (!(wParagraph.Text.Contains("[file") || wParagraph.Text.Equals("")) && quesModel.QuestionContent == null)
                    //                {
                    //                    quesModel.QuestionContent = "[html] " + wParagraph.Text.Replace("\v", "<cbr>");
                    //                }
                    //                else if (!(wParagraph.Text.Contains("[file") || wParagraph.Text.Equals("")))
                    //                {
                    //                    WTextRange text = pItem as WTextRange;
                    //                    quesModel.QuestionContent = quesModel.QuestionContent + "<cbr>" + wParagraph.Text;
                    //                }
                    //                break;
                    //            case EntityType.Picture:
                    //                WPicture wPicture = pItem as WPicture;
                    //                Image iImage = wPicture.Image;

                    //                MemoryStream m = new MemoryStream();
                    //                iImage.Save(m, iImage.RawFormat);
                    //                byte[] imageBytes = m.ToArray();

                    //                quesModel.Image = Convert.ToBase64String(imageBytes);
                    //                break;
                    //        }
                    //    }
                    //    if (quesModel.Image != null)
                    //    {
                    //        break;
                    //    }
                    //}

                    for (int i = 0; i < row.Cells[1].ChildEntities.Count; i++)
                    {
                        bool       inputWholeParagraph = false;
                        IEntity    bodyItemEntity      = row.Cells[1].ChildEntities[i];
                        WParagraph wParagraph          = bodyItemEntity as WParagraph;
                        if (wParagraph.ChildEntities.Count != 0)
                        {
                            foreach (var pChild in wParagraph.ChildEntities)
                            {
                                var pItem = pChild as ParagraphItem;
                                switch (pItem.EntityType)
                                {
                                case EntityType.TextRange:
                                    if (!inputWholeParagraph)
                                    {
                                        if (!wParagraph.Text.Equals("") && quesModel.QuestionContent == null)
                                        {
                                            quesModel.QuestionContent = "[html] " + wParagraph.Text.Replace("\v", "<cbr>").Split(new string[] { "[file" }, StringSplitOptions.None)[0];
                                            inputWholeParagraph       = true;
                                        }
                                        else if (!wParagraph.Text.Equals(""))
                                        {
                                            quesModel.QuestionContent = quesModel.QuestionContent + "<cbr>" + wParagraph.Text.Split(new string[] { "[file" }, StringSplitOptions.None)[0];
                                            inputWholeParagraph       = true;
                                        }
                                    }
                                    break;

                                case EntityType.Picture:
                                    WPicture             wPicture = pItem as WPicture;
                                    System.Drawing.Image iImage   = wPicture.Image;

                                    MemoryStream m = new MemoryStream();
                                    iImage.Save(m, iImage.RawFormat);
                                    byte[] imageBytes = m.ToArray();

                                    ImageViewModel image = new ImageViewModel();
                                    image.Source = Convert.ToBase64String(imageBytes);
                                    images.Add(image);
                                    break;
                                }
                            }
                        }
                    }
                }
                #endregion
                #region option content
                else if (key.Contains("."))
                {
                    var optionImages = new List <ImageViewModel>();
                    var optionCheck  = new DocViewModel();
                    optionCheck.Code = key.Replace(".", "").ToLower();
                    //for (int i = 0; i < row.Cells[1].ChildEntities.Count; i++)
                    //{
                    //    IEntity bodyItemEntity = row.Cells[1].ChildEntities[i];
                    //    WParagraph wParagraph = bodyItemEntity as WParagraph;
                    //    if (wParagraph.Text != "")
                    //    {
                    //        ParagraphItem pItem = wParagraph.ChildEntities[0] as ParagraphItem;
                    //        switch (pItem.EntityType)
                    //        {
                    //            //case EntityType.TextRange:
                    //            default:
                    //                if (!wParagraph.Text.Equals("") && optionModel.OptionContent == null)
                    //                {
                    //                    optionModel.IsCorrect = false;
                    //                    optionModel.OptionContent = wParagraph.Text.Replace("\v", "<cbr>");
                    //                }
                    //                else if (!wParagraph.Text.Equals(""))
                    //                {
                    //                    optionModel.OptionContent = optionModel.OptionContent + "<cbr>" + wParagraph.Text.Replace("\v", "<cbr>");
                    //                }
                    //                break;
                    //            case EntityType.Picture:
                    //                //WPicture wPicture = pItem as WPicture;
                    //                //Image iImage = wPicture.Image;

                    //                //MemoryStream m = new MemoryStream();
                    //                //iImage.Save(m, iImage.RawFormat);
                    //                //byte[] imageBytes = m.ToArray();

                    //                //quesModel.Image = Convert.ToBase64String(imageBytes);
                    //                break;
                    //        }
                    //    }
                    //}


                    for (int i = 0; i < row.Cells[1].ChildEntities.Count; i++)
                    {
                        bool       inputWholeParagraph = false;
                        IEntity    bodyItemEntity      = row.Cells[1].ChildEntities[i];
                        WParagraph wParagraph          = bodyItemEntity as WParagraph;
                        if (wParagraph.ChildEntities.Count != 0)
                        {
                            foreach (var pChild in wParagraph.ChildEntities)
                            {
                                var pItem = pChild as ParagraphItem;
                                switch (pItem.EntityType)
                                {
                                case EntityType.TextRange:
                                    if (!inputWholeParagraph)
                                    {
                                        if (!wParagraph.Text.Equals("") && optionModel.OptionContent == null)
                                        {
                                            optionModel.IsCorrect     = false;
                                            optionModel.OptionContent = wParagraph.Text.Replace("\v", "<cbr>");
                                            inputWholeParagraph       = true;
                                        }
                                        else if (!wParagraph.Text.Equals(""))
                                        {
                                            optionModel.OptionContent = optionModel.OptionContent + "<cbr>" + wParagraph.Text;
                                        }
                                    }
                                    break;

                                case EntityType.Picture:

                                    WPicture             wPicture = pItem as WPicture;
                                    System.Drawing.Image iImage   = wPicture.Image;

                                    MemoryStream m = new MemoryStream();
                                    iImage.Save(m, iImage.RawFormat);
                                    byte[] imageBytes = m.ToArray();

                                    ImageViewModel image = new ImageViewModel();
                                    image.Source = Convert.ToBase64String(imageBytes);
                                    optionImages.Add(image);
                                    break;
                                }
                            }
                        }
                    }
                    optionCheck.Content = optionModel.OptionContent;
                    optionCheckList.Add(optionCheck);
                    optionModel.Images = optionImages;
                    if (optionModel.OptionContent != null || (optionModel.Images != null && optionModel.Images.Count() != 0))
                    {
                        options.Add(optionModel);
                    }
                    optionModel = new OptionViewModel();
                }
                #endregion
                else if (key.Contains("ANSWER:"))
                {
                    IEntity    bodyItemEntity = row.Cells[1].ChildEntities[0];
                    WParagraph paragraph      = bodyItemEntity as WParagraph;
                    if (!paragraph.Text.Equals(""))
                    {
                        var    trim    = paragraph.Text.Replace(" ", "").ToLower();
                        char[] answers = trim.ToCharArray();
                        foreach (var optionCheck in optionCheckList)
                        {
                            for (int i = 0; i < answers.Length; i++)
                            {
                                if (optionCheck.Code.Equals(answers[i].ToString()))
                                {
                                    foreach (var option in options)
                                    {
                                        if (option.OptionContent.Equals(optionCheck.Content))
                                        {
                                            option.IsCorrect = true;
                                            break;
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
                else if (key.Contains("UNIT:"))
                {
                    IEntity    bodyItemEntity = row.Cells[1].ChildEntities[0];
                    WParagraph paragraph      = bodyItemEntity as WParagraph;
                    if (!paragraph.Text.Equals(""))
                    {
                        var number = Regex.Match(paragraph.Text, @"\d+$").ToString();
                        if (globalPrefix == "")
                        {
                            quesModel.LearningOutcome = paragraph.Text;
                        }
                        else
                        {
                            quesModel.LearningOutcome = globalPrefix + " " + number;
                        }
                    }
                }
                else if (key.Contains("MARK:"))
                {
                    IEntity    bodyItemEntity = row.Cells[1].ChildEntities[0];
                    WParagraph paragraph      = bodyItemEntity as WParagraph;
                    switch (paragraph.Text)
                    {
                    //default:
                    //    quesModel.Level = "Easy";
                    //    break;
                    case "1":
                        quesModel.Level = "Easy";
                        break;

                    case "2":
                        quesModel.Level = "Medium";
                        break;

                    case "3":
                        quesModel.Level = "Hard";
                        break;
                    }
                }
                else if (key.Contains("CATEGORY:"))
                {
                    IEntity    bodyItemEntity = row.Cells[1].ChildEntities[0];
                    WParagraph paragraph      = bodyItemEntity as WParagraph;
                    if (paragraph != null && !paragraph.Text.Equals(""))
                    {
                        quesModel.Category = paragraph.Text;
                    }
                }
            }
            if (images != null)
            {
                quesModel.Images = images;
            }
            quesModel.Options = options;
            listQuestion.Add(quesModel);
            quesModel       = new QuestionTmpModel();
            images          = new List <ImageViewModel>();
            options         = new List <OptionViewModel>();
            optionCheckList = new List <DocViewModel>();
        }