public Group_ElectionViewModel(Election election, Guid? userId)
        {
            if (election != null)
            {
                Id = election.Id;
                Title = election.Title;
                Stage = (ElectionStage)election.Stage;
                Quorum = election.Quorum;
                Turnout = election.Turnout;
                GroupModersCount = election.Group.ModeratorsCount;
                About = new Group_Election_AboutViewModel(election);

                GroupMember gm = null;
                if (userId.HasValue && election.GroupId.HasValue)
                    gm = GroupService.UserInGroup(userId.Value, election.GroupId.Value);

                IEnumerable<Candidate> candidates = election.Candidates.Where(x => x.Petition != null).OrderByDescending(x => x.Petition.Signers.Count);
                candidates = candidates.Union(election.Candidates.Where(x => x.Petition == null));
                if (election.Stage != (byte)ElectionStage.Agitation)
                    candidates = candidates.Where(x => x.Status == (byte)CandidateStatus.Confirmed);
                Candidates = candidates.Select(x => new Group_Election_CandidateViewModel(x, gm)).ToList();
                Winners = election.Candidates
                    .Where(x => x.Status == (byte)CandidateStatus.Winner)
                    .OrderByDescending(x => x.Electorate.Count)
                    .Select(x => new Group_Election_CandidateViewModel(x, gm))
                    .ToList();

                switch (Stage)
                {
                    case ElectionStage.Agitation: ElectionCandidates = new Group_ElectionCandidatesViewModel(election, userId); break;
                    case ElectionStage.Voting: ElectionVoting = new Group_ElectionVotingViewModel(election, userId); break;
                }
            }
            else
            {
                Candidates = new List<Group_Election_CandidateViewModel>();
                Winners = new List<Group_Election_CandidateViewModel>();
                About = new Group_Election_AboutViewModel();
                ElectionCandidates = new Group_ElectionCandidatesViewModel();
                ElectionVoting = new Group_ElectionVotingViewModel();
            }

            if (UserContext.Current != null)
            {
                var candidate = Candidates.FirstOrDefault(x => x.UserId == UserContext.Current.Id);
                if (candidate != null)
                {
                    IsCandidate = true;
                    CandidateId = candidate.Id;
                }
                else
                    CandidateId = null;
            }
        }
        public Group_ElectionVotingViewModel(Election election, Guid? userId)
        {
            CurrentUserTakingPart = false;
            CurrentUserVoted = false;

            if (election != null)
            {
                if(!election.PublishDate.HasValue)
                    throw new BusinessLogicException("Данные выборы еще не опубликованы");

                Id = election.Id;

                AgitationStart = election.PublishDate.Value;
                AgitationEnds = AgitationStart.AddDays(election.AgitationDuration);
                if (election.EndDate.HasValue)
                    ElectionsEnds = election.EndDate.Value;
                var ts = ElectionsEnds - DateTime.Now;
                TimeRemaining = string.Format("{0} дней {1:00}:{2:00}:{3:00}", (int)ts.TotalDays, ts.Hours, ts.Minutes, ts.Seconds);
                RelativeTimeRemaining = (election.AgitationDuration - (int)ts.TotalDays) * 100 / election.AgitationDuration;

                ElectionFrequency = DeclinationService.OfNumber(election.Group.ElectionFrequency,"месяц","месяца","месяцев");
                ModeratorsCount = DeclinationService.OfNumber(election.Group.ModeratorsCount, "человек", "человека", "человек");
                SignsMinimumLimit = DeclinationService.OfNumber(ConstHelper.CandidatePetitionNecessarySigners, "подпись", "подписи", "подписей");

                var membersCount = DataService.PerThread.GroupMemberSet.Count(x => x.GroupId == election.GroupId && (x.State == (byte)GroupMemberState.Approved || x.State == (byte)GroupMemberState.Moderator));

                var bulletin = DataService.PerThread.BulletinSet.OfType<ElectionBulletin>().SingleOrDefault(x => x.ElectionId == election.Id && x.Owner.UserId == userId);
                if (bulletin != null)
                {
                    CurrentUserTakingPart = true;
                    CurrentUserVoted = bulletin.Result.Count > 0;
                }

                foreach (var candidate in election.Candidates)
                    if (candidate.Status == (short) CandidateStatus.Confirmed)
                    {
                        var voted = bulletin != null &&
                                    bulletin.Result.SingleOrDefault(x => x.Id == candidate.Id) != null;

                        Candidates.Add(new Group_ElectionVoting_CandidateViewModel(candidate, membersCount, voted));
                    }
            }
        }
Beispiel #3
0
        private void FixupElection(Election previousValue)
        {
            if (previousValue != null && previousValue.Candidates.Contains(this))
            {
                previousValue.Candidates.Remove(this);
            }

            if (Election != null)
            {
                if (!Election.Candidates.Contains(this))
                {
                    Election.Candidates.Add(this);
                }
                if (ElectionId != Election.Id)
                {
                    ElectionId = Election.Id;
                }
            }
        }
        public Group_ElectionCandidatesViewModel(Election election, Guid? userId)
        {
            if (election != null)
            {

                Id = election.Id;

                if (election.Group!=null)
                GroupId = election.Group.Id;

                if(!election.PublishDate.HasValue)
                    throw new BusinessLogicException("Данные выборы еще не опубликованы");

                if (userId.HasValue && election.GroupId.HasValue)
                {
                    var gm = GroupService.UserInGroup(userId.Value, election.GroupId.Value);
                    if (gm.Candidate != null && gm.Candidate.ElectionId == election.Id)
                        IsCandidate = true;
                }

                AgitationStart = election.PublishDate.Value;
                AgitationEnds = AgitationStart.AddDays(election.AgitationDuration);
                var ts = AgitationEnds - DateTime.Now;
                AgitationTimeRemaining = string.Format("{0} дней {1:00}:{2:00}:{3:00}", (int)ts.TotalDays, ts.Hours, ts.Minutes, ts.Seconds);
                RelativeTimeRemaining = (election.AgitationDuration - (int)ts.TotalDays) * 100 / election.AgitationDuration;
                ElectionEnd = election.EndDate.Value;
                ElectionId = election.Id;
                ElectionFrequency = DeclinationService.OfNumber(election.Group.ElectionFrequency,"месяц","месяца","месяцев");
                ModeratorsCount = DeclinationService.OfNumber(election.Group.ModeratorsCount, "человек", "человека", "человек");
                SignsMinimumLimit = DeclinationService.OfNumber(ConstHelper.CandidatePetitionNecessarySigners, "подпись", "подписи", "подписей");

                var membersCount = DataService.PerThread.GroupMemberSet.Count(x => x.GroupId == election.GroupId && (x.State == (byte)GroupMemberState.Approved || x.State == (byte)GroupMemberState.Moderator));
                foreach (var candidate in election.Candidates)
                    Candidates.Add(new Group_ElectionCandidates_CandidateViewModel(candidate, membersCount));
            }
        }
Beispiel #5
0
        private void FixupElection(Election previousValue)
        {
            if (previousValue != null && previousValue.Candidates.Contains(this))
            {
                previousValue.Candidates.Remove(this);
            }

            if (Election != null)
            {
                if (!Election.Candidates.Contains(this))
                {
                    Election.Candidates.Add(this);
                }
                if (ElectionId != Election.Id)
                {
                    ElectionId = Election.Id;
                }
            }
        }
        public Election CreateElection(string groupUrl, Guid? authorId)
        {
            var group = GroupService.GetGroupByLabelOrId(groupUrl);

            GroupService.CanElectionsBeStarted(group, true);

            if (authorId.HasValue)
            {
                var author = GroupService.UserInGroup(authorId.Value, group.Id);

                if (author == null || author.State == (byte)GroupMemberState.Banned || author.State == (byte)GroupMemberState.NotMember)
                    throw new BusinessLogicException("Вы не являетесь участником указанной группы");
                if (author.State == (byte)GroupMemberState.NotApproved)
                    throw new BusinessLogicException("Вы еще не являетесь участником указанной группы");
                if (author.State != (byte)GroupMemberState.Moderator)
                    throw new BusinessLogicException("Вы не являетесь модератором указанной группы");
            }

            var electionNumber = group.Content.OfType<Election>().Count() + 1;

            var election = new Election
            {
                Group = group,
                AuthorId = authorId,
                CreationDate = DateTime.Now,
                PublishDate = DateTime.Now,
                Title = electionNumber + "-ые выборы модераторов",
                State = (byte)ContentState.Approved,
                Stage = (byte)ElectionStage.Agitation,
                AgitationDuration = ConstHelper.ElectionAgitationDurationDays,
                Duration = ConstHelper.ElectionDurationDays,
                Text = "Пришло время выбрать новых модераторов группы!",
                Quorum = (int)(group.GroupMembers.Count * ((float)group.ElectionQuorum / 100))
            };

            election.Tags.Add(TagService.GetTag("Очередные", group.Id, true)); // TODO: потом будут внеочередные и т.п.
            election.Tags.Add(TagService.GetTag("Выборы", group.Id, true));

            DataService.PerThread.SaveChanges();
            ContentService.Attach(election.Id, null, AttachDetachTarget.Group);

            MessageService.SendToGroup(group, new MessageStruct
                {
                    Date = DateTime.Now,
                    Text = string.Format("В группе <a href={0}>{1}</a> запущены <a href={2}>{3}</a>",
                        UrlHelper.GetUrl<Group>(group.Url), group.Name, UrlHelper.GetUrl<Voting>(election.Id), election.Title),
                    Type = (byte)MessageType.ElectionNotice
                }, GroupMessageRecipientType.Members | GroupMessageRecipientType.Moderators);

            return election;
        }
        public Group_Election_AboutViewModel(Election election)
        {
            if (election != null)
            {
                switch ((ElectionStage)election.Stage)
                {
                    case ElectionStage.Agitation:
                        StageClass = "agitation-stage";
                        break;
                    case ElectionStage.Voting:
                        StageClass = "voting-stage";
                        break;
                    case ElectionStage.Completed:
                        StageClass = "completed-stage";
                        break;
                    case ElectionStage.Failed:
                        StageClass = "failed-stage";
                        break;
                }

                GroupModersCountString = DeclinationService.OfNumber(election.Group.ModeratorsCount, "модератора", "модераторов", "модераторов");
                ElectionPeriodString = DeclinationService.OfNumber(election.Group.ElectionFrequency, "месяц", "месяца", "месяцов");
            }
        }