public QuestionOption GetQuestionOption(int questionOptionID)
 {
     using (var ctx = new QuizBuilderEntities())
     {
         return ctx.QuestionOptions.FirstOrDefault(qo => qo.QuestionOptionID == questionOptionID);
     }
 }
 public User GetUser(int userID)
 {
     using (var ctx = new QuizBuilderEntities())
     {
         return ctx.Users.FirstOrDefault(u => u.UserID == userID);
     }
 }
 public QuizAttempt GetQuizAttempt(int quizAttemptID)
 {
     using (var ctx = new QuizBuilderEntities())
     {
         return ctx.QuizAttempts.FirstOrDefault(qa => qa.QuizAttemptID == quizAttemptID);
     }
 }
 public Quiz GetQuiz(int quizID)
 {
     using (var ctx = new QuizBuilderEntities())
     {
         return ctx.Quizzes.FirstOrDefault(q => q.QuizID == quizID);
     }
 }
 public User GetUser(string username, string password)
 {
     using (var ctx = new QuizBuilderEntities())
     {
         return ctx.Users.FirstOrDefault(u => u.UserName == username && u.Password == password);
     }
 }
 public void SaveQuiz(Quiz quiz)
 {
     using (var ctx = new QuizBuilderEntities())
     {
         ctx.AddToQuizzes(quiz);
         ctx.SaveChanges();
     }
 }
 public void SaveUser(User user)
 {
     using (var ctx = new QuizBuilderEntities())
     {
         ctx.AddToUsers(user);
         ctx.SaveChanges();
     }
 }
 public void SaveQuizAttempt(QuizAttempt quizAttempt)
 {
     using (var ctx = new QuizBuilderEntities())
     {
         ctx.AddToQuizAttempts(quizAttempt);
         ctx.SaveChanges();
     }
 }
 public void SaveQuestionOption(QuestionOption questionOption)
 {
     using (var ctx = new QuizBuilderEntities())
     {
         ctx.AddToQuestionOptions(questionOption);
         ctx.SaveChanges();
     }
 }
 public List<Quiz> GetQuizzes()
 {
     using (var ctx = new QuizBuilderEntities())
     {
         return ctx.Quizzes
              .OrderBy(q => q.QuizName)
              .ToList();
     }
 }
 public List<QuizAttempt> GetQuizAttempts(int userID)
 {
     using (var ctx = new QuizBuilderEntities())
     {
         return ctx.QuizAttempts
          .Where(qa => qa.UserID == userID)
          .OrderBy(qa => qa.QuizDate)
          .ToList();
     }
 }
 public List<QuestionOption> GetQuestionOptions(int questionID)
 {
     using (var ctx = new QuizBuilderEntities())
     {
         return ctx.QuestionOptions
            .Where(qo => qo.QuestionID == questionID)
            .OrderBy(qo => qo.OptionSequence)
            .ToList();
     }
 }
 public List<User> GetUsers()
 {
     using (var ctx = new QuizBuilderEntities())
     {
         return ctx.Users
            .OrderBy(u => u.LastName)
            .OrderBy(u => u.FirstName)
            .ToList();
     }
 }
        //added 4/10/2013
        public Question GetLast(int scenarioID)
        {
            using (var ctx = new QuizBuilderEntities())
            {
                var question = ctx.Questions
                    .Where(s => s.ScenarioID == scenarioID)
                    .OrderByDescending(s => s.QuestionSequence)
                    .FirstOrDefault();

                return question;
            }
        }
        //added 4/10/2013
        public Scenario GetLast(int quizID)
        {
            using (var ctx = new QuizBuilderEntities())
            {
                var scenario = ctx.Scenarios
                    .Where(s => s.QuizID == quizID)
                    .OrderByDescending(s => s.ScenarioSequence)
                    .FirstOrDefault();

                return scenario;
            }
        }
        public QuestionResponse GetLastQuestionResponse(int quizAttemptID)
        {
            using (var ctx = new QuizBuilderEntities())
            {
                return ctx.QuestionResponses
                  .Where(qr => qr.QuizAttemptID == quizAttemptID)
                  .OrderByDescending(qr => qr.Question.Scenario.ScenarioSequence)
                  .ThenByDescending(qr => qr.Question.QuestionSequence)
                  .FirstOrDefault();

            }
        }
        //added 4/10/2013
        public QuestionOption GetLast(int questionID)
        {
            using (var ctx = new QuizBuilderEntities())
            {
                var questionOption = ctx.QuestionOptions
                    .Where(q => q.QuestionID == questionID)
                    .OrderByDescending(o => o.OptionSequence)
                    .FirstOrDefault();

                return questionOption;
            }
        }
        public void DeleteQuiz(int quizID)
        {
            using (var ctx = new QuizBuilderEntities())
            {
                var quiz = ctx.Quizzes.FirstOrDefault(q => q.QuizID == quizID);

                if (quiz != null)
                {
                    ctx.DeleteObject(quiz);
                    ctx.SaveChanges();
                }
            }
        }
        public void DeleteQuestionResponse(int questionResponseID)
        {
            using (var ctx = new QuizBuilderEntities())
            {
                var questionResponse = ctx.QuestionResponses.FirstOrDefault(qr => qr.QuestionResponseID == questionResponseID);

                if (questionResponse != null)
                {
                    ctx.DeleteObject(questionResponse);
                    ctx.SaveChanges();
                }
            }
        }
        public void DeleteQuizAttempt(int quizAttemptID)
        {
            using (var ctx = new QuizBuilderEntities())
            {
                var quizAttempt = ctx.QuizAttempts.FirstOrDefault(qa => qa.QuizAttemptID == quizAttemptID);

                if (quizAttempt != null)
                {
                    ctx.DeleteObject(quizAttempt);
                    ctx.SaveChanges();
                }
            }
        }
        public void DeleteScenario(int scenarioID)
        {
            using (var ctx = new QuizBuilderEntities())
            {
                var scenario = ctx.Scenarios.FirstOrDefault(s => s.ScenarioID == scenarioID);

                if (scenario != null)
                {
                    ctx.DeleteObject(scenario);
                    ctx.SaveChanges();
                }
            }
        }
        public void DeleteUser(int userID)
        {
            using (var ctx = new QuizBuilderEntities())
            {
                var user = ctx.Users.FirstOrDefault(u => u.UserID == userID);

                if (user != null)
                {
                    ctx.DeleteObject(user);
                    ctx.SaveChanges();
                }
            }
        }
        public void DeleteQuestion(int questionID)
        {
            using (var ctx = new QuizBuilderEntities())
            {
                var question = ctx.Questions.FirstOrDefault(q => q.QuestionID == questionID);

                if (question != null)
                {
                    ctx.DeleteObject(question);
                    ctx.SaveChanges();
                }
            }
        }
 public void UpdateQuizAttempt(QuizAttempt quizAttempt)
 {
     using (var ctx = new QuizBuilderEntities())
     {
         ctx.Attach(quizAttempt);
         var stateEntry = ctx.ObjectStateManager.GetObjectStateEntry(quizAttempt);
         foreach (var propertyName in stateEntry.CurrentValues
                              .DataRecordInfo.FieldMetadata
                              .Select(fm => fm.FieldType.Name))
         {
             stateEntry.SetModifiedProperty(propertyName);
         }
         ctx.SaveChanges();
     }
 }
    protected void btnAddQuestion_Click(object sender, EventArgs e)
    {
        using (QuizBuilderEntities QBContext = new QuizBuilderEntities())
                {
                    Question question = new Question();
                    question.ScenarioID = Convert.ToInt32(txtNewQScenarioID.Text);
                    question.QuestionSequence = Convert.ToInt32(txtNewQSequenceNumb.Text);
                    question.QuestionText = txtNewQTxt.Text;

                    QuestionRepository qR = new QuestionRepository();
                    qR.SaveQuestion(question);

            /*Reload page so gridview will display new scenario */
            Response.Redirect("Admin_Quiz.aspx");
        }

            clearQuestionInputControls();
    }
    protected void submitNewUserInfo_Click(object sender, EventArgs e)
    {
        using (QuizBuilderEntities QBContext = new QuizBuilderEntities())
        {
            User obj = new User();
            obj.FirstName = txtFirstName.Text;
            obj.LastName = txtLastName.Text;
            obj.UserName = txtUserName.Text;
            obj.Password = txtPassword.Text;
            obj.Email = txtEmail.Text;

            UserRepository uR = new UserRepository();
            uR.SaveUser(obj);

            Response.Redirect("Admin_Users.aspx");
        }

        clearInputControls();
    }
    protected void btnAddScenario_Click(object sender, EventArgs e)
    {
        using (QuizBuilderEntities QBContext = new QuizBuilderEntities())
        {
            Scenario scenario = new Scenario();
            scenario.ScenarioName = txtNewScenarioTxt.Text;
            scenario.ScenarioSequence = Convert.ToInt32(txtNewScenarioSeq.Text);
            scenario.ScenarioText = txtNewScenarioTxt.Text;

            ScenarioRepository sR = new ScenarioRepository();
            sR.SaveScenario(scenario);

            /*Reload page so gridview will display new scenario */
            Response.Redirect("Admin_Quiz.aspx");
        }

            clearScenarioInputControls();
            PanelScenario.Visible = false;
    }
        public Scenario GetPrevious(int scenarioID)
        {
            using (var ctx = new QuizBuilderEntities())
            {
                Scenario previousScenario = null;

                //fetch the scenario
                var currentScenario = ctx.Scenarios.FirstOrDefault(s => s.ScenarioID == scenarioID);
                if (currentScenario != null)
                {
                    //get the collection of scenarios based on the quiz
                    var quizScenarios = GetScenarios(currentScenario.QuizID);
                    //find this scenario in the collection
                    var current = quizScenarios.First(s => s.ScenarioID == scenarioID);
                    //is there a previous scenario? If not - return null
                    previousScenario = quizScenarios.ElementAtOrDefault(quizScenarios.IndexOf(current) - 1);
                }
                return previousScenario;
            }
        }
        public Question GetPrevious(int questionID)
        {
            using (var ctx = new QuizBuilderEntities())
            {
                Question previousQuestion = null;

                //fetch the question
                var currentQuestion = ctx.Questions.FirstOrDefault(q => q.QuestionID == questionID);
                if (currentQuestion != null)
                {
                    //get the collection of questions based on the scenario
                    var quizQuestions = GetQuestions(currentQuestion.ScenarioID);
                    //find this question in the collection
                    var current = quizQuestions.First(q => q.QuestionID == questionID);
                    //is there a previous question? If not - return null
                    previousQuestion = quizQuestions.ElementAtOrDefault(quizQuestions.IndexOf(current) - 1);
                }
                return previousQuestion;
            }
        }
        //added 2/29/2012
        public QuestionResponse GetPrevious(int questionResponseID)
        {
            using (var ctx = new QuizBuilderEntities())
            {
                QuestionResponse previousQuestionResponse = null;

                //fetch the questionResponse
                var currentQuestionResponse = ctx.QuestionResponses.FirstOrDefault(q => q.QuestionResponseID == questionResponseID);
                if (currentQuestionResponse != null)
                {
                    //get the collection of questionResponses based on the scenario
                    var quizQuestionResponses = GetQuestionResponses(currentQuestionResponse.QuizAttemptID);
                    //find this questionResponse in the collection
                    var current = quizQuestionResponses.First(q => q.QuestionResponseID == questionResponseID);
                    //is there a previous questionResponse? If not - return null
                    previousQuestionResponse = quizQuestionResponses.ElementAtOrDefault(quizQuestionResponses.IndexOf(current) - 1);
                }
                return previousQuestionResponse;
            }
        }