CourseworkServices.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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.CourseworkMgr;
  20. using System.Data;
  21. using EMISOnline.CommonLogic.ExamServices;
  22. using EMISOnline.ViewModel.Coursework;
  23. using System.Collections;
  24. namespace EMISOnline.CommonLogic.CourseworkServices
  25. {
  26. public class CourseworkServices : ICourseworkServices
  27. {
  28. public CourseworkDAL CourseworkDAL { get; set; }
  29. public QuestionDAL QuestionDAL { get; set; }
  30. public IGridResultSet<CourseworkAddView> GetCourseworkList(string testname, int pageIndex, int pageSize)
  31. {
  32. Expression<Func<Coursework, bool>> exp = (x => true);
  33. if (!string.IsNullOrEmpty(testname))
  34. exp = exp.And(q => q.WorkName.Contains(testname));
  35. return CourseworkDAL.GetCourseworkList(exp).Select(q => q).OrderByDescending(q => q.test_begin_date).ToGridResultSet(pageIndex, pageSize);
  36. }
  37. public bool ExamSettingSave(CourseworkAddView view, string LoginID)
  38. {
  39. Coursework test = new Coursework();
  40. if (view.id > 0)
  41. {
  42. test = CourseworkDAL.CourseworkRepository.GetSingle(q => q.ID == view.id);
  43. test.created_by = LoginID;
  44. test.created_date = DateTime.Now.ToString("yyyy-MM-dd");
  45. test.EndTime = view.test_end_date;
  46. test.BeginTime = view.test_begin_date;
  47. test.PaperID = view.test_paper_id;
  48. test.WorkName = view.WorkName;
  49. test.ModityContent = view.ModityContent;
  50. test.EducationMissionClassID = view.EducationMissionClassID;
  51. }
  52. else
  53. {
  54. test.created_by = LoginID;
  55. test.created_date = DateTime.Now.ToString("yyyy-MM-dd");
  56. test.EndTime = view.test_end_date;
  57. test.BeginTime = view.test_begin_date;
  58. test.PaperID = view.test_paper_id;
  59. test.WorkName = view.WorkName;
  60. test.ModityContent = view.ModityContent;
  61. test.EducationMissionClassID = view.EducationMissionClassID;
  62. CourseworkDAL.CourseworkRepository.UnitOfWork.Add(test);
  63. }
  64. CourseworkDAL.CourseworkRepository.UnitOfWork.Commit();
  65. return true;
  66. }
  67. public CourseworkAddView getCourseWork(decimal workid)
  68. {
  69. var q = CourseworkDAL.CourseworkRepository.UnitOfWork.Set<Coursework>().Include(x => x.test_paper).Single(qx => qx.ID == workid);
  70. CourseworkAddView view = new CourseworkAddView();
  71. view.id = q.ID;
  72. view.test_begin_date = q.BeginTime;
  73. view.test_end_date = q.EndTime;
  74. view.test_paper_id = q.PaperID;
  75. view.WorkName = q.WorkName;
  76. view.test_paper_name = q.test_paper.PaperName;
  77. view.PaperData.Add(q.PaperID.ToString(), q.test_paper.PaperName);
  78. view.ModityContent = q.ModityContent;
  79. return view;
  80. }
  81. /// <summary>
  82. /// 获取考试对应的试卷
  83. /// </summary>
  84. /// <param name="testid"></param>
  85. /// <returns></returns>
  86. public test_paper getPaper(decimal testid)
  87. {
  88. var q = CourseworkDAL.CourseworkRepository.UnitOfWork.Set<Coursework>().Include(x => x.test_paper).Single(qx => qx.ID == testid).test_paper;
  89. return q;
  90. }
  91. public bool UpdatePaperID(decimal test_id, string userid, decimal newPaperid)
  92. {
  93. return CourseworkDAL.UpdatePaperID(test_id, userid, newPaperid);
  94. }
  95. public List<decimal> PaperBuilder(decimal workid, decimal paper_id)
  96. {
  97. DataSet ds = CourseworkDAL.PaperBuilder(workid, paper_id);
  98. var json = new JavaScriptSerializer();
  99. var questionTb = ds.Tables[0];
  100. var typeTb = ds.Tables[1];
  101. var result = new StringBuilder();
  102. var questionset = ParsePaper(typeTb, questionTb, json);
  103. return questionset;
  104. }
  105. private List<decimal> ParsePaper(DataTable typeTb, DataTable paperTb, JavaScriptSerializer json)
  106. {
  107. var questionQuery = paperTb.AsEnumerable();
  108. JavaScriptSerializer serializer = new JavaScriptSerializer();
  109. var questionset = new List<decimal>();
  110. foreach (DataRow typeRow in typeTb.Rows)
  111. {
  112. var typeid = (decimal)typeRow["base_question_type_id"];
  113. var name = typeRow["Name"] as string;
  114. var query = from questions in questionQuery
  115. where questions.Field<decimal>("base_question_type_id") == typeid
  116. select questions.Field<string>("question_json");
  117. var questionList = new List<QuestionDetailView>();
  118. query.ToList().ForEach(q =>
  119. {
  120. questionList.Add(serializer.Deserialize<QuestionDetailView>(q));
  121. });
  122. questionset.AddRange(questionList.Select(q => q.question_id).ToList());
  123. }
  124. return questionset;
  125. }
  126. public bool TakePaper(decimal test_id, string userid, Dictionary<string, string> answers)
  127. {
  128. CourseworkDAL.AnswerRepository.UnitOfWork.Delete<CourseworkAnswer>(e => e.workid == test_id && e.user_id == userid);
  129. string errText = string.Empty;
  130. if (answers.Count > 0)
  131. {
  132. var json = new JavaScriptSerializer();
  133. List<AnswerXml> xmlValues = new List<AnswerXml>();
  134. foreach (var a in answers)
  135. {
  136. decimal question_id = 0;
  137. if (decimal.TryParse(a.Key, out question_id))
  138. {
  139. xmlValues.Add(new AnswerXml()
  140. {
  141. act_score = 0,
  142. answers = a.Value,
  143. memo = "",
  144. src_score = 0,
  145. test_question_Id = question_id
  146. });
  147. }
  148. }
  149. try
  150. {
  151. errText += "0-----\r\n";
  152. XmlSerializer serializer = new XmlSerializer(typeof(List<AnswerXml>));
  153. MemoryStream mstream = new MemoryStream();
  154. serializer.Serialize(mstream, xmlValues);
  155. mstream.Close();
  156. errText += "1-----\r\n";
  157. errText += Encoding.Default.GetString(mstream.ToArray());
  158. var examAnswer = new CourseworkAnswer()
  159. {
  160. workid = test_id,
  161. user_id = userid,
  162. resultXml = XElement.Parse(Encoding.UTF8.GetString(mstream.ToArray())).ToString()
  163. };
  164. errText += "2-----\r\n";
  165. errText = examAnswer.resultXml.ToString();
  166. CourseworkDAL.AnswerRepository.UnitOfWork.Add(examAnswer);
  167. }
  168. catch (Exception ex)
  169. {
  170. //System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.Current.Request.PhysicalApplicationPath + "\\lingx.txt", true, System.Text.Encoding.UTF8);
  171. //sw.WriteLine(DateTime.Now.ToString());
  172. //sw.WriteLine(errText); ;
  173. //sw.Close();
  174. }
  175. }
  176. var test_man = CourseworkDAL.ResultRepository.GetSingle(m => m.user_id == userid && m.workid == test_id);
  177. if (test_man != null)
  178. {
  179. test_man.state = 2;
  180. test_man.test_end_date = DateTime.Now;
  181. }
  182. CourseworkDAL.ResultRepository.UnitOfWork.Commit();
  183. return true;
  184. }
  185. public IGridResultSet<CourseworkReviewView> GetReviewList(string testname, decimal workid, int pageIndex, int pageSize)
  186. {
  187. Expression<Func<CourseworkResult, bool>> exp = (x => x.workid == workid);
  188. if (!string.IsNullOrEmpty(testname))
  189. exp = exp.And(q => q.Coursework.WorkName.Contains(testname));
  190. return CourseworkDAL.GetReviewList(exp).OrderBy(q => q.ResultID).ToGridResultSet(pageIndex, pageSize);
  191. }
  192. public CourseworkResult GetCourseWorkResult(decimal ResultID)
  193. {
  194. return CourseworkDAL.ResultRepository.GetSingle(q => q.ID == ResultID);
  195. }
  196. public string ShowPaperQuestion(decimal workid, decimal paper_id)
  197. {
  198. var QeustionSet = PaperBuilder(workid, paper_id);
  199. var questions = QuestionDAL.GetPaperQuestion(QeustionSet.ToArray()).ToList();
  200. var questionTypes = QuestionDAL.ListAllQuestionType();
  201. var Answers = CourseworkDAL.ExamineeAnswers(workid).ToDictionary(q => q.test_question_Id, q => q.answers);
  202. var result = new ArrayList();
  203. questions.ForEach(q =>
  204. {
  205. q.userAnswer = Answers.ContainsKey(q.question_id) ? Answers[q.question_id] : "";
  206. //ReadQuestion(q, q.userAnswer, (int)q.question_typeid);
  207. });
  208. foreach (var type in questionTypes)
  209. {
  210. if (questions.Any(q => q.question_typeid == type.base_question_type_id))
  211. result.Add(new { id = type.base_question_type_id, name = type.Name, questions = questions });
  212. }
  213. var data = new
  214. {
  215. score = questions.Sum(q => q.score),//试题总分
  216. studentscore = questions.Sum(q => q.userscore), //你的得分
  217. questionnumber = QeustionSet.Count(),//试题总数
  218. rightnumber = questions.Count(q => q.userscore > 0),//答对题数
  219. errornumber = questions.Count(q => q.userscore == 0 && !string.IsNullOrEmpty(q.userAnswer)),//答错题数
  220. unquestion = QeustionSet.Count() - QeustionSet.Count,//未答题数
  221. };
  222. var js = new JavaScriptSerializer();
  223. return js.Serialize(new { data = data, result = result });
  224. }
  225. }
  226. }