using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; using PSE.Models; //using SDGGroup.Common.BusinessLogic; using Microsoft.AspNet.Identity; using PSE.Purchase; using System.Configuration; namespace PSE.Controllers { [Authorize(Roles = "Permission-PropertySearch,Staff-IT,Staff-Director,Staff-Outworker")] //[RoutePrefix("Admin")] //[Route("Users/{action=Index}")] public class QuestionnaireController : Controller { private PropertySearchEntities db = new PropertySearchEntities(); public QuestionnaireController() { //Session["RunSession"] = "1"; //# fix OWIN/Session cookie bug //if (Session["Current_Questionnaire"] == null) Response.Redirect("../SearcherTasks"); } // constructor // GET: Questionnaire public ActionResult Index() { if (Session["RequestGUID"] == null) return RedirectToAction("Index", "SearcherTasks", new { sr_id = -1, stno = -1, p_sMessage = "Missing Request ID" }); string sUID = User.Identity.GetUserId(); IClientExtra oICE = new ClientExtra(); string sShow = oICE.Get(sUID, "Searcher Preference", "Questionnaire Show") ?? "Item"; Session["SearcherPrefShow"] = sShow; Session["SearcherPrefStyle"] = oICE.Get(sUID, "Searcher Preference", "Questionnaire Style") ?? "Radio"; // switch on user-prefs if (sShow == "Item") return RedirectToAction("EditSelect", "Questionnaire"); return RedirectToAction("EditAll", "Questionnaire"); } // end of ()o // GET: Questionnaire/Landing public ActionResult Landing(string p_sMessage = "") { try { ViewBag.Message = p_sMessage; // guidST Guid? guidST = Session["SearchTaskGUID"] as Guid?; if(!guidST.HasValue) throw new ApplicationException("Bad Task"); tblSearcherTask tabSearcherTask = db.tblSearcherTasks.FirstOrDefault(t => t.SearcherTaskGUID == guidST.Value); if (tabSearcherTask == null) throw new ApplicationException("Cannot find Task"); Guid? oRID = tabSearcherTask.RequestGUID; Session["RequestGUID"] = oRID; int nMask = N.I(Session["RequestMask"], -1); if (nMask < 0) nMask = tabSearcherTask.ST_Mask; Session["RequestMask"] = nMask; var vQA = from e in db.tblRequest_Questionnaire.Include(t => t.tblRequest_Answer) orderby e.M_Section, e.RQ_RunNo, e.M_Item where (e.RequestGUID == oRID) && ((e.RQ_Mask == 0) || (nMask == 0) || ((e.RQ_Mask & nMask) > 0)) && (e.RQ_RunNo >= 0) select e; ViewBag.Questionnaires = vQA.ToList(); // metrics DateTime dtNC = DateTime.Parse("01 Jan 2000"); ViewBag.ShowResp = true; ViewBag.NoOfQuest = vQA.Count(); //ViewBag.NoOfAnswer = vQA.Count(); ViewBag.NoOfNResp = vQA.Count(t => t.tblRequest_Answer.RA_Response == null || t.tblRequest_Answer.RA_Response.Trim() == ""); ViewBag.NoOfNConfirm = vQA.Where(t => t.tblRequest_Answer.RA_DTS == dtNC).Count(); ViewBag.NoOfLLC = tabSearcherTask.tblRequest.tblRequest_Register.Count(t => t.Register_Type == "LLC"); ViewBag.NoOfPlan = tabSearcherTask.tblRequest.tblRequest_Register.Count(t => t.Register_Type == "Plan"); ViewBag.NoOfBuild = tabSearcherTask.tblRequest.tblRequest_Register.Count(t => t.Register_Type == "Build"); // Proplog appointment data //### posible list all appointments for display PLSEntities dbPLS = new PLSEntities(); // Proplog-Search database List listAppointments = dbPLS.Appointments.Where(a => a.AP_SI_ID == tabSearcherTask.tblPurchase_Item.SI_ID).OrderBy(b => b.AP_Date).ThenBy(c => c.AP_Time).ToList(); //int nSI_ID = tabSearcherTask.tblPurchase_Item.SI_ID; //List listAppointments = dbPLS.Appointments.Where(a => a.AP_SI_ID == nSI_ID).OrderBy(b => b.AP_Date).ThenBy(c => c.AP_Time).ToList(); ViewBag.AppointmentCount = listAppointments.Count; ViewBag.AppointmentDate = (listAppointments.Count > 0) ? listAppointments[0].AP_Date.Value.ToString("ddd dd MMM yyyy") ?? "No date" : ""; ViewBag.AppointmentTime = (listAppointments.Count > 0) ? listAppointments[0].AP_Time ?? "No Time" : ""; //string sADate = ""; //string sATime = ""; //if (listAppointments.Count > 0) //{ // sADate = listAppointments[0].AP_Date.Value.ToString("ddd dd MMM yyyy") ?? "No date"; // sATime = listAppointments[0].AP_Time ?? "No Time"; //} //ViewBag.AppointmentDate = sADate; //ViewBag.AppointmentTime = sATime; // user-settings ViewBag.ShowList = new SelectList(new[] { new { sName = "All" }, new { sName = "Table" }, new { sName = "By Item" } }, "sName", "sName", N.Z(Session["SearcherPrefShow"], "Item")); ViewBag.StyleList = new SelectList(new[] { new { sName = "Radio" }, new { sName = "List" }, new { sName = "Combo" } }, "sName", "sName", N.Z(Session["SearcherPrefStyle"], "Radio")); // local search-sync-status //###to-do //### // download Plan(s) links //###to-be-tested int nSearchID = tabSearcherTask.tblPurchase_Item.tblPurchase.SE_ID; string sSearchID = nSearchID.ToString(); //var currentURL = Request.Url.Scheme + System.Uri.SchemeDelimiter + Request.Url.Host; string sHostURL = Request.Url.Scheme + System.Uri.SchemeDelimiter + Request.Url.Host + ConfigurationManager.AppSettings["ProplogFilePath"]; ViewBag.MapURL = sHostURL + sSearchID.Substring(0, 4) + "000/" + sSearchID + "/" + sSearchID + "_Map.pdf"; //###check file type // blog Guid guidP = tabSearcherTask.PurchaseGUID; ViewBag.Blog = Purchase.Purchase.PurchaseBlogGet(db, guidP, tabSearcherTask.PurchaseItemNo); // short multi-line //ViewBag.Blog2 = Purchase.Purchase.PurchaseBlogGet(db, guidP, tabSearcherTask.PurchaseItemNo, "", true); // long single-line // Session["SourceController"] = "Questionnaire"; Session["SourceView"] = "Landing"; return View(tabSearcherTask); } catch (Exception ex) { return RedirectToAction("Index", "SearcherTasks", new { p_sMessage = ex.Message }); } // end try-catch } // end of Landing(s)o // POST: Questionnaire/Landing [HttpPost, ActionName("Landing")] [ValidateAntiForgeryToken] public ActionResult LandingUpdate(string sUpdate) { string sUID = User.Identity.GetUserId(), sRet = ""; try { Guid? guidST = Session["SearchTaskGUID"] as Guid?; if (!guidST.HasValue) throw new ApplicationException("Bad Task"); tblSearcherTask tabSearcherTask = db.tblSearcherTasks.FirstOrDefault(t => t.SearcherTaskGUID == guidST.Value); if (tabSearcherTask == null) throw new ApplicationException("Cannot find Task"); bool bUpdate = false; switch (sUpdate) { case "Settings": IClientExtra oICE = new ClientExtra(); string sStyle = Request.Form["rblStyle"]; Session["SearcherPrefStyle"] = sStyle; string sShow = Request.Form["rblShow"]; Session["SearcherPrefShow"] = sShow; oICE.Put(sUID, "Searcher Preference", "Questionnaire Style", sStyle); // save to client-extra oICE.Put(sUID, "Searcher Preference", "Questionnaire Show", sShow); // save to client-extra sRet = "Changed your Preferences"; break; case "Booked": if (User.IsInRole("Staff-Outworker")) throw new ApplicationException("Outworkers not allowed"); if (tabSearcherTask.ST_State != 0) throw new ApplicationException("Current state is not 'Initialise'"); tabSearcherTask.ST_State = 1; bUpdate = true; break; case "Start": if (!(User.IsInRole("Staff-Outworker") || User.IsInRole("Permission-PSSearcher"))) throw new ApplicationException("Only Searchers allowed"); if (tabSearcherTask.ST_State != 1) throw new ApplicationException("Current state is not 'Booked'"); tabSearcherTask.ST_State = 2; bUpdate = true; break; case "Finish": // check for completed questionnaire if (!(User.IsInRole("Staff-Outworker") || User.IsInRole("Permission-PSSearcher"))) throw new ApplicationException("Only Searchers allowed"); if (tabSearcherTask.ST_State != 2) throw new ApplicationException("Current state is not 'Start'"); // >=6 Guid? oRID = tabSearcherTask.RequestGUID; int nMask = tabSearcherTask.ST_Mask;//###get from PI var vQA = from e in db.tblRequest_Answer where (e.RequestGUID == oRID) && ((e.tblRequest_Questionnaire.RQ_Mask == 0) || (nMask == 0) || ((e.tblRequest_Questionnaire.RQ_Mask & nMask) > 0)) && (e.tblRequest_Questionnaire.RQ_RunNo >= 0) select e; bool bBadResp = false; foreach(tblRequest_Answer tabRA in vQA) { if (N.Z(tabRA.RA_Response) == "") bBadResp = true; if (tabRA.RA_DTS.ToString("dd-MM-yyyy") == "01-01-2000") bBadResp = true; if (bBadResp) break; } // end if if (bBadResp) throw new ApplicationException("Incomplete Con-29 Questionnaire"); // check for register entries int nCount = db.tblRequest_Register.Count(t => t.RequestGUID == oRID && t.Register_Type == "Plan"); if (nCount <= 0) throw new ApplicationException("Missing Planning Registry entry"); nCount = db.tblRequest_Register.Count(t => t.RequestGUID == oRID && t.Register_Type == "Build"); if (nCount <= 0) throw new ApplicationException("Missing Building Control Registry entry"); nCount = db.tblRequest_Register.Count(t => t.RequestGUID == oRID && t.Register_Type == "LLC"); if (nCount <= 0) throw new ApplicationException("Missing Local Land Charges Registry entry"); //tabSearcherTask.ST_Status = sUpdate; tabSearcherTask.ST_State = 3; bUpdate = true; break; case "Done": if (!(User.IsInRole("Staff-Outworker") || User.IsInRole("Permission-PSSearcher"))) throw new ApplicationException("Only Searchers allowed"); if (tabSearcherTask.ST_State != 3) throw new ApplicationException("Current state is not 'Finish'"); //### fire store snapshot response data to PIX as XML 'Searcher' tabSearcherTask.ST_State = 4; bUpdate = true; break; case "Confirm": if (User.IsInRole("Staff-Outworker")) throw new ApplicationException("Outworkers not allowed"); if (tabSearcherTask.ST_State != 4) throw new ApplicationException("Current state is not 'Done'"); tabSearcherTask.ST_State = 5; bUpdate = true; break; case "Compile": if (User.IsInRole("Staff-Outworker")) throw new ApplicationException("Outworkers not allowed"); if (tabSearcherTask.ST_State != 5) throw new ApplicationException("Current state is not 'Confirm'"); tabSearcherTask.ST_State = 6; bUpdate = true; break; case "Locked": if (User.IsInRole("Staff-Outworker")) throw new ApplicationException("Outworkers not allowed"); if (tabSearcherTask.ST_State != 6) throw new ApplicationException("Current state is not 'Compile'"); //tabSearcherTask.ST_Status = sUpdate; tabSearcherTask.ST_State = 7; //### fire first archive step (data reduction) //### fire store snapshot response data to PIX as XML 'Locked' bUpdate = true; break; default: break; } // end switch if (bUpdate) { sRet = "Changed Status to '" + sUpdate + "'"; tabSearcherTask.ST_Status = sUpdate; Purchase.Purchase.PurchaseEventLogEntry(db, sUID, tabSearcherTask.PurchaseGUID, tabSearcherTask.PurchaseItemNo, "Search Event '" + sUpdate + "'", "Searcher Task"); db.Entry(tabSearcherTask).State = EntityState.Modified; db.SaveChanges(); int nSI_ID = tabSearcherTask.tblPurchase_Item.SI_ID; int nSIS_ID = tabSearcherTask.ST_State + 50; // form SIS-ID from task-state if (tabSearcherTask.ST_State < 7) ps.UpdateProplogStatus(nSI_ID, nSIS_ID); // not for locked or above } // end if } // end try catch (ApplicationException ex) { sRet = ex.Message; } catch (Exception ex) { sRet = "Error: " + ex.Message; } return RedirectToAction("Landing", new { p_sMessage = sRet }); } // end of LandingUpdate(s)o // GET Questionnaire/LandingRadon, partial-view return public ActionResult LandingRadon(Guid stguid) { //###get radon data here //int nNorth = 167968, nEast = 526171; //### Morden int nNorth = 0, nEast = 0; bool bRadon = false; string sUID = User.Identity.GetUserId(), sRet = ""; try { Guid? guidST = Session["SearchTaskGUID"] as Guid?; if (!guidST.HasValue) throw new ApplicationException("Bad Task"); tblSearcherTask tabSearcherTask = db.tblSearcherTasks.FirstOrDefault(t => t.SearcherTaskGUID == guidST.Value); if (tabSearcherTask == null) throw new ApplicationException("Cannot find Task"); tblSearcherTaskExtra tabSTX = tabSearcherTask.tblSearcherTaskExtras.FirstOrDefault(t => t.SearcherTaskExtraGroup == "Address Data" && t.SearcherTaskExtraName == "Easting"); if (tabSTX != null) nEast = tabSTX.STX_Flag; //### tabSTX = tabSearcherTask.tblSearcherTaskExtras.FirstOrDefault(t => t.SearcherTaskExtraGroup == "Address Data" && t.SearcherTaskExtraName == "Northing"); if (tabSTX != null) nNorth = tabSTX.STX_Flag; //### if (nEast == 0 || nNorth == 0) throw new ApplicationException("Missing point data"); try { bRadon = ps.GetRadon(nEast, nNorth); sRet = bRadon.ToString() + (bRadon ? " (Yes)" : " (No)"); } catch (Exception ex) { ps.ErrorLog(sUID, tabSearcherTask.PurchaseGUID, tabSearcherTask.PurchaseItemNo, 0, ex, "Landing-Radon:"); sRet = "System Error"; } sRet += " for Easting='" + nEast + "', Northing ='" + nNorth + "'."; } catch (Exception ex) { sRet = "Fault: " + ex.Message; } //### ViewBag.RadonReport = sRet; return PartialView(); } // end of LandingRadon(n)o private string AddBlogEntry(BlogQuestItemVM p_oBlogQIVM) { string sRet = "Problem adding new entry to Blog"; if (ModelState.IsValid) { try { sRet = ""; string sUID = User.Identity.GetUserId(); Guid? guidST = Session["SearchTaskGUID"] as Guid?; if (!guidST.HasValue) throw new ApplicationException("Bad Task"); tblSearcherTask tabSearcherTask = db.tblSearcherTasks.FirstOrDefault(t => t.SearcherTaskGUID == guidST.Value); if (tabSearcherTask == null) throw new ApplicationException("Cannot find Task"); Purchase.Purchase.PurchaseBlogEntry(db, sUID, tabSearcherTask.PurchaseGUID, tabSearcherTask.PurchaseItemNo, p_oBlogQIVM.BlogSubject, p_oBlogQIVM.BlogText, true); // all done } // end try catch (ApplicationException ex) { sRet = ex.Message; } catch (Exception ex) { sRet = "Fault " + ex.Message; } } // end if return sRet; } // end of (o)s private BlogQuestItemVM AddBlogEntry(int nQSection, int nQItem, string sQIdent, string sQSource) { BlogQuestItemVM oBlogQIVM = new BlogQuestItemVM(); string sRet = ""; try { // purchase guid Guid? guidST = Session["SearchTaskGUID"] as Guid?; if (!guidST.HasValue) throw new ApplicationException("Bad Task"); tblSearcherTask tabSearcherTask = db.tblSearcherTasks.FirstOrDefault(t => t.SearcherTaskGUID == guidST.Value); if (tabSearcherTask == null) throw new ApplicationException("Cannot find Task"); // fill view-model oBlogQIVM.M_Section = nQSection; oBlogQIVM.M_Item = nQItem; oBlogQIVM.RQ_Identifier = sQIdent; // derive subject string sSubject = ""; if (sQSource == "Item") sSubject = "Q.I. " + sQIdent; if (sQSource == "All") sSubject = "Q.I. " + sQIdent; oBlogQIVM.BlogSubject = sSubject; oBlogQIVM.BlogSource = sQSource; if (sQSource == "Land") oBlogQIVM.BlogEntries = Purchase.Purchase.PurchaseBlogGet(db, tabSearcherTask.PurchaseGUID, -1, "", true); // long single-line else oBlogQIVM.BlogEntries = Purchase.Purchase.PurchaseBlogGet(db, tabSearcherTask.PurchaseGUID, sSubject); // short multi-line } // end try catch (ApplicationException ex) { sRet = ex.Message; } catch (Exception ex) { sRet = "Fault: " + ex.Message; } if (sRet != "") { oBlogQIVM.BlogSource = "Fault"; oBlogQIVM.BlogEntries = sRet; } // end if return oBlogQIVM; } // end of (n,n,s,s)o // GET: Questionnaire/LandBlogEntry, partial view return public ActionResult LandBlogEntry() { // landing blog entries BlogQuestItemVM oBlogQIVM = AddBlogEntry(0, 0, "-", "Land"); if (oBlogQIVM.BlogSource == "fault") return RedirectToAction("BadBlog", new { p_sMessage = oBlogQIVM.BlogEntries }); return PartialView(oBlogQIVM); } // end of LandBlogEntry() // POST: Questionnaire/LandBlogEntry, partial view [HttpPost] [ValidateAntiForgeryToken] public ActionResult LandBlogEntry([Bind(Include = "M_Section,M_Item,RQ_Identifier,BlogText,BlogSubject,BlogSource")]BlogQuestItemVM oBlogQIVM) { // landing blog entry string sRet = AddBlogEntry(oBlogQIVM); if (sRet != "") return RedirectToAction("BadBlog", new { p_sMessage = sRet }); return RedirectToAction("LandBlogEntry"); } // end of LandBlogEntry() // GET: Questionnaire/QIBlogEntry, partial view return public ActionResult QIBlogEntry(int nQSection, int nQItem, string sQIdent, string sQSource) { // questionnaire item blog entries BlogQuestItemVM oBlogQIVM = AddBlogEntry(nQSection, nQItem, sQIdent, sQSource); if (oBlogQIVM.BlogSource == "fault") return RedirectToAction("BadBlog", new { p_sMessage = oBlogQIVM.BlogEntries }); return PartialView(oBlogQIVM); } // end of AddBlogEntry(n,n,s,s) // POST: Questionnaire/QIBlogEntry, partial view [HttpPost] [ValidateAntiForgeryToken] public ActionResult QIBlogEntry([Bind(Include = "M_Section,M_Item,RQ_Identifier,BlogText,BlogSubject,BlogSource")]BlogQuestItemVM oBlogQIVM) { // questionnaire item blog entry string sRet = AddBlogEntry(oBlogQIVM); if (sRet != "") return RedirectToAction("BadBlog", new { p_sMessage = sRet }); return RedirectToAction("QIBlogEntry", new { nQSection = oBlogQIVM.M_Section, nQItem = oBlogQIVM.M_Item, sQIdent = oBlogQIVM.RQ_Identifier, sQSource = oBlogQIVM.BlogSource }); } // end of AddBlogEntry(o) // GET: Questionnaire/_BlogEntry, full page view return in iframe public ActionResult _BlogEntry(int nQSection, int nQItem, string sQIdent, string sQSource) { // questionnaire all blog entry BlogQuestItemVM oBlogQIVM = AddBlogEntry(nQSection, nQItem, sQIdent, sQSource); if (oBlogQIVM.BlogSource == "fault") return RedirectToAction("_BadBlog", new { p_sMessage = oBlogQIVM.BlogEntries }); return View(oBlogQIVM); } // end of _BlogEntry(n,n,s,s) // POST: Questionnaire/_BlogEntry, full page view return in iframe [HttpPost] [ValidateAntiForgeryToken] public ActionResult _BlogEntry([Bind(Include = "M_Section,M_Item,RQ_Identifier,BlogText,BlogSubject,BlogSource")]BlogQuestItemVM oBlogQIVM) { // questionnaire all blog entry string sRet = AddBlogEntry(oBlogQIVM); if (sRet != "") return RedirectToAction("_BadBlog", new { p_sMessage = sRet }); return RedirectToAction("_BlogEntry", new { nQSection = oBlogQIVM.M_Section, nQItem = oBlogQIVM.M_Item, sQIdent = oBlogQIVM.RQ_Identifier, sQSource = oBlogQIVM.BlogSource }); } // end of _BlogEntry(o) // GET: Questionnaire/BadBlog, error message public ActionResult BadBlog(string p_sMessage) { ViewBag.Message = p_sMessage; return View(); } // end of (s)o // GET: Questionnaire/_BadBlog, error message in iframe public ActionResult _BadBlog(string p_sMessage) { ViewBag.Message = p_sMessage; return View(); } // end of (s)o // GET: Questionnaire/EditItem/5/5 public ActionResult EditItem(int? nQSection, int? nQItem) { try { // check parameters Guid? RID = Session["RequestGUID"] as Guid?; if (!RID.HasValue) throw new ApplicationException("Bad Request-ID Item"); if (!nQSection.HasValue || !nQItem.HasValue) return RedirectToAction("Landing", new { p_sMessage = "Bad Questionnaire Item" }); // fetch answer record tblRequest_Answer oRequest_Answer = db.tblRequest_Answer.Find(RID, nQSection, nQItem); if (oRequest_Answer == null) throw new ApplicationException("Missing Question Response: Section '" + nQSection + "', Item '" + nQItem + "'."); // check mask valid int nQMask = oRequest_Answer.tblRequest_Questionnaire.RQ_Mask, nSMask = N.I(Session["RequestMask"]); if ((nQMask != 0) && (nSMask != 0) && ((nQMask & nSMask) == 0)) return RedirectToAction("Landing", new { p_sMessage = "Bad Questionnaire Mask: Section '" + nQSection + "', Item '" + nQItem + "'." }); // fetch previous selection var vQExtras = db.tblRequest_QOption.Where(x => x.RequestGUID == RID && x.M_Section == nQSection && x.M_Item == nQItem && x.M_Option <= 0).OrderByDescending(y => y.M_Option); ViewBag.QExtras = new SelectList(vQExtras, "RQ_Option_Text", "RQ_Option_Text"); // user prefs ViewBag.QShow = Session["SearcherPrefShow"] ?? "Item"; ViewBag.QStyle = Session["SearcherPrefStyle"] ?? "Radio"; ViewBag.Header = Session["SearchHeaderData"] ?? "Show Header data here"; return View(oRequest_Answer); } catch (Exception ex) { return RedirectToAction("Index", "SearcherTasks", new { sr_id = -1, stno = -1, p_sMessage = ex.Message }); } // end try-catch } // end of EditItem(n,n)o // POST: Questionnaire/EditItem [HttpPost] [ValidateAntiForgeryToken] public ActionResult EditItem([Bind(Include = "RequestGUID,M_Section,M_Item,RA_OptionNo,RA_Value,RA_Response")] tblRequest_Answer oRA, int nMode = 1) { // nMode default = next ###,RA_DTS if (ModelState.IsValid) { tblRequest_Answer oRA2 = db.tblRequest_Answer.Find(oRA.RequestGUID, oRA.M_Section, oRA.M_Item); if ((oRA2 != null) && ((oRA2.RA_Response != oRA.RA_Response) || (oRA2.RA_OptionNo != oRA.RA_OptionNo))) { // changed response, save changes oRA2.RA_OptionNo = oRA.RA_OptionNo; oRA2.RA_Value = oRA.RA_Value; oRA2.RA_Response = oRA.RA_Response; oRA2.RA_DTS = DateTime.Now; db.Entry(oRA2).State = EntityState.Modified; db.SaveChanges(); } // end if if (nMode == 9) return RedirectToAction("Landing", new { p_sMessage = "Saved Item"}); return RedirectToAction("EditSelect", new { nQSection = oRA.M_Section, nQItem = oRA.M_Item, nMode = nMode }); // determine navigation } // end if var vQExtras = db.tblRequest_QOption.Where(x => x.RequestGUID == oRA.RequestGUID && x.M_Section == oRA.M_Section && x.M_Item == oRA.M_Item && x.M_Option <= 0).OrderByDescending(y => y.M_Option); ViewBag.QExtras = new SelectList(vQExtras, "M_Option", "RQ_Option_Text"); return View(oRA); } // end of EditItem(o,n)o // GET: Questionnaire/EditSection/5 public ActionResult EditSection(int? nQSection) { // check parameters Guid? RID = Session["RequestGUID"] as Guid?; if (!RID.HasValue) throw new ApplicationException("Bad Request-ID Section"); if (!nQSection.HasValue) return RedirectToAction("Landing", new { p_sMessage = "Bad Questionnaire Section" }); // fetch filtered answer records int nMask = N.I(Session["RequestMask"]); var vRQA = from e in db.tblRequest_Answer.Include(t => t.tblRequest_Questionnaire) orderby e.tblRequest_Questionnaire.RQ_RunNo, e.M_Item where (e.RequestGUID == RID) && (e.M_Section == nQSection) && (e.tblRequest_Questionnaire.RQ_RunNo >= 0) && (e.tblRequest_Questionnaire.RQ_ItemMode == 0) && ((e.tblRequest_Questionnaire.RQ_Mask == 0) || (nMask == 0) || ((e.tblRequest_Questionnaire.RQ_Mask & nMask) > 0)) select e; // default item-mode, 0=question List oRequest_Answers = vRQA.ToList(); if (oRequest_Answers.Count <= 0) return RedirectToAction("Landing", new { p_sMessage = "Empty Questionnaire Section" }); return View(oRequest_Answers); } // end of (n,n)o // POST: Questionnaire/EditSection [HttpPost] [ValidateAntiForgeryToken] public ActionResult EditSection(List oRAs) { // nMode default = next if (ModelState.IsValid) { tblRequest_Answer oRA2 = null; foreach (var oRA in oRAs) { oRA2 = db.tblRequest_Answer.Find(oRA.RequestGUID, oRA.M_Section, oRA.M_Item); if ((oRA2 != null) && (oRA.RA_Response != oRA2.RA_Response)) { // changed response, save changes oRA2.RA_OptionNo = oRA.RA_OptionNo; oRA2.RA_Value = oRA.RA_Value; oRA2.RA_Response = oRA.RA_Response; oRA2.RA_DTS = DateTime.Now; db.Entry(oRA2).State = EntityState.Modified; } // end if } // for-each db.SaveChanges(); return RedirectToAction("Landing", new { p_sMessage = "Saved Section Edits" }); } // end if return View(oRAs); } // end of (o)o // GET: Questionnaire/EditAll public ActionResult EditAll() { try { // check parameters Guid? RID = Session["RequestGUID"] as Guid?; if (!RID.HasValue) throw new ApplicationException("Bad Request-ID All"); // fetch filtered answer records int nMask = N.I(Session["RequestMask"]); var vRQA = from e in db.tblRequest_Answer.Include(t => t.tblRequest_Questionnaire) orderby e.M_Section, e.tblRequest_Questionnaire.RQ_RunNo, e.M_Item where (e.RequestGUID == RID) && (e.tblRequest_Questionnaire.RQ_RunNo >= 0) && (e.tblRequest_Questionnaire.RQ_ItemMode == 0) && ((e.tblRequest_Questionnaire.RQ_Mask == 0) || (nMask == 0) || ((e.tblRequest_Questionnaire.RQ_Mask & nMask) > 0)) select e; // default item-mode, 0=question List oRequest_Answers = vRQA.ToList(); if (oRequest_Answers.Count <= 0) return RedirectToAction("Landing", new { p_sMessage = "Empty Questionnaire All" }); // user prefs ViewBag.QShow = Session["SearcherPrefShow"] ?? "All"; ViewBag.QStyle = Session["SearcherPrefStyle"] ?? "Radio"; ViewBag.Header = Session["SearchHeaderData"] ?? "Show Header data here"; return View(oRequest_Answers); } catch (Exception ex) { return RedirectToAction("Index", "SearcherTasks", new { sr_id = -1, stno = -1, p_sMessage = ex.Message }); } // end try-catch } // end of EditAll(n,n)o // POST: Questionnaire/EditAll [HttpPost] [ValidateAntiForgeryToken] public ActionResult EditAll(List oRAs) { if (ModelState.IsValid) { tblRequest_Answer oRA2 = null; foreach (var oRA in oRAs) { oRA2 = db.tblRequest_Answer.Find(oRA.RequestGUID, oRA.M_Section, oRA.M_Item); if ((oRA2 != null) && ((oRA.RA_Response != oRA2.RA_Response) || (oRA2.RA_OptionNo != oRA.RA_OptionNo))) { // changed response, save changes oRA2.RA_OptionNo = oRA.RA_OptionNo; oRA2.RA_Value = oRA.RA_Value; oRA2.RA_Response = oRA.RA_Response; oRA2.RA_DTS = DateTime.Now; db.Entry(oRA2).State = EntityState.Modified; } // end if } // for-each db.SaveChanges(); return RedirectToAction("Landing", new { p_sMessage = "Saved All Edits"}); } // end if return View(oRAs); } // end of EditAll(o)o public ActionResult EditSelect(int nQSection = -1, int nQItem = -1, int nMode = 0) { // determine what to edit, nMode: 0=same, 1=next, 2=next not done, -1=prev, -2=prev not done Guid? RID = Session["RequestGUID"] as Guid?; DateTime dtsDefault = DateTime.Parse("01-01-2000"); if (!RID.HasValue) return RedirectToAction("Index", "SearcherTask", new { p_sMessage = "Bad Questionnaire" }); //sr_id = -1, stno = -1, tblRequest_Questionnaire oRQ = null, oRQ2 = null; int nMask = N.I(Session["RequestMask"]); // default 0 var vRQ = from e in db.tblRequest_Questionnaire select e; var vRQ1 = from e in db.tblRequest_Questionnaire.Include(t=>t.tblRequest_Answer) orderby e.M_Section, e.RQ_RunNo, e.M_Item where (e.RequestGUID == RID) && ((e.RQ_Mask == 0) || (nMask == 0) || ((e.RQ_Mask & nMask) > 0)) && (e.RQ_RunNo >= 0) && (e.RQ_ItemMode == 0) select e; // default item-mode, 0=question var vRQ2 = from e in db.tblRequest_Questionnaire orderby e.M_Section descending, e.RQ_RunNo descending, e.M_Item descending where (e.RequestGUID == RID) && ((e.RQ_Mask == 0) || (nMask == 0) || ((e.RQ_Mask & nMask) > 0)) && (e.RQ_RunNo >= 0) && (e.RQ_ItemMode == 0) select e; // as vRQ but with descending Order-By clause // determine edit/action to take if (nQSection < 0) { //### start from begining, by all/section/item //# by item // start at first section, first item if (nMode == 2) oRQ2 = vRQ1.FirstOrDefault(t => t.tblRequest_Answer.RA_Response == "" || t.tblRequest_Answer.RA_Response == null); // first to-do item else oRQ2 = vRQ1.FirstOrDefault(); // first item if (oRQ2 != null) return RedirectToAction("EditItem", new { nQSection = oRQ2.M_Section, nQItem = oRQ2.M_Item }); // if found, go there } else if (nQItem < 0) { //### start from begining of section, by section } else { //# from current section/item, by item oRQ = db.tblRequest_Questionnaire.Find(RID, nQSection, nQItem); // get current question if (oRQ == null) return RedirectToAction("Landing", new { p_sMessage = "Bad Questionnaire Item" }); switch (nMode) { case 1: vRQ = vRQ1.Where(t => t.M_Section == nQSection && ((t.RQ_RunNo > oRQ.RQ_RunNo) || ((t.RQ_RunNo == oRQ.RQ_RunNo) & (t.M_Item > nQItem)))); break; // next case 2: // next to do vRQ = vRQ1.Where(t => t.M_Section == nQSection && ((t.RQ_RunNo > oRQ.RQ_RunNo) || ((t.RQ_RunNo == oRQ.RQ_RunNo) & (t.M_Item > nQItem)))); vRQ = vRQ.Where(t => ((t.tblRequest_Answer.RA_Response == "") || (t.tblRequest_Answer.RA_Response == null) || (t.tblRequest_Answer.RA_OptionNo == 0) || (t.tblRequest_Answer.RA_DTS == dtsDefault))); break; case -1: vRQ = vRQ2.Where(t => t.M_Section == nQSection && ((t.RQ_RunNo < oRQ.RQ_RunNo) || ((t.RQ_RunNo == oRQ.RQ_RunNo) & (t.M_Item < nQItem)))); break; // prev case -2: vRQ = vRQ2.Where(t => t.M_Section == nQSection && ((t.RQ_RunNo < oRQ.RQ_RunNo) || ((t.RQ_RunNo == oRQ.RQ_RunNo) & (t.M_Item < nQItem)))); vRQ = vRQ.Where(t => ((t.tblRequest_Answer.RA_Response == "") || (t.tblRequest_Answer.RA_Response == null) || (t.tblRequest_Answer.RA_OptionNo == 0) || (t.tblRequest_Answer.RA_DTS == dtsDefault))); break; // prev not done default: vRQ = vRQ1.Where(t => t.M_Section == nQSection && t.M_Item == nQItem); break; // same } // end switch oRQ2 = vRQ.FirstOrDefault(); // get first filtered record if (oRQ2 == null) { // not found, re-try by section switch (nMode) { case 1: oRQ2 = vRQ1.FirstOrDefault(t => t.M_Section > nQSection); break; // try next section, first item case 2: oRQ2 = vRQ1.Where(t => t.M_Section > nQSection && ((t.tblRequest_Answer.RA_Response == "") || (t.tblRequest_Answer.RA_Response == null) || (t.tblRequest_Answer.RA_OptionNo == 0) || (t.tblRequest_Answer.RA_DTS == dtsDefault))).FirstOrDefault(); break; // try next section, first item not done case -1: oRQ2 = vRQ2.FirstOrDefault(t => t.M_Section < nQSection); break; // try prev section, last item case -2: oRQ2 = vRQ2.Where(t => t.M_Section < nQSection && ((t.tblRequest_Answer.RA_Response == "") || (t.tblRequest_Answer.RA_Response == null) || (t.tblRequest_Answer.RA_OptionNo == 0) || (t.tblRequest_Answer.RA_DTS == dtsDefault))).FirstOrDefault(); break; // try prev section, last item not done default: break; // ignore } // end switch } // end if if (oRQ2 != null) return RedirectToAction("EditItem", new { nQSection = oRQ2.M_Section, nQItem = oRQ2.M_Item }); // if found, go there } // end if return RedirectToAction("Landing", new { p_sMessage = "End of Questionnaire" }); } // end of EditSelect(n,n,n)o //### needs a bit of work protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); //dbSDG.Dispose(); } base.Dispose(disposing); } } }