- A+
所属分类:.NET技术
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 return View(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.解决方案
无