ExamPaperServices.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using EMISOnline.DataLogic.ExamSetting;
  6. using Bowin.Common.Linq.Entity;
  7. using EMISOnline.ViewModel.ExamView;
  8. using System.Linq.Expressions;
  9. using EMISOnline.Entities;
  10. using System.Linq.Expressions;
  11. using Bowin.Common.Linq;
  12. using EMISOnline.ViewModel;
  13. using Bowin.Common;
  14. using System.Web.Script.Serialization;
  15. using System.Xml.Serialization;
  16. using System.IO;
  17. using System.Xml.Linq;
  18. using EMISOnline.DataLogic.SystemDAL;
  19. using System.Runtime.Remoting.Contexts;
  20. using System.Data;
  21. namespace EMISOnline.CommonLogic.ExamServices
  22. {
  23. public class ExamPaperServices : IExamPaperServices
  24. {
  25. public ExamPaperDAL ExamPaperDAL { get; set; }
  26. public object ListPaper(int maximumRows, int startRowIndex, string PaperName)
  27. {
  28. Expression<Func<test_paper, bool>> exp = (x => true);
  29. if (!string.IsNullOrEmpty(PaperName))
  30. exp = exp.And(q => q.PaperName.Contains(PaperName));
  31. var query = ExamPaperDAL.ListPaper(maximumRows, startRowIndex, exp);
  32. return query.OrderBy(q => q.test_paper_id).ToGridResultSet(maximumRows, startRowIndex);
  33. }
  34. public test_paper GetPaperByPaperID(decimal PaperID)
  35. {
  36. return ExamPaperDAL.PaperRepository.GetSingle(q => q.test_paper_id == PaperID);
  37. }
  38. /// <summary>
  39. /// 根据一序列的条件查找不同题型的题数是多少
  40. /// </summary>
  41. /// <param name="lib_ids"></param>
  42. /// <param name="diff_min"></param>
  43. /// <param name="diff_max"></param>
  44. /// <param name="err_rate"></param>
  45. /// <param name="usecount_min"></param>
  46. /// <param name="usecount_max"></param>
  47. /// <param name="question_type"></param>
  48. /// <returns></returns>
  49. public object CountQuesSumByCondition(List<decimal> lib_ids, decimal diff_min, decimal diff_max, decimal err_rate,
  50. int usecount_min, int usecount_max)
  51. {
  52. return ExamPaperDAL.CountQuesSumByCondition(lib_ids, diff_min, diff_max, err_rate, usecount_min, usecount_max);
  53. }
  54. public bool ExamPaperSave(PaperAddView PaperAdd, string LoginID)
  55. {
  56. var paper = ExamPaperDAL.PaperRepository.GetSingle(q => q.test_paper_id == PaperAdd.PaperID);
  57. if (paper == null)
  58. {
  59. paper = new test_paper();
  60. Assignment(PaperAdd, paper, LoginID);
  61. ExamPaperDAL.PaperRepository.UnitOfWork.Add(paper);
  62. }
  63. else
  64. {
  65. Assignment(PaperAdd, paper, LoginID);
  66. //分数
  67. paper.paper_score = CalcPaperScore(PaperAdd.QuestionTypeString);
  68. }
  69. ExamPaperDAL.PaperRepository.UnitOfWork.Commit();
  70. return true;
  71. }
  72. private void Assignment(PaperAddView PaperAdd, test_paper paper, string LoginID)
  73. {
  74. paper.created_by = LoginID;
  75. paper.created_date = DateTime.Now;
  76. paper.difficulty_degree_minvalue = Convert.ToDecimal(PaperAdd.QuesLevelBeg);
  77. paper.difficulty_degree_maxvalue = Convert.ToDecimal(PaperAdd.QuesLevelEnd);
  78. paper.used_count_min = Convert.ToInt32(PaperAdd.UseFBeg);
  79. paper.used_count_max = Convert.ToInt32(PaperAdd.UseFEnd);
  80. //paper.distributing_option = Convert.ToInt32(this.rblDis.SelectedValue);
  81. paper.error_percent = Convert.ToDecimal(PaperAdd.ErrRate);
  82. paper.is_vaild = PaperAdd.IsEnable == "0" ? false : true;
  83. paper.PaperName = PaperAdd.PageName;
  84. paper.Prority = Convert.ToInt32(GetQuesChooseSort(PaperAdd.QuesFCount, PaperAdd.QuesUpdateTime));
  85. paper.question_libary_set_id = TranQuesLib(PaperAdd.LibarysString);
  86. paper.is_autogenerate = false;
  87. paper.is_dynamic = true;
  88. paper.note = PaperAdd.LibarysString;
  89. paper.question_setting = PaperAdd.QuestionTypeString;
  90. }
  91. private string TranQuesLib(string ques_lib)
  92. {
  93. var json = new JavaScriptSerializer();
  94. var libarys = json.Deserialize<List<QuestionLibaryArrangeView>>(ques_lib);
  95. return string.Join(",", libarys.Select(q => q.Libaryid).ToArray());
  96. }
  97. private decimal CalcPaperScore(string ques)
  98. {
  99. var json = new JavaScriptSerializer();
  100. var libarys = json.Deserialize<List<QuestionTypeView>>(ques);
  101. return libarys.Sum(q => q.PSum * q.QScore);
  102. }
  103. public PageSetupParam SetPageSetupCondition(PaperAddView PaperAdd)
  104. {
  105. PageSetupParam psp = new PageSetupParam();
  106. psp.IsEnable = PaperAdd.IsEnable;
  107. psp.PageName = PaperAdd.PageName;
  108. psp.PageType = PaperAdd.PageType;
  109. psp.QuesChooseSort = GetQuesChooseSort(PaperAdd.QuesFCount, PaperAdd.QuesUpdateTime);
  110. psp.QuesDis = "";//this.rblDis.SelectedValue;
  111. psp.QuesLevel = string.Format("{0}|{1}", PaperAdd.QuesLevelBeg, PaperAdd.QuesLevelEnd);
  112. psp.QuesLib = PaperAdd.LibarysString;
  113. psp.QuesSetting = PaperAdd.QuestionTypeString;
  114. psp.ErrRate = PaperAdd.ErrRate;
  115. psp.UseCount = string.Format("{0}|{1}", PaperAdd.UseFBeg, PaperAdd.UseFEnd);
  116. return psp;
  117. }
  118. /// <summary>
  119. /// 获取选择值
  120. /// </summary>
  121. /// <returns></returns>
  122. private string GetQuesChooseSort(bool QuesFCount, bool QuesUpdateTime)
  123. {
  124. if (QuesFCount && !QuesUpdateTime)
  125. {
  126. return "1";
  127. }
  128. else if (!QuesFCount && QuesUpdateTime)
  129. {
  130. return "2";
  131. }
  132. else if (QuesFCount && QuesUpdateTime)
  133. {
  134. return "3";
  135. }
  136. return "0";
  137. }
  138. public DataTable GetQuestionByPaperID(decimal paperid)
  139. {
  140. return ExamPaperDAL.GetQuestionByPaperID(paperid);
  141. }
  142. public DataTable GetQuestionByParam(PageSetupParam psp)
  143. {
  144. string test_question_libary_id = TranQuesLib(psp.QuesLib);
  145. string difficulty_degree_min = psp.QuesLevel.Split('|')[0];
  146. string difficulty_degree_max = psp.QuesLevel.Split('|')[1];
  147. string wrong_percent = psp.ErrRate;
  148. string used_count_min = psp.UseCount.Split('|')[0];
  149. string used_count_max = psp.UseCount.Split('|')[1];
  150. DataTable quesDt = ExamPaperDAL.SelectQuestions(test_question_libary_id, difficulty_degree_min, difficulty_degree_max,
  151. wrong_percent, used_count_min, used_count_max);
  152. //按照题库数量过滤试题
  153. quesDt = LibaryFilter(quesDt, psp.QuesLib);
  154. //按照设置条件过滤试题
  155. quesDt = QuestionFilter(quesDt, psp.QuesChooseSort, psp.PageType, psp.QuesDis, psp.QuesSetting);
  156. //quesDt.Select("1=1", "created_date des");
  157. return quesDt;
  158. }
  159. private DataTable LibaryFilter(DataTable dt, string quesLib)
  160. {
  161. var json = new JavaScriptSerializer();
  162. var libSettingArray = json.Deserialize<List<QuestionLibaryArrangeView>>(quesLib);
  163. DataTable filteredDT = dt.Clone();
  164. DataTable radioDT = null;
  165. foreach (var item in libSettingArray)
  166. {
  167. string libid = item.Libaryid.ToString();
  168. int libnum = item.Number;
  169. radioDT = TableFilterByLib(dt, libid, libnum);
  170. filteredDT.Merge(radioDT);
  171. }
  172. return filteredDT;
  173. }
  174. /// <summary>
  175. /// 按题库数量从各题库筛选试题
  176. /// </summary>
  177. /// <param name="dt"></param>
  178. /// <param name="typeid"></param>
  179. /// <returns></returns>
  180. private DataTable TableFilterByLib(DataTable dt, string libid, int number)
  181. {
  182. DataTable filteredDT = dt.Clone();
  183. int count = 0;
  184. var dtView = dt.DefaultView;
  185. dtView.RowFilter = "test_question_libary_id=" + libid;
  186. foreach (DataRowView dr in dtView)
  187. {
  188. if (dr["test_question_libary_id"].ToString() == libid)
  189. {
  190. filteredDT.Rows.Add(dr.Row.ItemArray);
  191. if (number == ++count)
  192. break;
  193. }
  194. }
  195. return filteredDT;
  196. }
  197. /// <summary>
  198. /// 试题过滤
  199. /// </summary>
  200. /// <param name="dt"></param>
  201. /// <param name="chooseSortBy"></param>
  202. /// <param name="pageType"></param>
  203. /// <param name="quesDis"></param>
  204. /// <param name="pageSetting"></param>
  205. /// <returns></returns>
  206. private DataTable QuestionFilter(DataTable dt, string chooseSortBy, string pageType, string quesDis, string pageSetting)
  207. {
  208. var json = new JavaScriptSerializer();
  209. var pageSettingArray = json.Deserialize<List<QuestionTypeView>>(pageSetting);
  210. DataTable filteredDT = dt.Clone();
  211. DataTable radioDT = null;
  212. foreach (var item in pageSettingArray)
  213. {
  214. string question_type = item.QType.ToString();
  215. int pickerSum = (int)item.PSum;
  216. int totalSum = (int)item.TSum;
  217. radioDT = TableFilterByType(dt, question_type);
  218. radioDT = TableFilterByCondition(radioDT, chooseSortBy, pageType, quesDis, pickerSum, totalSum);
  219. filteredDT.Merge(radioDT);
  220. }
  221. return filteredDT;
  222. }
  223. /// <summary>
  224. /// 抽取试题第一道工序
  225. /// 过滤不同类型的试题形成各个子Table
  226. /// </summary>
  227. /// <param name="dt"></param>
  228. /// <param name="typeid"></param>
  229. /// <returns></returns>
  230. private DataTable TableFilterByType(DataTable dt, string typeid)
  231. {
  232. DataTable filteredDT = dt.Clone();
  233. foreach (DataRow dr in dt.Rows)
  234. {
  235. if (dr["base_question_type_id"].ToString() == typeid)
  236. {
  237. filteredDT.Rows.Add(dr.ItemArray);
  238. }
  239. }
  240. return filteredDT;
  241. }
  242. private DataTable TableFilterByCondition(DataTable dt, string chooseSortBy, string pageType, string quesDis, int pickerSum, int totalSum)
  243. {
  244. DataTable filteredDT = dt.Clone();
  245. string sortOrder = "";
  246. if (chooseSortBy == "1")
  247. {
  248. sortOrder = " used_count DESC ";
  249. }
  250. else if (chooseSortBy == "2")
  251. {
  252. sortOrder = " created_date DESC ";
  253. }
  254. else if (chooseSortBy == "3")
  255. {
  256. sortOrder = " used_count ,created_date DESC ";
  257. }
  258. int counter = 0;
  259. DataRow[] filteredRows = dt.Select("", sortOrder);
  260. //静态卷的抽取
  261. if (pageType == "0")
  262. {
  263. if (filteredRows.Length <= pickerSum)
  264. {
  265. counter = filteredRows.Length;
  266. }
  267. else
  268. {
  269. counter = pickerSum;
  270. }
  271. }
  272. else //动态抽卷
  273. {
  274. if (filteredRows.Length <= totalSum)
  275. {
  276. counter = filteredRows.Length;
  277. }
  278. else
  279. {
  280. counter = totalSum;
  281. }
  282. }
  283. //抽取
  284. for (int i = 0; i < counter; i++)
  285. {
  286. filteredDT.Rows.Add(filteredRows[i].ItemArray);
  287. }
  288. return filteredDT;
  289. }
  290. public void PaperEdit(decimal paperid, string question_str)
  291. {
  292. // test_paper p = paperMgr.GetSingle(paperid);
  293. ExamPaperDAL.DeletePaper(paperid);
  294. var paper = ExamPaperDAL.PaperRepository.GetSingle(q => q.test_paper_id == paperid);
  295. var quesList = question_str.Split(',');
  296. int order = 0;
  297. decimal score = 0m;
  298. List<QuestionXml> xmlValues = new List<QuestionXml>();
  299. foreach (var q in quesList)
  300. {
  301. if (q.Length > 0)
  302. {
  303. test_paper_question_set paper_ques = new test_paper_question_set();
  304. paper_ques.order = order;
  305. decimal? pSore = Convert.ToDecimal(q.Split('|')[1]);
  306. xmlValues.Add(new QuestionXml()
  307. {
  308. Question_id = decimal.Parse(q.Split('|')[0]),
  309. Score = pSore.Value
  310. });
  311. score += pSore ?? 0;
  312. }
  313. }
  314. XmlSerializer serializer = new XmlSerializer(typeof(List<QuestionXml>));
  315. MemoryStream mstream = new MemoryStream();
  316. serializer.Serialize(mstream, xmlValues);
  317. mstream.Close();
  318. test_paper_question_set qSet = new test_paper_question_set()
  319. {
  320. order = order,
  321. test_paper_id = paperid,
  322. questionXml = XElement.Parse(Encoding.Default.GetString(mstream.ToArray())).ToString()
  323. };
  324. //分数
  325. paper.paper_score = score;
  326. paper.test_paper_question_set.Add(qSet);
  327. ExamPaperDAL.PaperRepository.UnitOfWork.Commit();
  328. }
  329. public void AddPaperDetail(PageSetupParam psp, string question_str, string UserID)
  330. {
  331. string difficulty_degree_min = psp.QuesLevel.Split('|')[0];
  332. string difficulty_degree_max = psp.QuesLevel.Split('|')[1];
  333. string used_count_min = psp.UseCount.Split('|')[0];
  334. string used_count_max = psp.UseCount.Split('|')[1];
  335. test_paper paper = new test_paper();
  336. paper.created_by = UserID;
  337. paper.created_date = DateTime.Now;
  338. paper.difficulty_degree_maxvalue = Convert.ToDecimal(difficulty_degree_max);
  339. paper.difficulty_degree_minvalue = Convert.ToDecimal(difficulty_degree_min);
  340. //paper.distributing_option = Convert.ToInt32(psp.QuesDis);
  341. paper.error_percent = Convert.ToDecimal(psp.ErrRate);
  342. paper.is_vaild = psp.IsEnable == "0" ? false : true;
  343. paper.PaperName = psp.PageName;
  344. //paper.Prority = Convert.ToInt32(psp.QuesDis);
  345. paper.question_libary_set_id = TranQuesLib(psp.QuesLib);
  346. paper.is_autogenerate = false;
  347. paper.is_dynamic = psp.PageType == "0" ? false : true;
  348. paper.question_setting = psp.QuesSetting;
  349. paper.note = psp.QuesLib;
  350. paper.used_count_max = int.Parse(used_count_max);
  351. paper.used_count_min = int.Parse(used_count_min);
  352. var quesList = question_str.Split(',');
  353. int order = 0;
  354. decimal score = 0m;
  355. List<QuestionXml> xmlValues = new List<QuestionXml>();
  356. foreach (var q in quesList)
  357. {
  358. if (q.Length > 0)
  359. {
  360. test_paper_question_set paper_ques = new test_paper_question_set();
  361. paper_ques.order = order;
  362. decimal? pSore = Convert.ToDecimal(q.Split('|')[1]);
  363. xmlValues.Add(new QuestionXml()
  364. {
  365. Question_id = decimal.Parse(q.Split('|')[0]),
  366. Score = pSore.Value
  367. });
  368. score += pSore ?? 0;
  369. }
  370. }
  371. XmlSerializer serializer = new XmlSerializer(typeof(List<QuestionXml>));
  372. MemoryStream mstream = new MemoryStream();
  373. serializer.Serialize(mstream, xmlValues);
  374. mstream.Close();
  375. test_paper_question_set qSet = new test_paper_question_set()
  376. {
  377. order = order,
  378. questionXml = XElement.Parse(Encoding.Default.GetString(mstream.ToArray())).ToString()
  379. };
  380. //分数
  381. paper.paper_score = score;
  382. paper.test_paper_question_set.Add(qSet);
  383. ExamPaperDAL.PaperRepository.UnitOfWork.Add(paper);
  384. ExamPaperDAL.PaperRepository.UnitOfWork.Commit();
  385. }
  386. /// <summary>
  387. /// 根据试卷id获取试卷信息以json格式返回
  388. /// </summary>
  389. /// <param name="paperid"></param>
  390. /// <returns></returns>
  391. public string GetPaperQuestions(decimal paperid)
  392. {
  393. test_paper paper = ExamPaperDAL.PaperRepository.GetSingle(q => q.test_paper_id == paperid);
  394. if (paper.is_dynamic)
  395. {
  396. var question = GetDynamicPaperById(paperid);
  397. return question;
  398. }
  399. else
  400. {
  401. var question = ExamPaperDAL.GetPaperQuestions(paperid);
  402. return new JavaScriptSerializer().Serialize(question);
  403. }
  404. }
  405. /// <summary>
  406. /// 获取动态试卷
  407. /// </summary>
  408. /// <param name="paperid"></param>
  409. /// <returns></returns>
  410. public string GetDynamicPaperById(decimal paperid)
  411. {
  412. var t_paper = ExamPaperDAL.PaperRepository.GetSingle(q => q.test_paper_id == paperid);
  413. if (t_paper != null)
  414. {
  415. var json = new JavaScriptSerializer();
  416. var libs = json.Deserialize<List<QuestionLibaryArrangeView>>(t_paper.note);
  417. decimal diff_min = t_paper.difficulty_degree_minvalue.HasValue ? t_paper.difficulty_degree_minvalue.Value : 0m;
  418. decimal diff_max = t_paper.difficulty_degree_maxvalue.HasValue ? t_paper.difficulty_degree_maxvalue.Value : 0m;
  419. decimal wrong_rate = t_paper.error_percent.HasValue ? t_paper.error_percent.Value : 0m;
  420. int usedcount_min = t_paper.used_count_min.HasValue ? t_paper.used_count_min.Value : 0;
  421. int usedcount_max = t_paper.used_count_max.HasValue ? t_paper.used_count_max.Value : 0;
  422. int prority = t_paper.Prority.HasValue ? t_paper.Prority.Value : 0;
  423. var ques_setting = new JavaScriptSerializer().Deserialize<QuestionSettingJsonObj[]>(t_paper.question_setting);
  424. var questions = from q in ExamPaperDAL.questionRepository.Entities
  425. where q.difficulty_degree.Value >= diff_min && q.difficulty_degree.Value <= diff_max
  426. && q.used_count >= usedcount_min && q.used_count <= usedcount_max
  427. && q.wrong_percent >= wrong_rate
  428. && q.filled == false
  429. select q;
  430. var filterQuestions = questions.Where(q => 1 == 2);
  431. foreach (var item in libs)
  432. {
  433. int libid = Convert.ToInt32(item.Libaryid);
  434. int libnum = Convert.ToInt32(item.Number);
  435. filterQuestions = filterQuestions.Union(questions.Where(q => q.test_question_libary_id == libid).Take(libnum));
  436. }
  437. questions = filterQuestions;
  438. if (prority == 1)
  439. {
  440. questions = questions.OrderByDescending(q => q.used_count);
  441. }
  442. else if (prority == 2)
  443. {
  444. questions = questions.OrderByDescending(q => q.created_date);
  445. }
  446. else if (prority == 3)
  447. {
  448. questions = questions.OrderByDescending(q => q.created_date).OrderByDescending(q => q.used_count);
  449. }
  450. var question2 = from q in questions
  451. orderby q.test_question_Id
  452. select new
  453. {
  454. question_id = q.test_question_Id,
  455. typeName = q.test_base_question_type.Name,
  456. q.content,
  457. q.base_question_type_id,
  458. hasImg = q.question_file_id.HasValue,
  459. url = q.question_file_id.HasValue ? q.test_question_file.url : "",
  460. q.score,
  461. rightAnswer = q.answers,
  462. answers = q.test_question_provid_answer.Select(a => new Answers
  463. {
  464. content = a.answer_name,
  465. answer_id = a.provid_answer_id,
  466. hasImg = a.file_id.HasValue,
  467. url = a.file_id.HasValue ? a.test_question_file.url : "",
  468. })
  469. };
  470. var result = new List<object>();
  471. foreach (var t in ques_setting)
  472. {
  473. var questionInstance = question2.Where(q => q.base_question_type_id == t.QType).Take(t.PSum).ToArray();
  474. var rs = new
  475. {
  476. id = t.QType,
  477. tscore = t.QScore * t.PSum,
  478. qscore = t.QScore,
  479. name = questionInstance.Select(type => type.typeName).FirstOrDefault(),
  480. questions = questionInstance
  481. };
  482. if (rs.questions.Count() > 0)
  483. result.Add(rs);
  484. }
  485. return new JavaScriptSerializer().Serialize(result.ToArray());
  486. }
  487. return "";
  488. }
  489. }
  490. }