Ejemplo n.º 1
0
        // GET: Play
        public async Task <ActionResult> IndexAsync(string Id)
        {
            if (Id == null)
            {
                return(RedirectToAction("Index"));
            }
            try
            {
                ////get quiz
                var quiz = await quizRepo.GetQuizByIdAsync(Guid.Parse(Id));

                var EvalVm = new EvaluationVM();
                EvalVm.QuizId   = quiz.QuizID.ToString();
                EvalVm.QuizName = quiz.QuizName;

                ////vragen ophalen
                var questions = await questionRepo.GetQuestionsByQuizAsync(quiz.QuizID);

                foreach (var question in questions)
                {
                    //antwoord ophalen
                    Answer answer = await answerRepo.GetAnswerByQuestionAsync(question.QuestionId);

                    var q = new QuestionVM()
                    {
                        QuestionID   = question.QuestionId.ToString(),
                        QuestionText = question.QuestionText,
                    };
                    q.Answers.Add(answer);

                    //choices toevoegen
                    IEnumerable <Choice> choices = await choiceRepo.GetChoicesAsync(question.QuestionId);

                    List <Choice> ListChoices = choices.ToList();
                    //overlopen choices
                    foreach (var c in ListChoices)
                    {
                        Answer choice = new Answer()
                        {
                            AnswerID   = c.ChoiceID,
                            AnswerText = c.ChoiceText,
                            QuestionID = c.QuestionID
                        };
                        q.Answers.Add(choice);
                        q.Answers = q.Answers.OrderBy(m => m.AnswerText).ToList();
                    }
                    EvalVm.Questions.Add(q);
                }
                return(View(EvalVm));
            }
            catch (Exception)
            {
                throw;
            }
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> DeleteQuiz(string Id, IFormCollection collection, Quiz quiz)
        {
            try
            {
                // TODO: Add update logic here
                if (Id == null)
                {
                    return(RedirectToAction("Quizzes"));
                }

                //delete quiz
                await quizRepo.DeleteQuiz(Guid.Parse(Id));

                //delete questions
                var questions = await questionRepo.GetQuestionsByQuizAsync(Guid.Parse(Id));

                foreach (var item in questions)
                {
                    //get answer
                    var answer = await answerRepo.GetAnswerByQuestionAsync(item.QuestionId);

                    //delete answer
                    await answerRepo.DeleteAnswer(answer.AnswerID);

                    //get choices
                    var choices = await choiceRepo.GetChoicesAsync(item.QuestionId);

                    foreach (var choice in choices)
                    {
                        await choiceRepo.DeleteChoice(choice.ChoiceID);
                    }
                }
                return(RedirectToAction("Quizzes"));
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"update error. {ex.Message}");
                ModelState.AddModelError("", "Update actie mislukt." + ex.InnerException.Message);
                return(View(quiz));
            }
        }