/// <summary>
        /// Rebuilds this instance.
        /// </summary>
        /// <returns></returns>
        public bool Rebuild()
        {
            List <UPQuestionnaireQuestionGroup> questionnaireGroups = new List <UPQuestionnaireQuestionGroup>();
            List <UPSurveyAnswer>   answerArray     = new List <UPSurveyAnswer>();
            UPQuestionnaireQuestion currentQuestion = this.Questionnaire.Questions[0];

            while (currentQuestion != null)
            {
                if (currentQuestion.Hide)
                {
                    currentQuestion = currentQuestion.NextQuestion;
                }
                else
                {
                    UPSurveyAnswer currentAnswer = this.Survey.AnswerDictionary.ValueOrDefault(currentQuestion.QuestionId);
                    if (currentAnswer == null)
                    {
                        return(false);   // should never happen
                    }

                    answerArray.Add(currentAnswer);
                    if (!questionnaireGroups.Contains(currentQuestion.QuestionnaireGroup))
                    {
                        questionnaireGroups.Add(currentQuestion.QuestionnaireGroup);
                    }

                    currentQuestion = currentAnswer.NextQuestion;
                }
            }

            this.VisibleQuestionnaireGroupArray = questionnaireGroups;
            this.AnswerArray = answerArray;
            return(true);
        }
Beispiel #2
0
        private void SurveyAnswersLoaded(UPCRMResult result)
        {
            int count = result.RowCount;

            for (int i = 0; i < count; i++)
            {
                UPCRMResultRow row = (UPCRMResultRow)result.ResultRowAtIndex(i);
                Dictionary <string, object> valuesWithFunction = row.ValuesWithFunctions();
                string questionKey = valuesWithFunction.ValueOrDefault(Constants.QuestionnaireQuestionNumber) as string;
                UPQuestionnaireQuestion question = this.Questionnaire?.QuestionForId(questionKey);
                if (question == null)
                {
                    continue;
                }

                UPSurveyAnswer answer = null;
                if (question.Multiple)
                {
                    answer = this.surveyAnswers.ValueOrDefault(questionKey);
                }

                if (answer == null)
                {
                    answer = new UPSurveyAnswer(row.RootRecordIdentification, valuesWithFunction, question, this);
                    this.surveyAnswers[questionKey] = answer;
                }
                else
                {
                    answer.AddAnswer(valuesWithFunction, row.RootRecordIdentification);
                }
            }

            this.crmQuery = null;
            this.LoadFinished();
        }
Beispiel #3
0
        /// <summary>
        /// Sets the answer array for question key.
        /// </summary>
        /// <param name="answerArray">The answer array.</param>
        /// <param name="questionKey">The question key.</param>
        /// <returns></returns>
        public UPSurveyActionResult SetAnswerArrayForQuestionKey(List <string> answerArray, string questionKey)
        {
            UPSurveyActionResult result = new UPSurveyActionResult();

            if (!string.IsNullOrEmpty(questionKey) && this.CurrentQuestion.QuestionId != questionKey)
            {
                UPQuestionnaireQuestion quest = this.Questionnaire.QuestionForId(questionKey);
                if (quest == null)
                {
                    result.Ok = false;
                    return(result);
                }

                this.SurveyPath.CurrentQuestion = quest;
            }
            else
            {
                questionKey = this.CurrentQuestion.QuestionId;
            }

            UPSurveyAnswer currentAnswer = this.surveyAnswers.ValueOrDefault(questionKey);

            if (currentAnswer == null)
            {
                result.Ok = false;
                return(result);
            }

            UPQuestionnaireQuestion currentNextQuestion = currentAnswer.NextQuestion;

            if (currentAnswer.Multiple)
            {
                currentAnswer.SetAnswerArray(answerArray);
            }
            else
            {
                currentAnswer.Answer = answerArray.Count > 0 ? answerArray[0] : null;
            }

            if (!this.CurrentQuestion.HasAnswerOptions)
            {
                this.SurveyPath.CurrentQuestion = currentNextQuestion;
                return(result);
            }

            UPQuestionnaireQuestion nextQuestion = currentAnswer.NextQuestion;

            if (nextQuestion != currentNextQuestion)
            {
                result.QuestionOrderChanged = true;
                this.SurveyPath.Rebuild();
            }

            this.SurveyPath.CurrentQuestion = nextQuestion;
            return(result);
        }
Beispiel #4
0
        private void LoadFinished()
        {
            if (this.Questionnaire != null)
            {
                foreach (string questionId in this.Questionnaire.QuestionsById.Keys)
                {
                    UPSurveyAnswer currentAnswer = this.surveyAnswers.ValueOrDefault(questionId);
                    if (currentAnswer == null)
                    {
                        UPQuestionnaireQuestion quest = this.Questionnaire.QuestionForId(questionId);
                        if (quest == null)
                        {
                            continue;
                        }

                        currentAnswer = new UPSurveyAnswer(quest, this);
                        if (Convert.ToInt32(quest.DefaultAnswer) > 0)
                        {
                            currentAnswer.DefaultAnswer = quest.DefaultAnswer;
                        }

                        this.surveyAnswers[questionId] = currentAnswer;
                    }
                }
            }

            if (this.ForeignFields != null)
            {
                foreach (UPSurveyForeignField foreignField in this.ForeignFields.Values)
                {
                    foreach (UPQuestionnaireQuestion quest in foreignField.QuestionArray)
                    {
                        UPSurveyAnswer currentAnswer = this.surveyAnswers[quest.QuestionId];
                        if (!string.IsNullOrEmpty(foreignField.Value))
                        {
                            currentAnswer.DefaultAnswer = foreignField.Value;
                        }

                        currentAnswer.SurveyForeignField = foreignField;
                    }
                }
            }

            this.SurveyPath = new UPSurveyPath(this);
            this.TheDelegate.SurveyDidFinishWithResult(this, this);
        }
Beispiel #5
0
        /// <summary>
        /// Gets the changed records.
        /// </summary>
        /// <param name="rootTemplateFilter">The root template filter.</param>
        /// <param name="baseTemplateFilter">The base template filter.</param>
        /// <param name="ignoreDefault">if set to <c>true</c> [ignore default].</param>
        /// <returns></returns>
        public List <UPCRMRecord> ChangedRecords(UPConfigFilter rootTemplateFilter, UPConfigFilter baseTemplateFilter, bool ignoreDefault)
        {
            UPCRMRecord rootRecord;
            bool        changedRoot = false;

            if (!string.IsNullOrEmpty(this.AnswerRootRecordIdentification))
            {
                rootRecord = new UPCRMRecord(this.AnswerRootRecordIdentification);
            }
            else
            {
                rootRecord = new UPCRMRecord(this.SurveySearchAndList.InfoAreaId);
                rootRecord.AddLink(new UPCRMLink(this.RootRecordIdentification));
                if (this.Questionnaire.Manager.LinkSurveyToQuestionnaire)
                {
                    rootRecord.AddLink(new UPCRMLink(this.Questionnaire.RecordIdentification));
                }

                if (this.SurveyTemplateFilter != null)
                {
                    UPConfigFilter filter = this.SurveyTemplateFilter.FilterByApplyingValueDictionaryDefaults(this.Parameters, true);
                    rootRecord.ApplyValuesFromTemplateFilter(filter);
                }

                changedRoot = true;
            }

            Dictionary <string, UPCRMRecord> foreignFieldDictionary = new Dictionary <string, UPCRMRecord>();
            List <UPCRMRecord> answerRecords = new List <UPCRMRecord>();

            foreach (UPQuestionnaireQuestion question in this.Questionnaire.Questions)
            {
                UPSurveyAnswer     answer = this.surveyAnswers[question.QuestionId];
                List <UPCRMRecord> currentAnswerRecords = answer.ChangedRecords(rootRecord, this.Parameters, ignoreDefault);
                if (currentAnswerRecords.Count > 0)
                {
                    answerRecords.AddRange(currentAnswerRecords);
                }

                if (question.Save)
                {
                    UPSurveyForeignField foreignField = answer.SurveyForeignField;
                    if (!string.IsNullOrEmpty(foreignField.RecordIdentification) && !string.IsNullOrEmpty(foreignField.Value) &&
                        !string.IsNullOrEmpty(answer.Answer) && foreignField.Value != answer.Answer)
                    {
                        UPCRMRecord foreignRecord = foreignFieldDictionary.ValueOrDefault(foreignField.RecordIdentification);
                        if (foreignRecord == null)
                        {
                            foreignRecord = new UPCRMRecord(foreignField.RecordIdentification);
                            foreignFieldDictionary[foreignField.RecordIdentification] = foreignRecord;
                        }

                        UPCRMFieldValue fieldValue = new UPCRMFieldValue(answer.Answer, foreignRecord.InfoAreaId, foreignField.FieldInfo.FieldId, this.Questionnaire.Manager.AutomaticOnlineSaveOfForeignFields);
                        foreignRecord.AddValue(fieldValue);
                    }
                }
            }

            List <UPCRMRecord> changedRecords = new List <UPCRMRecord>();

            if (changedRoot)
            {
                changedRecords.Add(rootRecord);
            }

            if (answerRecords.Count > 0)
            {
                changedRecords.AddRange(answerRecords);
            }

            if (changedRecords.Count > 0)
            {
                UPCRMRecord rootSyncRecord = new UPCRMRecord(rootRecord, "Sync");
                changedRecords.Add(rootSyncRecord);
            }

            if (foreignFieldDictionary.Count > 0)
            {
                changedRecords.AddRange(foreignFieldDictionary.Values);
            }

            if (rootTemplateFilter != null)
            {
                rootTemplateFilter = rootTemplateFilter.FilterByApplyingValueDictionaryDefaults(this.Parameters, true);
                if (rootTemplateFilter != null)
                {
                    UPCRMRecord record = new UPCRMRecord(rootRecord);
                    record.ApplyValuesFromTemplateFilter(rootTemplateFilter);
                    changedRecords.Add(record);
                }
            }

            if (baseTemplateFilter != null)
            {
                baseTemplateFilter = baseTemplateFilter.FilterByApplyingValueDictionaryDefaults(this.Parameters, true);
                if (baseTemplateFilter != null)
                {
                    UPCRMRecord record = new UPCRMRecord(this.RecordIdentification);
                    record.ApplyValuesFromTemplateFilter(baseTemplateFilter);
                    changedRecords.Add(record);
                }
            }

            return(changedRecords);
        }