public void DeleteSurveyQuestionOption(SurveyQuestionOption option)
        {
            var query = from surveyquestionoption in db.SurveyQuestionOptions
                        select surveyquestionoption;

            query = query.Where(sqos => sqos.SurveyQuestionID.Equals(option.SurveyQuestionID));
            query = query.OrderBy("SortOrder", false);

            List<SurveyQuestionOption> surveyquestionoptions = query.ToList();

            bool found = false;
            foreach (SurveyQuestionOption sqo in surveyquestionoptions)
            {
                if (found)
                {
                    sqo.SortOrder -= 1;
                    db.Entry(sqo).State = EntityState.Modified;
                }
                if (sqo.SurveyQuestionOptionID == option.SurveyQuestionOptionID)
                {
                    found = true;
                    db.SurveyQuestionOptions.Remove(sqo);
                }
            }

            db.SaveChanges();
        }
        public void MoveSurveyQuestionOption(SurveyQuestionOption option, bool ismoveup)
        {
            var query = from surveyquestionoption in db.SurveyQuestionOptions
                        select surveyquestionoption;
            query = query.Where(sqs => sqs.SurveyQuestionID.Equals(option.SurveyQuestionID));
            query = query.OrderBy("SortOrder", false);

            List<SurveyQuestionOption> surveyquestionoptions = query.ToList();

            // Get the current and max sort orders
            int currentsortorder = option.SortOrder;
            int maxsortorder = 1;
            foreach (SurveyQuestionOption sqo in surveyquestionoptions)
            {
                if (sqo.SortOrder > maxsortorder)
                    maxsortorder = sqo.SortOrder;
            }

            // Adjust the appropriate sort orders
            foreach (SurveyQuestionOption sqo in surveyquestionoptions)
            {
                if (ismoveup)
                {
                    if (sqo.SurveyQuestionOptionID == option.SurveyQuestionOptionID) // move current question up
                    {
                        if (currentsortorder > 1)
                            option.SortOrder -= 1;
                    }
                    else // find the previous item and increment it
                    {
                        if (sqo.SortOrder == currentsortorder - 1)
                        {
                            sqo.SortOrder += 1;
                            db.Entry(sqo).State = EntityState.Modified;
                        }
                    }
                }
                else
                {
                    if (sqo.SurveyQuestionOptionID == option.SurveyQuestionOptionID) // move current question down
                    {
                        if (currentsortorder < maxsortorder)
                            option.SortOrder += 1;
                    }
                    else // find the next item and decrement it
                    {
                        if (sqo.SortOrder == currentsortorder + 1)
                        {
                            sqo.SortOrder -= 1;
                            db.Entry(sqo).State = EntityState.Modified;
                        }
                    }
                }
            }

            db.SaveChanges();
        }
        public ActionResult Create(int id, SurveyQuestionOption surveyquestionoption)
        {
            try
            {
                if (Session["UserAccountID"] == null)
                    return RedirectToAction("Validate", "Login");
                User user = (User)Session["User"];
                ViewData["LoginInfo"] = Utility.BuildUserAccountString(user.Username, Convert.ToString(Session["UserAccountName"]));
                if (user.IsAdmin)
                    ViewData["txtIsAdmin"] = "true";
                else
                    ViewData["txtIsAdmin"] = "false";

                // Get the survey id for redirection
                ISurveyQuestionRepository qrep = new EntitySurveyQuestionRepository();
                SurveyQuestion surveyquestion = qrep.GetSurveyQuestion(id);
                ViewData["SurveyID"] = surveyquestion.SurveyID;

                if (ModelState.IsValid)
                {
                    surveyquestionoption.SurveyQuestionID = id;
                    // Note: Proper sort order is applied in the repository

                    string validation = ValidateInput(surveyquestionoption);
                    if (!String.IsNullOrEmpty(validation))
                    {
                        ViewData["ValidationMessage"] = validation;
                        return View(surveyquestionoption);
                    }
                    else
                    {
                        repository.CreateSurveyQuestionOption(surveyquestionoption);

                        CommonMethods.CreateActivityLog((User)Session["User"], "SurveyQuestionOption", "Add",
                            "Added survey question option '" + surveyquestionoption.SurveyQuestionOptionText + "' - ID: " + surveyquestionoption.SurveyQuestionOptionID.ToString());

                        return RedirectToAction("Edit", "Survey", new { id = surveyquestion.SurveyID });
                    }
                }
                return View(surveyquestionoption);
            }
            catch (Exception ex)
            {
                Helpers.SetupApplicationError("SurveyQuestionOption", "Create POST", ex.Message);
                return RedirectToAction("Index", "ApplicationError");
            }
        }
        public void CreateSurveyQuestionOption(SurveyQuestionOption option)
        {
            // Get the maximum sort order
            var query = from surveyquestionoption in db.SurveyQuestionOptions
                        select surveyquestionoption;
            query = query.Where(sqos => sqos.SurveyQuestionID.Equals(option.SurveyQuestionID));
            query = query.OrderBy("SortOrder", true);

            List<SurveyQuestionOption> surveyquestionoptions = query.ToList();

            int maxsortorder = 0;
            if (surveyquestionoptions.Count > 0)
                maxsortorder = surveyquestionoptions[0].SortOrder;

            option.SortOrder = maxsortorder + 1;
            db.SurveyQuestionOptions.Add(option);
            db.SaveChanges();
        }
        private string ValidateInput(SurveyQuestionOption surveyquestionoption)
        {
            if (surveyquestionoption.SurveyQuestionID == 0)
                return "Survey Question ID is not valid.";

            if (String.IsNullOrEmpty(surveyquestionoption.SurveyQuestionOptionText))
                return "Survey Question Option Text is required.";

            return String.Empty;
        }
        //
        // Support Methods
        private SurveyQuestionOption CreateNewSurveyQuestionOption(int id)
        {
            SurveyQuestionOption surveyquestionoption = new SurveyQuestionOption();
            surveyquestionoption.SurveyQuestionOptionID = 0;
            surveyquestionoption.SurveyQuestionID = 0;
            surveyquestionoption.SurveyQuestionOptionText = String.Empty;
            surveyquestionoption.SortOrder = 1;

            return surveyquestionoption;
        }
 public void UpdateSurveyQuestionOption(SurveyQuestionOption surveyquestionoption)
 {
     db.Entry(surveyquestionoption).State = EntityState.Modified;
     db.SaveChanges();
 }