public IActionResult Index(CurrentPageVM vm) { try { PageVM pageVm = null; try { //Get the current PageVm from Session (throws exception if session is null/timed out) pageVm = _sessionService.GetPageById(vm.PageId, false); } catch { return(GetCustomErrorCode(EnumStatusCode.FormPageContinueSessionNullError, "Error with user session. Session is null: Id='" + vm.PageId + "'")); } if (pageVm == null) { return(GetCustomErrorCode(EnumStatusCode.FormPageContinueNullError, "Error with user session. pageVm is null: Id='" + vm.PageId + "'")); } if (!string.IsNullOrWhiteSpace(_sessionService.PageForEdit)) { if (_sessionService.PageForEdit == pageVm.PageId) { //this page was revisited and edited _sessionService.RemoveNavOrderFrom(pageVm.PageId); } } var userSession = _sessionService.GetUserSession(); if (userSession == null)//shouldn't happen as it's handled above { return(GetCustomErrorCode(EnumStatusCode.FormPageContinueSessionNullError, "Error with user session. Session is null: Id='" + vm.PageId + "'")); } if (string.IsNullOrWhiteSpace(userSession.LocationName)) { return(GetCustomErrorCode(EnumStatusCode.FormContinueLocationNullError, "Error with user session. Location is null: Id='" + vm.PageId + "'")); } var serviceNotFound = userSession.LocationName.Equals("the service"); ViewBag.BackLink = new BackLinkVM { Show = true, Url = GetPreviousPage(pageVm, serviceNotFound), Text = "Back" }; if (Request?.Form != null) { //Validate the Response against the page json and update PageVm to contain the answers _gdsValidate.ValidatePage(pageVm, Request.Form, true, restrictedWords); } //Get the error count var errorCount = pageVm.Questions?.Count(m => m.Validation != null && m.Validation.IsErrored); //If we have errors return to the View if (errorCount > 0) { return(View(pageVm)); } //Now we need to update the FormVM in session. _sessionService.UpdatePageVmInFormVm(pageVm); //No errors redirect to the Index page with our new PageId var nextPageId = pageVm.NextPageId; //************************************************ //Hack to make Good Journey work //Need to refactor into GDS Helpers when we have time if (vm.PageId == "give-your-feedback") { var formVm = _sessionService.GetFormVmFromSession(); var questions = formVm.Pages.SelectMany(m => m.Questions).ToList(); var startGoodJourney = questions.FirstOrDefault(m => m.QuestionId == "what-you-want-to-tell-us-about-01"); if (startGoodJourney != null && startGoodJourney.Answer == "Good experience") { nextPageId = "can-we-share"; } } //************************************************ //Check the nextPageId for preset controller names switch (nextPageId) { case "HowWeUseYourInformation": return(RedirectToAction("Index", "HowWeUseYourInformation")); case "CheckYourAnswers": return(RedirectToAction("Index", "CheckYourAnswers")); case "Home": return(RedirectToAction("Index", "Home")); } //Finally, No Errors so load the next page return(RedirectToAction("Index", new { id = nextPageId })); } catch (Exception ex) { ex.Data.Add("GFCError", "Unexpected error updating PageVM. Id:='" + vm.PageId + "'"); throw ex; } }
public IActionResult Index(CurrentPageVM vm) { try { PageVM pageVm = null; try { //Get the current PageVm from Session (throws exception if session is null/timed out) pageVm = _sessionService.GetPageById(vm.PageId, false); } catch { //user session has timed out return(GetCustomErrorCode(EnumStatusCode.FormPageContinueSessionNullError, "Error with user session. Session is null: Id='" + vm.PageId + "'")); } if (pageVm == null) { return(GetCustomErrorCode(EnumStatusCode.FormPageContinueNullError, "Error with user session. pageVm is null: Id='" + vm.PageId + "'")); } var skipNextQuestions = false;//is true if for example user changes from "bad" to "good and bad" var formVm = _sessionService.GetFormVmFromSession(); if (pageVm.PageId == _config.Value.ServiceNotFoundPage) { //this happens when a user changes from a selected location to a location not found _sessionService.UpdateNavOrder(pageVm.PageId);//this enables the page for edit to be this page //so remove any previously selected location var searchPage = formVm.Pages.FirstOrDefault(p => p.PageId == "search"); if (searchPage != null) { searchPage.Questions.FirstOrDefault().Answer = string.Empty; //remove search from the nav order _sessionService.RemoveFromNavOrder(searchPage.PageId); } _sessionService.UpdateNavOrder(pageVm.PageId); //update any previously entered location not found var previousLocation = _sessionService.GetUserSession().LocationName; var defaultServiceName = _config.Value.SiteTextStrings.DefaultServiceName; //Store the user entered details _sessionService.SetUserSessionVars(new UserSessionVM { LocationId = "0", LocationName = defaultServiceName, ProviderId = "" }); _sessionService.UpdateFormData(new List <DataItemVM>() { new DataItemVM() { Id = "LocationName", Value = defaultServiceName }, new DataItemVM() { Id = "LocationId", Value = "0" }, new DataItemVM() { Id = "LocationCategory", Value = "unknown" } }); //Set up our replacement text var replacements = new Dictionary <string, string> { { previousLocation, defaultServiceName } }; _sessionService.SaveFormVmToSession(formVm, replacements); } /* * Commented out second part of IF statement * * Reason: this was stopping future nav order being edited properly when journey has changed. * Issue: it's not certain why this was included in the first place; it's possible that it should have been a second 'NOT' condition * Consequences: in some unusual circumstances(without this second IF section), it is possible to have a journey where you can't hit * all of the questions to edit them.We have not been able to reproduce this, however, so leaving the change in for now. * Original line: * if (!string.IsNullOrWhiteSpace(_sessionService.PageForEdit) && (string.IsNullOrWhiteSpace(_sessionService.GetChangeModeRedirectId()))) */ if (!string.IsNullOrWhiteSpace(_sessionService.PageForEdit)) // && (string.IsNullOrWhiteSpace(_sessionService.GetChangeModeRedirectId()))) { if (_sessionService.PageForEdit == pageVm.PageId) { if ((_sessionService.GetChangeMode() ?? "") == _config.Value.SiteTextStrings.ReviewPageId) { //this page was revisited and edited from check your answers so we have a completed form if (!_pageHelper.HasAnswerChanged(Request, pageVm.Questions) && _pageHelper.IsQuestionAnswered(Request, pageVm.Questions)) { //nothings changed so bomb out _sessionService.ClearChangeMode(); return(RedirectToAction("Index", "CheckYourAnswers")); } if (pageVm.Questions.Any() && _pageHelper.HasPathChanged(Request, pageVm.Questions) && (!_sessionService.ChangedLocationMode)) { //user journey will now go down a different path //so save the page where the journey goes back to the existing path _sessionService.SaveChangeModeRedirectId(pageVm.ChangeModeTriggerPageId); if (pageVm.NextPageId == _config.Value.SiteTextStrings.ReviewPageId) { //remove any possible answered questions further along the path _sessionService.RemoveNavOrderFrom(pageVm.PageId); } else { //remove only the questions between this one and the end of the new path _sessionService.RemoveNavOrderSectionFrom(pageVm.PageId, pageVm.ChangeModeTriggerPageId); } } else { //there's been a change but no change in the path so skip all the next questions skipNextQuestions = true; } } else { if (pageVm.Questions.Any() && _pageHelper.HasPathChanged(Request, pageVm.Questions) && (!_sessionService.ChangedLocationMode)) { //page revisited from multiple back button clicks and the journey path has changed //so remove any possible answered questions further along the path _sessionService.RemoveNavOrderFrom(pageVm.PageId); //user will have to go through the entire journey again _sessionService.ClearChangeModeRedirectId(); } //the path hasn't changed e.g. "Bad" to "Good And Bad" } } } var userSession = _sessionService.GetUserSession(); if (userSession == null)//shouldn't happen as it's handled above { return(GetCustomErrorCode(EnumStatusCode.FormPageContinueSessionNullError, "Error with user session. Session is null: Id='" + vm.PageId + "'")); } if (string.IsNullOrWhiteSpace(userSession.LocationName)) { return(GetCustomErrorCode(EnumStatusCode.FormContinueLocationNullError, "Error with user session. Location is null: Id='" + vm.PageId + "'")); } var serviceNotFound = userSession.LocationName.Equals(_config.Value.SiteTextStrings.DefaultServiceName); ViewBag.BackLink = new BackLinkVM { Show = true, Url = _pageHelper.GetPreviousPage(pageVm, _sessionService, _config, Url, serviceNotFound), Text = _config.Value.SiteTextStrings.BackLinkText }; //We need to store the nextPageId before calling ValidatePage, so we can check if it gets updated by a question's answer logic var rootNextPageId = !string.IsNullOrWhiteSpace(pageVm.NextPageReferenceId) ? _pageHelper.GetNextPageIdFromPage(formVm, pageVm.NextPageReferenceId) : pageVm.NextPageId; if (Request?.Form != null) { //Validate the Response against the page json and update PageVm to contain the answers //This will also update the NextPageId with any answer logic in the questions. _gdsValidate.ValidatePage(pageVm, Request.Form, true, restrictedWords); } //Get the error count var errorCount = pageVm.Questions?.Count(m => m.Validation != null && m.Validation.IsErrored); //If we have errors return to the View if (errorCount > 0) { ViewBag.Title = "Error: " + pageVm.PageTitle + _config.Value.SiteTextStrings.SiteTitleSuffix; return(View(pageVm)); } _sessionService.ChangedLocationMode = false;//always reset this //Now we need to update the FormVM in session. _sessionService.UpdatePageVmInFormVm(pageVm); //No errors redirect to the Index page with our new PageId var nextPageId = GetNextPageId(formVm, pageVm, userSession.LocationName, skipNextQuestions, serviceNotFound, rootNextPageId); //Check the nextPageId for preset controller names switch (nextPageId) { case "HowWeUseYourInformation": return(RedirectToAction("Index", "HowWeUseYourInformation")); case "CheckYourAnswers": return(RedirectToAction("Index", "CheckYourAnswers")); case "Home": return(RedirectToAction("Index", "Home")); case "search": return(RedirectToAction("Index", "Search")); } //Finally, No Errors so load the next page return(RedirectToAction("Index", new { id = nextPageId })); } catch (Exception ex) { ex.Data.Add("GFCError", "Unexpected error updating PageVM. Id:='" + vm.PageId + "'"); throw ex; } }
public IActionResult Index(CurrentPageVM vm) { try { PageVM pageVm = null; try { //Get the current PageVm from Session (throws exception if session is null/timed out) pageVm = _sessionService.GetPageById(vm.PageId, false); } catch { return(GetCustomErrorCode(EnumStatusCode.FormPageContinueSessionNullError, "Error with user session. Session is null: Id='" + vm.PageId + "'")); } if (pageVm == null) { return(GetCustomErrorCode(EnumStatusCode.FormPageContinueNullError, "Error with user session. pageVm is null: Id='" + vm.PageId + "'")); } var skipNextQuestions = false;//is true if for example user changes from "bad" to "good and bad" /* * Commented out second part of IF statement * * Reason: this was stopping future nav order being edited properly when journey has changed. * Issue: it's not certain why this was included in the first place; it's possible that it should have been a second 'NOT' condition * Consequences: in some unusual circumstances(without this second IF section), it is possible to have a journey where you can't hit * all of the questions to edit them.We have not been able to reproduce this, however, so leaving the change in for now. * Original line: * if (!string.IsNullOrWhiteSpace(_sessionService.PageForEdit) && (string.IsNullOrWhiteSpace(_sessionService.GetChangeModeRedirectId()))) */ if (!string.IsNullOrWhiteSpace(_sessionService.PageForEdit)) // && (string.IsNullOrWhiteSpace(_sessionService.GetChangeModeRedirectId()))) { if (_sessionService.PageForEdit == pageVm.PageId) { if ((_sessionService.GetChangeMode() ?? "") == _config.Value.SiteTextStrings.ReviewPageId) { //this page was revisited and edited from check your answers so we have a completed form if (!_pageHelper.HasAnswerChanged(Request, pageVm.Questions)) { //nothings changed so bomb out _sessionService.ClearChangeMode(); return(RedirectToAction("Index", "CheckYourAnswers")); } if (pageVm.Questions.Any() && _pageHelper.HasPathChanged(Request, pageVm.Questions)) { //user journey will now go down a different path //so save the page where the journey goes back to the existing path _sessionService.SaveChangeModeRedirectId(pageVm.ChangeModeTriggerPageId); if (pageVm.NextPageId == _config.Value.SiteTextStrings.ReviewPageId) { //remove any possible answered questions further along the path _sessionService.RemoveNavOrderFrom(pageVm.PageId); } else { //remove only the questions between this one and the end of the new path _sessionService.RemoveNavOrderSectionFrom(pageVm.PageId, pageVm.ChangeModeTriggerPageId); } } else { //there's been a change but no change in the path so skip all the next questions skipNextQuestions = true; } } else { //page revisited from back button click //this would be the first journey through the questions at this point //so remove any possible answered questions further along the path _sessionService.RemoveNavOrderFrom(pageVm.PageId); } } } var userSession = _sessionService.GetUserSession(); if (userSession == null)//shouldn't happen as it's handled above { return(GetCustomErrorCode(EnumStatusCode.FormPageContinueSessionNullError, "Error with user session. Session is null: Id='" + vm.PageId + "'")); } if (string.IsNullOrWhiteSpace(userSession.LocationName)) { return(GetCustomErrorCode(EnumStatusCode.FormContinueLocationNullError, "Error with user session. Location is null: Id='" + vm.PageId + "'")); } var serviceNotFound = userSession.LocationName.Equals(_config.Value.SiteTextStrings.DefaultServiceName); ViewBag.BackLink = new BackLinkVM { Show = true, Url = _pageHelper.GetPreviousPage(pageVm, _sessionService, _config, Url, serviceNotFound), Text = _config.Value.SiteTextStrings.BackLinkText }; if (Request?.Form != null) { //Validate the Response against the page json and update PageVm to contain the answers _gdsValidate.ValidatePage(pageVm, Request.Form, true, restrictedWords); } //Get the error count var errorCount = pageVm.Questions?.Count(m => m.Validation != null && m.Validation.IsErrored); //If we have errors return to the View if (errorCount > 0) { ViewBag.Title = "Error: " + pageVm.PageTitle + _config.Value.SiteTextStrings.SiteTitleSuffix; return(View(pageVm)); } //Now we need to update the FormVM in session. _sessionService.UpdatePageVmInFormVm(pageVm); //No errors redirect to the Index page with our new PageId var nextPageId = pageVm.NextPageId; if (pageVm.PathChangeQuestion != null) { //branch the user journey if a previous question has a specific answer var formVm = _sessionService.GetFormVmFromSession(); var questions = formVm.Pages.SelectMany(m => m.Questions).ToList(); var startChangeJourney = questions.FirstOrDefault(m => m.QuestionId == pageVm.PathChangeQuestion.QuestionId); if (startChangeJourney != null && startChangeJourney.Answer == pageVm.PathChangeQuestion.Answer) { nextPageId = pageVm.PathChangeQuestion.NextPageId; } } //check if this is the end of the changed question flow in edit mode if ((_sessionService.GetChangeModeRedirectId() ?? string.Empty) == nextPageId || (_sessionService.GetChangeModeRedirectId() ?? string.Empty) == pageVm.NextPageId || skipNextQuestions) { nextPageId = _config.Value.SiteTextStrings.ReviewPageId; } //Check the nextPageId for preset controller names switch (nextPageId) { case "HowWeUseYourInformation": return(RedirectToAction("Index", "HowWeUseYourInformation")); case "CheckYourAnswers": return(RedirectToAction("Index", "CheckYourAnswers")); case "Home": return(RedirectToAction("Index", "Home")); } //Finally, No Errors so load the next page return(RedirectToAction("Index", new { id = nextPageId })); } catch (Exception ex) { ex.Data.Add("GFCError", "Unexpected error updating PageVM. Id:='" + vm.PageId + "'"); throw ex; } }