private Question GetQuestion(string attributeID)
        {
            var attr        = CSAttributePK.Find(graph, attributeID);
            var question    = new Question(attr);
            var controlType = attr.ControlType;
            var attrID      = question.AttributeID;

            switch (controlType)
            {
            case 2:     // Combo
            case 6:     // Multi Select Combo
                question.Details = SurveyUtils.
                                   GetAttributeDetails(graph, attrID).
                                   OrderBy(det => det.SortOrder ?? 0).
                                   Select(x => new QuestionDetail()
                {
                    ValueID = x.ValueID, Description = x.Description
                });
                break;

            case 7:     // Selector
                        // TODO
                break;
            }
            return(question);
        }
        private IEnumerable <string> RenderComponentsForPage(Survey survey, SurveyUser user, TemplateContext context, int pageNbr)
        {
            graph.Survey.Current = survey;
            var activeComponents   = graph.Details.Select().FirstTableItems.Where(det => det.Active == true);
            var selectedComponents = SurveyUtils.SelectPages(activeComponents, pageNbr);
            var firstOfSelected    = selectedComponents.FirstOrDefault();
            var renderedComponents = new List <string>();

            if (firstOfSelected == null)
            {
                return(renderedComponents);
            }
            var selectedPageNbr = firstOfSelected?.PageNbr.Value ?? 99999;
            var nextComponents  = SurveyUtils.SelectPages(activeComponents, ++selectedPageNbr);

            FillPageInfoLookAhead(context, activeComponents, nextComponents);
            var url = GetUrl(context, firstOfSelected.PageNbr.Value);

            context.SetValue(new ScriptVariableGlobal(URL), url);
            foreach (var detail in selectedComponents)
            {
                var component = SurveyComponent.PK.Find(graph, detail.ComponentID);
                var template  = Template.Parse(component.Body);
                AddDetailContext(context, detail, component);
                FillPageInfo(context, activeComponents, detail);
                var rendered = template.Render(context);
                renderedComponents.Add(rendered);
            }
            // TODO Handle nothing rendered
            return(renderedComponents);
        }
        public (string content, string token) GenerateSurveyPage(string token, int pageNbr)
        {
            var(survey, user, _, userCollector) = SurveyUtils.GetSurveyAndUser(graph, token);
            // Redirect with new token as anonymous surveys will pass the SurveyID as a token
            //userCollector.RefNoteID = new System.Guid();
            if (userCollector.Token != token)
            {
                return(null, userCollector.Token);
            }
            graph.Survey.Current = survey;
            var    mainTemplateID   = survey.TemplateID;
            var    mainTemplate     = SurveyComponent.PK.Find(graph, mainTemplateID);
            string mainTemplateText = mainTemplate.Body;

            if (string.IsNullOrEmpty(mainTemplateText?.Trim()))
            {
                throw new PXException(Messages.TemplateNeeded);
            }
            var template = Template.Parse(mainTemplateText);
            var context  = GetSurveyContext(survey, user, token);

            FillEntityInfo(survey, user, userCollector, graph, context);
            var renderedComps = RenderComponentsForPage(survey, user, context, pageNbr);

            context.SetValue(new ScriptVariableGlobal(INNER_CONTENT_LIST), renderedComps);
            context.SetValue(new ScriptVariableGlobal(INNER_CONTENT), string.Join("\n", renderedComps));
            var rendered = template.Render(context);

            return(rendered, null);
        }
 public string GetUrl(string token, int?pageNbr)
 {
     var(survey, _, answerCollector, _) = SurveyUtils.GetSurveyAndUser(graph, token);
     return(GetUrl(survey, answerCollector.Token, pageNbr));
 }