Exemple #1
0
        //public async Task<IActionResult> AttemptQuiz(int? id,int? comp) f**k off
        //{
        //    if (id == null)
        //    {
        //        return NotFound();
        //    }
        //    var quiz = await _context.Quiz
        //        .Include(q => q.Questions)
        //        .ThenInclude(q => q.Answers)
        //        .FirstOrDefaultAsync(e => e.ID == id);
        //    if (quiz == null)
        //    {
        //        return NotFound();
        //    }

        //    string quizData = Newtonsoft.Json.JsonConvert.SerializeObject(quiz, new Newtonsoft.Json.JsonSerializerSettings()
        //    {
        //        PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects,
        //        Formatting = Newtonsoft.Json.Formatting.Indented
        //    });
        //    ViewBag.jsonQuiz = (quizData);
        //    return View();
        //}
        //GET:
        public async Task <IActionResult> AttemptQuiz2(int?id, int?comp, string vs)
        {
            if (id == null)
            {
                return(NotFound());
            }
            var quiz = await _context.Quiz
                       .Include(q => q.Questions)
                       .ThenInclude(q => q.Answers)
                       .FirstOrDefaultAsync(e => e.ID == id);

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

            AttemptQuizViewModel viewModel = new AttemptQuizViewModel();

            viewModel.fillFromQuiz(quiz);
            if (comp != null)
            {
                viewModel.Comp = comp.Value;
                var check = _context.Competitions.FirstOrDefault(x => x.ID == comp);
                if (check.Deadline < DateTime.Now)
                {
                    return(Forbid());
                }
            }
            if (vs != null && vs != "")
            {
                viewModel.vs = vs;
            }
            return(View(viewModel));
        }
        public async Task <ActionResult> Attempt(AttemptQuizViewModel model)
        {
            var quiz = QuizzesService.Instance.GetQuiz(model.QuizID);

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

            StudentQuiz studentQuiz = new StudentQuiz();

            studentQuiz.StudentID  = User.Identity.GetUserId();
            studentQuiz.QuizID     = quiz.ID;
            studentQuiz.StartedAt  = DateTime.Now;
            studentQuiz.ModifiedOn = DateTime.Now;

            if (await StudentQuizzesService.Instance.NewStudentQuiz(studentQuiz))
            {
                model.QuizType       = quiz.QuizType;
                model.StudentQuizID  = studentQuiz.ID;
                model.TotalQuestions = quiz.Questions.Count;
                model.Question       = quiz.Questions.FirstOrDefault();
                model.QuestionIndex  = 0;

                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
            {
                return(new HttpStatusCodeResult(500));
            }
        }
        public async Task <ActionResult> Attempt(AttemptQuizViewModel model)
        {
            var quiz = await db.Quizzes.FindAsync(model.QuizID);

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

            if (quiz.Questions.Count == 0)
            {
                return(HttpNotFound());
            }

            StudentQuiz studentQuiz = new StudentQuiz();

            studentQuiz.StudentID  = User.Identity.GetUserId();
            studentQuiz.Quiz       = quiz;
            studentQuiz.StartedAt  = DateTime.Now;
            studentQuiz.IsActive   = true;
            studentQuiz.ModifiedOn = DateTime.Now;

            db.StudentQuizzes.Add(studentQuiz);
            await db.SaveChangesAsync();

            model.StudentQuizID  = studentQuiz.ID;
            model.TotalQuestions = quiz.Questions.Count;
            model.Question       = quiz.Questions.Where(q => q.IsActive).FirstOrDefault();
            model.QuestionIndex  = 0;

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

            return(PartialView("QuizQuestion", 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));
                }
            }
        }