筆試考試系統 ____學生考試
阿新 • • 發佈:2020-08-03
1.今日任務
學生考試
考試列表:顯示正在進行或者已經結束的考試
檢視已經考過的試卷
正在進行的考試
提交試卷:
完成考試即可檢視試卷 獲取成績:
控制器程式碼:
1 public class ExamController : Controller 2 { 3 private static Dictionary<int, List<Exam_Answer>> listanswer = new Dictionary<int, List<Exam_Answer>>(); 4 //GET: Exam 5 public ActionResult Index(int page = 1) 6 { 7 var currentuser = Session[CommonFeild.SessionName] as Exam_User; 8 ViewBag.ID = currentuser.UserID; 9 IPagedList list = PaperRuleService.GetListEnable(page); 10 returnView(list); 11 } 12 [HttpGet] 13 /// <summary> 14 /// 開始考試 15 /// </summary> 16 /// <param name="ruleid"></param> 17 /// <returns></returns> 18 public ActionResult BeginExam(int ruleid) 19 {20 var currentuser = Session[CommonFeild.SessionName] as Exam_User; 21 var data = PaperRuleService.FindPaperRuleByID(ruleid); 22 ViewBag.Rule = data; 23 ViewBag.date = data.RuleEndDate.ToString("yyyy/MM/dd HH:mm:ss"); 24 25 var list = ExamPaperService.GeneratePaper(ruleid, currentuser.UserID); 26 if(!listanswer.ContainsKey(currentuser.UserID)) 27 { 28 listanswer.Add(currentuser.UserID, list); 29 } 30 //獲取questioID陣列 31 List<Exam_Question> questionlist = new List<Exam_Question>(); 32 List<ExamPaperBLL> paperbll = new List<ExamPaperBLL>(); 33 foreach (var item in listanswer[currentuser.UserID]) 34 { 35 ExamPaperBLL examPaperBLL = new ExamPaperBLL(); 36 examPaperBLL.Exam_Question = QuestionService.GetdataByID(item.QuestionID); 37 examPaperBLL.AnswerOptionID = item.AnswerOptionID; 38 paperbll.Add(examPaperBLL); 39 // questionlist.Add(QuestionService.GetdataByID(item.QuestionID)); 40 } 41 return View(paperbll); 42 } 43 /// <summary> 44 /// 單選題 45 /// </summary> 46 /// <param name="data"></param> 47 public void GetRadioData(string data) 48 { 49 var currentuser = Session[CommonFeild.SessionName] as Exam_User; 50 string[] arry = data.Split(','); 51 string optionid = arry[0]; 52 int questionid = Convert.ToInt32(arry[1]); 53 //防止併發 54 lock (this) 55 { 56 var answer = listanswer[currentuser.UserID].Where(x => x.QuestionID == questionid).FirstOrDefault(); 57 answer.AnswerOptionID = optionid; 58 } 59 60 } 61 /// <summary> 62 /// 多選題 63 /// </summary> 64 /// <param name="data"></param> 65 /// <param name="check"></param> 66 public void GetChechData(string data, bool check) 67 { 68 var currentuser = Session[CommonFeild.SessionName] as Exam_User; 69 string[] arry = data.Split(','); 70 string optionid = arry[0]; 71 int questionid = Convert.ToInt32(arry[1]); 72 //判斷是否是選中 或者取消 73 if (check) 74 { 75 //防止併發 76 lock (this) 77 { 78 var answer = listanswer[currentuser.UserID].Where(x => x.QuestionID == questionid).FirstOrDefault(); 79 answer.AnswerOptionID += optionid + ","; 80 81 } 82 } 83 else 84 { 85 //防止併發 86 lock (this) 87 { 88 var answer = listanswer[currentuser.UserID].Where(x => x.QuestionID == questionid).FirstOrDefault(); 89 string temp = answer.AnswerOptionID; 90 if (temp.Contains(optionid)) 91 { 92 int index = temp.IndexOf(optionid); 93 string newstr = temp.Remove(index, optionid.Length + 1); 94 answer.AnswerOptionID = newstr; 95 } 96 } 97 } 98 } 99 /// <summary> 100 /// 儲存 101 /// </summary> 102 /// <returns></returns> 103 [HttpPost] 104 public ActionResult Save() 105 { 106 var currentuser = Session[CommonFeild.SessionName] as Exam_User; 107 108 try 109 { 110 foreach (var item in listanswer[currentuser.UserID]) 111 { 112 if (item.AnswerOptionID != "") 113 { 114 if (item.AnswerOptionID.EndsWith(",")) 115 { 116 string temp = item.AnswerOptionID; 117 item.AnswerOptionID = temp.Remove(temp.Length - 1, 1); 118 } 119 } 120 AnswerService.Update(item); 121 } 122 } 123 catch (Exception ex) 124 { 125 126 return Json(new { success = false, msg = "儲存失敗" + ex }); 127 } 128 return Json(new { success = true, msg = "儲存成功"}); 129 130 } 131 [HttpPost] 132 public ActionResult GetCount() 133 { 134 var currentuser = Session[CommonFeild.SessionName] as Exam_User; 135 int count = listanswer[currentuser.UserID].Where(x => x.AnswerOptionID == "").Count(); 136 137 return Json(new { success = true,num=count }); 138 139 } 140 141 /// <summary> 142 /// 提交 143 /// </summary> 144 /// <returns></returns> 145 [HttpPost] 146 public ActionResult Conmit() 147 { 148 var currentuser = Session[CommonFeild.SessionName] as Exam_User; 149 int paperid = 0; 150 try 151 { 152 foreach (var item in listanswer[currentuser.UserID]) 153 { 154 paperid = item.PaperID; 155 if (item.AnswerOptionID != "") 156 { 157 if (item.AnswerOptionID.EndsWith(",")) 158 { 159 string temp = item.AnswerOptionID; 160 item.AnswerOptionID = temp.Remove(temp.Length - 1, 1); 161 } 162 } 163 //提交試卷 164 AnswerService.Update(item); 165 } 166 //更新試卷狀態 獲取分數 167 AnswerService.GetScore(paperid, currentuser.UserID); 168 } 169 catch (Exception ex) 170 { 171 172 return Json(new { success = false, msg = "提交失敗" + ex }); 173 } 174 return Json(new { success = true, msg = "提交成功" }); 175 } 176 /// <summary> 177 /// 試卷詳情 178 /// </summary> 179 /// <param name="PaperID">試卷編號</param> 180 /// <returns></returns> 181 public ActionResult ExamDetail(int ruleid) 182 { 183 var currentuser = Session[CommonFeild.SessionName] as Exam_User; 184 185 //獲取考試資訊 186 var paper = ExamPaperService.CheckPaper(ruleid,currentuser.UserID); 187 188 ViewBag.User = currentuser; 189 ViewBag.Rule = PaperRuleService.FindPaperRuleByID(paper.RuleID); 190 ViewBag.Score = paper.UserScore; 191 //獲取答題資訊 192 List<Exam_Answer> list = AnswerService.GetAnswer(currentuser.UserID,paper.PaperID); 193 //載入試卷模型 194 List<ExamPaperBLL> paperbll = new List<ExamPaperBLL>(); 195 foreach (var item in list) 196 { 197 ExamPaperBLL examPaperBLL = new ExamPaperBLL(); 198 examPaperBLL.Exam_Question = QuestionService.GetdataByID(item.QuestionID); 199 examPaperBLL.AnswerOptionID = item.AnswerOptionID; 200 paperbll.Add(examPaperBLL); 201 202 } 203 return View(paperbll); 204 } 205 }
service方法
1 public class ExamPaperService 2 { 3 /// <summary> 4 /// 根據試卷規則編號生成試卷,並將題目生成到答題資訊表中 5 /// </summary> 6 /// <param name="ruleid"></param> 7 /// <param name="UserID"></param> 8 public static List<Exam_Answer> GeneratePaper(int ruleid, int UserID) 9 { 10 //定義答題卡 11 List<Exam_Answer> AnswerList = new List<Exam_Answer>(); 12 //定義試卷變數 13 List<Exam_Question> QuestionList = new List<Exam_Question>(); 14 //定義組卷詳情變數 15 List<Exam_RuleDetail> RuledetailList = RuleDetailService.GetDetailQuestion(ruleid); 16 int answercount = 0; 17 //查詢試卷總題目數量 18 int num = PaperRuleService.FindPaperRuleByID(ruleid).QuestionNum; 19 //判斷試卷資訊表是否已經存在,如果不存在需要建立(需要考慮中途退出的同學) 20 Exam_Paper paper = CheckPaper(ruleid, UserID); 21 if (paper == null) 22 { 23 paper = CreatePaper(ruleid, UserID); 24 //生成答題卡 25 } 26 //判斷答題卡是否存在資訊 27 answercount = AnswerService.GetUserQuestionCount(UserID, paper.PaperID); 28 //如果存在的話 將試卷資訊加載出來 29 if (answercount == num) 30 { 31 var data = AnswerService.GetAnswer(UserID, paper.PaperID); 32 AnswerList.AddRange(data); 33 } 34 ///如果不存在 隨機生成試題 35 else 36 { 37 using (ExamSysDBContext db = new ExamSysDBContext()) 38 { 39 //先將答題卡清空 40 AnswerService.Clear(UserID, paper.PaperID); 41 //根據規則詳情 隨機生成試題 42 foreach (var item in RuledetailList) 43 { 44 var temp = db.Exam_Question.Where(x => x.LibraryID == item.LibraryID).OrderBy(x => Guid.NewGuid()).Take(item.QuestionNum).ToList(); 45 QuestionList.AddRange(temp); 46 } 47 //將試題 新增到答題卡 48 foreach (var question in QuestionList) 49 { 50 51 Exam_Answer answer = new Exam_Answer 52 { 53 AnswerOptionID = "", 54 LibraryID = question.LibraryID, 55 PaperID = paper.PaperID, 56 UserID = UserID, 57 QuestionID = question.QuestionID, 58 OptionID = QuestionOptionsService.GetOptionID(question.QuestionAnswer, question.QuestionID), 59 }; 60 61 AnswerList.Add(answer); 62 } 63 //將答題資訊新增到資料庫 64 db.Exam_Answer.AddRange(AnswerList); 65 db.SaveChanges(); 66 } 67 } 68 return AnswerList; 69 } 70 /// <summary> 71 /// 檢查試卷是否已經生成,將試卷資訊返回 72 /// </summary> 73 /// <param name="ruleid"></param> 74 /// <param name="UserID"></param> 75 /// <returns></returns> 76 public static Exam_Paper CheckPaper(int ruleid, int userid) 77 { 78 using (ExamSysDBContext db = new ExamSysDBContext()) 79 { 80 return db.Exam_Paper.Where(x => x.RuleID == ruleid && x.UserID == userid).FirstOrDefault(); 81 } 82 } 83 /// <summary> 84 /// 建立試卷 85 /// </summary> 86 /// <param name="ruleid"></param> 87 /// <param name="userid"></param> 88 /// <returns></returns> 89 public static Exam_Paper CreatePaper(int ruleid, int userid) 90 { 91 //獲取試卷規則資訊 92 var rule = PaperRuleService.FindPaperRuleByID(ruleid); 93 94 //獲取使用者資訊 95 var user = UsersService.GetUserByID(userid); 96 //新增試卷規則 97 Exam_Paper paper = new Exam_Paper { States = false, RealName = user.RealName, UserID = user.UserID, RuleID = rule.PaperRuleID, TotalScore = rule.Score, UserScore = 0 }; 98 var Newpaper = AddPaper(paper); 99 return Newpaper; 100 101 } 102 /// <summary> 103 /// 新增試卷 104 /// </summary> 105 /// <param name="paper"></param> 106 /// <returns></returns> 107 public static Exam_Paper AddPaper(Exam_Paper paper) 108 { 109 using (ExamSysDBContext db = new ExamSysDBContext()) 110 { 111 db.Exam_Paper.Add(paper); 112 db.SaveChanges(); 113 return paper; 114 } 115 } 116 /// <summary> 117 /// 更新試卷分數 118 /// </summary> 119 /// <param name="paperid"></param> 120 /// <param name="score"></param> 121 public static void UpdateScore(int paperid, int score) 122 { 123 using (ExamSysDBContext db = new ExamSysDBContext()) 124 { 125 var data = db.Exam_Paper.Where(x => x.PaperID == paperid).FirstOrDefault(); 126 data.UserScore = score; 127 db.SaveChanges(); 128 } 129 } 130 /// <summary> 131 /// 獲取試卷規則編號 132 /// </summary> 133 /// <param name="paperid"></param> 134 /// <returns></returns> 135 public static Exam_Paper GetPaper(int paperid) 136 { 137 using (ExamSysDBContext db = new ExamSysDBContext()) 138 { 139 var data = db.Exam_Paper.Where(x => x.PaperID == paperid).FirstOrDefault(); 140 return data; 141 142 } 143 } 144 /// <summary> 145 /// 獲取試卷狀態 已提交還是未提交 146 /// </summary> 147 /// <param name="ruleid"></param> 148 /// <param name="userid"></param> 149 /// <returns></returns> 150 public static bool CkeckScore(int ruleid, int userid) 151 { 152 using (ExamSysDBContext db = new ExamSysDBContext()) 153 { 154 var data = db.Exam_Paper.Where(x => x.RuleID == ruleid && x.UserID == userid).FirstOrDefault(); 155 if(data==null) 156 { 157 return false; 158 } 159 else 160 { 161 return data.States; 162 } 163 } 164 } 165 166 /// <summary> 167 /// 統計成績資訊 168 /// </summary> 169 /// <param name="ruleid"></param> 170 /// <returns></returns> 171 public static ScoreTotleModel GetScoreModel(int ruleid) 172 { 173 ScoreTotleModel score = new ScoreTotleModel(); 174 using (ExamSysDBContext db = new ExamSysDBContext()) 175 { 176 score.MaxScore = db.Exam_Paper.Where(x => x.RuleID == ruleid).Select(x => x.UserScore).Max(); 177 178 score.MinScore = db.Exam_Paper.Where(x => x.RuleID == ruleid).Select(x => x.UserScore).Min(); 179 180 score.ScoreAvg = db.Exam_Paper.Where(x => x.RuleID == ruleid).Select(x => x.UserScore).Average(); 181 score.Score60 = db.Exam_Paper.Where(x => x.RuleID == ruleid && x.UserScore < 60).Count(); 182 score.Score6070 = db.Exam_Paper.Where(x => x.RuleID == ruleid && 60 <= x.UserScore && x.UserScore < 70).Count(); 183 score.Score7080 = db.Exam_Paper.Where(x => x.RuleID == ruleid && 70 <= x.UserScore && x.UserScore < 80).Count(); 184 score.Score8090 = db.Exam_Paper.Where(x => x.RuleID == ruleid && 80 <= x.UserScore && x.UserScore < 90).Count(); 185 score.Score90100 = db.Exam_Paper.Where(x => x.RuleID == ruleid && 90 <= x.UserScore && x.UserScore < 100).Count(); 186 score.Score100 = db.Exam_Paper.Where(x => x.RuleID == ruleid && 100 <= x.UserScore).Count(); 187 188 score.StudentNum = db.Exam_Paper.Where(x => x.RuleID == ruleid).Count(); 189 return score; 190 191 } 192 } 193 194 }
3.遇到問題:
無
4.解決方案
無