TestPaperServices.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using EMISOnline.Entities;
  6. using System.Xml.Linq;
  7. using EMISOnline.ViewModel.ExamView;
  8. using System.Web.Script.Serialization;
  9. using EMISOnline.DataLogic.ExamSetting;
  10. using EMISOnline.ViewModel;
  11. using System.Data;
  12. using System.Collections;
  13. namespace EMISOnline.CommonLogic.ExamServices
  14. {
  15. public class TestPaperServices : ITestPaperServices
  16. {
  17. public TestPaperDAL TestPaperDAL { get; set; }
  18. /// <summary>
  19. /// 创建动态子卷
  20. /// </summary>
  21. /// <param name="o_paperid"></param>
  22. /// <returns></returns>
  23. public decimal CreateSinglePaper(decimal testid, test_paper t_paper)
  24. {
  25. decimal paperid = 0;
  26. if (t_paper != null)
  27. {
  28. string libs = t_paper.question_libary_set_id;
  29. decimal diff_min = t_paper.difficulty_degree_minvalue.HasValue ? t_paper.difficulty_degree_minvalue.Value : 0m;
  30. decimal diff_max = t_paper.difficulty_degree_maxvalue.HasValue ? t_paper.difficulty_degree_maxvalue.Value : 0m;
  31. decimal wrong_rate = t_paper.error_percent.HasValue ? t_paper.error_percent.Value : 0m;
  32. int usedcount_min = t_paper.used_count_min.HasValue ? t_paper.used_count_min.Value : 0;
  33. int usedcount_max = t_paper.used_count_max.HasValue ? t_paper.used_count_max.Value : 0;
  34. int prority = t_paper.Prority.HasValue ? t_paper.Prority.Value : 0;
  35. var ques_setting = new JavaScriptSerializer().Deserialize<QuestionSettingJsonObj[]>(t_paper.question_setting);
  36. //新试卷
  37. test_paper newpaper = new test_paper();
  38. newpaper.created_by = t_paper.created_by;
  39. newpaper.created_date = t_paper.created_date;
  40. newpaper.difficulty_degree_maxvalue = t_paper.difficulty_degree_maxvalue;
  41. newpaper.difficulty_degree_minvalue = t_paper.difficulty_degree_minvalue;
  42. newpaper.distributing_option = t_paper.distributing_option;
  43. newpaper.error_percent = t_paper.error_percent;
  44. newpaper.is_autogenerate = true;
  45. newpaper.is_dynamic = false;
  46. newpaper.is_vaild = t_paper.is_vaild;
  47. newpaper.paper_score = t_paper.paper_score;
  48. newpaper.PaperName = string.Format("{0}", t_paper.PaperName);
  49. newpaper.Prority = t_paper.Prority;
  50. newpaper.question_libary_set_id = t_paper.question_libary_set_id;
  51. newpaper.question_setting = t_paper.question_setting;
  52. newpaper.question_update_date_max = t_paper.question_update_date_max;
  53. newpaper.question_update_date_min = t_paper.question_update_date_min;
  54. newpaper.test_paper_parent_id = t_paper.test_paper_id;
  55. newpaper.used_count = t_paper.used_count;
  56. newpaper.used_count_max = t_paper.used_count_max;
  57. newpaper.used_count_min = t_paper.used_count_min;
  58. List<XElement> list = new List<XElement>();
  59. foreach (var t in ques_setting)
  60. {
  61. XElement x = new XElement("QType");
  62. x.SetAttributeValue("QTypeID", t.QType);
  63. x.SetAttributeValue("PSum", t.PSum);
  64. x.SetAttributeValue("TSum", t.TSum);
  65. x.SetAttributeValue("QScore", t.QScore);
  66. list.Add(x);
  67. }
  68. XElement xml = new XElement("paper_set", list);
  69. newpaper.question_setting_xml = xml.ToString();
  70. TestPaperDAL.PaperRepository.UnitOfWork.Add(newpaper);
  71. TestPaperDAL.PaperRepository.UnitOfWork.Commit();
  72. paperid = newpaper.test_paper_id;
  73. ////筛选题目并存储关联数据
  74. TestPaperDAL.SetQuestion(paperid, diff_min, diff_max, usedcount_min, usedcount_max, wrong_rate);
  75. //context.SubmitChanges();
  76. }
  77. return paperid;
  78. }
  79. public bool UpdatePaperID(decimal test_id, string userid, decimal newPaperid)
  80. {
  81. return TestPaperDAL.UpdatePaperID(test_id, userid, newPaperid);
  82. }
  83. public void RootReadPaper(decimal testid, string userid)
  84. {
  85. TestPaperDAL.RootReadPaper(testid, userid);
  86. }
  87. public bool IsControllable(decimal id, out int test_method)
  88. {
  89. test_method = 0;
  90. var result = TestPaperDAL.onlinetestRepository.Entities.Where(t => t.onlinetest_id == id).Select(t => new { t.isControllable, t.test_method_set_id }).FirstOrDefault();
  91. if (result == null)
  92. return false;
  93. test_method = (int)(result.test_method_set_id.HasValue ? result.test_method_set_id.Value : 0);
  94. return result.isControllable ?? false;
  95. }
  96. public bool UpdateExamState(decimal test_id, string userid)
  97. {
  98. if (GetExamStatus(test_id, userid) == Examinee_ExamStatus.考试中)
  99. {
  100. var query = from test in TestPaperDAL.ExamResultRepository.Entities
  101. where test.onlinetest_id == test_id
  102. where test.user_id == userid
  103. select test;
  104. var testman = query.FirstOrDefault();
  105. var onlinetest = TestPaperDAL.onlinetestRepository.Entities.Where(q => q.onlinetest_id == test_id).FirstOrDefault();
  106. testman.relogin_count = (testman.relogin_count ?? 0) + 1;
  107. //if (testman.test_onlinetest.test_method_set_id.Value != 0 && testman.state != 2)
  108. //{
  109. testman.state = 1;
  110. //}
  111. testman.lastLoginTime = DateTime.Now;
  112. // 原设计仅在第一次进入是设定开始时间,重新进入则从这个时间计时
  113. //if (testman.relogin_count == 1)
  114. if (onlinetest.test_method_set_id == 0)
  115. {
  116. testman.test_begin_date = testman.lastLoginTime;
  117. testman.test_end_date = null;
  118. }
  119. TestPaperDAL.manRepository.UnitOfWork.Commit();
  120. return true;
  121. }
  122. return false;
  123. }
  124. public Examinee_ExamStatus GetExamStatus(decimal test_id, string userid)
  125. {
  126. var query = from man in TestPaperDAL.ExamResultRepository.Entities
  127. join test in TestPaperDAL.onlinetestRepository.Entities on man.onlinetest_id equals test.onlinetest_id
  128. where man.onlinetest_id == test_id
  129. where man.user_id == userid
  130. select new
  131. {
  132. status_id = test.status_id,
  133. beginTime = test.test_begin_date,
  134. endTime = test.test_end_date,
  135. loginCount = test.relogin_count,
  136. reloginCount = man.relogin_count,
  137. lastLoginTime = man.lastLoginTime,
  138. testMethod = test.test_method_set_id,
  139. limited = man.limited_minutes,
  140. manBeginTime = man.test_begin_date,
  141. state = man.state
  142. };
  143. var testman = query.FirstOrDefault();
  144. var now = DateTime.Now;
  145. if (testman == null)
  146. return Examinee_ExamStatus.不能参加该场考试;
  147. if (testman.status_id == 0 || testman.beginTime > now)
  148. return Examinee_ExamStatus.考试未开始;
  149. if (testman.testMethod.Value == 1)
  150. {
  151. // 现场考试
  152. if (now > testman.beginTime.Value.AddMinutes(testman.limited.Value))
  153. return Examinee_ExamStatus.考试已结束;
  154. // 现场考试需要检查是否已交卷,在线考试无需判断
  155. if (testman.state >= 2)
  156. return Examinee_ExamStatus.已交卷;
  157. }
  158. else
  159. {
  160. if (testman.endTime < now && !testman.lastLoginTime.HasValue)
  161. return Examinee_ExamStatus.考试已结束;
  162. //现在不用判断考试时长
  163. //if (testman.manBeginTime.HasValue && now > testman.manBeginTime.Value.AddMinutes(testman.limited.Value))
  164. // return Examinee_ExamStatus.考试已结束;
  165. }
  166. if (testman.reloginCount >= testman.loginCount)
  167. return Examinee_ExamStatus.登录次数已大于规定的次数;
  168. return Examinee_ExamStatus.考试中;
  169. }
  170. public void AddExchange(decimal test_id, int type, int receiveType, string receiver, int extendIntData)
  171. {
  172. TestPaperDAL.AddExchange(test_id, type, receiveType, receiver, extendIntData);
  173. }
  174. public ExamResult GetExam(string userid, decimal test_id)
  175. {
  176. return TestPaperDAL.ExamResultRepository.GetSingle(q => q.onlinetest_id == test_id && q.user_id == userid);
  177. }
  178. public double GetExamLastLength(decimal test_id, string userid)
  179. {
  180. var result = TestPaperDAL.ExamResultRepository.GetSingle(q => q.onlinetest_id == test_id && q.user_id == userid);
  181. return (result.test_begin_date.Value.AddMinutes(result.limited_minutes.Value) - DateTime.Now).TotalSeconds;
  182. }
  183. public object PaperBuilder(decimal test_id, decimal paper_id)
  184. {
  185. DataSet ds = TestPaperDAL.PaperBuilder(test_id, paper_id);
  186. var json = new JavaScriptSerializer();
  187. var questionTb = ds.Tables[0];
  188. var typeTb = ds.Tables[1];
  189. var examTb = ds.Tables[2];
  190. var waringTb = ds.Tables[3];
  191. var result = new StringBuilder();
  192. var examInfo = ParseExamInfo(examTb, questionTb.Rows.Count, json);
  193. var paper = ParsePaper(typeTb, questionTb, json);
  194. //result.AppendFormat("var examInfo = {0};", ParseExamInfo(examTb, questionTb.Rows.Count, json));
  195. //result.AppendFormat("var paper = [{0}];", ParsePaper(typeTb, questionTb, json));
  196. //result.Append(ParseWaring(waringTb, json));
  197. return new { examInfo = examInfo, paper = paper, allWarring = getWaring(waringTb.AsEnumerable(), 2, json), personWarring = getWaring(waringTb.AsEnumerable(), 1, json) }; ;
  198. }
  199. private object ParseExamInfo(DataTable examTb, int questionCount, JavaScriptSerializer json)
  200. {
  201. var examRow = examTb.Rows[0];
  202. var examInfo = new
  203. {
  204. name = examRow["test_name"].ToString(),
  205. test_id = (decimal)examRow["onlinetest_id"],
  206. begin = (DateTime)examRow["test_begin_date"],
  207. end = (DateTime)examRow["test_end_date"],
  208. limited = (int)examRow["limited_minutes"],
  209. breaking = (short)examRow["breaking_id"],
  210. display_type = examRow["display_type"].ToString(),
  211. //controllable = examRow["isControllable"] == DBNull.Value ? false : (bool)examRow["isControllable"],
  212. questionCount = questionCount
  213. };
  214. return examInfo;
  215. }
  216. private object ParsePaper(DataTable typeTb, DataTable paperTb, JavaScriptSerializer json)
  217. {
  218. var buff = new StringBuilder();
  219. var questionQuery = paperTb.AsEnumerable();
  220. ArrayList list = new ArrayList();
  221. JavaScriptSerializer serializer =new JavaScriptSerializer();
  222. foreach (DataRow typeRow in typeTb.Rows)
  223. {
  224. var typeid = (decimal)typeRow["base_question_type_id"];
  225. var name = typeRow["Name"] as string;
  226. var query = from questions in questionQuery
  227. where questions.Field<decimal>("base_question_type_id") == typeid
  228. select questions.Field<string>("question_json");
  229. var questionList = new List<QuestionDetailView>();
  230. query.ToList().ForEach(q =>
  231. {
  232. questionList.Add(serializer.Deserialize<QuestionDetailView>(q));
  233. });
  234. if (questionList.Count > 0)
  235. {
  236. if (buff.Length > 0)
  237. buff.Append(",");
  238. //buff.AppendFormat("{{id: {0},name: {1},questions:[{2}]}}",
  239. // typeid, json.Serialize(name), string.Join(",", questionList));
  240. list.Add(new { id = typeid, name = name, questions = questionList });
  241. }
  242. }
  243. return list;
  244. }
  245. private object ParseWaring(DataTable waringTb, JavaScriptSerializer json)
  246. {
  247. var buff = new StringBuilder();
  248. var query = waringTb.AsEnumerable();
  249. //buff.AppendFormat("var allWarring = {0};", getWaring(query, 2, json));
  250. //buff.AppendFormat("var personWarring = {0};", getWaring(query, 1, json));
  251. return new { allWarring = getWaring(query, 2, json), personWarring = getWaring(query, 1, json) };
  252. }
  253. private object getWaring(EnumerableRowCollection<DataRow> query, int type, JavaScriptSerializer json)
  254. {
  255. var result = query.Where(q => q.Field<int>("AlertType") == type).Select(q => new
  256. {
  257. key = q.Field<int>("ID"),
  258. value = q.Field<string>("Message")
  259. });
  260. return result.ToArray();
  261. }
  262. public test_onlinetest GetOnlineTest(decimal id)
  263. {
  264. return TestPaperDAL.onlinetestRepository.Entities.Single(q => q.onlinetest_id == id);
  265. }
  266. public Examinee_ExamStatus GetExamStartLength(decimal test_id, string userid, out double limit)
  267. {
  268. limit = 0;
  269. var query = from man in TestPaperDAL.ExamResultRepository.Entities
  270. join test in TestPaperDAL.onlinetestRepository.Entities on man.onlinetest_id equals test.onlinetest_id
  271. where man.onlinetest_id == test_id
  272. where man.user_id == userid
  273. select new
  274. {
  275. status_id = test.status_id,
  276. beginTime = test.test_begin_date,
  277. endTime = test.test_end_date,
  278. loginCount = test.relogin_count,
  279. reloginCount = man.relogin_count,
  280. lastLoginTime = man.lastLoginTime,
  281. testMethod = test.test_method_set_id,
  282. limited = man.limited_minutes,
  283. manBeginTime = man.test_begin_date,
  284. state = man.state
  285. };
  286. var testman = query.FirstOrDefault();
  287. var now = DateTime.Now;
  288. if (testman == null)
  289. return Examinee_ExamStatus.不能参加该场考试;
  290. if (testman.status_id == 0)
  291. return Examinee_ExamStatus.考试未开始;
  292. if (testman.testMethod.Value == 1)
  293. {
  294. // 现场考试
  295. if (now > testman.beginTime.Value.AddMinutes(testman.limited.Value))
  296. return Examinee_ExamStatus.考试已结束;
  297. if (testman.state >= 2)
  298. return Examinee_ExamStatus.已交卷;
  299. }
  300. else
  301. {
  302. if (testman.endTime < now && !testman.lastLoginTime.HasValue)
  303. return Examinee_ExamStatus.考试已结束;
  304. //在线考试不受考试时间限制
  305. //if (testman.manBeginTime.HasValue && now > testman.manBeginTime.Value.AddMinutes(testman.limited.Value))
  306. // return Examinee_ExamStatus.考试已结束;
  307. }
  308. if (testman.reloginCount >= testman.loginCount)
  309. return Examinee_ExamStatus.登录次数已大于规定的次数;
  310. limit = (testman.beginTime.Value - DateTime.Now).TotalSeconds;
  311. return Examinee_ExamStatus.考试中;
  312. }
  313. public string ListExchange(decimal test_id, int receiveType, string receiver, DateTime? sendTime)
  314. {
  315. var query = TestPaperDAL.ListExchange(test_id, receiveType, receiver, sendTime);
  316. JavaScriptSerializer serial = new JavaScriptSerializer();
  317. return serial.Serialize(query);
  318. }
  319. }
  320. }