Beispiel #1
0
        public AnswerSheetResult MakeAnswerSheet(string sessionToken, Models.AnswerSheet answerSheet)
        {
            var result = new AnswerSheetResult();

            var userID = _checkSessionToken(sessionToken, result);

            if (userID.HasValue && mContext.Exam.FirstOrDefault(e => e.Id == answerSheet.ExamId) != null)
            {
                answerSheet.Id        = 0;
                answerSheet.UserId    = userID.Value;
                answerSheet.Score     = 0;
                answerSheet.IsClosed  = false;
                answerSheet.CreatedAt = DateTime.Now;

                mContext.AnswerSheet.Add(answerSheet);
                mContext.SaveChanges();

                result.AnswerSheet = answerSheet;
            }
            else
            {
                result.MessageError = string.Format("Không tìm thấy đề thi!");
            }

            return(result);
        }
Beispiel #2
0
        public BasicResult Answer(string sessionToken, Models.AnswerSheet answerSheet, long quizzesId, string answer)
        {
            var result = new BasicResult();
            var userId = _checkSessionToken(sessionToken, result);
            var ans    = mContext.AnswerSheet.FirstOrDefault(a => a.Id == answerSheet.Id && a.UserId == userId);

            if (ans != null)
            {
                var exam     = mContext.Exam.FirstOrDefault(e => e.Id == ans.ExamId);
                var deadline = ans.CreatedAt.AddMinutes(exam.Time);
                if (deadline <= DateTime.Now)
                {
                    ans.IsClosed = true;
                }

                var quizzesIds = exam.Quizzes.Split(new char[] { ',' });
                if (ans.IsClosed == true)
                {
                    result.MessageError = string.Format("Bài kiểm tra đã được đóng, không thể tiếp tục");

                    var answers = mContext.AnswerSheetItem
                                  .Where(ai => ai.AnswerSheetId == ans.Id).ToArray();
                    ans.Score = answers.Select(i => i.Match ? 1.0 : 0.0).Sum() / answers.Length;

                    mContext.SaveChanges();
                }
                else if (quizzesIds.Any(q => q == quizzesId.ToString()))
                {
                    var answerItem = mContext.AnswerSheetItem
                                     .FirstOrDefault(ai => ai.AnswerSheetId == ans.Id && ai.QuizzesId == quizzesId);
                    if (answerItem == null)
                    {
                        answerItem = new Models.AnswerSheetItem()
                        {
                            AnswerSheetId = ans.Id,
                            QuizzesId     = quizzesId,
                            Answer        = answer,
                            Match         = false
                        };
                        mContext.AnswerSheetItem.Add(answerItem);
                    }

                    var quizzes = mContext.Quizzes.FirstOrDefault(q => q.Id == quizzesId);
                    answerItem.Match = quizzes != null?quizzes.Answer.Trim() == answer.Trim() : false;

                    mContext.SaveChanges();
                }
                else
                {
                    result.MessageError = string.Format("Câu hỏi không có trong đề thi!");
                }
            }

            return(result);
        }
Beispiel #3
0
        public QuizzesResult NextQuizzes(string sessionToken, Models.AnswerSheet answerSheet)
        {
            var result = new QuizzesResult();
            var userID = _checkSessionToken(sessionToken, result);
            var exam   = mContext.Exam.FirstOrDefault(e => e.Id == answerSheet.ExamId);

            if (userID.HasValue && exam != null)
            {
                var quizzesIds = exam.Quizzes.Split(new char[] { ',' })
                                 .Select(id => int.Parse(id)).ToArray();

                var index       = 0;
                var nextQuizzes = 0;
                for (int i = 0; i < quizzesIds.Length; ++i)
                {
                    if (answerSheet.CurrentQuizzes.HasValue == false ||
                        answerSheet.CurrentQuizzes == 0 ||
                        answerSheet.CurrentQuizzes == quizzesIds[i])
                    {
                        nextQuizzes = quizzesIds[++i < quizzesIds.Length ? i : 0];
                        index       = i;
                        break;
                    }
                }

                answerSheet.CurrentQuizzes = nextQuizzes;
                mContext.SaveChanges();

                var quizzes = mContext.Quizzes.FirstOrDefault(q => q.Id == nextQuizzes);
                if (quizzes != null)
                {
                    result.AnswerSheet = answerSheet;
                    result.Index       = index;
                    result.Question    = quizzes.Question;
                    result.ChooseA     = quizzes.ChooseA;
                    result.ChooseB     = quizzes.ChooseB;
                    result.ChooseC     = quizzes.ChooseC;
                    result.ChooseD     = quizzes.ChooseD;

                    result.Answer = mContext.AnswerSheetItem
                                    .Where(ai => ai.AnswerSheetId == answerSheet.Id && ai.QuizzesId == nextQuizzes)
                                    .Select(ai => ai.Answer)
                                    .FirstOrDefault();
                }
                else
                {
                    result.MessageError = string.Format("Không tìm thấy câu hỏi. :(");
                }
            }

            return(result);
        }
Beispiel #4
0
        public DoneAnswerSheetResult DoneAnswerSheet(string sessionToken, Models.AnswerSheet answerSheet)
        {
            var result = new DoneAnswerSheetResult();
            var userId = _checkSessionToken(sessionToken, result);
            var ans    = mContext.AnswerSheet.FirstOrDefault(a => a.Id == answerSheet.Id && a.UserId == userId);

            if (ans != null)
            {
                var answers = mContext.AnswerSheetItem
                              .Where(ai => ai.AnswerSheetId == ans.Id).ToArray();
                ans.IsClosed = true;
                ans.Score    = Math.Round((answers.Select(i => i.Match ? 1.0 : 0.0).Sum() / answers.Length) * 10, 1);

                mContext.SaveChanges();

                result.AnswerSheet = ans;
            }

            return(result);
        }
Beispiel #5
0
        public LastAnswerSheetResult GetLastAnswerSheet(string sessionToken)
        {
            var result = new LastAnswerSheetResult();
            var userId = _checkSessionToken(sessionToken, result);

            if (userId.HasValue)
            {
                var answerSheets = mContext.AnswerSheet
                                   .Where(a => a.IsClosed != true && a.UserId == userId)
                                   .OrderByDescending(a => a.Id)
                                   .ToArray();

                Models.AnswerSheet lastAnswerSheet = null;
                Models.Exam        lastExam        = null;

                foreach (var item in answerSheets)
                {
                    var exam = mContext.Exam.FirstOrDefault(e => e.Id == item.ExamId);
                    if (exam != null)
                    {
                        var deadline = item.CreatedAt.AddMinutes(exam.Time);
                        if (deadline < DateTime.Now && lastAnswerSheet == null)
                        {
                            lastAnswerSheet = item;
                            lastExam        = exam;
                        }
                        else
                        {
                            item.IsClosed = true;
                        }
                    }
                }

                mContext.SaveChanges();

                result.AnswerSheet = lastAnswerSheet;
                result.Exam        = lastExam;
            }

            return(result);
        }