PracticeController.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using EMISOnline.CommonLogic.ExamServices;
  7. using EMISOnline.ViewModel;
  8. using jEMISOnline.Utility;
  9. using System.Web.Script.Serialization;
  10. namespace EMISOnline.Web.Controllers.StudentView
  11. {
  12. [Authorization]
  13. public class PracticeController : Controller
  14. {
  15. public IQuestionServices IQuestionServices { get; set; }
  16. //
  17. // GET: /Practice/
  18. public ActionResult PracticeSetting()
  19. {
  20. var cookie = Request.Cookies["practice_record"];
  21. ViewBag.ShowRecordButton = false;
  22. if (cookie != null && !string.IsNullOrEmpty(cookie.Values["QuestionType"]))
  23. {
  24. var QuestionType = cookie.Values["QuestionType"].Split(',').Select(q=>decimal.Parse(q)).ToArray();
  25. var settings = new PracticeParams();
  26. settings.QuestionType = QuestionType;
  27. settings.QuestionLength = 999;
  28. Session["PracticeParams"] = settings;
  29. ViewBag.QuestionIndex = cookie.Values["QuestionIndex"];
  30. ViewBag.ShowRecordButton = true;
  31. }
  32. return View();
  33. }
  34. public JsonResult ListAllQuestionType()
  35. {
  36. var list = IQuestionServices.ListAllQuestionType();
  37. return Json(list, JsonRequestBehavior.AllowGet);
  38. }
  39. [HttpPost]
  40. public ActionResult PracticeSetting(decimal[] cklType, int? txtNum, bool? ckbAll)
  41. {
  42. try
  43. {
  44. var user = ((EMISOnline.Utility.FormValidate.CustomPrincipal)HttpContext.User);
  45. var settings = new PracticeParams();
  46. settings.QuestionType = cklType;
  47. settings.QuestionLength = txtNum.Value;
  48. if (ckbAll == true)
  49. {
  50. settings.QuestionLength = 9999;
  51. SaveRecord(cklType);
  52. }
  53. else
  54. {
  55. var cookie = Request.Cookies["practice_record"];
  56. if (cookie != null)
  57. {
  58. cookie.Expires = DateTime.Now.AddDays(-1);
  59. Response.Cookies.Add(cookie);
  60. }
  61. }
  62. Session["PracticeParams"] = settings;
  63. Session["Answers"] = null;
  64. Session["QeustionSet"] = null;
  65. return Json(new ReturnMessage()
  66. {
  67. IsSuccess = true,
  68. Message = "开始练习!",
  69. RedictUrl = Url.Content("~/Practice/PracticeView")
  70. });
  71. }
  72. catch (Exception ex)
  73. {
  74. return Json(new ReturnMessage()
  75. {
  76. IsSuccess = false,
  77. Message = "请输入练习数量:" + ex.Message
  78. });
  79. }
  80. }
  81. public ActionResult PracticeView()
  82. {
  83. var PracticeParams = Session["PracticeParams"] as PracticeParams;
  84. if (PracticeParams == null)
  85. return Redirect("~/Practice/PracticeSetting");
  86. QeustionSet = IQuestionServices.GeneratePractice(new string[] { }, PracticeParams.QuestionType.Select(q => q.ToString()).ToList(), PracticeParams.QuestionLength);
  87. var QuestionIndex = 0;
  88. if (!string.IsNullOrEmpty(Request.QueryString["QuestionIndex"]))
  89. QuestionIndex = int.Parse(Request.QueryString["QuestionIndex"]);
  90. if (QeustionSet != null && QeustionSet.Length > 0)
  91. {
  92. ViewBag.txtQuestionCount = QeustionSet.Length.ToString();
  93. LoadQuestion(QeustionSet[QuestionIndex], QuestionIndex);
  94. }
  95. var js = new JavaScriptSerializer();
  96. ViewBag.QeustionSet = js.Serialize(QeustionSet);
  97. return View();
  98. }
  99. public string ShowAnswer(decimal questionid, string right_answers, int currentIndex)
  100. {
  101. SaveQuestionAnswer(questionid.ToString(), right_answers);
  102. ChangeRecord(currentIndex);
  103. return "";
  104. }
  105. public ActionResult ShowPaper()
  106. {
  107. if (Answers == null || QeustionSet == null)
  108. return Redirect("~/Practice/PracticeSetting");
  109. var questions = IQuestionServices.ShowPaperQuestion(QeustionSet, Answers);
  110. ViewBag.questions = questions;
  111. return View();
  112. }
  113. private void SaveRecord(decimal[] QuestionType)
  114. {
  115. HttpCookie cookie = new HttpCookie("practice_record");
  116. cookie.Expires = DateTime.Now.AddMonths(1);
  117. cookie.Values.Add("QuestionType", string.Join(",", QuestionType.Select(q => q.ToString())));
  118. Response.Cookies.Add(cookie);
  119. }
  120. private void LoadQuestion(decimal questionid, int QuestionIndex)
  121. {
  122. var questionjson = IQuestionServices.GetQuestion(questionid);
  123. ViewBag.question = questionjson;
  124. ViewBag.currentIndex = QuestionIndex;
  125. ViewBag.fileServerUrl = string.Format("'{0}'", FileServerConfig.URL);
  126. }
  127. public string GetQuestion(decimal questionid, string right_answers,int currentIndex)
  128. {
  129. SaveQuestionAnswer(questionid.ToString(), right_answers);
  130. ChangeRecord(currentIndex);
  131. var nextQuestion = QeustionSet[currentIndex];
  132. var questionjson = IQuestionServices.GetQuestion(nextQuestion);
  133. return questionjson;
  134. }
  135. private void SaveQuestionAnswer(string questionid, string answer)
  136. {
  137. if ( !string.IsNullOrEmpty(answer))
  138. {
  139. if (!Answers.Keys.Contains(questionid))
  140. Answers.Add(questionid, answer);
  141. else
  142. Answers[questionid] = answer;
  143. }
  144. }
  145. private void ChangeRecord(int QuestionIndex)
  146. {
  147. var cookie = Request.Cookies["practice_record"];
  148. if (cookie != null)
  149. {
  150. if (string.IsNullOrEmpty(cookie.Values["QuestionIndex"]))
  151. cookie.Values.Add("QuestionIndex", QuestionIndex.ToString());
  152. else
  153. cookie.Values["QuestionIndex"] = QuestionIndex.ToString();
  154. Response.Cookies.Add(cookie);
  155. }
  156. }
  157. private Dictionary<string, string> Answers
  158. {
  159. get
  160. {
  161. if (Session["Answers"] == null)
  162. {
  163. var dic = new Dictionary<string, string>();
  164. Session["Answers"] = dic;
  165. return dic;
  166. }
  167. return Session["Answers"] as Dictionary<string, string>;
  168. }
  169. }
  170. private decimal[] QeustionSet
  171. {
  172. get
  173. {
  174. return Session["QeustionSet"] as decimal[];
  175. }
  176. set
  177. {
  178. Session["QeustionSet"] = value;
  179. }
  180. }
  181. }
  182. public class PracticeParams
  183. {
  184. public decimal[] QuestionType { get; set; }
  185. public int QuestionLength { get; set; }
  186. }
  187. }