Пример #1
0
        /// <summary>
        /// Replace the pipe token with the question answers
        /// </summary>
        protected virtual string PipeQuestionAnswers(int questionId, VoterAnswersData.VotersAnswersDataTable surveyAnswers, string languageCode)
        {
            StringBuilder builder = new StringBuilder();

            VoterAnswersData.VotersAnswersRow[] rowArray = (VoterAnswersData.VotersAnswersRow[])surveyAnswers.Select("QuestionId=" + questionId);
            int index = 0;

            while (index < rowArray.Length)
            {
                PipeData.AnswersRow[] rowArray2 = (PipeData.AnswersRow[]) this._pipeData.Answers.Select("AnswerId=" + rowArray[index].AnswerId);
                if (rowArray2.Length > 0)
                {
                    builder.Append(rowArray2[0].AnswerText);
                }
                index++;
                if ((index + 1) == rowArray.Length)
                {
                    builder.Append(ResourceManager.GetString("PipeValuesSeparator", languageCode));
                }
                else if (index < rowArray.Length)
                {
                    builder.Append(", ");
                }
            }
            return(builder.ToString());
        }
Пример #2
0
 /// <summary>
 /// Replace the pipe token with the answer text entered by
 /// the user
 /// </summary>
 protected virtual string PipeAnswerText(int answerId, VoterAnswersData.VotersAnswersDataTable surveyAnswers)
 {
     VoterAnswersData.VotersAnswersRow[] rowArray = (VoterAnswersData.VotersAnswersRow[])surveyAnswers.Select("AnswerId=" + answerId);
     if (rowArray.Length > 0)
     {
         return(rowArray[0].AnswerText);
     }
     return(string.Empty);
 }
Пример #3
0
        /// <summary>
        /// Parse the source text for pipe token and replace
        /// the token with the correct text depending on the
        /// given survey answers
        /// </summary>
        public string PipeValuesInText(int questionId, string sourceText, VoterAnswersData.VotersAnswersDataTable surveyAnswers, string languageCode)
        {
            Regex regex = new Regex(@"\[{2}((\S)+)\]{2}");

            if (surveyAnswers != null)
            {
                Match match = regex.Match(sourceText);
                if (match.Success)
                {
                    if (HttpContext.Current != null)
                    {
                        if (HttpContext.Current.Cache["NSurvey:PipeData"] == null)
                        {
                            HttpContext.Current.Cache.Insert("NSurvey:PipeData", new Surveys().GetSurveyPipeDataFromQuestionId(questionId), null, DateTime.Now.AddMinutes(1.0), TimeSpan.Zero);
                        }
                        this._pipeData = (PipeData)HttpContext.Current.Cache["NSurvey:PipeData"];
                    }
                    else
                    {
                        this._pipeData = new Surveys().GetSurveyPipeDataFromQuestionId(questionId);
                    }
                    while (match.Success)
                    {
                        string str = match.Groups[1].ToString();
                        if (str.Length > 0)
                        {
                            PipeData.QuestionsRow[] rowArray = (PipeData.QuestionsRow[]) this._pipeData.Questions.Select("QuestionPipeAlias='" + str + "'");
                            if (rowArray.Length > 0)
                            {
                                sourceText = sourceText.Replace("[[" + str + "]]", this.PipeQuestionAnswers(rowArray[0].QuestionId, surveyAnswers, languageCode));
                            }
                            else
                            {
                                PipeData.AnswersRow[] rowArray2 = (PipeData.AnswersRow[]) this._pipeData.Answers.Select("AnswerPipeAlias='" + str + "'");
                                if (rowArray2.Length > 0)
                                {
                                    sourceText = sourceText.Replace("[[" + str + "]]", this.PipeAnswerText(rowArray2[0].AnswerId, surveyAnswers));
                                }
                            }
                        }
                        match = match.NextMatch();
                    }
                }
                match = regex.Match(sourceText);
                if ((match.Length > 0) && (this.previousMatchCount != match.Length))
                {
                    this.previousMatchCount = match.Length;
                    return(this.PipeValuesInText(questionId, sourceText, surveyAnswers, languageCode));
                }
            }
            return(sourceText);
        }
        /// <summary>
        /// Returns if answer has been selected by the user
        /// </summary>
        /// <param name="answer"></param>
        /// <param name="voterAnswersState"></param>
        /// <returns></returns>
        private static bool IsUserSelected(int answerId, int sectionNumber, VoterAnswersData.VotersAnswersDataTable voterAnswersState)
        {
            bool flag = false;

            if ((voterAnswersState != null) && (voterAnswersState.Rows.Count > 0))
            {
                VoterAnswersData.VotersAnswersRow[] rowArray = (VoterAnswersData.VotersAnswersRow[])voterAnswersState.Select(string.Concat(new object[] { "AnswerId = ", answerId, " AND SectionNumber=", sectionNumber }));
                if (rowArray.Length > 0)
                {
                    flag = true;
                }
            }
            return(flag);
        }
        /// <summary>
        /// Returns, if any, the text entered by the user
        /// </summary>
        /// <param name="answer"></param>
        /// <param name="voterAnswersState"></param>
        /// <returns></returns>
        private static string GetUserText(int answerId, int sectionNumber, VoterAnswersData.VotersAnswersDataTable voterAnswersState)
        {
            string answerText = null;

            if ((voterAnswersState != null) && (voterAnswersState.Rows.Count > 0))
            {
                VoterAnswersData.VotersAnswersRow[] rowArray = (VoterAnswersData.VotersAnswersRow[])voterAnswersState.Select(string.Concat(new object[] { "AnswerId = ", answerId, " AND SectionNumber=", sectionNumber }));
                if (rowArray.Length > 0)
                {
                    answerText = rowArray[0].AnswerText;
                }
            }
            return(answerText);
        }
        public static QuestionItem Create(QuestionData.QuestionsRow question, string languageCode, string parentControlUniqueID,
                                          int questionRandomSeed, VoterAnswersData.VotersAnswersDataTable voterAnswersState,
                                          bool enableAnswerDefaults, bool isDesignMode = false)
        {
            QuestionItem item;

            if ((question.TypeAssembly.Length == 0) || (question.TypeAssembly == null))
            {
                throw new ApplicationException("Could not create an instance because the question data has no assembly type specified");
            }
            try
            {
                item = (QuestionItem)Assembly.Load(question.TypeAssembly).CreateInstance(question.TypeNameSpace);
            }
            catch (InvalidCastException)
            {
                throw new InvalidCastException("Concrete type " + question.TypeNameSpace + " is not a child of the questionitem abstract class");
            }
            catch (NullReferenceException)
            {
                throw new InvalidCastException("specfied type " + question.TypeNameSpace + " could not be found in the specifed assembly " + question.TypeAssembly);
            }
            item.ID         = "Question" + question.QuestionId.ToString();
            item.QuestionId = question.QuestionId;
            item.Text       = new PipeManager().PipeValuesInText(question.QuestionId, question.QuestionText +
                                                                 ((question.ShowHelpText && !isDesignMode)?(
                                                                      question.IsHelpTextNull()?string.Empty: HelpTextInSmallFont(question.HelpText)):string.Empty)//JJSurveyBoxChange
                                                                 , voterAnswersState, languageCode);
            item.LanguageCode    = languageCode;
            item.QuestionId_Text = question.QuestionIdText;
            item.HelpText        = question.HelpText;
            SectionQuestion question2 = item as SectionQuestion;

            if ((question2 != null) && (question.RepeatableSectionModeId != 0))
            {
                question2.RepeatMode  = (RepeatableSectionMode)question.RepeatableSectionModeId;
                question2.MaxSections = question.MaxSections;
                question2.GridAnswers = new Questions().GetQuestionSectionGridAnswers(question.QuestionId);
                if ((question.AddSectionLinkText != null) && (question.AddSectionLinkText.Length > 0))
                {
                    question2.AddSectionLinkText = question.AddSectionLinkText;
                }
                if ((question.DeleteSectionLinkText != null) && (question.DeleteSectionLinkText.Length > 0))
                {
                    question2.DeleteSectionLinkText = question.DeleteSectionLinkText;
                }
                if ((question.EditSectionLinkText != null) && (question.EditSectionLinkText.Length > 0))
                {
                    question2.EditSectionLinkText = question.EditSectionLinkText;
                }
                if ((question.UpdateSectionLinkText != null) && (question.UpdateSectionLinkText.Length > 0))
                {
                    question2.UpdateSectionLinkText = question.UpdateSectionLinkText;
                }
            }
            SingleQuestion question3 = item as SingleQuestion;

            if (question3 != null)
            {
                question3.VoterAnswersState    = voterAnswersState;
                question3.EnableAnswersDefault = enableAnswerDefaults;
                question3.MinSelectionRequired = question.MinSelectionRequired;
                question3.MaxSelectionAllowed  = question.MaxSelectionAllowed;
                question3.ColumnsNumber        = question.ColumnsNumber;
                question3.LayoutMode           = question.IsLayoutModeIdNull() ? QuestionLayoutMode.Vertical : ((QuestionLayoutMode)question.LayoutModeId);
                if (question.RandomizeAnswers)
                {
                    question3.DataSource = new Answers().GetRandomAnswers(question.QuestionId, questionRandomSeed, languageCode);
                    return(question3);
                }
                question3.DataSource = new Answers().GetAnswers(question.QuestionId, languageCode);
                return(question3);
            }
            MatrixQuestion question4 = item as MatrixQuestion;

            if (question4 != null)
            {
                question4.VoterAnswersState    = voterAnswersState;
                question4.EnableAnswersDefault = enableAnswerDefaults;
                question4.MinSelectionRequired = question.MinSelectionRequired;
                question4.MaxSelectionAllowed  = question.MaxSelectionAllowed;
                question4.DataSource           = new Questions().GetMatrixChildQuestions(question.QuestionId, languageCode);
                return(question4);
            }
            return(item);
        }
        /// <summary>
        /// Converts a stronlgy typed MatrixChildQuestionData dataset
        /// to a webcontrol collection
        /// </summary>
        /// <param name="childQuestions">The questions / answers data</param>
        /// <param name="parentControlUniqueID">
        /// Unique ID required to identify global groups
        /// like radiobutton groups
        /// </param>
        /// <returns>a web control collection of MatrixChildQuestion</returns>
        public static MatrixChildCollection CreateQuestionChildCollection(MatrixChildQuestionData childQuestions, Section sectionContainer, string languageCode, string parentControlUniqueID, AnswerSelectionMode selectionMode, Style answerStyle, ControlRenderMode renderMode, VoterAnswersData.VotersAnswersDataTable voterAnswersState, bool enableAnswersDefault)
        {
            MatrixChildCollection childs = new MatrixChildCollection();

            foreach (MatrixChildQuestionData.ChildQuestionsRow row in childQuestions.ChildQuestions.Rows)
            {
                MatrixChildQuestion question = new MatrixChildQuestion();
                question.QuestionId = row.QuestionId;
                question.Text       = new PipeManager().PipeValuesInText(row.QuestionId, row.QuestionText, voterAnswersState, languageCode);
                AnswerData answers = new AnswerData();

//                answers.Merge(row.GetAnswersRows());

                MatrixChildQuestionData.AnswersRow[] ar = row.GetAnswersRows();
                foreach (MatrixChildQuestionData.AnswersRow r in ar)
                {
                    r.Table.Namespace = answers.Namespace;
                }
                answers.Merge(ar);

                question.Answers = AnswerItemFactory.CreateAnswerItemCollection(answers, question, sectionContainer, selectionMode, answerStyle, renderMode, languageCode, parentControlUniqueID, false, voterAnswersState, enableAnswersDefault);
                childs.Add(question);
            }
            return(childs);
        }
        /// <summary>
        /// Creates a new answer item instance from the db answer data
        /// </summary>
        /// <param name="answer">The data source to create the instance from</param>
        /// <param name="question">The question container</param>
        /// <param name="defaultSelectionMode">The default selection mode for the
        /// item (Radio, checkbox etc...)</param>
        /// <param name="parentControlUniqueID">
        /// Unique ID required to identify global selection groups
        /// like radiobutton groups
        /// </param>
        /// <param name="showAnswerText">
        /// Assigns the text to the answeritem control ?
        /// </param>
        /// <param name="enableDefaults">
        /// Does the answer set the user default values of fields to the answer web controls
        /// </param>
        public static AnswerItem Create(AnswerData.AnswersRow answer, QuestionItem question, Section section, AnswerSelectionMode defaultSelectionMode, Style answerStyle, ControlRenderMode renderMode, string languageCode, string parentControlUniqueID, bool showAnswerText, VoterAnswersData.VotersAnswersDataTable voterAnswersState, bool enableDefaults)
        {
            AnswerItem item = null;

            try
            {
                if (answer.TypeAssembly != null)
                {
                    item                  = (AnswerItem)Assembly.Load(answer.TypeAssembly).CreateInstance(answer.TypeNameSpace);
                    item.AnswerId         = answer.AnswerId;
                    item.ImageUrl         = answer.IsImageURLNull() ? null : answer.ImageURL;
                    item.ID               = (section == null) ? (GlobalConfig.AnswerItemName + answer.AnswerId) : string.Concat(new object[] { GlobalConfig.AnswerItemName, answer.AnswerId, GlobalConfig.AnswerSectionName, section.SectionUid });
                    item.AnswerStyle      = answerStyle;
                    item.Question         = question;
                    item.QuestionId       = answer.QuestionId;
                    item.RenderMode       = renderMode;
                    item.Text             = new PipeManager().PipeValuesInText(answer.QuestionId, answer.AnswerText, voterAnswersState, languageCode);
                    item.ShowAnswerText   = showAnswerText;
                    item.SectionContainer = section;
                    item.LanguageCode     = languageCode;
                }
            }
            catch (FileNotFoundException)
            {
                item          = new AnswerTextItem();
                item.AnswerId = -1;
                item.Text     = string.Format(ResourceManager.GetString("AnswerTypeAssemblyNotFoundMessage"), answer.TypeAssembly);
                return(item);
            }
            catch (NullReferenceException)
            {
                item          = new AnswerTextItem();
                item.AnswerId = -1;
                item.Text     = string.Format(ResourceManager.GetString("AnswerTypeNotFoundMessage"), answer.TypeNameSpace, answer.TypeAssembly);
                return(item);
            }
            catch (InvalidCastException)
            {
                item          = new AnswerTextItem();
                item.AnswerId = -1;
                item.Text     = string.Format(ResourceManager.GetString("AnswerTypeInvalidMessage"), answer.TypeNameSpace, answer.TypeAssembly);
                return(item);
            }
            if (item is IClientScriptValidator)
            {
                IClientScriptValidator validator = (IClientScriptValidator)item;
                validator.JavascriptCode         = answer.JavascriptCode;
                validator.JavascriptFunctionName = answer.JavascriptFunctionName;
                if (answer.JavascriptErrorMessage != null)
                {
                    validator.JavascriptErrorMessage = (ResourceManager.GetString(answer.JavascriptErrorMessage, languageCode) == null) ? answer.JavascriptErrorMessage : ResourceManager.GetString(answer.JavascriptErrorMessage, languageCode);
                }
                else
                {
                    validator.JavascriptErrorMessage = null;
                }
                validator.EnableValidation = true;
            }
            if (item is IMandatoryAnswer)
            {
                ((IMandatoryAnswer)item).Mandatory = answer.Mandatory;
            }
            if ((item is IRegExValidator) && (answer.RegExpression != null))
            {
                ((IRegExValidator)item).RegExpression             = answer.RegExpression;
                ((IRegExValidator)item).RegExpressionErrorMessage = answer.RegExMessage;
            }
            AnswerSelectionItem item2 = item as AnswerSelectionItem;

            if (item2 != null)
            {
                item2.UniqueGroupId = parentControlUniqueID;
                item2.AnswerId      = answer.AnswerId;
                item2.TypeMode      = (AnswerTypeMode)answer.TypeMode;
                item2.SelectionMode = defaultSelectionMode;
                bool flag = (section == null) ? IsUserSelected(answer.AnswerId, 0, voterAnswersState) : IsUserSelected(answer.AnswerId, section.SectionNumber, voterAnswersState);
                if (flag)
                {
                    item2.Selected = flag;
                }
                else
                {
                    item2.Selected = enableDefaults ? answer.Selected : false;
                }
            }
            if (item is IFieldItem)
            {
                IFieldItem item3 = (IFieldItem)item;
                item3.FieldHeight = answer.FieldHeight;
                item3.FieldWidth  = answer.FieldWidth;
                item3.FieldLength = answer.FieldLength;
            }
            else if (item is AnswerDataSourceItem)
            {
                AnswerDataSourceItem item4 = (AnswerDataSourceItem)item;
                item4.QuestionId = answer.QuestionId;
                item4.AnswerId   = answer.AnswerId;
                if (item4 is AnswerXmlItem)
                {
                    item4.DataSource = answer.XmlDatasource;
                }
                else if (answer.DataSource != null)
                {
                    item4.DataSource = ParseDefaultAnswerText(answer.QuestionId, answer.DataSource, voterAnswersState, languageCode);
                }
                item4.ImageUrl = answer.IsImageURLNull() ? null : answer.ImageURL;
            }
            string str = (section == null) ? GetUserText(answer.AnswerId, 0, voterAnswersState) : GetUserText(answer.AnswerId, section.SectionNumber, voterAnswersState);

            if (str != null)
            {
                item.DefaultText = str;
            }
            else if ((enableDefaults && !answer.IsDefaultTextNull()) && (answer.DefaultText.Length > 0))
            {
                item.DefaultText = ParseDefaultAnswerText(answer.QuestionId, answer.DefaultText, voterAnswersState, languageCode);
            }
            if (item is ExtendedAnswerItem)
            {
                ((ExtendedAnswerItem)item).RestoreProperties();
            }
            return(item);
        }
        /// <summary>
        /// Parse the given string and replace any
        /// available templates by their runtime values
        /// </summary>
        private static string ParseDefaultAnswerText(int questionId, string currentDefaultText, VoterAnswersData.VotersAnswersDataTable voterAnswersState, string languageCode)
        {
            currentDefaultText = new PipeManager().PipeValuesInText(questionId, currentDefaultText, voterAnswersState, languageCode);
            string str = new Regex(@"#{2}((\S)+)#{2}").Match(currentDefaultText).Groups[1].ToString();

            if ((str.Length > 0) && (HttpContext.Current != null))
            {
                currentDefaultText = (HttpContext.Current.Request[str] == null) ? string.Empty : HttpContext.Current.Request[str];
                return(currentDefaultText);
            }
            string str2 = new Regex(@"%{2}((\S)+)%{2}").Match(currentDefaultText).Groups[1].ToString();

            if ((str2.Length > 0) && (HttpContext.Current != null))
            {
                currentDefaultText = (HttpContext.Current.Request.ServerVariables[str2] == null) ? string.Empty : HttpContext.Current.Request.ServerVariables[str2];
                return(currentDefaultText);
            }
            string str3 = new Regex(@"@{2}((\S)+)@{2}").Match(currentDefaultText).Groups[1].ToString();

            if ((str3.Length > 0) && (HttpContext.Current != null))
            {
                currentDefaultText = (HttpContext.Current.Session[str3] == null) ? string.Empty : HttpContext.Current.Session[str3].ToString();
                return(currentDefaultText);
            }
            string str4 = new Regex(@"&{2}((\S)+)&{2}").Match(currentDefaultText).Groups[1].ToString();

            if ((str4.Length > 0) && (HttpContext.Current != null))
            {
                currentDefaultText = (HttpContext.Current.Request.Cookies[str4] == null) ? string.Empty : HttpContext.Current.Request.Cookies[str4].Value.ToString();
                return(currentDefaultText);
            }
            return(currentDefaultText);
        }
        /// <summary>
        /// Parse an AnswerDataCollection, converts the data and
        /// returns an AnswerItemCollection filled with the correct
        /// child controls
        /// </summary>
        /// <param name="answers">A collection of answerdata entities</param>
        /// <param name="defaultSelectionMode">The default selection mode for the
        /// item (Radio, checkbox etc...)</param>
        /// <param name="parentcontrolUniqueID">
        /// Unique ID required to identify global groups
        /// like radiobutton groups
        /// </param>
        /// <param name="showAnswerText">
        /// Assigns the text to the answeritem control ?
        /// </param>
        /// <param name="voterAnswersState">
        /// Current state of answers, used for piping. If not available
        /// leave it to null
        /// </param>
        /// <param name="enableDefaults">
        /// Does the answer set the user default values of fields to the answer web controls
        /// </param>
        /// <returns>A collection of answeritem web controls</returns>
        public static AnswerItemCollection CreateAnswerItemCollection(AnswerData answers, QuestionItem question, Section section, AnswerSelectionMode defaultSelectionMode, Style answerStyle, ControlRenderMode renderMode, string languageCode, string parentControlUniqueID, bool showAnswerText, VoterAnswersData.VotersAnswersDataTable voterAnswersState, bool enableDefaults)
        {
            AnswerItemCollection items = new AnswerItemCollection();

            foreach (AnswerData.AnswersRow row in answers.Answers.Rows)
            {
                items.Add(Create(row, question, section, defaultSelectionMode, answerStyle, renderMode, languageCode, parentControlUniqueID, showAnswerText, voterAnswersState, enableDefaults));
            }
            return(items);
        }