private static SurveyCollectorData FindCollectorData(SurveyMaint graph, SurveyCollector collector, int?pageNbr)
        {
            SurveyCollectorData collData = PXSelect <SurveyCollectorData,
                                                     Where <SurveyCollectorData.token, Equal <Required <SurveyCollectorData.token> >,
                                                            And <SurveyCollectorData.pageNbr, Equal <Required <SurveyCollectorData.pageNbr> > > > > .Select(graph, collector.Token, pageNbr);

            return(collData);
        }
        private static SurveyCollector GetAnonymousCollector(SurveyMaint graph, Survey survey, SurveyCollector userCollector)
        {
            var coll = SurveyCollector.PK.Find(graph, userCollector.AnonCollectorID);

            if (coll == null)
            {
                // Rare case where AnonCollectorID points to a deleted Collector, should have been cleared.
                var(_, anon) = InsertAnonymous(graph, survey, null, true, false);
                userCollector.AnonCollectorID = anon?.CollectorID;
                graph.Collectors.Update(userCollector);
                graph.Actions.PressSave();
                coll = anon;
            }
            return(coll);
        }
        public static (SurveyUser user, SurveyCollector coll) InsertAnonymous(SurveyMaint graph, Survey survey, Guid?refNoteID, bool saveNow, bool isTest)
        {
            SurveySetup setup = PXSelect <SurveySetup> .SelectWindowed(graph, 0, 1);

            var contactID = setup.AnonContactID;

            if (contactID == null)
            {
                throw new PXException("An Anonymous user needs to be setup in the Survey Preferences");
            }
            if (survey.AllowAnonymous != true && survey.KeepAnswersAnonymous != true)
            {
                throw new PXException("Survey {0} ({1}) does not allow anonymous answers", survey.SurveyID, survey.Title);
            }
            var user      = graph.InsertOrFindUser(survey, contactID, true);
            var collector = graph.DoUpsertCollector(survey, user, refNoteID, saveNow, isTest);

            return(user, collector);
        }
 public SurveyGenerator(SurveyMaint graph)
 {
     this.graph = graph;
     GetReturnUrl();
 }
        private void FillEntityInfo(Survey survey, SurveyUser user, SurveyCollector collector, SurveyMaint graph, TemplateContext context)
        {
            // This collector might be an anonymous collector without the RefNoteID, let's find the real collector
            if (collector.RefNoteID == null && survey.KeepAnswersAnonymous == true)
            {
                collector = SurveyCollector.UK.ByToken.Find(graph, collector.Token);//?? collector;
            }
            if (collector.RefNoteID == null)
            {
                return;
            }
            var noteID    = collector.RefNoteID;
            var eh        = new EntityHelper(graph);
            var entityRow = eh.GetEntityRow(noteID);

            // Due to Acumatica
            if (entityRow == null)
            {
                return;
            }
            var entityType = entityRow.GetType();
            var entityName = eh.GetFriendlyEntityName(noteID);
            var fvp        = eh.GetFieldValuePairs(entityRow, entityType);
            var desc       = eh.GetEntityDescription(noteID, entityType);

            context.SetValue(new ScriptVariableGlobal(ENTITY_ROW), entityRow);
            context.SetValue(new ScriptVariableGlobal(ENTITY_TYPE), entityType);
            context.SetValue(new ScriptVariableGlobal(ENTITY_NAME), entityName);
            context.SetValue(new ScriptVariableGlobal(ENTITY_DESC), desc);
            context.SetValue(new ScriptVariableGlobal(ENTITY_FIELDS), fvp);
        }
        public static (Survey survey, SurveyUser user, SurveyCollector answerCollector, SurveyCollector userCollector) GetSurveyAndUser(SurveyMaint graph, string token)
        {
            SurveyCollector answerCollector;
            SurveyCollector userCollector = null;
            Survey          survey;
            SurveyUser      user;

            token = token?.Trim();
            if (token.Length <= 15)
            {
                // Anonymous survey, token is SurveyID
                survey = Survey.PK.Find(graph, token);
                if (survey == null)
                {
                    throw new PXException("Cannot find a survey with token {0}", token);
                }
                (user, answerCollector) = InsertAnonymous(graph, survey, null, true, false);
                token = answerCollector.Token;
            }
            else
            {
                answerCollector = SurveyCollector.UK.ByToken.Find(graph, token);
                survey          = Survey.PK.Find(graph, answerCollector.SurveyID);
                // If answers must be kept anonymous, then retrieve the anonymous collector of the user collector
                if (survey.KeepAnswersAnonymous == true && answerCollector.Anonymous != true)
                {
                    userCollector   = answerCollector;
                    answerCollector = GetAnonymousCollector(graph, survey, userCollector);
                }
                user = SurveyUser.PK.Find(graph, survey.SurveyID, answerCollector.UserLineNbr);
            }
            if (answerCollector == null)
            {
                throw new PXException(Messages.TokenNoFound, token);
            }
            if (survey == null)
            {
                throw new PXException(Messages.TokenNoSurvey, token);
            }
            if (user == null)
            {
                throw new PXException(Messages.TokenNoUser, token);
            }
            if (userCollector == null)
            {
                userCollector = answerCollector;
            }
            return(survey, user, answerCollector, userCollector);
        }