/// <summary> /// 上传前整理treeview中节点内容,并生成最终上传内容 /// </summary> /// <param name="treenode"></param> /// <param name="subjectkind"></param> /// <param name="subjectItem"></param> private void createSubjectItem(Subject subjectParent, ref SubjectItem subjectItem) { foreach (var subject in subjectParent.ListChild) { subject.subjecthtmlcontent = subject.subjecthtmlcontent.Replace("<check>", "").Replace("</check>", "").Replace("<nocheck>", "").Replace("</nocheck>", ""); SubjectItem subjectItemtemp = new SubjectItem(); //插入节信息 subjectItemtemp.subjectkinds = subject.subjecttextcontent; string html = ReplaceImgNbsp(subject.subjecthtmlcontent); html = Regex.Replace(html, @"^<pre[^>]*>([\s\S]+?)</pre>(.*)", "$1$2", RegexOptions.IgnoreCase); subjectItemtemp.subjecthtmlcontent = html; //替换答案中图片中的\r\n subject.answer.subjecthtmlanswer = ReplaceImgNbsp(subject.answer.subjecthtmlanswer); //匹配图片中的img src内容返回src地址 //string timedir = "/files/tsubjectfile/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/" + "image/"; List<string> img = new List<string>(); img.AddRange(HtmlUtil.imgSrcForName(ref subjectItemtemp.subjecthtmlcontent, uploadimgsdic));//防火html中img图片地址列表,并修改地址为网络地址 subjectItemtemp.subjecttextcontent = subject.subjecttextcontent; subjectItemtemp.questionthinking = SubjectTool.ReplaceHTML(subject.questionthinking,false); subjectItemtemp.score = subject.分值; subjectItemtemp.sequence = subject.套卷序号; //subjectItemtemp.selectItemCount = subject.选项个数; subjectItemtemp.isobjective = subject.客观题.GetHashCode(); subjectItemtemp.subjecttype = subject.题目类型.GetHashCode().ToString(); //过滤html中vml内容,存入过滤后的字段 //subjectItemtemp.subjectfilterhtmlcontent = SubjectTypeMark(subjectItemtemp.subjecthtmlcontent, subject.题目类型.GetHashCode()); if (subject.answer != null && subject.answer.subjecthtmlanswer != null)//处理答案相关数据 { //subjectItemtemp.answerbinarycontent = subject.answer.subjectbinaryanswer; string htmla = subject.answer.subjecthtmlanswer; subjectItemtemp.answerhtmlcontent = htmla; img.AddRange(HtmlUtil.imgSrcForName(ref subjectItemtemp.answerhtmlcontent, uploadimgsdic)); subjectItemtemp.answertextcontent = subject.answer.subjecttextanswer; //过滤html中vml内容,单选题、多选题、对错题才用存文本形式 if (subjectItemtemp.subjecttype.Equals(Subject.SubjecttypeEnum.单选题) || subjectItemtemp.subjecttype.Equals(Subject.SubjecttypeEnum.多选题) || subjectItemtemp.subjecttype.Equals(Subject.SubjecttypeEnum.判断题)) { subjectItemtemp.answerhtmlcontent = subject.answer.subjecttextanswer.Trim(); } } //替换解题思路中的图片 if (!string.IsNullOrEmpty(subject.questionthinking)) { subjectItemtemp.questionthinking = ReplaceImgNbsp(subject.questionthinking); img.AddRange(HtmlUtil.imgSrcForName(ref subjectItemtemp.questionthinking, uploadimgsdic)); } //替换音频 img.AddRange(HtmlUtil.Mp3SrcForName(ref subjectItemtemp.subjecthtmlcontent, uploadimgsdic)); subjectItemtemp.img = img.ToArray(); SubjectTypeMarkAndCreateOption(ref subjectItemtemp, subject.题目类型.GetHashCode());//选项标识并列出选项 subjectItem.subSubjectItemList.Add(subjectItemtemp); if(subject.ListChild.Count>0) { createSubjectItem(subject, ref subjectItemtemp); } } }
/// <summary> /// 压缩并上传拆分后的题目 /// </summary> /// <param name="onlyZip"></param> private void zipFileAndUpload(bool onlyZip) { try { Paper paper = new Paper(); //遍历子节点中的内容 for (int i = 0; i < listSubject.Count; i++) { SubjectItem subjectItem = new SubjectItem(); createSubjectItem(listSubject[i], ref subjectItem); subjectItem.subjecttextcontent = SubjectTool.ReplaceHTML(listSubject[i].subjecthtmlcontent); subjectItem.subjecthtmlcontent = SubjectTool.ReplaceHTML(listSubject[i].subjecthtmlcontent); paper.bigSubject.Add(subjectItem); } if (paper.bigSubject.Count > 0) { if (paper.bigSubject[0].subSubjectItemList.Count == 0) { MessageBox.Show("文件未被识别或第一个大题没有试题。", "提示"); return; } } paper.examclipid = testpaper.ExamClipId; paper.xueke = testpaper.XueKe; paper.xueduan = Convert.ToInt32(testpaper.XuekDuan); paper.tag = testpaper.Tag; paper.papername = testpaper.PaperName; gPaper = paper; string xmlpaper = PaperSerializerXml(paper); //生成xml文件 //上传文件内容 if (!onlyZip) { PostPaper(xmlpaper); } } catch (Exception ex) { string strErr = ex.Message; MessageBox.Show("上传失败,请重新上传!", "错误"); } }
private static bool SelectAnswerPair(SubjectItem subjectitem, int i, bool isanswer) { if (subjectitem.answertextcontent != null) { switch (i) { case 1: if (subjectitem.answertextcontent.Contains("A") || subjectitem.answertextcontent.Contains("a")) isanswer = true; break; case 2: if (subjectitem.answertextcontent.Contains("B") || subjectitem.answertextcontent.Contains("b")) isanswer = true; break; case 3: if (subjectitem.answertextcontent.Contains("C") || subjectitem.answertextcontent.Contains("c")) isanswer = true; break; case 4: if (subjectitem.answertextcontent.Contains("D") || subjectitem.answertextcontent.Contains("d")) isanswer = true; break; case 5: if (subjectitem.answertextcontent.Contains("E") || subjectitem.answertextcontent.Contains("e")) isanswer = true; break; case 6: if (subjectitem.answertextcontent.Contains("F") || subjectitem.answertextcontent.Contains("f")) isanswer = true; break; case 7: if (subjectitem.answertextcontent.Contains("G") || subjectitem.answertextcontent.Contains("g")) isanswer = true; break; case 8: if (subjectitem.answertextcontent.Contains("H") || subjectitem.answertextcontent.Contains("h")) isanswer = true; break; case 9: if (subjectitem.answertextcontent.Contains("I") || subjectitem.answertextcontent.Contains("i")) isanswer = true; break; } } return isanswer; }
/// <summary> /// 试题类型处理 /// 1.单(多)项选择题拆分选项。 /// 2.填空题添加标识 /// 3.判断题转换数据 /// </summary> /// <param name="subjectitem">题目内容</param> /// <param name="subjecttype">题目类型</param> /// <returns></returns> private void SubjectTypeMarkAndCreateOption(ref SubjectItem subjectitem, int subjecttype) { //html = HtmlUtil.htmlreplace(html); if (subjecttype == Subject.SubjecttypeEnum.单选题.GetHashCode() || subjecttype == Subject.SubjecttypeEnum.多选题.GetHashCode()) { subjectitem.subjecthtmlcontent = HtmlUtil.selectSubjectOptionMarkUpload(subjectitem.subjecthtmlcontent); string tempsubjecthtmlcontent = subjectitem.subjecthtmlcontent;//临时存放试题提干变量 bool isanswer = false; string[] htmls = Regex.Split(tempsubjecthtmlcontent, MatchArray.Select_Option, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline); for (int i = 0; i < htmls.Length; i++) { if (i == 0) { subjectitem.subjecthtmlcontent = SubjectTool.ReplaceHTML(htmls[i]); } else { isanswer = false; //判断答案是否正确 isanswer = SelectAnswerPair(subjectitem, i, isanswer); string answercontent = SubjectTool.ReplaceHTML(htmls[i]); Answer answer = new Answer(answercontent, isanswer, i); subjectitem.answers.Add(answer); } } return; } if (subjecttype == Subject.SubjecttypeEnum.填空题.GetHashCode()) { subjectitem.subjecthtmlcontent = HtmlUtil.spacePlaceMarkUpLoad(subjectitem.subjecthtmlcontent); Regex regspacenum = new Regex(MatchArray.Space_Num); int i = 0; // 取得匹配项列表 while (regspacenum.IsMatch(subjectitem.subjecthtmlcontent)) { subjectitem.subjecthtmlcontent = regspacenum.Replace(subjectitem.subjecthtmlcontent, string.Format(MatchArray.Space_Num_Replace, i + 1), 1); i++; } int sort = 0;//答案顺序 string[] answerarr = subjectitem.answerhtmlcontent.Split('|'); foreach (string content in answerarr) { sort++; Answer answer = new Answer(SubjectTool.ReplaceHTML(content), true, sort); subjectitem.answers.Add(answer); } while (sort < i) { sort++; Answer answer = new Answer("", true, sort); subjectitem.answers.Add(answer); } return; } if (subjecttype == Subject.SubjecttypeEnum.判断题.GetHashCode()) { string answerstr = "0"; if (subjectitem.answertextcontent.IndexOf("正确") >= 0 || subjectitem.answertextcontent.IndexOf("对") >= 0) answerstr = "1"; Answer answer = new Answer(answerstr, true, 1); subjectitem.answers.Add(answer); return; } Answer answerother = new Answer(subjectitem.answerhtmlcontent, true, 1); subjectitem.answers.Add(answerother); }