QuestionServices.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using EMISOnline.DataLogic.ExamSetting;
  6. using System.Web.Script.Serialization;
  7. using System.Collections;
  8. using EMISOnline.ViewModel.ExamView;
  9. using Bowin.Web.Controls.Mvc;
  10. using System.Data;
  11. namespace EMISOnline.CommonLogic.ExamServices
  12. {
  13. public class QuestionServices : IQuestionServices
  14. {
  15. public QuestionDAL QuestionDAL { get; set; }
  16. public Entities.test_base_question_type[] ListAllQuestionType()
  17. {
  18. return QuestionDAL.ListAllQuestionType();
  19. }
  20. public decimal[] GeneratePractice(string[] libaryids, List<string> typeID, int? num)
  21. {
  22. return QuestionDAL.GeneratePractice(libaryids, typeID, num);
  23. }
  24. public string GetQuestion(decimal question_id)
  25. {
  26. var question = QuestionDAL.GetQuestion(new decimal[] { question_id });
  27. var js = new JavaScriptSerializer();
  28. return js.Serialize(question);
  29. }
  30. public string ShowPaperQuestion(decimal[] QeustionSet, Dictionary<string, string> Answers)
  31. {
  32. var questions = QuestionDAL.GetPaperQuestion(Answers.Keys.Select(q => decimal.Parse(q)).ToArray()).ToList();
  33. var questionTypes = QuestionDAL.ListAllQuestionType();
  34. var result = new ArrayList();
  35. questions.ForEach(q =>
  36. {
  37. q.userAnswer = Answers[q.question_id.ToString()];
  38. ReadQuestion(q, q.userAnswer, (int)q.question_typeid);
  39. });
  40. foreach (var type in questionTypes)
  41. {
  42. if (questions.Any(q => q.question_typeid == type.base_question_type_id))
  43. result.Add(new { id = type.base_question_type_id, name = type.Name, questions = questions });
  44. }
  45. var data = new
  46. {
  47. score = questions.Sum(q => q.score),//试题总分
  48. studentscore = questions.Sum(q => q.userscore), //你的得分
  49. questionnumber = QeustionSet.Count(),//试题总数
  50. rightnumber = questions.Count(q => q.userscore > 0),//答对题数
  51. errornumber = questions.Count(q => q.userscore == 0 && !string.IsNullOrEmpty(q.userAnswer)),//答错题数
  52. unquestion = QeustionSet.Count() - Answers.Count,//未答题数
  53. };
  54. var js = new JavaScriptSerializer();
  55. return js.Serialize(new { data = data, result = result });
  56. }
  57. private void ReadQuestion(PaperQuestionView question, string useranser, int questionType)
  58. {
  59. var isRight = false;
  60. switch (questionType)
  61. {
  62. case 1:
  63. isRight = question.rightAnswer == useranser.Trim();
  64. break;
  65. case 2:
  66. var provid = question.answers.Select(q => true).ToArray();
  67. isRight = string.Join("|", provid) == useranser.Trim();
  68. break;
  69. case 3:
  70. isRight = question.rightAnswer == OrderAnswer(useranser);
  71. break;
  72. case 4:
  73. isRight = question.rightAnswer == useranser.Trim();
  74. break;
  75. default:
  76. break;
  77. }
  78. if (isRight)
  79. question.userscore = question.score;
  80. }
  81. private string OrderAnswer(string answer)
  82. {
  83. var answerArr = answer.Split('|');
  84. return string.Join(",", answerArr.OrderBy(q => q).ToArray());
  85. }
  86. public List<TreeItem> ListQuesLibTreeStu()
  87. {
  88. List<TreeItem> rtn = new List<TreeItem>();
  89. var TreeDT = QuestionDAL.ListQuesLibTreeStu();
  90. var query = TreeDT.AsEnumerable().Where(e => e.Field<decimal>("parent_id") == 1);
  91. foreach (var node in query.ToList())
  92. {
  93. TreeItem item = new TreeItem();
  94. item.id = node.Field<decimal>("test_question_libary_id").ToString();
  95. item.text = node.Field<string>("name").ToString(); ;
  96. item.attributes = "";
  97. item.children = CreateChildTree(TreeDT, node.Field<decimal>("test_question_libary_id"));
  98. rtn.Add(item);
  99. }
  100. return rtn;
  101. }
  102. public List<TreeItem> CreateChildTree(DataTable TreeDT, decimal parent_id)
  103. {
  104. List<TreeItem> rtn = new List<TreeItem>();
  105. var query = TreeDT.AsEnumerable().Where(e => e.Field<decimal>("parent_id") == parent_id);
  106. foreach (var node in query.ToList())
  107. {
  108. TreeItem item = new TreeItem();
  109. item.id = node.Field<decimal>("test_question_libary_id").ToString();
  110. item.text = node.Field<string>("name").ToString(); ;
  111. item.attributes = "";
  112. item.children = CreateChildTree(TreeDT, node.Field<decimal>("test_question_libary_id"));
  113. rtn.Add(item);
  114. }
  115. return rtn;
  116. }
  117. }
  118. }