private void CreateQuestion(PX.SurveyMonkeyReader.SurveyMonkeyReader dataReader, SurveyResponseQuestion surveyResponseQuestion)
        {
            // Question does not specify a parent question?  Set current parent question to null.
            if (surveyResponseQuestion.ParentQuestionID == null)
            {
                ParentSurveyQuestions.Current = null;
            }
            else
            {
                CRSurveyQuestion parentQuestion =
                    PXSelect <CRSurveyQuestion,
                              Where <CRSurveyQuestion.surveyQuestionID,
                                     Equal <Required <CRSurveyQuestion.surveyQuestionID> >,
                                     And <CRSurveyQuestion.surveyLastModified,
                                          Equal <Required <CRSurveyQuestion.surveyLastModified> > > > >
                    .Select(this,
                            surveyResponseQuestion.ParentQuestionID,
                            surveyResponseQuestion.SurveyLastModified);

                // Parent question not yet in DB?  Request parent question info from api and insert it into the DB
                if (parentQuestion == null)
                {
                    parentQuestion          = GetQuestionDetails(dataReader, surveyResponseQuestion.ParentQuestionID.Value);
                    SurveyQuestions.Current = null; // Don't hang onto old question info
                    parentQuestion          = ParentSurveyQuestions.Insert(parentQuestion);
                }

                ParentSurveyQuestions.Current = parentQuestion;
            }

            // Request question info from api and insert it into the DB
            var question = GetQuestionDetails(dataReader, surveyResponseQuestion.ResponseIdentifier.QuestionID);

            SurveyQuestions.Current = SurveyQuestions.Insert(question);

            // Put the answers for the newly created question into the database too
            var newSurveyAnswers = GetQuestionAnswers(dataReader, surveyResponseQuestion.ResponseIdentifier.QuestionID);

            if (newSurveyAnswers != null)
            {
                foreach (var newSurveyAnswer in newSurveyAnswers)
                {
                    SurveyAnswers.Insert(newSurveyAnswer);
                }
            }
        }
Exemple #2
0
        public List <SurveyResponse> GetSurveyResponsesByIdAndDateRange(string surveyId, DateTime?startDate, DateTime?endDate, int page, out bool isLastPage)
        {
            var surveyResponseList = new List <SurveyResponse>();
            var allSurveyQuestions = GetSurveyQuestions(surveyId);

            var surveyJsonString = _commands.GetSurveyResponsesByIdAndDateRange(surveyId, startDate, endDate, page);

            JObject surveyJson;

            if (!JObjectExt.TryParse(surveyJsonString, out surveyJson))
            {
                isLastPage = true;
                return(surveyResponseList);
            }

            if (surveyJson.SelectToken("data") == null)
            {
                isLastPage = true;
                return(surveyResponseList);
            }

            var totalResponses = long.Parse(surveyJson.SelectToken("total").ToString());

            isLastPage = (totalResponses <= _commands.ResultsPerPage * page);

            foreach (var dataItem in surveyJson.SelectToken("data").Children())
            {
                var surveyResponseCustomValues = GetSurveyResponseCustomData(dataItem.SelectToken("custom_value"));
                if (surveyResponseCustomValues == null)
                {
                    continue;
                }

                var allAnsweredQuestions       = new Dictionary <QuestionAnswerIdentifier, SurveyResponseQuestion>();
                var surveyResponseQuestionList = new List <SurveyResponseQuestion>();
                foreach (var pageItem in dataItem.SelectToken("pages").Children())
                {
                    foreach (var question in pageItem.SelectToken("questions").Children())
                    {
                        var questionHeaderId = question.SelectToken("id").ToString();

                        foreach (var answer in question.SelectToken("answers").Children())
                        {
                            var choiceId = answer.SelectToken("choice_id");
                            var rowId    = answer.SelectToken("row_id");
                            var text     = answer.SelectToken("text");
                            var otherId  = answer.SelectToken("other_id");

                            SurveyResponseQuestion newResponseQuestion = null;
                            if (choiceId != null && rowId != null)
                            {
                                newResponseQuestion = new SurveyResponseQuestion
                                {
                                    ParentQuestionID   = long.Parse(questionHeaderId),
                                    ResponseIdentifier = new QuestionAnswerIdentifier
                                    {
                                        QuestionID = long.Parse(rowId.ToString()),
                                        AnswerID   = long.Parse(choiceId.ToString())
                                    },
                                    Answer = null
                                };
                            }
                            else if (choiceId != null)
                            {
                                newResponseQuestion = new SurveyResponseQuestion
                                {
                                    ParentQuestionID   = null,
                                    ResponseIdentifier = new QuestionAnswerIdentifier
                                    {
                                        QuestionID = long.Parse(questionHeaderId),
                                        AnswerID   = long.Parse(choiceId.ToString())
                                    },
                                    Answer = null
                                };
                            }
                            else if (otherId != null && text != null)
                            {
                                newResponseQuestion = new SurveyResponseQuestion
                                {
                                    ParentQuestionID   = null,
                                    ResponseIdentifier = new QuestionAnswerIdentifier
                                    {
                                        QuestionID = long.Parse(otherId.ToString()),
                                        AnswerID   = 0
                                    },
                                    Answer = text.ToString()
                                };
                            }
                            else if (text != null)
                            {
                                newResponseQuestion = new SurveyResponseQuestion
                                {
                                    ParentQuestionID   = null,
                                    ResponseIdentifier = new QuestionAnswerIdentifier
                                    {
                                        QuestionID = long.Parse(questionHeaderId),
                                        AnswerID   = 0
                                    },
                                    Answer = text.ToString()
                                };
                            }

                            if (newResponseQuestion != null)
                            {
                                allAnsweredQuestions.Add(newResponseQuestion.ResponseIdentifier, newResponseQuestion);
                                surveyResponseQuestionList.Add(newResponseQuestion);
                            }
                        }
                    }
                }

                // Add rows for questions that weren't answered by the respondent, in case we want to add answers for them later
                var allAvailableQuestions = GetSurveyQuestions(surveyId);
                foreach (var availableQuestion in allAvailableQuestions)
                {
                    var answeredQuestion = from answers in allAnsweredQuestions
                                           where answers.Key.QuestionID == availableQuestion.QuestionID
                                           select answers;

                    if (!answeredQuestion.Any())
                    {
                        surveyResponseQuestionList.Add(new SurveyResponseQuestion
                        {
                            ParentQuestionID   = availableQuestion.ParentQuestionID,
                            ResponseIdentifier = new QuestionAnswerIdentifier
                            {
                                QuestionID = availableQuestion.QuestionID,
                                AnswerID   = 0
                            },
                            Answer = null
                        });
                    }
                    else
                    {
                        foreach (var answer in answeredQuestion)
                        {
                            surveyResponseQuestionList.Add(new SurveyResponseQuestion
                            {
                                ParentQuestionID   = answer.Value.ParentQuestionID,
                                ResponseIdentifier = new QuestionAnswerIdentifier
                                {
                                    QuestionID = answer.Key.QuestionID,
                                    AnswerID   = answer.Key.AnswerID
                                },
                                Answer = answer.Value.Answer
                            });
                        }
                    }
                }

                surveyResponseList.Add(new SurveyResponse
                {
                    CaseCD       = surveyResponseCustomValues[3],
                    ResponseID   = long.Parse(dataItem.SelectToken("id").ToString()),
                    ResponseDate = DateTime.Parse(dataItem.SelectToken("date_modified").ToString()),
                    Questions    = surveyResponseQuestionList
                });
            }

            return(surveyResponseList);
        }
        public void SetQuestionAnswer(PX.SurveyMonkeyReader.SurveyMonkeyReader dataReader, SurveyResponseQuestion surveyResponseQuestion)
        {
            var question = (CRSurveyQuestion)PXSelect <CRSurveyQuestion,
                                                       Where <CRSurveyQuestion.surveyQuestionID,
                                                              Equal <Required <CRSurveyQuestion.surveyQuestionID> >,
                                                              And <CRSurveyQuestion.surveyLastModified,
                                                                   Equal <Required <CRSurveyQuestion.surveyLastModified> > > > >
                           .Select(this, surveyResponseQuestion.ResponseIdentifier.QuestionID, surveyResponseQuestion.SurveyLastModified);

            // Question already in DB?  Set the current question to it
            if (question != null)
            {
                SurveyQuestions.Current = question;
            }
            else
            {
                CreateQuestion(dataReader, surveyResponseQuestion);
            }

            // If the current question's answers can be found in the answers table, use the answer ID for the current answer.
            // Otherwise, the answer is stored as freetext in the survey responses table, so don't store it in the current answer at all
            SurveyAnswers.Current = surveyResponseQuestion.ResponseIdentifier.AnswerID > 0
                                             ? SurveyAnswers.Search <CRSurveyAnswer.surveyAnswerID>(surveyResponseQuestion.ResponseIdentifier.AnswerID)
                                             : null;

            //Other CRCaseSurveyResponse fields will be field based on current values for other views through PXDBDefault
            var caseSurveyResponse = new CRCaseSurveyResponse {
                Answer = surveyResponseQuestion.Answer
            };

            if (surveyResponseQuestion.ResponseIdentifier.AnswerID == 0)
            {
                caseSurveyResponse.AnswerLineNbr = 0;
            }
            CaseSurveyResponses.Insert(caseSurveyResponse);
        }