public ActionResult ConfirmTest(string ok = "")
        {
            if (Session[SessionKey.UserObject] == null || Session[SessionKey.ExamObject] == null)
            {
                return(RedirectToAction("Index", "Home"));
            }
            var exam       = (TN_Exam)Session[SessionKey.ExamObject];
            var user       = (TN_User)Session[SessionKey.UserObject];
            var tnExamUser = new TN_ExamUser()
            {
                JoinDate     = DateTime.Now,
                ExamID       = exam.ID,
                UserID       = user.ID,
                Duration     = 0,
                CorrectCount = 0
            };

            db.TN_ExamUser.AddObject(tnExamUser);
            db.SaveChanges();

            Session[SessionKey.ExamUserObject] = tnExamUser;

            // Tạo danh sách câu hỏi cho User
            var listQuestionExam = db.TN_ExamQuestion.Where(m => m.ExamID == exam.ID).ToList();

            if (listQuestionExam.Count > 0)
            {
                if (listQuestionExam.Count == tnExamUser.TN_Exam.QuestionCount)
                {
                    foreach (var item in listQuestionExam)
                    {
                        TN_ExamUserAnswer tnExamUserAnswer = new TN_ExamUserAnswer();
                        tnExamUserAnswer.ExamUserID = tnExamUser.ID;
                        tnExamUserAnswer.QuestionID = item.QuestionID;
                        db.TN_ExamUserAnswer.AddObject(tnExamUserAnswer);
                    }
                    db.SaveChanges();
                }
                else
                {
                    var allQuestion       = listQuestionExam.Select(m => m.TN_Question).GroupBy(m => m.Type).ToList();
                    var eachTypeQuestions = Math.Floor((double)exam.QuestionCount / (double)allQuestion.Count);
                    var listQuestion      = new List <TN_Question>();
                    foreach (var item in allQuestion)
                    {
                        listQuestion.AddRange(GetRandomListQuestion(item.ToList(), (int)eachTypeQuestions));
                    }
                    foreach (var item in listQuestion)
                    {
                        TN_ExamUserAnswer tnExamUserAnswer = new TN_ExamUserAnswer();
                        tnExamUserAnswer.ExamUserID = tnExamUser.ID;
                        tnExamUserAnswer.QuestionID = item.ID;
                        db.TN_ExamUserAnswer.AddObject(tnExamUserAnswer);
                    }
                    db.SaveChanges();
                }
            }

            return(RedirectToAction("Test", "Home"));
        }
Beispiel #2
0
        public ActionResult AddTimeForUser(TN_ExamUser tnExamUser, string addTime)
        {
            if (string.IsNullOrEmpty(addTime))
            {
                return(RedirectToAction("ManagerTest"));
            }

            double duration = 0;

            double.TryParse(addTime.Replace(".", ","), out duration);

            if (duration == 0)
            {
                return(RedirectToAction("ManagerTest"));
            }

            int minute = (int)duration;
            int second = (int)((duration - minute) * 60);

            var db  = DB.GetContext();
            var obj = db.TN_ExamUser.FirstOrDefault(m => m.ID == tnExamUser.ID);

            if (obj != null)
            {
                var timeEnd = obj.JoinDate.AddSeconds(obj.TN_Exam.Time * 60 + obj.Duration);
                if (DateTime.Now > timeEnd)
                {
                    // xử lý
                    int timeforAdd = ((int)(DateTime.Now - timeEnd).TotalSeconds) + minute * 60 + second;
                    obj.Duration += timeforAdd;
                }
                else
                {
                    obj.Duration += minute * 60 + second;
                }
                obj.IsEnd = false;
                db.SaveChanges();
            }
            return(RedirectToAction("ManagerTest"));
        }