public ActionResult displayAnswerSection(FormCollection postedData) { //ViewBag.Message = "This is the [POST] Method"; // Rebuild the Question Object for reuse int id = Convert.ToInt32(postedData["question_id"]); int newSelIndex = -1; try { newSelIndex = Convert.ToInt16(postedData["AnswerTypesDDL"]); } catch { } OBSQuestion obsQuestion = new OBSQuestion(id); //set the ddl to the new index value based on the posted form obsQuestion.fullAnswerTypeDDL.Clear(); obsQuestion.setAnswerTypeDDL((short)newSelIndex); if (postedData["save"].Equals("true")) { try { obsQuestion.selectedAT.selAnsList = assign_new_selAnsList_to_OBSQuestion(postedData["userSelAnsList"]); } catch { } SaveDefaultAnswerType(obsQuestion); obsQuestion = new OBSQuestion(id); } obsQuestion.templates = obsQuestion.getTemplates(obsQuestion.selectedAT.ATcathegory); return View(obsQuestion); }
public void SaveDefaultAnswerType(OBSQuestion obsQuestion) { int selected_ans_type_id = obsQuestion.selectedAT.ATid; int question_id = obsQuestion.questionId; //string default_sel_ans_types = formData["default_selected_ans_types"]; //first lets check if user submitted None as a default answer type if (selected_ans_type_id < 1) { OBS_QUEST_ANS_TYPES oBS_QUEST_ANS_TYPES = new OBS_QUEST_ANS_TYPES(); try { oBS_QUEST_ANS_TYPES = db.OBS_QUEST_ANS_TYPES.Single(item => item.obs_question_id == question_id && item.obs_qat_default_ans_type_yn == "Y"); oBS_QUEST_ANS_TYPES.obs_qat_default_ans_type_yn = "N"; db.SaveChanges(); } catch { } }//end if (String.IsNullOrEmpty(answer_type_id)) else //if we're here, that means user submtted answer type that <>None { //now we need to check if this question/answer type combination already exist in obs_quest_ans_type table if (!obsQuestion.hasInstances) // oBS_QUEST_ANS_TYPES Table does not have Any records { //if we're here, that means we need to insert a new record in OBS_QUEST_ANS_TYPE table //first we need to check if this selected answer type requires a record in OBS_QUEST_SLCT_ANS if (isQuest_Slct_Ans_Required(selected_ans_type_id)) { // oBS_QUEST_ANS_TYPES Table does not have Any records and The Seacted Ans Type requires a record in OBS_QUEST_SLCT_ANS //so we are here, that means we need to save both OBS_QUEST_ANS_TYPE and OBS_QUEST_SLCT_ANS //-- First Insert record into 'OBS_QUEST_ANS_TYPES' Table OBS_QUEST_ANS_TYPES oBS_QUEST_ANS_TYPES = new OBS_QUEST_ANS_TYPES(); oBS_QUEST_ANS_TYPES.obs_question_id = question_id; oBS_QUEST_ANS_TYPES.obs_ans_type_id = (short)selected_ans_type_id; oBS_QUEST_ANS_TYPES.obs_qat_default_ans_type_yn = "Y"; //oBS_QUEST_ANS_TYPES.obs_qat_end_eff_dt = Convert.ToDateTime("12/31/2060"); db.OBS_QUEST_ANS_TYPES.Add(oBS_QUEST_ANS_TYPES); db.SaveChanges(); //-- Second Insert record into 'OBS_QUEST_SLCT_ANS' Table //short temp_selected_ans_type_id = (short)selected_ans_type_id; int createdQAT_id = db.OBS_QUEST_ANS_TYPES.SingleOrDefault(item => item.obs_ans_type_id == obsQuestion.selectedAT.ATid && item.obs_question_id == question_id).obs_qat_id; short order = 1; foreach (string str in obsQuestion.selectedAT.selAnsList) { OBS_QUEST_SLCT_ANS oBS_QUEST_SLCT_ANS = new OBS_QUEST_SLCT_ANS(); oBS_QUEST_SLCT_ANS.obs_qat_id = createdQAT_id; oBS_QUEST_SLCT_ANS.obs_qsa_text = str; oBS_QUEST_SLCT_ANS.obs_qsa_order = order; oBS_QUEST_SLCT_ANS.obs_qsa_wt = order; oBS_QUEST_SLCT_ANS.obs_qsa_dflt_yn = "N"; oBS_QUEST_SLCT_ANS.obs_qsa_eff_st_dt = DateTime.Now; oBS_QUEST_SLCT_ANS.obs_qsa_eff_end_dt = Convert.ToDateTime("12/31/2060"); db.OBS_QUEST_SLCT_ANS.Add(oBS_QUEST_SLCT_ANS); order++; } db.SaveChanges(); }//end of if (isQuest_Slct_Ans_Required(selected_ans_type_id)) else { //-- Insert record into 'OBS_QUEST_ANS_TYPES' Table OBS_QUEST_ANS_TYPES oBS_QUEST_ANS_TYPES = new OBS_QUEST_ANS_TYPES(); oBS_QUEST_ANS_TYPES.obs_question_id = question_id; oBS_QUEST_ANS_TYPES.obs_ans_type_id = (short)selected_ans_type_id; oBS_QUEST_ANS_TYPES.obs_qat_default_ans_type_yn = "Y"; // oBS_QUEST_ANS_TYPES.obs_qat_end_eff_dt = Convert.ToDateTime("12/31/2060"); db.OBS_QUEST_ANS_TYPES.Add(oBS_QUEST_ANS_TYPES); db.SaveChanges(); } }// End of if (!obsQuestion.hasInstances) //if (isNew_Quest_Ans_Type(selected_ans_type_id, question_id)) else if (obsQuestion.indexOfDefaultQA >= 0 &&(obsQuestion.selectedAT.ATid ==obsQuestion.OBSQA_List[obsQuestion.indexOfDefaultQA].answerTypeId)&& obsQuestion.selectedAT.requiresSelectableAnswers) { //logic to update existing "OBS_QUEST_SLCT_ANS" when submitted answer type is default goes here //first lets find the obs_qat_id from OBS_QUEST_ANS_TYPES table for this question id and selected answer type id int default_qat_id = db.OBS_QUEST_ANS_TYPES.SingleOrDefault(item => item.obs_ans_type_id == obsQuestion.selectedAT.ATid && item.obs_question_id == question_id).obs_qat_id; List<string> current_default_sel_ans_list = db.OBS_QUEST_SLCT_ANS.Where(item => item.obs_qat_id == default_qat_id && item.obs_qsa_eff_st_dt <= DateTime.Today && item.obs_qsa_eff_end_dt > DateTime.Today).Select(x =>x.obs_qsa_text).ToList(); //at this point we have 2 lists of strings(current default selected answers and ones user passed from the form) and we need to compare them if(isEqualList(current_default_sel_ans_list, obsQuestion.selectedAT.selAnsList, obsQuestion.selectedAT.ATcathegory)) {//if we're here that means 2 lists are the same and we only need to change the order of selected answers list short order = 1; foreach (string str in obsQuestion.selectedAT.selAnsList) { OBS_QUEST_SLCT_ANS oBS_QUEST_SLCT_ANS = db.OBS_QUEST_SLCT_ANS.Single(item => item.obs_qat_id == default_qat_id && item.obs_qsa_text == str && item.obs_qsa_eff_st_dt <= DateTime.Today && item.obs_qsa_eff_end_dt > DateTime.Today); oBS_QUEST_SLCT_ANS.obs_qsa_order = order; oBS_QUEST_SLCT_ANS.obs_qsa_wt = order; db.SaveChanges(); order++; } } else//if we're here, that means user passed a different list of selected answers and we need to disable the current one and add new { List<OBS_QUEST_SLCT_ANS> oBS_QUEST_SLCT_ANS = db.OBS_QUEST_SLCT_ANS.Where(item => item.obs_qat_id == default_qat_id).ToList(); oBS_QUEST_SLCT_ANS.ForEach(x => x.obs_qsa_eff_end_dt = DateTime.Today);//update end effective date to todays date short order = 1; foreach (string str in obsQuestion.selectedAT.selAnsList)//now lets create a new record with updated selected answers { OBS_QUEST_SLCT_ANS UPDATED_oBS_QUEST_SLCT_ANS = new OBS_QUEST_SLCT_ANS(); UPDATED_oBS_QUEST_SLCT_ANS.obs_qat_id = default_qat_id; UPDATED_oBS_QUEST_SLCT_ANS.obs_qsa_text = str; UPDATED_oBS_QUEST_SLCT_ANS.obs_qsa_order = order; UPDATED_oBS_QUEST_SLCT_ANS.obs_qsa_wt = order; UPDATED_oBS_QUEST_SLCT_ANS.obs_qsa_dflt_yn = "N"; UPDATED_oBS_QUEST_SLCT_ANS.obs_qsa_eff_st_dt = DateTime.Now; UPDATED_oBS_QUEST_SLCT_ANS.obs_qsa_eff_end_dt = Convert.ToDateTime("12/31/2060"); db.OBS_QUEST_SLCT_ANS.Add(UPDATED_oBS_QUEST_SLCT_ANS); order++; }// end foreach db.SaveChanges(); } }// end of "else if (obsQuestion.indexOfDefaultQA >= 0 &&(obsQuestion.selectedAT.ATid ==obsQuestion.OBSQA_List[obsQuestion.indexOfDefaultQA].answerTypeId)&& obsQuestion.selectedAT.requiresSelectableAnswers) " else// this branch should take care of the scenario where this question/answer type exists in the obs_quest_ans_type table { //Check if the id a default "Y" record. If so, set it to "N" if (obsQuestion.indexOfDefaultQA >= 0) { setExistingDefaultToN(obsQuestion.questionId); } //Check if the selected "AT id" and "Question Id" combination exist in the " OBS_QUEST_ANS_TYPES" table if (obsQuestion.OBSQA_List.Where(x => x.answerTypeId == selected_ans_type_id).Count() > 0) { OBS_QUEST_ANS_TYPES oBS_QUEST_ANS_TYPES = new OBS_QUEST_ANS_TYPES(); try { oBS_QUEST_ANS_TYPES = db.OBS_QUEST_ANS_TYPES.Single(item => item.obs_question_id == question_id && item.obs_ans_type_id == selected_ans_type_id); oBS_QUEST_ANS_TYPES.obs_qat_default_ans_type_yn = "Y"; db.SaveChanges(); //after we set new answer type to be default, we need to check if selected answer types require updates as well if (obsQuestion.selectedAT.requiresSelectableAnswers) { int default_qat_id = oBS_QUEST_ANS_TYPES.obs_qat_id; List<string> current_default_sel_ans_list = db.OBS_QUEST_SLCT_ANS.Where(item => item.obs_qat_id == default_qat_id && item.obs_qsa_eff_st_dt <= DateTime.Today && item.obs_qsa_eff_end_dt > DateTime.Today).Select(x => x.obs_qsa_text).ToList(); if (isEqualList(current_default_sel_ans_list, obsQuestion.selectedAT.selAnsList, obsQuestion.selectedAT.ATcathegory)) {//if we're here that means 2 lists are the same and we only need to change the order of selected answers list short order = 1; foreach (string str in obsQuestion.selectedAT.selAnsList) { OBS_QUEST_SLCT_ANS oBS_QUEST_SLCT_ANS = db.OBS_QUEST_SLCT_ANS.Single(item => item.obs_qat_id == default_qat_id && item.obs_qsa_text == str && item.obs_qsa_eff_st_dt <= DateTime.Today && item.obs_qsa_eff_end_dt > DateTime.Today); oBS_QUEST_SLCT_ANS.obs_qsa_order = order; db.SaveChanges(); order++; } } else//if we're here, that means user passed a different list of selected answers and we need to disable the current one and add new { List<OBS_QUEST_SLCT_ANS> oBS_QUEST_SLCT_ANS = db.OBS_QUEST_SLCT_ANS.Where(item => item.obs_qat_id == default_qat_id).ToList(); oBS_QUEST_SLCT_ANS.ForEach(x => x.obs_qsa_eff_end_dt = DateTime.Now);//update end effective date to todays date short order = 1; foreach (string str in obsQuestion.selectedAT.selAnsList)//now lets create a new record with updated selected answers { OBS_QUEST_SLCT_ANS UPDATED_oBS_QUEST_SLCT_ANS = new OBS_QUEST_SLCT_ANS(); UPDATED_oBS_QUEST_SLCT_ANS.obs_qat_id = default_qat_id; UPDATED_oBS_QUEST_SLCT_ANS.obs_qsa_text = str; UPDATED_oBS_QUEST_SLCT_ANS.obs_qsa_order = order; UPDATED_oBS_QUEST_SLCT_ANS.obs_qsa_wt = order; UPDATED_oBS_QUEST_SLCT_ANS.obs_qsa_dflt_yn = "N"; UPDATED_oBS_QUEST_SLCT_ANS.obs_qsa_eff_st_dt = DateTime.Now; UPDATED_oBS_QUEST_SLCT_ANS.obs_qsa_eff_end_dt = Convert.ToDateTime("12/31/2060"); db.OBS_QUEST_SLCT_ANS.Add(UPDATED_oBS_QUEST_SLCT_ANS); order++; }// end foreach db.SaveChanges(); }//end of else }//if (obsQuestion.selectedAT.requiresSelectableAnswers) } catch { } } else { //It doesn't exist. We need to create a new record with "Y" default indicator //First check if the new AT type selection requires selectable answers //-- First Insert record into 'OBS_QUEST_ANS_TYPES' Table OBS_QUEST_ANS_TYPES oBS_QUEST_ANS_TYPES = new OBS_QUEST_ANS_TYPES(); oBS_QUEST_ANS_TYPES.obs_question_id = question_id; oBS_QUEST_ANS_TYPES.obs_ans_type_id = (short)selected_ans_type_id; oBS_QUEST_ANS_TYPES.obs_qat_default_ans_type_yn = "Y"; //oBS_QUEST_ANS_TYPES.obs_qat_end_eff_dt = Convert.ToDateTime("12/31/2060"); db.OBS_QUEST_ANS_TYPES.Add(oBS_QUEST_ANS_TYPES); db.SaveChanges(); if (isQuest_Slct_Ans_Required(selected_ans_type_id)) { //If selectable answers are required: //-- Second Insert record into 'OBS_QUEST_SLCT_ANS' Table //short temp_selected_ans_type_id = (short)selected_ans_type_id; int createdQAT_id = db.OBS_QUEST_ANS_TYPES.SingleOrDefault(item => item.obs_ans_type_id == obsQuestion.selectedAT.ATid && item.obs_question_id == question_id).obs_qat_id; short order = 1; foreach (string str in obsQuestion.selectedAT.selAnsList) { OBS_QUEST_SLCT_ANS oBS_QUEST_SLCT_ANS = new OBS_QUEST_SLCT_ANS(); oBS_QUEST_SLCT_ANS.obs_qat_id = createdQAT_id; oBS_QUEST_SLCT_ANS.obs_qsa_text = str; oBS_QUEST_SLCT_ANS.obs_qsa_order = order; oBS_QUEST_SLCT_ANS.obs_qsa_wt = order; oBS_QUEST_SLCT_ANS.obs_qsa_dflt_yn = "N"; oBS_QUEST_SLCT_ANS.obs_qsa_eff_st_dt = DateTime.Now; oBS_QUEST_SLCT_ANS.obs_qsa_eff_end_dt = Convert.ToDateTime("12/31/2060"); db.OBS_QUEST_SLCT_ANS.Add(oBS_QUEST_SLCT_ANS); order++; } db.SaveChanges(); }//end of if (isQuest_Slct_Ans_Required(selected_ans_type_id)) } } } }
public ActionResult displayAnswerSection(int id) { // This is a test method that accepts a question Id and the selected Index of the dropdown list // and returns the section for Answers OBSQuestion obsQuestion = new OBSQuestion(id); //ViewBag.list_of_answers = obsQuestion.fullAnswerTypeDDL; //ViewBag.Message = "This is the [GET] Method"; return View(obsQuestion); }