public ActionResult AttemptDetails(int studentQuizID, bool?timerExpired, bool?isPartial = false)
        {
            var studentQuiz = StudentQuizzesService.Instance.GetStudentQuiz(studentQuizID);

            if (studentQuiz == null)
            {
                return(HttpNotFound());
            }

            StudentQuizViewModel model = new StudentQuizViewModel();

            model.TimerExpired = timerExpired.HasValue ? timerExpired.Value : false;

            model.PageInfo = new PageInfo()
            {
                PageTitle       = "Assessment Attempt Details",
                PageDescription = "Details of Attempted Assessment"
            };

            model.StudentQuiz = studentQuiz;

            if (isPartial.HasValue && isPartial.Value)
            {
                return(PartialView("_AttemptDetails", model));
            }
            else
            {
                return(View(model));
            }
        }
        public ActionResult PrintResult(int studentQuizID)
        {
            var studentQuiz = StudentQuizzesService.Instance.GetStudentQuiz(studentQuizID);

            if (studentQuiz == null)
            {
                return(HttpNotFound());
            }

            StudentQuizViewModel model = new StudentQuizViewModel();

            model.PageInfo = new PageInfo()
            {
                PageTitle       = "Quiz Attempt Details",
                PageDescription = "Details of Attempted Quiz"
            };

            model.StudentQuiz = studentQuiz;

            return(PartialView("_PrintResult", model));
        }
        public async Task <ActionResult> AttemptDetails(int attemptID)
        {
            var studentQuiz = await db.StudentQuizzes.FindAsync(attemptID);

            if (studentQuiz == null || !studentQuiz.IsActive)
            {
                return(HttpNotFound());
            }

            StudentQuizViewModel model = new StudentQuizViewModel();

            model.PageInfo = new PageInfo()
            {
                PageTitle       = "Quiz Attempt Details",
                PageDescription = "Details of Attempted Quiz"
            };

            model.StudentQuiz = studentQuiz;

            return(View(model));
        }
        public async Task <ActionResult> AnswerQuestion(AttemptQuizViewModel model)
        {
            Thread.Sleep(2000);

            var studentQuiz = StudentQuizzesService.Instance.GetStudentQuiz(model.StudentQuizID);

            if (studentQuiz == null)
            {
                return(new HttpStatusCodeResult(500));
            }

            if (model.TimerExpired)
            {
                studentQuiz.CompletedAt = DateTime.Now;
                studentQuiz.ModifiedOn  = DateTime.Now;

                if (await StudentQuizzesService.Instance.UpdateStudentQuiz(studentQuiz))
                {
                    StudentQuizViewModel studentQuizModel = new StudentQuizViewModel();

                    studentQuizModel.StudentQuiz  = studentQuiz;
                    studentQuizModel.TimerExpired = model.TimerExpired;

                    return(PartialView("_AttemptDetails", studentQuizModel));
                }
                else
                {
                    return(new HttpStatusCodeResult(500));
                }
            }
            else
            {
                var quiz = QuizzesService.Instance.GetQuiz(model.QuizID);

                if (quiz == null)
                {
                    return(HttpNotFound());
                }

                var question = QuestionsService.Instance.GetQuizQuestion(quiz.ID, model.QuestionID);

                if (question == null)
                {
                    return(HttpNotFound());
                }

                var selectedOptions = QuestionsService.Instance.GetOptionsByIDs(model.SelectedOptionIDs.CSVToListInt());

                if (selectedOptions == null)
                {
                    return(HttpNotFound());
                }

                AttemptedQuestion attemptedQuestion = new AttemptedQuestion();

                attemptedQuestion.StudentQuizID   = studentQuiz.ID;
                attemptedQuestion.QuestionID      = question.ID;
                attemptedQuestion.SelectedOptions = selectedOptions.Select(x => new AttemptedOption()
                {
                    AttemptedQuestionID = attemptedQuestion.ID, Option = x, OptionID = x.ID
                }).ToList();
                attemptedQuestion.Score      = Calculator.CalculateAttemptedQuestionScore(question.Options, attemptedQuestion.SelectedOptions);
                attemptedQuestion.AnsweredAt = DateTime.Now;
                attemptedQuestion.ModifiedOn = DateTime.Now;

                if (await StudentQuizzesService.Instance.NewAttemptedQuestion(attemptedQuestion))
                {
                    if (model.QuestionIndex != quiz.Questions.Count() - 1)
                    {
                        model.QuizType = quiz.QuizType;

                        model.TotalQuestions = quiz.Questions.Count;
                        model.Question       = quiz.Questions.ElementAtOrDefault(model.QuestionIndex + 1);
                        model.QuestionIndex  = model.QuestionIndex + 1;

                        model.Options = new List <Option>();
                        model.Options.AddRange(model.Question.Options);
                        model.Options.Shuffle();

                        model.EnableQuestionTimer = quiz.EnableQuestionTimer;
                        model.Seconds             = Calculator.CalculateAllowedQuestionTime(quiz);

                        return(PartialView("_QuizQuestion", model));
                    }
                    else //this was the Last question so display the result
                    {
                        studentQuiz.CompletedAt = DateTime.Now;

                        if (!await StudentQuizzesService.Instance.UpdateStudentQuiz(studentQuiz))
                        {
                            return(new HttpStatusCodeResult(500));
                        }

                        return(RedirectToAction("AttemptDetails", new { studentQuizID = studentQuiz.ID, isPartial = true, timerExpired = model.TimerExpired }));
                    }
                }
                else
                {
                    return(new HttpStatusCodeResult(500));
                }
            }
        }
        public async Task <ActionResult> AnswerQuestion(AttemptQuizViewModel model)
        {
            if (model.TimerExpired)
            {
                var studentQuiz = await db.StudentQuizzes.FindAsync(model.StudentQuizID);

                studentQuiz.CompletedAt = DateTime.Now;
                studentQuiz.ModifiedOn  = DateTime.Now;

                db.Entry(studentQuiz).State = System.Data.Entity.EntityState.Modified;
                await db.SaveChangesAsync();

                StudentQuizViewModel studentQuizModel = new StudentQuizViewModel();

                studentQuizModel.StudentQuiz  = studentQuiz;
                studentQuizModel.TimerExpired = model.TimerExpired;

                return(PartialView("AttemptDetailsPartial", studentQuizModel));
            }
            else
            {
                var quiz = await db.Quizzes.FindAsync(model.QuizID);

                if (quiz == null || !quiz.IsActive)
                {
                    return(HttpNotFound());
                }

                var question = quiz.Questions.Find(q => q.ID == model.QuestionID);

                if (question == null || !question.IsActive)
                {
                    return(HttpNotFound());
                }

                var selectedOption = question.Options.Find(o => o.ID == model.SelectedOptionID);

                if (selectedOption == null || !selectedOption.IsActive)
                {
                    return(HttpNotFound());
                }

                AttemptedQuestion attemptedQuestion = new AttemptedQuestion();

                attemptedQuestion.Question       = question;
                attemptedQuestion.SelectedOption = selectedOption;
                attemptedQuestion.AnsweredAt     = DateTime.Now;

                if (selectedOption.IsCorrect)
                {
                    attemptedQuestion.IsCorrect = true;
                }

                var studentQuiz = await db.StudentQuizzes.FindAsync(model.StudentQuizID);

                if (studentQuiz.AttemptedQuestions == null)
                {
                    studentQuiz.AttemptedQuestions = new List <AttemptedQuestion>();
                }

                attemptedQuestion.IsActive   = true;
                attemptedQuestion.ModifiedOn = DateTime.Now;

                studentQuiz.AttemptedQuestions.Add(attemptedQuestion);

                if (model.QuestionIndex == quiz.Questions.Count - 1)                 //this was the Last question
                {
                    studentQuiz.CompletedAt = DateTime.Now;
                }

                db.Entry(studentQuiz).State = System.Data.Entity.EntityState.Modified;
                await db.SaveChangesAsync();

                if (model.QuestionIndex != quiz.Questions.Where(q => q.IsActive).Count() - 1)
                {
                    model.TotalQuestions = quiz.Questions.Count;
                    model.Question       = quiz.Questions.Where(q => q.IsActive).ElementAtOrDefault(model.QuestionIndex + 1);
                    model.QuestionIndex  = model.QuestionIndex + 1;

                    model.Options = new List <Option>();
                    model.Options.AddRange(model.Question.Options.Where(o => o.IsActive));
                    model.Options.Shuffle();

                    return(PartialView("QuizQuestion", model));
                }
                else                 //this was the Last question so display the result
                {
                    StudentQuizViewModel studentQuizModel = new StudentQuizViewModel();

                    studentQuizModel.StudentQuiz  = studentQuiz;
                    studentQuizModel.TimerExpired = model.TimerExpired;

                    return(PartialView("AttemptDetailsPartial", studentQuizModel));
                }
            }
        }