ExamSettingServices.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 EMISOnline.DataLogic.Educational;
  20. namespace EMISOnline.CommonLogic.ExamServices
  21. {
  22. public class ExamSettingServices : IExamSettingServices
  23. {
  24. public ExamSettingDAL ExamSettingDAL { get; set; }
  25. public TestPaperDAL PaperDAL { get; set; }
  26. public UserDAL UserDAL { get; set; }
  27. public EducationMissionClassDAL EducationMissionClassDAL { get; set; }
  28. public IGridResultSet<OnlineTestView> GetOnlinetestList(string testname, int pageIndex, int pageSize)
  29. {
  30. Expression<Func<test_onlinetest, bool>> exp = (x => true);
  31. if (!string.IsNullOrEmpty(testname))
  32. exp = exp.And(q => q.test_name.Contains(testname));
  33. return ExamSettingDAL.GetOnlinetestList(exp).Select(q => new OnlineTestView()
  34. {
  35. test_name = q.test_name,
  36. onlinetest_id = q.onlinetest_id,
  37. test_method_set = q.test_method_set_id == 0 ? "在线考试" : "现场考试",
  38. test_begin_date = q.test_begin_date,
  39. limited_minutes = q.limited_minutes,
  40. status = q.status_id == (int)OnlineTestStatus.公开成绩 ? "成绩已发布" : "成绩未发布"
  41. }).OrderBy(q => q.onlinetest_id).ToGridResultSet(pageIndex, pageSize);
  42. }
  43. public IGridResultSet<OnlineTestView> GetStudentOnlinetestList(string testname, DateTime? beginTime, DateTime? endTime, string UserID, int pageIndex, int pageSize)
  44. {
  45. Expression<Func<test_onlinetest, bool>> exp = (x => true);
  46. if (!string.IsNullOrEmpty(testname))
  47. exp = exp.And(q => q.test_name.Contains(testname));
  48. if (beginTime.HasValue)
  49. exp = exp.And(q => q.test_begin_date >= beginTime);
  50. if (endTime.HasValue)
  51. exp = exp.And(q => q.test_end_date <= endTime);
  52. //根据UserID,查询用户对应的年级专业
  53. var Executable = EducationMissionClassDAL.GetEducationMissionClassByUserID(Guid.Parse(UserID)).Select(q => q.EducationMissionClassID).ToArray();
  54. exp = exp.And(q => Executable.Contains(q.EducationMissionClassID.Value));
  55. return ExamSettingDAL.GetStudentOnlinetestList(exp, UserID).Select(q => new OnlineTestView()
  56. {
  57. test_name = q.test_name,
  58. onlinetest_id = q.onlinetest_id,
  59. test_method_set = q.test_method_set_id == 0 ? "在线考试" : "现场考试",
  60. test_begin_date = q.test_begin_date,
  61. limited_minutes = q.limited_minutes,
  62. status = q.status_id == (int)OnlineTestStatus.公开成绩 ? "成绩已发布" : "成绩未发布"
  63. }).OrderBy(q => q.onlinetest_id).ToGridResultSet(pageIndex, pageSize);
  64. }
  65. public OnlineTestView GetOnlineTestByID(decimal test_id)
  66. {
  67. OnlineTestView test = new OnlineTestView();
  68. var view = ExamSettingDAL.OnlinetestRepository.GetSingle(q => q.onlinetest_id == test_id);
  69. test.onlinetest_id = view.onlinetest_id;
  70. test.breaking_id = view.breaking_id;
  71. test.breaking_id = view.breaking_id;
  72. test.display_type = view.display_type;
  73. test.ExamerState = view.ExamerState;
  74. test.ExamID = view.ExamID;
  75. test.ISautoBegin = view.ISautoBegin;
  76. test.isControllable = view.isControllable;
  77. test.isOpenAnswer = view.isOpenAnswer;
  78. test.isPublishScore = view.isPublishScore;
  79. test.isReadPaper = view.isReadPaper;
  80. test.limited_minutes = view.limited_minutes;
  81. test.ModityContent = view.ModityContent;
  82. test.notice_datetime = view.notice_datetime;
  83. test.notice_method_id = view.notice_method_id;
  84. test.PassRate = view.PassRate;
  85. test.rand_test_count = view.rand_test_count;
  86. test.rand_test_point_setid = view.rand_test_point_setid;
  87. test.rand_test_point_type = view.rand_test_point_type;
  88. test.rand_Test_time = view.rand_Test_time;
  89. test.relogin_count = view.relogin_count;
  90. test.status_id = view.status_id;
  91. test.SubCode = view.SubCode;
  92. test.subjectiveQAutoRead = view.subjectiveQAutoRead;
  93. test.test_address = view.test_address;
  94. test.test_begin_date = view.test_begin_date;
  95. test.test_code = view.test_code;
  96. test.test_end_date = view.test_end_date;
  97. test.test_method_set_id = view.test_method_set_id;
  98. test.test_name = view.test_name;
  99. test.test_paper_id = view.test_paper_id;
  100. test.time_of_filling = view.time_of_filling;
  101. test.version = view.version;
  102. if (view.EducationMissionClassID.HasValue)
  103. {
  104. var executable = EducationMissionClassDAL.EducationMissionClassRepository.GetSingle(x => view.EducationMissionClassID == x.EducationMissionClassID);
  105. test.EducationMissionClassID = view.EducationMissionClassID.Value;
  106. test.ExecutablePlanData.Add(test.EducationMissionClassID.ToString(), executable.Name);
  107. }
  108. return test;
  109. }
  110. public bool ExamSettingSave(OnlineTestView view, string LoginID)
  111. {
  112. test_onlinetest test = new test_onlinetest();
  113. if (view.onlinetest_id > 0)
  114. {
  115. test = ExamSettingDAL.OnlinetestRepository.GetSingle(q => q.onlinetest_id == view.onlinetest_id);
  116. test.breaking_id = view.breaking_id;
  117. test.breaking_id = view.breaking_id;
  118. test.display_type = view.display_type;
  119. test.ExamerState = view.ExamerState;
  120. test.ExamID = view.ExamID;
  121. test.ISautoBegin = view.ISautoBegin;
  122. test.isControllable = view.isControllable;
  123. test.isOpenAnswer = view.isOpenAnswer;
  124. test.isPublishScore = view.isPublishScore;
  125. test.isReadPaper = view.isReadPaper;
  126. test.limited_minutes = view.limited_minutes;
  127. test.ModityContent = view.ModityContent;
  128. test.notice_datetime = view.notice_datetime;
  129. test.notice_method_id = view.notice_method_id;
  130. test.PassRate = view.PassRate;
  131. test.rand_test_count = view.rand_test_count;
  132. test.rand_test_point_setid = view.rand_test_point_setid;
  133. test.rand_test_point_type = view.rand_test_point_type;
  134. test.rand_Test_time = view.rand_Test_time;
  135. test.relogin_count = view.relogin_count;
  136. test.status_id = view.status_id;
  137. test.SubCode = view.SubCode;
  138. test.subjectiveQAutoRead = view.subjectiveQAutoRead;
  139. test.test_address = view.test_address;
  140. test.test_begin_date = view.test_begin_date;
  141. test.test_code = view.test_code;
  142. test.test_end_date = view.test_end_date;
  143. test.test_method_set_id = view.test_method_set_id;
  144. test.test_name = view.test_name;
  145. test.test_paper_id = view.test_paper_id;
  146. test.time_of_filling = view.time_of_filling;
  147. test.version = view.version;
  148. test.EducationMissionClassID = view.EducationMissionClassID;
  149. }
  150. else
  151. {
  152. test.breaking_id = view.breaking_id;
  153. test.created_by = LoginID;
  154. test.created_date = DateTime.Now;
  155. test.display_type = view.display_type;
  156. test.ExamerState = view.ExamerState;
  157. test.ExamID = Guid.NewGuid();
  158. test.ISautoBegin = view.ISautoBegin;
  159. test.isControllable = view.isControllable;
  160. test.isOpenAnswer = view.isOpenAnswer;
  161. test.isPublishScore = view.isPublishScore;
  162. test.isReadPaper = view.isReadPaper;
  163. test.limited_minutes = view.limited_minutes;
  164. test.ModityContent = view.ModityContent;
  165. test.notice_datetime = view.notice_datetime;
  166. test.notice_method_id = view.notice_method_id;
  167. test.PassRate = view.PassRate;
  168. test.rand_test_count = view.rand_test_count;
  169. test.rand_test_point_setid = view.rand_test_point_setid;
  170. test.rand_test_point_type = view.rand_test_point_type;
  171. test.rand_Test_time = view.rand_Test_time;
  172. test.relogin_count = view.relogin_count;
  173. test.status_id = view.status_id;
  174. test.SubCode = view.SubCode;
  175. test.subjectiveQAutoRead = view.subjectiveQAutoRead;
  176. test.test_address = view.test_address;
  177. test.test_begin_date = view.test_begin_date;
  178. test.test_code = view.test_code;
  179. test.test_end_date = view.test_end_date;
  180. test.test_method_set_id = view.test_method_set_id;
  181. test.test_name = view.test_name;
  182. test.test_paper_id = view.test_paper_id;
  183. test.time_of_filling = view.time_of_filling;
  184. test.version = view.version;
  185. test.EducationMissionClassID = view.EducationMissionClassID;
  186. ExamSettingDAL.OnlinetestRepository.UnitOfWork.Add(test);
  187. }
  188. ExamSettingDAL.OnlinetestRepository.UnitOfWork.Commit();
  189. //WinUpdateExam(test);
  190. return true;
  191. }
  192. public object GetPaperList(string papername, int pageIndex, int pageSize)
  193. {
  194. Expression<Func<test_paper, bool>> exp = (x => true);
  195. if (!string.IsNullOrEmpty(papername))
  196. exp = exp.And(q => q.PaperName.Contains(papername));
  197. return ExamSettingDAL.GetPaperList(exp).ToList().Select(paper => new
  198. {
  199. paper.PaperName,
  200. paper.paper_score,
  201. Creator = paper.created_by,
  202. is_dynamic = paper.is_dynamic ? "是" : "否",
  203. created_date = paper.created_date.Value.ToString("yyyy-MM-dd"),
  204. paper.test_paper_id
  205. }).OrderBy(q => q.test_paper_id).AsQueryable().ToGridResultSet(pageIndex, pageSize);
  206. }
  207. /// <summary>
  208. /// 生成考生信息及试卷信息
  209. /// </summary>
  210. /// <param name="setting"></param>
  211. /// <returns></returns>
  212. public bool WinUpdateExam(test_onlinetest setting)
  213. {
  214. bool bool_rev = false;
  215. decimal testid = setting.onlinetest_id;
  216. bool isChangePaper = setting.ExamerState.HasValue ? (setting.ExamerState.Equals(2) ? true : false) : false;//是否修改试卷
  217. InsertExamResult(testid);
  218. if (isChangePaper)//如果有更换考卷
  219. {
  220. //删除考试结果
  221. ExamSettingDAL.ExamineeAnswerRepository.UnitOfWork.Delete<ExamineeAnswer>(q => q.onlinetest_id == testid);
  222. }
  223. setting.ExamerState = 1;
  224. ExamSettingDAL.OnlinetestRepository.UnitOfWork.Commit();
  225. bool_rev = true;
  226. return bool_rev;
  227. }
  228. public void InsertExamResult(decimal testid)
  229. {
  230. var TestMans = ExamSettingDAL.GetExamUsers(testid);
  231. var TestOnlie = ExamSettingDAL.OnlinetestRepository.GetSingle(q => q.onlinetest_id == testid);
  232. List<ExamResult> Mans = new List<ExamResult>();
  233. foreach (var Man in TestMans)
  234. {
  235. ExamResult result = new ExamResult()
  236. {
  237. user_id = Man.UserID.ToString(),
  238. test_name = TestOnlie.test_name,
  239. test_paper_id = TestOnlie.test_paper_id.Value,
  240. test_begin_date = TestOnlie.test_begin_date,
  241. test_end_date = TestOnlie.test_end_date,
  242. limited_minutes = TestOnlie.limited_minutes,
  243. test_address = TestOnlie.test_address,
  244. onlinetest_id = TestOnlie.onlinetest_id,
  245. OrgId = Man.ClassmajorID.ToString(),
  246. username = Man.StudentCardNo
  247. };
  248. Mans.Add(result);
  249. }
  250. ExamSettingDAL.ExamResultRepository.UnitOfWork.AddRange(Mans);
  251. }
  252. public ExamResult GetExam(string userid, decimal test_id)
  253. {
  254. var exam = ExamSettingDAL.GetExam(userid, test_id);
  255. //首次进入考试,如果该考生拥有该场考试,则把该考生信息插入到考生信息表
  256. if (exam == null)
  257. {
  258. Expression<Func<test_onlinetest, bool>> exp = (x => x.onlinetest_id == test_id);
  259. //根据UserID,查询用户对应的年级专业
  260. var Executable = EducationMissionClassDAL.GetEducationMissionClassByUserID(Guid.Parse(userid)).Select(q => q.EducationMissionClassID).ToArray();
  261. exp = exp.And(q => Executable.Contains(q.EducationMissionClassID.Value));
  262. var test = ExamSettingDAL.GetStudentOnlinetestList(exp, userid).FirstOrDefault();
  263. if (test != null)
  264. {
  265. exam = new ExamResult()
  266. {
  267. is_auto_read = test.isReadPaper,
  268. test_name = test.test_name,
  269. test_paper_id = test.test_paper_id,
  270. test_begin_date = test.test_begin_date,
  271. limited_minutes = test.limited_minutes,
  272. test_end_date = test.test_end_date,
  273. onlinetest_id = test.onlinetest_id,
  274. user_id = userid,
  275. filled = false,
  276. };
  277. ExamSettingDAL.ExamResultRepository.UnitOfWork.Add(exam);
  278. ExamSettingDAL.ExamResultRepository.UnitOfWork.Commit();
  279. exam = ExamSettingDAL.GetExam(userid, test_id);
  280. }
  281. }
  282. return exam;
  283. }
  284. public Examinee_ExamStatus GetExamStartLength(decimal test_id, string userid, out double limit)
  285. {
  286. limit = 0;
  287. var testman = ExamSettingDAL.GetExam(userid, test_id);
  288. var now = DateTime.Now;
  289. if (testman == null)
  290. return Examinee_ExamStatus.不能参加该场考试;
  291. if (testman.test_onlinetest.status_id == 0)
  292. return Examinee_ExamStatus.考试未开始;
  293. if (testman.test_onlinetest.test_method_set_id.Value == 1)
  294. {
  295. // 现场考试
  296. if (now > testman.test_begin_date.Value.AddMinutes(testman.limited_minutes.Value))
  297. return Examinee_ExamStatus.考试已结束;
  298. if (testman.state >= 2)
  299. return Examinee_ExamStatus.已交卷;
  300. }
  301. else
  302. {
  303. if (testman.test_end_date.HasValue && testman.test_end_date < now && !testman.lastLoginTime.HasValue)
  304. return Examinee_ExamStatus.考试已结束;
  305. // 在线考试不受考试时间限制
  306. if (testman.test_end_date.HasValue && testman.test_begin_date.HasValue && now > testman.test_end_date.Value.AddMinutes(testman.limited_minutes.Value))
  307. return Examinee_ExamStatus.考试已结束;
  308. }
  309. //if (testman.reloginCount >= testman.test_onlinetest.relogin_count)
  310. // return Examinee_ExamStatus.登录次数已大于规定的次数;
  311. limit = (testman.test_begin_date.Value - DateTime.Now).TotalSeconds;
  312. return Examinee_ExamStatus.考试中;
  313. }
  314. /// <summary>
  315. /// 获取考试对应的试卷
  316. /// </summary>
  317. /// <param name="testid"></param>
  318. /// <returns></returns>
  319. public test_paper getPaper(decimal testid)
  320. {
  321. var q = ExamSettingDAL.OnlinetestRepository.UnitOfWork.Set<test_onlinetest>().Include("test_paper").Single(qx => qx.onlinetest_id == testid).test_paper;
  322. return q;
  323. }
  324. public bool TakePaper(decimal test_id, string userid, string answer)
  325. {
  326. ExamSettingDAL.ExamineeAnswerRepository.UnitOfWork.Delete<ExamineeAnswer>(e => e.onlinetest_id == test_id && e.user_id == userid);
  327. string errText = string.Empty;
  328. if (!string.IsNullOrEmpty(answer))
  329. {
  330. var json = new JavaScriptSerializer();
  331. var answers = json.Deserialize<answerEntry[]>(answer);
  332. List<AnswerXml> xmlValues = new List<AnswerXml>();
  333. foreach (var a in answers)
  334. {
  335. decimal question_id = 0;
  336. if (decimal.TryParse(a.key, out question_id))
  337. {
  338. xmlValues.Add(new AnswerXml()
  339. {
  340. act_score = 0,
  341. answers = a.value,
  342. memo = "",
  343. src_score = 0,
  344. test_question_Id = question_id
  345. });
  346. }
  347. }
  348. try
  349. {
  350. errText += "0-----\r\n";
  351. XmlSerializer serializer = new XmlSerializer(typeof(List<AnswerXml>));
  352. MemoryStream mstream = new MemoryStream();
  353. serializer.Serialize(mstream, xmlValues);
  354. mstream.Close();
  355. errText += "1-----\r\n";
  356. errText += Encoding.Default.GetString(mstream.ToArray());
  357. var examAnswer = new ExamineeAnswer()
  358. {
  359. ID = Guid.NewGuid(),
  360. onlinetest_id = test_id,
  361. user_id = userid,
  362. resultXml = XElement.Parse(Encoding.UTF8.GetString(mstream.ToArray())).ToString()
  363. };
  364. errText += "2-----\r\n";
  365. errText = examAnswer.resultXml.ToString();
  366. ExamSettingDAL.ExamineeAnswerRepository.UnitOfWork.Add(examAnswer);
  367. }
  368. catch (Exception ex)
  369. {
  370. //System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.Current.Request.PhysicalApplicationPath + "\\lingx.txt", true, System.Text.Encoding.UTF8);
  371. //sw.WriteLine(DateTime.Now.ToString());
  372. //sw.WriteLine(errText); ;
  373. //sw.Close();
  374. }
  375. }
  376. var test_man = PaperDAL.ExamResultRepository.GetSingle(m => m.user_id == userid && m.onlinetest_id == test_id);
  377. if (test_man != null)
  378. {
  379. test_man.state = 2;
  380. test_man.test_end_date = DateTime.Now;
  381. }
  382. PaperDAL.ExamResultRepository.UnitOfWork.Commit();
  383. return true;
  384. }
  385. }
  386. public class answerEntry
  387. {
  388. public string key { get; set; }
  389. public string value { get; set; }
  390. }
  391. }