//Quizzes
        public async Task <IActionResult> Quizzes(string?exc)
        {
            if (exc != null)
            {
                ViewData["Exception"] = exc;
            }
            IEnumerable <Quiz> quizzes = null;

            quizzes = await quizRepo.GetAllQuizzesAsync();

            foreach (Quiz quiz in quizzes)
            {
                IEnumerable <Question> questionsEnum = await quizRepo.GetAllQuestionsByQuizId(quiz.Id);

                List <Question> questions = questionsEnum.ToList();
                quiz.Questions = questions;
            }
            return(View(quizzes));
        }
Beispiel #2
0
        public async Task <IActionResult> ShowQuestion()
        {
            IEnumerable <Question> questions = await quizRepo.GetAllQuestionsByQuizId(QuizController.QuizId);

            List <Question> questionsList  = questions.ToList();
            List <Question> wrongQuestions = new List <Question>();

            //Antwoorden checken of question inorde is
            foreach (Question q in questionsList)
            {
                IEnumerable <Answer> answers = await quizRepo.GetAllAnswersByQuestionId(q.Id);

                bool error = false;
                if (answers.Count() < 2)
                {
                    error = true;
                }
                else
                {
                    int  countTrues       = 0;
                    bool onlyDiscriptions = true;
                    bool onlyPhotos       = true;
                    foreach (Answer answer in answers)
                    {
                        if (answer.Correct.ToString() == "True")
                        {
                            countTrues = countTrues + 1;
                        }
                        if (answer.Description == null)
                        {
                            onlyDiscriptions = false;
                        }
                        if (answer.FotoURL == null)
                        {
                            onlyPhotos = false;
                        }
                    }
                    if (!onlyDiscriptions && !onlyPhotos)
                    {
                        error = true;
                    }
                    if (countTrues == 0)
                    {
                        error = true;
                    }
                    else if (countTrues > 1)
                    {
                        error = true;
                    }
                }
                if (error)
                {
                    wrongQuestions.Add(q);
                }
            }

            foreach (Question q in wrongQuestions)
            {
                questionsList.Remove(q);
            }

            double progressPercentage = Math.Floor((double.Parse(QuizController.currentQuestion.ToString()) / float.Parse(questionsList.Count().ToString())) * 100);

            ViewData["ProgressPercentage"] = progressPercentage;
            ViewData["PotentialScore"]     = QuizController.potentialScore;
            ViewData["Score"] = QuizController.score;
            if (QuizController.currentQuestion == questionsList.Count())
            {
                return(RedirectToAction(nameof(EndQuiz)));
            }
            Question question = await quizRepo.GetQuestionForIdAsync(questionsList[QuizController.currentQuestion].Id);

            return(View(question));
        }