/// <summary> /// 取得试题. /// </summary> /// <param name="examinationID"></param> /// <returns></returns> public MeExamination GetExamination(long examinationID) { using (MyExaminationContext context = new MyExaminationContext()) { MeExamination result = context.Examinations.Find(examinationID); return(result); } }
/// <summary> /// 开始考试. /// </summary> /// <param name="userID"></param> /// <param name="examinationID"></param> /// <returns></returns> public long StartExamination(long userID, long examinationID) { try { using (MyExaminationContext context = new MyExaminationContext()) { // 用户检查. MeUser user = context.Users.Find(userID); if (user == null) { // 用户不存在. return(-1); } // 考试主表检查. MeExamination examination = context.Examinations.Find(examinationID); if (examination == null) { // 考试主表不存在. return(-1); } MeUserExamination userExamination = new MeUserExamination() { // 用户 UserID = userID, // 考试. ExaminationID = examinationID, // 开始时间. ExaminationStartTime = DateTime.Now, }; context.UserExaminations.Add(userExamination); context.SaveChanges(); return(userExamination.UserExaminationID); } } catch (Exception ex) { logger.Error(ex.Message, ex); return(-1); } }
/// <summary> /// 结束考试. /// </summary> /// <param name="userID"></param> /// <param name="examinationID"></param> /// <returns></returns> public bool FinishExamination(long userID, long examinationID, long userExaminationID) { try { using (MyExaminationContext context = new MyExaminationContext()) { // 用户检查. MeUser user = context.Users.Find(userID); if (user == null) { // 用户不存在. return(false); } // 考试主表检查. MeExamination examination = context.Examinations.Find(examinationID); if (examination == null) { // 考试主表不存在. return(false); } // 用户考试数据检查. MeUserExamination userExamination = context.UserExaminations.Find(userExaminationID); if (userExamination == null) { // 用户考试数据不存在. return(false); } if (userExamination.UserID != userID) { // 参数不正确, 当前用户尝试结束别人的考试. return(false); } if (userExamination.ExaminationID != examinationID) { // 参数不正确, 当前用户尝试结束另外一个考试代码不匹配的考试. return(false); } if (userExamination.ExaminationFinishTime != null) { // 考试已经结束. return(false); } // ##### 计算成绩. ##### // 先获取用户回答数据. var userAnswerQuery = from data in context.UserAnswers where data.UserExaminationID == userExaminationID select data; List <MeUserAnswer> userAnswerList = userAnswerQuery.ToList(); // 获取试题与选项. var query = from data in context.Questions.Include("QuestionOptionList") where data.ExaminationID == examinationID select data; List <MeQuestion> questionList = query.ToList(); // 总成绩. int totalPoint = 0; // 遍历每一题. foreach (var questionData in questionList) { // 正确答案. List <string> rightAnswerList = new List <string>(); switch (questionData.QuestionType) { case MeQuestionType.OneOption: // 单选. var rigthData = questionData.QuestionOptionList.SingleOrDefault(p => p.IsRightOption); if (rigthData == null) { logger.WarnFormat("题目答案出现异常, 第 {0} 题, 不存在正确的选择项目", questionData.QuestionID); } else { rightAnswerList.Add(rigthData.QuestionOptionID.ToString()); } break; case MeQuestionType.MulOption: // 多选. List <string> answerList = questionData.QuestionOptionList.Where(p => p.IsRightOption).Select(p => p.QuestionOptionID.ToString()).ToList(); rightAnswerList.AddRange(answerList); break; default: // 未知. break; } // 获取用户输入. MeUserAnswer userAnswer = userAnswerList.SingleOrDefault(p => p.UserExaminationID == userExaminationID && p.QuestionID == questionData.QuestionID); if (userAnswer == null) { logger.WarnFormat("回答出现一些问题, 第 {0} 题, 用户 {1} 没有做回答。", questionData.QuestionID, userID); // 用户未回答当前题目. continue; } if (CompareUserAnswer(rightAnswerList, userAnswer.UserAnswer)) { // 回答正确时, 获得分数. userAnswer.AnswerPoint = questionData.QuestionPoint; // 累加总成绩. totalPoint += questionData.QuestionPoint; } } // 设置结束时间. userExamination.ExaminationFinishTime = DateTime.Now; // 设置总成绩. userExamination.ExaminationPoint = totalPoint; context.SaveChanges(); return(true); } } catch (Exception ex) { logger.Error(ex.Message, ex); return(false); } }