コード例 #1
0
        /// <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);
            }
        }
コード例 #2
0
        /// <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);
            }
        }
コード例 #3
0
        public bool BatchSubmitUserAnswer(BatchSubmitUserAnswerInput data)
        {
            try
            {
                using (MyExaminationContext context = new MyExaminationContext())
                {
                    // 用户检查.
                    MeUser user = context.Users.Find(data.UserID);
                    if (user == null)
                    {
                        // 用户不存在.
                        return(false);
                    }

                    // 用户考试数据检查.
                    MeUserExamination userExamination = context.UserExaminations.Find(data.UserExaminationID);
                    if (userExamination == null)
                    {
                        // 用户考试数据不存在.
                        return(false);
                    }
                    if (userExamination.UserID != data.UserID)
                    {
                        // 参数不正确, 当前用户对别人的考试进行回答.
                        return(false);
                    }
                    if (userExamination.ExaminationFinishTime != null)
                    {
                        // 考试已经结束.
                        return(false);
                    }



                    foreach (QuestionAnswer item in data.QuestionAnswers)
                    {
                        // 获取具体的问题.
                        MeQuestion question = context.Questions.Find(item.QuestionID);
                        if (question == null)
                        {
                            // 问题不存在.
                            return(false);
                        }


                        // 单选的情况下,检查 提交的答案, 是否是 可选答案.
                        if (question.QuestionType == MeQuestionType.OneOption)
                        {
                            long questionOptionID = 0;
                            if (!Int64.TryParse(item.OneOptionAnswer, out questionOptionID))
                            {
                                // 对于 单选/多选, 提交的数据, 无法解析为 long 值.
                                return(false);
                            }

                            MeQuestionOption questionOption = context.QuestionOptions.Find(questionOptionID);
                            if (questionOption == null)
                            {
                                // 不存在的 答案选项.
                                return(false);
                            }
                            if (questionOption.QuestionID != item.QuestionID)
                            {
                                // 答案选项, 与 问题不匹配.
                                return(false);
                            }
                        }
                        // 多选的情况下,检查 提交的答案, 是否是 可选答案.
                        if (question.QuestionType == MeQuestionType.MulOption)
                        {
                            foreach (string answerString in item.MulOptionAnswer)
                            {
                                long questionOptionID = 0;
                                if (!Int64.TryParse(answerString, out questionOptionID))
                                {
                                    // 对于 单选/多选, 提交的数据, 无法解析为 long 值.
                                    return(false);
                                }
                                MeQuestionOption questionOption = context.QuestionOptions.Find(questionOptionID);
                                if (questionOption == null)
                                {
                                    // 不存在的 答案选项.
                                    return(false);
                                }
                                if (questionOption.QuestionID != item.QuestionID)
                                {
                                    // 答案选项, 与 问题不匹配.
                                    return(false);
                                }
                            }
                        }

                        var answerQuery =
                            from answerData in context.UserAnswers
                            where
                            answerData.UserExaminationID == data.UserExaminationID &&
                            answerData.UserID == data.UserID &&
                            answerData.QuestionID == item.QuestionID
                            select
                            answerData;

                        MeUserAnswer userAnswerData = answerQuery.SingleOrDefault();

                        if (userAnswerData == null)
                        {
                            // 是首次回答.
                            userAnswerData = new MeUserAnswer()
                            {
                                // 用户考试ID.
                                UserExaminationID = data.UserExaminationID,
                                // 用户ID.
                                UserID = data.UserID,
                                // 问题ID.
                                QuestionID = item.QuestionID,
                                // 回答.
                                UserAnswer = item.ResultAnswer,
                            };
                            context.UserAnswers.Add(userAnswerData);
                        }
                        else
                        {
                            // 是修改之前的回答.
                            userAnswerData.UserAnswer = item.ResultAnswer;
                        }
                    }



                    context.SaveChanges();
                }

                return(true);
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
                return(false);
            }
        }