// GET: QuizTemplateAnswers
        public ActionResult Index(int questionId)
        {
            QuizTemplateQuestion question = db.QuizTemplateQuestions.Find(questionId);

            db.Entry(question).Collection(q => q.Answers).Load();
            return(View(question));
        }
        public ActionResult DeleteConfirmed(int id)
        {
            QuizTemplateQuestion quizTemplateQuestion = db.QuizTemplateQuestions.Find(id);

            quizTemplateQuestion.Answers.ToList().ForEach(q => db.QuizTemplateAnswers.Remove(q));
            db.QuizTemplateQuestions.Remove(quizTemplateQuestion);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
 public ActionResult Edit([Bind(Include = "ID,Text,CourseID")] QuizTemplateQuestion quizTemplateQuestion)
 {
     if (ModelState.IsValid)
     {
         db.Entry(quizTemplateQuestion).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index", new { courseId = quizTemplateQuestion.CourseID }));
     }
     ViewBag.CourseID = new SelectList(db.Courses, "ID", "Name", quizTemplateQuestion.CourseID);
     return(View(quizTemplateQuestion));
 }
        // GET: QuizTemplateQuestions/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            QuizTemplateQuestion quizTemplateQuestion = db.QuizTemplateQuestions.Find(id);

            if (quizTemplateQuestion == null)
            {
                return(HttpNotFound());
            }
            return(View(quizTemplateQuestion));
        }
        // GET: QuizTemplateQuestions/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            QuizTemplateQuestion quizTemplateQuestion = db.QuizTemplateQuestions.Find(id);

            if (quizTemplateQuestion == null)
            {
                return(HttpNotFound());
            }
            db.Entry(quizTemplateQuestion).Reference(c => c.Course).Load();
            return(View(quizTemplateQuestion));
        }
        public ActionResult Create(AddQuizTemplateQuestionViewModel view)
        {
            if (ModelState.IsValid)
            {
                QuizTemplateQuestion quizTemplateQuestion = new QuizTemplateQuestion()
                {
                    CourseID = view.CourseId,
                    Text     = view.Text
                };
                db.QuizTemplateQuestions.Add(quizTemplateQuestion);
                db.SaveChanges();
                return(RedirectToAction("Index", new { courseId = view.CourseId }));
            }

            return(View(view));
        }
        public ActionResult Create(SelectQuizQuestionsViewModel view)
        {
            if (ModelState.IsValid)
            {
                foreach (var q in view.SelectedQuestions)
                {
                    //Get the selected quiz template.
                    QuizTemplateQuestion temp = db.QuizTemplateQuestions.Find(Convert.ToInt32(q));
                    db.Entry(temp).Collection(a => a.Answers).Load();

                    //Copy question template to new quiz question object and save to database.
                    QuizQuestion quizQuestion = new QuizQuestion()
                    {
                        Text      = temp.Text,
                        SessionID = view.Session.ID
                    };
                    db.QuizQuestions.Add(quizQuestion);
                    db.SaveChanges();

                    //Copy template answers to new choice objects and save each to database.
                    foreach (var a in temp.Answers)
                    {
                        QuizChoice quizChoice = new QuizChoice()
                        {
                            Text       = a.Text,
                            QuestionID = quizQuestion.ID,
                            IsCorrect  = a.IsCorrect
                        };

                        db.QuizChoices.Add(quizChoice);
                    }
                    db.SaveChanges();
                }

                return(RedirectToAction("Index", "Questions", new { sessionId = view.Session.ID }));
            }

            return(View(view));
        }