/// <summary> /// Creates the first survey response in the response table /// </summary> /// <param name="responseContext"></param> /// <param name="surveyAnswerRequest1"></param> /// <param name="surveyAnswerDTO"></param> /// <param name="surveyResponseBuilder"></param> /// <param name="dataEntryService"></param> /// <param name="isEditMode"></param> /// <param name="currentOrgId"></param> /// <returns></returns> public static SurveyAnswerDTO CreateSurveyResponse(IResponseContext responseContext, SurveyAnswerRequest surveyAnswerRequest1, SurveyAnswerDTO surveyAnswerDTO, SurveyResponseBuilder surveyResponseBuilder, IDataEntryService dataEntryService) { SurveyAnswerRequest surveyAnswerRequest = new SurveyAnswerRequest(); surveyAnswerRequest.ResponseContext = responseContext; FormResponseDetail responseDetail = responseContext.ToFormResponseDetail(); surveyAnswerDTO.ResponseDetail = responseDetail; responseContext.ToSurveyAnswerDTOLite(surveyAnswerDTO); surveyAnswerDTO.DateCreated = DateTime.UtcNow; surveyAnswerDTO.SurveyId = responseContext.FormId; surveyAnswerDTO.Status = RecordStatus.InProcess; surveyAnswerDTO.LastActiveOrgId = responseContext.UserOrgId; surveyAnswerDTO.LastActiveUserId = responseContext.UserId; surveyAnswerDTO.LoggedInUserOrgId = responseContext.UserOrgId; surveyAnswerDTO.LoggedInUserId = responseContext.UserId; surveyAnswerDTO.RecordSourceId = RecordSource.CloudEnter; surveyAnswerDTO.ParentResponseId = responseContext.ParentResponseId; surveyAnswerRequest.Criteria.UserOrganizationId = responseContext.UserOrgId; surveyAnswerRequest.Criteria.UserId = responseContext.UserId; surveyAnswerRequest.Criteria.UserName = responseContext.UserName; surveyAnswerRequest.SurveyAnswerList.Add(surveyAnswerDTO); surveyAnswerRequest.Action = responseContext.IsChildResponse ? RequestAction.CreateChild : RequestAction.Create; dataEntryService.SetSurveyAnswer(surveyAnswerRequest); return(surveyAnswerDTO); }
public static void UpdateSurveyResponse(SurveyInfoModel surveyInfoModel, MvcDynamicForms.Form form, SurveyAnswerRequest surveyAnswerRequest, SurveyResponseBuilder surveyResponseBuilder, IDataEntryService dataEntryService, SurveyAnswerResponse surveyAnswerResponse, string responseId, SurveyAnswerDTO surveyAnswerDTO, bool isSubmited, bool isSaved, int pageNumber, int orgId, int userId) { // 1 Get the record for the current survey response // 2 update the current survey response // 3 save the current survey response var savedResponseDetail = surveyAnswerDTO.ResponseDetail; if (!isSubmited) { // 2 a. update the current survey answer request surveyAnswerRequest.SurveyAnswerList = surveyAnswerResponse.SurveyResponseList; surveyResponseBuilder.Add(form); FormResponseDetail formResponseDetail = surveyResponseBuilder.UpdateResponseDetail(savedResponseDetail, form.CurrentPage, form.PageId); surveyAnswerRequest.SurveyAnswerList[0].ResponseDetail = formResponseDetail; // 2 b. save the current survey response surveyAnswerRequest.Action = RequestAction.Update; var currentPageNumber = form.CurrentPage; FormResponseDetail currentFormResponseDetail = surveyAnswerRequest.SurveyAnswerList[0].ResponseDetail; PageResponseDetail currentPageResponseDetail = currentFormResponseDetail.GetPageResponseDetailByPageNumber(currentPageNumber); var mergedResponseDetail = MergeResponseDetail(savedResponseDetail, currentPageResponseDetail); surveyAnswerRequest.SurveyAnswerList[0].ResponseDetail.PageIds = mergedResponseDetail.PageIds; // keep only the pages that have updates var updatedPageResponseDetailList = mergedResponseDetail.PageResponseDetailList.ToList(); surveyAnswerRequest.SurveyAnswerList[0].ResponseDetail.PageResponseDetailList.Clear(); surveyAnswerRequest.SurveyAnswerList[0].ResponseDetail.PageResponseDetailList.AddRange(updatedPageResponseDetailList); } var updatedFromResponseDetail = surveyAnswerRequest.SurveyAnswerList[0].ResponseDetail; if (updatedFromResponseDetail.UserId <= 0) { updatedFromResponseDetail.UserId = userId; } if (updatedFromResponseDetail.UserOrgId <= 0) { updatedFromResponseDetail.UserOrgId = orgId; } ////Update page number before saving response if (surveyAnswerRequest.SurveyAnswerList[0].CurrentPageNumber != 0) { updatedFromResponseDetail.LastPageVisited = pageNumber; } if (form.HiddenFieldsList != null) { updatedFromResponseDetail.HiddenFieldsList = form.HiddenFieldsList; } if (form.HighlightedFieldsList != null) { updatedFromResponseDetail.HighlightedFieldsList = form.HighlightedFieldsList; } if (form.DisabledFieldsList != null) { updatedFromResponseDetail.DisabledFieldsList = form.DisabledFieldsList; } if (form.RequiredFieldsList != null) { updatedFromResponseDetail.RequiredFieldsList = form.RequiredFieldsList; } // AssignList List <KeyValuePair <string, string> > fieldsList = GetHiddenFieldsList(form).Where(kvp => kvp.Value != null).ToList(); if (fieldsList.Count > 0) { var formId = form.SurveyInfo.SurveyId; var metadataAccessor = form.SurveyInfo as MetadataAccessor; var formDigest = metadataAccessor.GetFormDigest(formId); foreach (var fieldsListKvp in fieldsList) { var fieldName = fieldsListKvp.Key.ToLower(); var pageId = formDigest.FieldNameToPageId(fieldName); var pageResponseDetail = updatedFromResponseDetail.PageResponseDetailList.SingleOrDefault(p => p.PageId == pageId); if (pageResponseDetail == null) { var pageDigest = metadataAccessor.GetPageDigestByPageId(formId, pageId); pageResponseDetail = new PageResponseDetail { PageId = pageId, PageNumber = pageDigest.PageNumber }; updatedFromResponseDetail.AddPageResponseDetail(pageResponseDetail); } pageResponseDetail.ResponseQA[fieldName] = fieldsListKvp.Value; pageResponseDetail.HasBeenUpdated = true; } } if (isSaved) { surveyAnswerRequest.SurveyAnswerList[0].Status = RecordStatus.Saved; surveyAnswerRequest.SurveyAnswerList[0].ReasonForStatusChange = RecordStatusChangeReason.Update; } /////Update Survey Mode //////////////////// surveyAnswerRequest.SurveyAnswerList[0].IsDraftMode = surveyAnswerDTO.IsDraftMode; //surveyAnswerRequest.Criteria.UserId = UserId; ResponseContext responseContext = ((IResponseContext)updatedFromResponseDetail).CloneResponseContext(); if (responseContext.UserId == 0) { responseContext.UserId = userId; } if (responseContext.UserOrgId == 0) { responseContext.UserOrgId = orgId; } surveyAnswerRequest.ResponseContext = responseContext; dataEntryService.SetSurveyAnswer(surveyAnswerRequest); }