123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using EMISOnline.CommonLogic.ExamServices;
- using EMISOnline.ViewModel;
- using jEMISOnline.Utility;
- using System.Web.Script.Serialization;
- namespace EMISOnline.Web.Controllers.StudentView
- {
- [Authorization]
- public class PracticeController : Controller
- {
- public IQuestionServices IQuestionServices { get; set; }
- //
- // GET: /Practice/
- public ActionResult PracticeSetting()
- {
- var cookie = Request.Cookies["practice_record"];
- ViewBag.ShowRecordButton = false;
- if (cookie != null && !string.IsNullOrEmpty(cookie.Values["QuestionType"]))
- {
- var QuestionType = cookie.Values["QuestionType"].Split(',').Select(q=>decimal.Parse(q)).ToArray();
- var settings = new PracticeParams();
- settings.QuestionType = QuestionType;
- settings.QuestionLength = 999;
- Session["PracticeParams"] = settings;
- ViewBag.QuestionIndex = cookie.Values["QuestionIndex"];
- ViewBag.ShowRecordButton = true;
- }
- return View();
- }
- public JsonResult ListAllQuestionType()
- {
- var list = IQuestionServices.ListAllQuestionType();
- return Json(list, JsonRequestBehavior.AllowGet);
- }
- [HttpPost]
- public ActionResult PracticeSetting(decimal[] cklType, int? txtNum, bool? ckbAll)
- {
- try
- {
- var user = ((EMISOnline.Utility.FormValidate.CustomPrincipal)HttpContext.User);
- var settings = new PracticeParams();
- settings.QuestionType = cklType;
- settings.QuestionLength = txtNum.Value;
- if (ckbAll == true)
- {
- settings.QuestionLength = 9999;
- SaveRecord(cklType);
- }
- else
- {
- var cookie = Request.Cookies["practice_record"];
- if (cookie != null)
- {
- cookie.Expires = DateTime.Now.AddDays(-1);
- Response.Cookies.Add(cookie);
- }
- }
- Session["PracticeParams"] = settings;
- Session["Answers"] = null;
- Session["QeustionSet"] = null;
- return Json(new ReturnMessage()
- {
- IsSuccess = true,
- Message = "开始练习!",
- RedictUrl = Url.Content("~/Practice/PracticeView")
- });
- }
- catch (Exception ex)
- {
- return Json(new ReturnMessage()
- {
- IsSuccess = false,
- Message = "请输入练习数量:" + ex.Message
- });
- }
- }
- public ActionResult PracticeView()
- {
- var PracticeParams = Session["PracticeParams"] as PracticeParams;
- if (PracticeParams == null)
- return Redirect("~/Practice/PracticeSetting");
- QeustionSet = IQuestionServices.GeneratePractice(new string[] { }, PracticeParams.QuestionType.Select(q => q.ToString()).ToList(), PracticeParams.QuestionLength);
- var QuestionIndex = 0;
- if (!string.IsNullOrEmpty(Request.QueryString["QuestionIndex"]))
- QuestionIndex = int.Parse(Request.QueryString["QuestionIndex"]);
- if (QeustionSet != null && QeustionSet.Length > 0)
- {
- ViewBag.txtQuestionCount = QeustionSet.Length.ToString();
- LoadQuestion(QeustionSet[QuestionIndex], QuestionIndex);
- }
- var js = new JavaScriptSerializer();
- ViewBag.QeustionSet = js.Serialize(QeustionSet);
- return View();
- }
- public string ShowAnswer(decimal questionid, string right_answers, int currentIndex)
- {
- SaveQuestionAnswer(questionid.ToString(), right_answers);
- ChangeRecord(currentIndex);
- return "";
- }
- public ActionResult ShowPaper()
- {
- if (Answers == null || QeustionSet == null)
- return Redirect("~/Practice/PracticeSetting");
- var questions = IQuestionServices.ShowPaperQuestion(QeustionSet, Answers);
- ViewBag.questions = questions;
- return View();
- }
- private void SaveRecord(decimal[] QuestionType)
- {
- HttpCookie cookie = new HttpCookie("practice_record");
- cookie.Expires = DateTime.Now.AddMonths(1);
- cookie.Values.Add("QuestionType", string.Join(",", QuestionType.Select(q => q.ToString())));
- Response.Cookies.Add(cookie);
- }
- private void LoadQuestion(decimal questionid, int QuestionIndex)
- {
- var questionjson = IQuestionServices.GetQuestion(questionid);
- ViewBag.question = questionjson;
- ViewBag.currentIndex = QuestionIndex;
- ViewBag.fileServerUrl = string.Format("'{0}'", FileServerConfig.URL);
- }
- public string GetQuestion(decimal questionid, string right_answers,int currentIndex)
- {
- SaveQuestionAnswer(questionid.ToString(), right_answers);
- ChangeRecord(currentIndex);
- var nextQuestion = QeustionSet[currentIndex];
- var questionjson = IQuestionServices.GetQuestion(nextQuestion);
- return questionjson;
- }
- private void SaveQuestionAnswer(string questionid, string answer)
- {
- if ( !string.IsNullOrEmpty(answer))
- {
- if (!Answers.Keys.Contains(questionid))
- Answers.Add(questionid, answer);
- else
- Answers[questionid] = answer;
- }
- }
- private void ChangeRecord(int QuestionIndex)
- {
- var cookie = Request.Cookies["practice_record"];
- if (cookie != null)
- {
- if (string.IsNullOrEmpty(cookie.Values["QuestionIndex"]))
- cookie.Values.Add("QuestionIndex", QuestionIndex.ToString());
- else
- cookie.Values["QuestionIndex"] = QuestionIndex.ToString();
- Response.Cookies.Add(cookie);
- }
- }
- private Dictionary<string, string> Answers
- {
- get
- {
- if (Session["Answers"] == null)
- {
- var dic = new Dictionary<string, string>();
- Session["Answers"] = dic;
- return dic;
- }
- return Session["Answers"] as Dictionary<string, string>;
- }
- }
- private decimal[] QeustionSet
- {
- get
- {
- return Session["QeustionSet"] as decimal[];
- }
- set
- {
- Session["QeustionSet"] = value;
- }
- }
- }
- public class PracticeParams
- {
- public decimal[] QuestionType { get; set; }
- public int QuestionLength { get; set; }
- }
- }
|