示例#1
0
        public DateTime?GetSurveyLastModifiedDateTime(string surveyId)
        {
            if (_surveyLastModifiedDateTimeCache != null)
            {
                return(_surveyLastModifiedDateTimeCache);
            }

            var     surveyJsonString = _commands.GetSurveyDetailsById(surveyId);
            JObject surveyJson;

            if (!JObjectExt.TryParse(surveyJsonString, out surveyJson))
            {
                return(null);
            }

            _surveyLastModifiedDateTimeCache = DateTime.Parse(surveyJson.SelectToken("date_modified").ToString());
            return(_surveyLastModifiedDateTimeCache);
        }
示例#2
0
        public string GetAccessToken(string code)
        {
            var accessTokenJsonString = _commands.GetAccessToken(code);

            JObject accessTokenJson;

            if (!JObjectExt.TryParse(accessTokenJsonString, out accessTokenJson))
            {
                return(null);
            }

            if (accessTokenJson.SelectToken("access_token") != null)
            {
                return(accessTokenJson.SelectToken("access_token").ToString());
            }

            if (accessTokenJson.SelectToken("error") != null)
            {
                throw new Exception(string.Concat(accessTokenJson.SelectToken("error").ToString(),
                                                  accessTokenJson.SelectToken("error_description")?.ToString()));
            }

            throw new Exception("Could not retrieve an access token");
        }
示例#3
0
        public List <SurveyQuestion> GetSurveyQuestions(string surveyId)
        {
            if (_surveyQuestionListCache != null)
            {
                return(_surveyQuestionListCache);
            }

            var surveyQuestions = new List <SurveyQuestion>();

            var surveyJsonString = _commands.GetSurveyDetailsById(surveyId);

            JObject surveyJson;

            if (!JObjectExt.TryParse(surveyJsonString, out surveyJson))
            {
                return(surveyQuestions);
            }

            var surveyLastModified = GetSurveyLastModifiedDateTime(surveyId).GetValueOrDefault();

            foreach (var contentPage in surveyJson.SelectToken("pages").Children())
            {
                foreach (var pageQuestion in contentPage.SelectToken("questions").Children())
                {
                    var  questionFamily = pageQuestion.SelectToken("family").ToString();
                    long tempQuestionId;
                    if (!string.Equals(questionFamily, "matrix"))
                    {
                        tempQuestionId = long.Parse(pageQuestion.SelectToken("id").ToString());
                        surveyQuestions.Add(new SurveyQuestion
                        {
                            SurveyLastModified = surveyLastModified,
                            ParentQuestionID   = null,
                            QuestionID         = tempQuestionId,
                            Question           = pageQuestion.SelectToken("headings")[0].SelectToken("heading").ToString(),
                            Answers            = GetSurveyAnswersForSingleQuestion(surveyId, tempQuestionId)
                        });

                        if (pageQuestion.SelectToken("answers") != null &&
                            pageQuestion.SelectToken("answers").SelectToken("other") != null)
                        {
                            tempQuestionId = long.Parse(pageQuestion.SelectToken("answers").SelectToken("other").SelectToken("id").ToString());
                            surveyQuestions.Add(new SurveyQuestion
                            {
                                SurveyLastModified = surveyLastModified,
                                ParentQuestionID   = null,
                                QuestionID         = long.Parse(pageQuestion.SelectToken("answers").SelectToken("other").SelectToken("id").ToString()),
                                Question           = pageQuestion.SelectToken("answers").SelectToken("other").SelectToken("text").ToString(),
                                Answers            = GetSurveyAnswersForSingleQuestion(surveyId, tempQuestionId)
                            });
                        }

                        continue;
                    }

                    var parentQuestionId = long.Parse(pageQuestion.SelectToken("id").ToString());

                    foreach (var questionRow in pageQuestion.SelectToken("answers").SelectToken("rows").Children())
                    {
                        tempQuestionId = long.Parse(questionRow.SelectToken("id").ToString());
                        surveyQuestions.Add(new SurveyQuestion
                        {
                            SurveyLastModified = surveyLastModified,
                            ParentQuestionID   = parentQuestionId,
                            QuestionID         = tempQuestionId,
                            Question           = questionRow.SelectToken("text").ToString(),
                            Answers            = GetSurveyAnswersForSingleQuestion(surveyId, tempQuestionId)
                        });
                    }

                    if (pageQuestion.SelectToken("answers").SelectToken("other") != null)
                    {
                        tempQuestionId = long.Parse(pageQuestion.SelectToken("answers").SelectToken("other").SelectToken("id").ToString());
                        surveyQuestions.Add(new SurveyQuestion
                        {
                            SurveyLastModified = surveyLastModified,
                            ParentQuestionID   = parentQuestionId,
                            QuestionID         = tempQuestionId,
                            Question           = pageQuestion.SelectToken("answers").SelectToken("other").SelectToken("text").ToString(),
                            Answers            = GetSurveyAnswersForSingleQuestion(surveyId, tempQuestionId)
                        });
                    }

                    tempQuestionId = long.Parse(pageQuestion.SelectToken("id").ToString());
                    surveyQuestions.Add(new SurveyQuestion
                    {
                        SurveyLastModified = surveyLastModified,
                        ParentQuestionID   = null,
                        QuestionID         = tempQuestionId,
                        Question           = pageQuestion.SelectToken("headings")[0].SelectToken("heading").ToString(),
                        Answers            = GetSurveyAnswersForSingleQuestion(surveyId, tempQuestionId)
                    });
                }
            }

            _surveyQuestionListCache = surveyQuestions;
            return(_surveyQuestionListCache);
        }
示例#4
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);
        }
示例#5
0
        public List <SurveyAnswer> GetSurveyAnswers(string surveyId)
        {
            if (_surveyAnswerListCache != null)
            {
                return(_surveyAnswerListCache);
            }

            var surveyAnswers = new List <SurveyAnswer>();

            var surveyJsonString = _commands.GetSurveyDetailsById(surveyId);

            JObject surveyJson;

            if (!JObjectExt.TryParse(surveyJsonString, out surveyJson))
            {
                return(surveyAnswers);
            }

            foreach (var contentPage in surveyJson.SelectToken("pages").Children())
            {
                foreach (var pageQuestion in contentPage.SelectToken("questions").Children())
                {
                    if (pageQuestion.SelectToken("answers") == null)
                    {
                        continue;
                    }

                    var questionFamily = pageQuestion.SelectToken("family").ToString();
                    if (!string.Equals(questionFamily, "matrix"))
                    {
                        if (pageQuestion.SelectToken("answers").SelectToken("choices") == null)
                        {
                            continue;
                        }

                        var questionId = long.Parse(pageQuestion.SelectToken("id").ToString());
                        foreach (var questionChoice in pageQuestion.SelectToken("answers").SelectToken("choices").Children())
                        {
                            surveyAnswers.Add(new SurveyAnswer
                            {
                                QuestionID = questionId,
                                AnswerID   = long.Parse(questionChoice.SelectToken("id").ToString()),
                                Answer     = questionChoice.SelectToken("text").ToString()
                            });
                        }

                        continue;
                    }

                    foreach (var questionRow in pageQuestion.SelectToken("answers").SelectToken("rows").Children())
                    {
                        var questionID = long.Parse(questionRow.SelectToken("id").ToString());

                        foreach (var questionChoice in pageQuestion.SelectToken("answers").SelectToken("choices").Children())
                        {
                            surveyAnswers.Add(new SurveyAnswer
                            {
                                QuestionID = questionID,
                                AnswerID   = long.Parse(questionChoice.SelectToken("id").ToString()),
                                Answer     = questionChoice.SelectToken("text").ToString()
                            });
                        }
                    }
                }
            }

            _surveyAnswerListCache = surveyAnswers;
            return(_surveyAnswerListCache);
        }