Example #1
0
 public ActionResult DeleteConfirmed(Guid id)
 {
     Data.Quiz quiz = db.Quizzes.Find(id);
     db.Quizzes.Remove(quiz);
     db.SaveChanges();
     return(RedirectToAction("Index"));
 }
Example #2
0
        public async Task <int> CreateAsync(string userId)
        {
            var rng = new Random();

            var quiz = new Data.Quiz {
                Finished = DateTime.Now
            };

            await this.db.Quizzes.AddAsync(quiz);

            var questions = this.db.Questions.ToArray();
            var questionsForCurrentQuiz = new List <Question>();

            for (int i = 0; i < 5; i++)
            {
                var question = questions[rng.Next(questions.Count())];
                while (questionsForCurrentQuiz.Any(q => q.Id == question.Id))
                {
                    question = questions[rng.Next(questions.Count())];
                }

                questionsForCurrentQuiz.Add(question);
                await this.db.QuestionQuizzes.AddAsync(new QuestionQuiz
                {
                    Question = question,
                    Quiz     = quiz
                });
            }

            await this.db.SaveChangesAsync();

            return(quiz.Id);
        }
Example #3
0
 public ActionResult Edit(Data.Quiz quiz)
 {
     if (ModelState.IsValid)
     {
         db.Entry(quiz).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(quiz));
 }
Example #4
0
        public ActionResult Create([Bind(Include = "Id,Name,Description")] Data.Quiz quiz)
        {
            if (ModelState.IsValid)
            {
                quiz.Id = Guid.NewGuid();
                db.Quizzes.Add(quiz);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(quiz));
        }
Example #5
0
 // GET: Admin/Quiz/Edit/5
 public ActionResult Edit(Guid?id)
 {
     if (id == null)
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
     Data.Quiz quiz = db.Quizzes.Find(id);
     if (quiz == null)
     {
         return(HttpNotFound());
     }
     return(View(quiz));
 }
Example #6
0
        public async Task <bool> CreateQuiz(Models.NewQuizModel newQuiz)
        {
            var quiz = new Data.Quiz
            {
                Title     = newQuiz.Title,
                Questions = new List <Data.Question>()
            };

            foreach (var question in newQuiz.NewQuestions)
            {
                var questionToAdd = new Data.Question
                {
                    QuestionString = question.Question,
                    CorrectAnswer  = new Answer {
                        AnswerText = question.CorrectAnswer
                    },
                    WrongAnswers = new WrongAnswers()
                    {
                        Answers = new List <Answer>()
                    }
                };

                foreach (var wrongAnswer in question.WrongAnswers)
                {
                    var wrongAnswerToAdd = new Answer
                    {
                        AnswerText = wrongAnswer
                    };

                    questionToAdd.WrongAnswers.Answers.Add(wrongAnswerToAdd);
                }
                ;

                quiz.Questions.Add(questionToAdd);
            }
            ;


            try
            {
                await _quizManagerContext.Quizzes.AddAsync(quiz);

                await _quizManagerContext.SaveChangesAsync();

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Example #7
0
        public ActionResult Questions(Guid?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Data.Quiz quiz = db.Quizzes.Find(id);
            if (quiz == null)
            {
                return(HttpNotFound());
            }

            var questions = new List <Question>();

            foreach (var quizSection in quiz.Sections)
            {
                questions.AddRange(quizSection.Questions.Select(quizQuestion => quizQuestion.Question));
            }



            return(View(questions));
        }