/// <summary> /// Given an id of an exam template it returns /// an exam template EAD variable. /// </summary> /// <param name="id"></param> /// <returns></returns> public ExamTemplate getExam(string id) { db = new EAD.ExamAssessmentEntities(); List <EAD.ExamTemplate> ExamTemplate = db.ExamTemplate.Where(s => s.ExamTemplateID == id).ToList(); ExamTemplate exam = new ExamTemplate(); exam.PKID = ExamTemplate.First().PKID; exam.CreatedDate = ExamTemplate.First().CreatedDate; exam.ExamTemplateName = ExamTemplate.First().ExamTemplateName; exam.ExamTemplateID = ExamTemplate.FirstOrDefault().ExamTemplateID; exam.ExamType.PKID = ExamTemplate.FirstOrDefault().ExamType.PKID; exam.ExamType.ExamTypeName = ExamTemplate.FirstOrDefault().ExamType.ExamTypeName; return(exam); }
/// <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); } }