internal static void AddUpdatePollVote(PollVote vote)
        {
            ValidatePollVote(vote);

            var existingVote = GetPollVote(vote.PollId, vote.UserId);

            if (existingVote != null && existingVote.PollAnswerId == vote.PollAnswerId)
            {
                return;
            }

            bool isCreate = existingVote == null;

            if (isCreate)
            {
                PublicApi.PollVotes.Events.OnBeforeCreate(vote);
            }
            else
            {
                PublicApi.PollVotes.Events.OnBeforeUpdate(vote);
            }

            PollingDataService.AddUpdatePollVote(vote);
            ExpirePoll(vote.PollId);

            if (isCreate)
            {
                PublicApi.PollVotes.Events.OnAfterCreate(vote);
            }
            else
            {
                PublicApi.PollVotes.Events.OnAfterUpdate(vote);
            }
        }
        internal static void AddUpdatePollAnswer(PollAnswer answer)
        {
            bool isCreate = answer.Id == Guid.Empty;

            ValidatePollAnswer(answer);

            if (isCreate)
            {
                PublicApi.PollAnswers.Events.OnBeforeCreate(answer);
            }
            else
            {
                PublicApi.PollAnswers.Events.OnBeforeUpdate(answer);
            }

            PollingDataService.AddUpdatePollAnswer(answer);
            ExpirePoll(answer.PollId);

            if (isCreate)
            {
                PublicApi.PollAnswers.Events.OnAfterCreate(answer);
            }
            else
            {
                PublicApi.PollAnswers.Events.OnAfterUpdate(answer);
            }
        }
        internal static void AddUpdatePoll(Poll poll)
        {
            bool isCreate = poll.Id == Guid.Empty;

            ValidatePoll(poll);

            if (isCreate)
            {
                PublicApi.Polls.Events.OnBeforeCreate(poll);
            }
            else
            {
                PublicApi.Polls.Events.OnBeforeUpdate(poll);
            }

            PollingDataService.AddUpdatePoll(poll);
            ExpirePolls(poll.ApplicationId);

            if (isCreate)
            {
                PublicApi.Polls.Events.OnAfterCreate(poll);
            }
            else
            {
                PublicApi.Polls.Events.OnAfterUpdate(poll);
            }
        }
 internal static void ReassignPolls(int oldUserId, int newUserId)
 {
     if (oldUserId != newUserId)
     {
         PollingDataService.ReassigneUser(oldUserId, newUserId);
         // We don't expire the cache here because it would be potentially too large of an expiration
     }
 }
 internal static void DeletePollVote(PollVote vote)
 {
     ValidatePollVote(vote);
     PublicApi.PollVotes.Events.OnBeforeDelete(vote);
     PollingDataService.DeletePollVote(vote.PollId, vote.UserId);
     ExpirePoll(vote.PollId);
     PublicApi.PollVotes.Events.OnAfterDelete(vote);
 }
 internal static void DeletePollAnswer(PollAnswer answer)
 {
     ValidatePollAnswer(answer);
     PublicApi.PollAnswers.Events.OnBeforeDelete(answer);
     PollingDataService.DeletePollAnswer(answer.Id);
     ExpirePoll(answer.PollId);
     PublicApi.PollAnswers.Events.OnAfterDelete(answer);
 }
        internal static void DeletePoll(Poll poll)
        {
            ValidatePoll(poll);

            PublicApi.Polls.Events.OnBeforeDelete(poll);
            PollingDataService.DeletePoll(poll.Id);
            ExpirePolls(poll.ApplicationId);
            PublicApi.Polls.Events.OnAfterDelete(poll);
        }
        internal static PollVote GetPollVote(Guid pollId, int userId)
        {
            var pollVote = PollingDataService.GetPollVote(pollId, userId);

            if (pollVote != null && GetPoll(pollVote.PollId) != null)
            {
                return(pollVote);
            }

            return(null);
        }
        internal static PollAnswer GetPollAnswer(Guid pollAnswerId)
        {
            var pollAnswer = PollingDataService.GetPollAnswer(pollAnswerId);

            if (pollAnswer != null && GetPoll(pollAnswer.PollId) != null)
            {
                return(pollAnswer);
            }

            return(null);
        }
        internal static void DeletePolls(int groupId)
        {
            var group = TEApi.Groups.Get(new GroupsGetOptions()
            {
                Id = groupId
            });

            if (group != null)
            {
                PollingDataService.DeletePollsByGroup(group.ApplicationId);
                ExpirePolls(group.ApplicationId);
            }
        }
        internal static PagedList <Poll> ListPolls(int groupId, PollsApi.PollsListOptions options)
        {
            var group = TEApi.Groups.Get(new GroupsGetOptions()
            {
                Id = groupId
            });

            if (group == null)
            {
                return(new PagedList <Poll>());
            }

            Guid[] filterGroupIds = null;
            if (options.IncludeSubGroups == true)
            {
                var groupsToQuery = TEApi.Groups.List(new GroupsListOptions()
                {
                    IncludeAllSubGroups = true, ParentGroupId = groupId, PageSize = 9999
                });
                groupsToQuery.Add(group);

                IEnumerable <Guid> ids;
                ids = from g1 in groupsToQuery
                      select g1.ApplicationId;

                filterGroupIds = ids.ToArray();

                if (filterGroupIds.Length == 0)
                {
                    return(new PagedList <Poll>());
                }
            }

            //PagedList<Poll> polls = (PagedList<Poll>)CacheService.Get(PollsCacheKey(group.ApplicationId, options), CacheScope.Context | CacheScope.Process);
            //if (polls == null)
            //{
            var polls = PollingDataService.ListPolls(group, TEApi.Users.AccessingUser, filterGroupIds, options);

            //CacheService.Put(PollsCacheKey(group.ApplicationId, options), polls, CacheScope.Context | CacheScope.Process, new string[] { PollTag(group.ApplicationId) });
            //}

            return(polls);
        }
        internal static Poll GetPoll(Guid pollId)
        {
            Poll poll = (Poll)CacheService.Get(PollCacheKey(pollId), CacheScope.All);

            if (poll == null)
            {
                poll = PollingDataService.GetPoll(pollId);
                if (poll != null)
                {
                    CacheService.Put(PollCacheKey(pollId), poll, CacheScope.All, new string[] { PollTag(poll.ApplicationId) });
                }
            }

            if (poll != null && PollingPermissionService.CanReadPolls(TEApi.Groups.ContentTypeId, poll.ApplicationId))
            {
                return(poll);
            }

            return(null);
        }
 internal static List <Poll> ListPollsToSendNewPollEmail()
 {
     return(PollingDataService.ListPollsToSendNewPollEmail());
 }
 internal static void SetPollsAsIndexed(IEnumerable <Guid> pollIds)
 {
     PollingDataService.SetPollsAsIndexed(pollIds);
 }
 internal static PagedList <Poll> ListPollsToReindex(int pageSize, int pageIndex)
 {
     return(PollingDataService.ListPollsToReindex(pageSize, pageIndex));
 }