Exemplo n.º 1
0
        public async Task <ActionResult> AttemptSDetails(int attemptID)
        {
            var studentQuiz = await db.StudentSurveys.FindAsync(attemptID);

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

            StudentSurveyViewModel model = new StudentSurveyViewModel();

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

            model.StudentSurvey = studentQuiz;

            return(View(model));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> AnswerSQuestion(AttemptSurveyViewModel model)
        {
            if (model.TimerExpired)
            {
                var studentQuiz = await db.StudentSurveys.FindAsync(model.StudentSurveyID);

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

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

                StudentSurveyViewModel studentQuizModel = new StudentSurveyViewModel();

                studentQuizModel.StudentSurvey = studentQuiz;
                studentQuizModel.TimerExpired  = model.TimerExpired;

                return(PartialView("AttemptSDetailsPartial", studentQuizModel));
            }
            else
            {
                var quiz = await db.Surveys.FindAsync(model.SurveyID);

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

                var question = quiz.SQuestions.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());
                }

                AttemptedSQuestion attemptedQuestion = new AttemptedSQuestion();

                attemptedQuestion.SQuestion      = question;
                attemptedQuestion.SelectedOption = selectedOption;
                attemptedQuestion.AnswerValue    = model.AnswerValue;
                attemptedQuestion.AnsweredAt     = DateTime.Now;

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

                var studentQuiz = await db.StudentSurveys.FindAsync(model.StudentSurveyID);

                if (studentQuiz.AttemptedSQuestions == null)
                {
                    studentQuiz.AttemptedSQuestions = new List <AttemptedSQuestion>();
                }

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

                studentQuiz.AttemptedSQuestions.Add(attemptedQuestion);

                if (model.QuestionIndex == quiz.SQuestions.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.SQuestions.Where(q => q.IsActive).Count() - 1)
                {
                    model.TotalQuestions = quiz.SQuestions.Count;
                    model.SQuestion      = quiz.SQuestions.Where(q => q.IsActive).ElementAtOrDefault(model.QuestionIndex + 1);
                    model.QuestionIndex  = model.QuestionIndex + 1;

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

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

                    studentQuizModel.StudentSurvey = studentQuiz;
                    studentQuizModel.TimerExpired  = model.TimerExpired;

                    //return PartialView("AttemptSDetailsPartial", studentQuizModel);
                    return(RedirectToAction("ThankYou", "HOme"));
                }
            }
        }