Exemple #1
0
        public async static Task SeedQuizold(IQuizRepo quizRepo, IQuestionRepo questionRepo)
        {
            string[]         quizNames = { "Animals", "trivia", "geography" };
            List <QuizClass> quizzes   = new List <QuizClass>();

            foreach (var quizName in quizNames)
            {
                var quiz = await quizRepo.GetQuizByNameAsync(quizName);

                if (quiz == null)
                {
                    QuizClass newQuiz = new QuizClass()
                    {
                        Name        = quizName,
                        Difficulty  = 2,
                        Description = "A quiz about " + quizName
                    };
                    await quizRepo.Create(newQuiz);
                }
                quizzes.Add(quiz);
            }

            Question vraag1 = new Question()
            {
                Description = "hoeveel weegt pikachu"
            };
            Option option1 = new Option()
            {
                OptionDescription = "5 kg",
                CorrectAnswer     = true
            };
            Option option2 = new Option()
            {
                OptionDescription = "8 kg",
                CorrectAnswer     = false
            };

            List <Option> opties = new List <Option>();

            opties.Add(option1);
            opties.Add(option2);
            vraag1.PossibleOptions = opties;

            var question = await questionRepo.GetQuestionByDescriptionAsync(vraag1.Description);

            if (question == null)
            {
                await questionRepo.Create(vraag1);
            }
            await quizRepo.AddQuestionToQuizAsync(quizzes[0].QuizId, vraag1.QuestionId);
        }
        public async Task <ActionResult> CreateQuestion(IFormCollection collection, Question_VM model)
        {
            try
            {
                if (!ModelState.IsValid) //validatie error
                {
                    return(View(model));
                }
                ViewBag.quizId = model.QuizId;
                if (await quizRepo.GetQuizByIdAsync(model.QuizId) == null)
                {
                    return(Redirect("/Error/404"));
                }
                if (await questionRepo.GetQuestionByDescriptionAsync(model.Description) != null)
                {
                    ModelState.AddModelError("", "This description is already being used by another question");
                    return(View(model));
                }

                List <Option> options  = new List <Option>();
                int           corrects = 0;
                for (int i = 0; i < model.OptionDescriptions.Count(); i++)
                {
                    if (!string.IsNullOrWhiteSpace(model.OptionDescriptions[i]))
                    {
                        Option option = new Option()
                        {
                            OptionDescription = model.OptionDescriptions[i],
                            CorrectAnswer     = model.OptionAnswers[i]
                        };
                        options.Add(option);
                        if (model.OptionAnswers[i])
                        {
                            corrects += 1;
                        }
                    }
                }
                if (options.Count() < 2)
                {
                    ModelState.AddModelError("", "Add at least 2 Options");
                    return(View(model));
                }
                if (corrects == 0)
                {
                    ModelState.AddModelError("", "Add at least 1 right answer");
                    return(View(model));
                }

                Question question = new Question()
                {
                    Description     = model.Description,
                    PossibleOptions = options
                };
                if (model.ImageString != null)
                {
                    byte[] b;
                    using (BinaryReader br = new BinaryReader(model.ImageString.OpenReadStream()))
                    {
                        b = br.ReadBytes((int)model.ImageString.OpenReadStream().Length);
                        question.ImageData = b;
                    }
                }
                if (await questionRepo.Create(question) == null)
                {
                    throw new Exception("Couldn't create the question");
                }
                if (await quizRepo.AddQuestionToQuizAsync(model.QuizId, question.QuestionId) == null)
                {
                    throw new Exception("Couldn't add the question to the quiz");
                }
                return(RedirectToAction("Questions", new { id = model.QuizId }));
            }
            catch (Exception exc)
            {
                Console.WriteLine("Create is giving an error: " + exc.Message);
                ModelState.AddModelError("", "Create actie is failed try again");
                return(View());
            }
        }
Exemple #3
0
        public async static Task SeedQuiz(IQuizRepo quizRepo, IQuestionRepo questionRepo)
        {
            //quiz over Trivia aanmaken
            string quizName = "Trivia";

            if (await quizRepo.GetQuizByNameAsync(quizName) == null)
            {
                QuizClass newQuiz = new QuizClass()
                {
                    Name        = quizName,
                    Difficulty  = 5,
                    Description = "A quiz about all sorts of things"
                };
                await quizRepo.Create(newQuiz);

                List <Option> opties = new List <Option>();
                Question      vraag  = new Question()
                {
                    Description = "Which of the following items was owned by the fewest U.S. homes in 1990?"
                };
                if (await questionRepo.GetQuestionByDescriptionAsync(vraag.Description) == null)
                {
                    Option option1 = new Option()
                    {
                        OptionDescription = "home computer",
                        CorrectAnswer     = false
                    };
                    Option option2 = new Option()
                    {
                        OptionDescription = "compact disk player",
                        CorrectAnswer     = true
                    };
                    Option option3 = new Option()
                    {
                        OptionDescription = "dishwasher",
                        CorrectAnswer     = false
                    };
                    opties.Add(option1);
                    opties.Add(option2);
                    opties.Add(option3);
                    vraag.PossibleOptions = opties;
                    await questionRepo.Create(vraag);

                    await quizRepo.AddQuestionToQuizAsync(newQuiz.QuizId, vraag.QuestionId);
                }

                vraag = new Question()
                {
                    Description = "How tall is the tallest man on earth?"
                };
                if (await questionRepo.GetQuestionByDescriptionAsync(vraag.Description) == null)
                {
                    Option option1 = new Option()
                    {
                        OptionDescription = "2.72 m",
                        CorrectAnswer     = true
                    };
                    Option option2 = new Option()
                    {
                        OptionDescription = "2.64 m",
                        CorrectAnswer     = false
                    };
                    Option option3 = new Option()
                    {
                        OptionDescription = "3.05 m",
                        CorrectAnswer     = false
                    };
                    opties = new List <Option>();
                    opties.Add(option1);
                    opties.Add(option2);
                    opties.Add(option3);
                    vraag.PossibleOptions = opties;
                    await questionRepo.Create(vraag);

                    await quizRepo.AddQuestionToQuizAsync(newQuiz.QuizId, vraag.QuestionId);
                }
                vraag = new Question()
                {
                    Description = "Which racer holds the record for the most Grand Prix wins?"
                };
                if (await questionRepo.GetQuestionByDescriptionAsync(vraag.Description) == null)
                {
                    Option option1 = new Option()
                    {
                        OptionDescription = "Michael Schumacher",
                        CorrectAnswer     = true
                    };
                    Option option2 = new Option()
                    {
                        OptionDescription = "Mario Andretti",
                        CorrectAnswer     = false
                    };
                    Option option3 = new Option()
                    {
                        OptionDescription = "Lewis Hamilton",
                        CorrectAnswer     = false
                    };
                    opties = new List <Option>();
                    opties.Add(option1);
                    opties.Add(option2);
                    opties.Add(option3);
                    vraag.PossibleOptions = opties;
                    await questionRepo.Create(vraag);

                    await quizRepo.AddQuestionToQuizAsync(newQuiz.QuizId, vraag.QuestionId);
                }
            }
            return;
        }