/// <summary>
        /// Returns all the exam Questions
        /// This includes everything attached to an exam Questions
        /// such as all the categories
        /// and all the Questions attached to that exam Question
        /// And all the answes attached to the Questions.
        /// </summary>
        /// <returns></returns>
        public List <ExamQuestion> GetAllExamQuestion()
        {
            AutoMapperConfiguration.Configure();
            List <ExamQuestion> result = new List <ExamQuestion>();


            var dbExamQuestion = db.ExamQuestion.ToList();
            var dbCategories   = db.Categories.ToList();
            var dbSubtopic     = db.Subtopic.ToList();
            var dbCatSub       = db.Categories_Subtopic.ToList();
            var dbQuestExam    = db.ExamQuestionList.ToList();
            var dbQuestion     = db.Question.ToList();



            for (int i = 0; i < dbExamQuestion.Count; i++)
            {
                ExamQuestion question = new ExamQuestion();
                question.ExamQuestionID                = dbExamQuestion.ElementAt(i).ExamQuestionID;
                question.ExamQuestionName              = dbExamQuestion.ElementAt(i).ExamQuestionName;
                question.PKID                          = dbExamQuestion.ElementAt(i).PKID;
                question.QuestionType.PKID             = dbExamQuestion.ElementAt(i).QuestionTypeID;
                question.QuestionType.QuestionTypeName = dbExamQuestion.ElementAt(i).QuestionType.QuestionTypeName;
                for (int j = 0; j < dbExamQuestion.ElementAt(i).ExamQuestion_Categories.Count; j++)
                {
                    Category cat = new Category();


                    cat.Categories_ID   = dbExamQuestion.ElementAt(i).ExamQuestion_Categories.ElementAt(j).Categories_ID;
                    cat.Categories_Name = dbCategories.Where(s => s.Categories_ID == dbExamQuestion.ElementAt(i).ExamQuestion_Categories.ElementAt(j).Categories_ID).First().Categories_Name;

                    List <int> listofSub = dbCatSub.Where(s => s.Categories_ID == cat.Categories_ID).Select(s => s.Subtopic_ID).ToList();
                    for (int k = 0; k < listofSub.Count(); k++)
                    {
                        var      subtopic = dbSubtopic.Where(s => s.Subtopic_ID == listofSub.ElementAt(k));
                        SubTopic sub      = new SubTopic();
                        sub.Subtopic_ID   = subtopic.ElementAt(0).Subtopic_ID;
                        sub.Subtopic_Name = subtopic.ElementAt(0).Subtopic_Name;
                        cat.subtopics.Add(sub);
                    }
                    question.ExamQuestion_Categories.Add(cat);
                }

                List <int>   QuestionIDs = dbQuestExam.Where(s => s.ExamQuestionID == question.ExamQuestionID).Select(c => c.QuestionID).ToList();
                EAD.Question tempQuestion;

                for (int j = 0; j < QuestionIDs.Count; j++)
                {
                    tempQuestion = dbQuestion.Where(s => s.PKID == QuestionIDs.ElementAt(j)).First();
                    Question newQuest = new Question();
                    newQuest.PKID        = tempQuestion.PKID;
                    newQuest.Description = tempQuestion.Description;
                    newQuest.Answers     = GetAnswersQuestion(newQuest.PKID);
                    question.quest.Add(newQuest);
                }
                result.Add(question);
            }

            return(result);
        }
        /// <summary>
        /// Given an EAD ExamQuestion it will convert the Exam Question
        /// into a DTO exam question.
        /// </summary>
        /// <param name="ExamQuestion"></param>
        /// <returns></returns>
        public ExamQuestion getExamQuestion(EAD.ExamQuestion ExamQuestion)
        {
            AutoMapperConfiguration.Configure();
            ExamQuestion ExamQ = new ExamQuestion();

            ExamQ.ExamQuestionID                = ExamQuestion.ExamQuestionID;
            ExamQ.ExamQuestionName              = ExamQuestion.ExamQuestionName;
            ExamQ.PKID                          = ExamQuestion.PKID;
            ExamQ.QuestionType.PKID             = ExamQuestion.QuestionType.PKID;
            ExamQ.QuestionType.QuestionTypeName = ExamQuestion.QuestionType.QuestionTypeName;
            return(ExamQ);
        }
        public void AddExamQuestion(ExamQuestion examQuestion)    // Adds an exam question using a whole exam question object/model/EAD
        {
            if (examQuestion == null)                             //Exam Question does it exist
            {
                throw new ArgumentNullException("Exam Question"); //You passed in a empty exam Question Object
            }
            AutoMapperConfiguration.Configure();
            EAD.ExamQuestion DALExamQuestion = new EAD.ExamQuestion();
            DALExamQuestion.ExamQuestionID   = examQuestion.ExamQuestionID;
            DALExamQuestion.ExamQuestionName = examQuestion.ExamQuestionName;
            DALExamQuestion.QuestionTypeID   = examQuestion.QuestionType.PKID;

            db.ExamQuestion.Add(DALExamQuestion);
            db.SaveChanges();
            foreach (var subquestion in examQuestion.quest)
            {
                EAD.Question         questiontoAdd       = new EAD.Question();
                EAD.ExamQuestionList questioncombination = new EAD.ExamQuestionList();
                questiontoAdd.Description = subquestion.Description;
                db.Question.Add(questiontoAdd);
                db.SaveChanges();


                questioncombination.ExamQuestionID = DALExamQuestion.ExamQuestionID;
                questioncombination.QuestionID     = questiontoAdd.PKID;


                //adds to subquestion table

                db.ExamQuestionList.Add(questioncombination); //adds to subquestion/examquestion junction table

                foreach (var answer in subquestion.Answers)
                {
                    EAD.Answer answertoAdd = new EAD.Answer();
                    answertoAdd.Answer1           = answer.Answer1;
                    answertoAdd.AddLanguageTypeID = 1; //Change the Language type to 1
                    EAD.QuestionAnswers answercombination = new EAD.QuestionAnswers();
                    db.Answer.Add(answertoAdd);        // adds answer to answer Table
                    db.SaveChanges();
                    answercombination.AnswerID   = answertoAdd.PKID;
                    answercombination.QuestionID = questiontoAdd.PKID;
                    answercombination.IsCorrect  = answer.correct.isCorrect;


                    db.QuestionAnswers.Add(answercombination); // Add the QuestionAnswers junction table row
                    db.SaveChanges();
                }
            }
        }
        /// <summary>
        /// Returns a full exam Template
        /// which inclueds the Exam Template itself
        /// as well as all the exam Questions attached to that exam.
        /// The exam Questions will have multiple questions attached to them.
        /// Exam Questions Can have multiple categoreis attached to them.
        /// Each question has all the answers attached to it.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ExamTemplate getExamTemplate(String id)
        {
            AutoMapperConfiguration.Configure();

            List <EAD.ExamTemplate> ExamTemplate = db.ExamTemplate.Where(s => s.ExamTemplateID == id).ToList();
            ExamTemplate            exam         = ExamAction.getExam(id);


            List <EAD.ExamTemplateQuestions> ExamQuestions = db.ExamTemplateQuestions.Where(s => s.ExamTemplateID == exam.ExamTemplateID).ToList();

            if (ExamQuestions.Count < 1)
            {
                return(exam);
            }
            else
            {
                var dbExamQuestion     = db.ExamQuestion.ToList();
                var dbExamQuestionList = db.ExamQuestionList.ToList();
                var dbQuestionAnswers  = db.QuestionAnswers.ToList();
                var dbquestion         = db.Question.ToList();
                var dbanswer           = db.Answer.ToList();
                var dbCategories       = db.Categories.ToList();
                var dbSubtopics        = db.Subtopic.ToList();

                for (int i = 0; i < ExamQuestions.Count(); i++)
                {
                    List <EAD.ExamQuestion> ExamQuestion = dbExamQuestion.Where(s => s.ExamQuestionID == ExamQuestions.ElementAt(i).ExamQuestionID).ToList();


                    ExamQuestion ExamQ = new ExamQuestion();

                    ExamQ.ExamQuestionID                = ExamQuestion.ElementAt(0).ExamQuestionID;
                    ExamQ.ExamQuestionName              = ExamQuestion.ElementAt(0).ExamQuestionName;
                    ExamQ.PKID                          = ExamQuestion.ElementAt(0).PKID;
                    ExamQ.QuestionType.PKID             = ExamQuestion.ElementAt(0).QuestionType.PKID;
                    ExamQ.QuestionType.QuestionTypeName = ExamQuestion.ElementAt(0).QuestionType.QuestionTypeName;
                    var             categoryIDs     = ExamQuestion.ElementAt(0).ExamQuestion_Categories.Select(x => x.Categories_ID).ToList();
                    List <Category> ExamQCategories = new List <Category>();

                    foreach (var item in categoryIDs)
                    {
                        Category newCategory = new Category();
                        newCategory = Mapper.Map <Category>(dbCategories.Where(x => x.Categories_ID == item).First());
                        var subtopicIDS = dbCategories.Where(x => x.Categories_ID == item).First().Categories_Subtopic.Select(x => x.Subtopic_ID).ToList();
                        foreach (var subID in subtopicIDS)
                        {
                            SubTopic newSubtopic = new SubTopic();
                            newSubtopic = Mapper.Map <SubTopic>(dbSubtopics.Where(x => x.Subtopic_ID == subID).First());
                            newCategory.subtopics.Add(newSubtopic);
                        }
                        ExamQCategories.Add(newCategory);
                    }
                    ExamQ.ExamQuestion_Categories = ExamQCategories;
                    var ExamQuestionList = dbExamQuestionList.Where(s => s.ExamQuestionID == ExamQ.ExamQuestionID).ToList();
                    for (int j = 0; j < ExamQuestionList.Count; j++)
                    {
                        int tempID = ExamQuestionList.ElementAt(j).QuestionID;

                        List <EAD.Question> Question = dbquestion.Where(s => s.PKID == tempID).ToList();
                        Question            quest    = new Question();
                        quest.PKID        = Question.ElementAt(0).PKID;
                        quest.Description = Question.ElementAt(0).Description;
                        List <EAD.QuestionAnswers> AnswersID = dbQuestionAnswers.Where(s => s.QuestionID == quest.PKID).ToList();
                        for (int k = 0; k < AnswersID.Count; k++)
                        {
                            int answer    = AnswersID.ElementAt(k).AnswerID;
                            var TheAnswer = from tempAnswer in dbanswer
                                            where tempAnswer.PKID == answer
                                            select tempAnswer;
                            Answers ans = new Answers();
                            ans.PKID              = TheAnswer.ToArray()[0].PKID;
                            ans.Answer1           = TheAnswer.FirstOrDefault().Answer1;
                            ans.correct.isCorrect = AnswersID.ElementAt(k).IsCorrect;

                            quest.Answers.Add(ans);
                        }
                        ExamQ.quest.Add(quest);
                        exam.ExamQuestions.Add(ExamQ);
                    }
                }
                return(exam);
            }
        }