Ejemplo n.º 1
0
        /// <Summary>Update the Ballot status of this ballot, based on these Votes.</Summary>
        /// <param name="ballot">The Ballot or vBallotInfo to check and update.</param>
        /// <param name="currentVotes">The list of Votes in this Ballot</param>
        /// <param name="refreshVoteStatus"></param>
        /// <returns>Returns the updated status code</returns>
        public BallotStatusWithSpoilCount UpdateBallotStatus(Ballot ballot, List <VoteInfo> currentVotes,
                                                             bool refreshVoteStatus, bool forceSave = false)
        {
            //double check:
            currentVotes.ForEach(vi => AssertAtRuntime.That(vi.BallotGuid == ballot.BallotGuid));

            if (refreshVoteStatus)
            {
                VoteAnalyzer.UpdateAllStatuses(currentVotes, new VoteCacher(Db).AllForThisElection, new Savers(Db).VoteSaver);
            }

            if (DetermineStatusFromVotesList(ballot.StatusCode, currentVotes, out var newStatus, out var spoiledCount) ||
                forceSave)
            {
                BallotSaver(DbAction.Attach, ballot);
                ballot.StatusCode = newStatus;
                //                BallotSaver(DbAction.Save, ballot);

                new BallotCacher(Db).UpdateItemAndSaveCache(ballot);
                Db.SaveChanges();
            }

            Db.SaveChanges();

            return(new BallotStatusWithSpoilCount
            {
                Status = BallotStatusEnum.Parse(newStatus),
                SpoiledCount = spoiledCount
            });
        }
Ejemplo n.º 2
0
 public static Ballot ForTests(this Ballot ballot, BallotStatusEnum status)
 {
     ballot.BallotGuid   = Guid.NewGuid();
     ballot.C_RowId      = _rowId++;
     ballot.StatusCode   = status;
     ballot.LocationGuid = _locationGuid;
     Db.Ballot.Add(ballot);
     return(ballot);
 }
Ejemplo n.º 3
0
        public override object BallotInfoForJs(Ballot b, List <Vote> allVotes)
        {
            var spoiledCount = (allVotes ?? new VoteCacher(Db).AllForThisElection)
                               .Count(v => v.BallotGuid == b.BallotGuid && v.StatusCode != VoteHelper.VoteStatusCode.Ok);

            return(new
            {
                Id = b.C_RowId,
                Code = b.C_BallotCode,
                b.StatusCode,
                StatusCodeText = BallotStatusEnum.TextFor(b.StatusCode),
                SpoiledCount = spoiledCount
            });
        }
Ejemplo n.º 4
0
        public override object BallotInfoForJs(Ballot b, List <Vote> allVotes)
        {
            var spoiledCount = b.StatusCode != BallotStatusEnum.Ok ? 0 : (allVotes ?? new VoteCacher(Db).AllForThisElection)
                               .Count(v => v.BallotGuid == b.BallotGuid && v.StatusCode != VoteStatusCode.Ok);

            return(new
            {
                Id = b.C_RowId,
                Guid = b.ComputerCode == ComputerModel.ComputerCodeForImported ? (Guid?)b.BallotGuid : null,
                Code = b.C_BallotCode,
                b.StatusCode,
                StatusCodeText = BallotStatusEnum.TextFor(b.StatusCode),
                SpoiledCount = spoiledCount
            });
        }
Ejemplo n.º 5
0
        /// <Summary>Update the Ballot status of this ballot, based on these Votes.</Summary>
        /// <param name="ballot">The Ballot or vBallotInfo to check and update.</param>
        /// <param name="currentVotes">The list of Votes in this Ballot</param>
        /// <param name="refreshVoteStatus"></param>
        /// <returns>Returns the updated status code</returns>
        public BallotStatusWithSpoilCount UpdateBallotStatus(Ballot ballot, List <VoteInfo> currentVotes, bool refreshVoteStatus)
        {
            //double check:
            currentVotes.ForEach(vi => AssertAtRuntime.That(vi.BallotGuid == ballot.BallotGuid));

            if (refreshVoteStatus)
            {
                VoteAnalyzer.UpdateAllStatuses(currentVotes, new VoteCacher(Db).AllForThisElection, new Savers(Db).VoteSaver);
            }

            string newStatus;
            int    spoiledCount;


            //if (IsSingleNameElection)
            //{
            //  if (ballot.StatusCode != BallotStatusEnum.Ok)
            //  {
            //    BallotSaver(DbAction.Attach, ballot);

            //    ballot.StatusCode = BallotStatusEnum.Ok;

            //    BallotSaver(DbAction.Save, ballot);
            //  }
            //  return new BallotStatusWithSpoilCount
            //    {
            //      Status = BallotStatusEnum.Ok,
            //      SpoiledCount = 0
            //    };
            //}


            if (DetermineStatusFromVotesList(ballot.StatusCode, currentVotes, out newStatus, out spoiledCount))
            {
                BallotSaver(DbAction.Attach, ballot);
                ballot.StatusCode = newStatus;
                BallotSaver(DbAction.Save, ballot);
            }
            return(new BallotStatusWithSpoilCount
            {
                Status = BallotStatusEnum.Parse(newStatus),
                SpoiledCount = spoiledCount
            });
        }
Ejemplo n.º 6
0
        public object GetCurrentResults()
        {
            try
            {
                var resultSummaryFinal =
                    _analyzer.ResultSummaryFinal; // resultSummaries.SingleOrDefault(rs => rs.ResultType == ResultType.Final);

                //TODO build online checks into base analysis

                // don't show any details if review is needed or online ballots need to be processed
                var issues = new List <string>();
                if (_election.OnlineCurrentlyOpen)
                {
                    issues.Add("Online voting is still open. It must be Closed before analyzing ballots.");
                }

                var unprocessedOnlineBallots = _election.OnlineWhenOpen.HasValue
          ? Db.OnlineVotingInfo
                                               .Join(Db.Person.Where(p => p.VotingMethod == VotingMethodEnum.Online.Value), ovi => ovi.PersonGuid, p => p.PersonGuid, (ovi, p) => ovi)
                                               .Count(ovi => ovi.ElectionGuid == UserSession.CurrentElectionGuid && ovi.Status == OnlineBallotStatusEnum.Submitted)
          : 0;
                if (unprocessedOnlineBallots > 0)
                {
                    issues.Add($"Online ballots waiting to be accepted: {unprocessedOnlineBallots}");
                }

                if (resultSummaryFinal.BallotsNeedingReview != 0 || issues.Any())
                {
                    var locations         = new LocationCacher(Db).AllForThisElection;
                    var multipleLocations = locations.Count() > 1;

                    var needReview = _analyzer.VoteInfos.Where(VoteAnalyzer.VoteNeedReview)
                                     .Join(locations, vi => vi.LocationId, l => l.C_RowId,
                                           (vi, location) => new { vi, location })
                                     .Select(x => new
                    {
                        x.vi.LocationId,
                        x.vi.BallotId,
                        Status = BallotStatusEnum.TextFor(x.vi.BallotStatusCode),
                        Ballot = multipleLocations ? $"{x.vi.C_BallotCode} ({x.location.Name})" : x.vi.C_BallotCode,
                    })
                                     .Distinct()
                                     .OrderBy(x => x.Ballot);

                    var needReview2 = _analyzer.Ballots.Where(BallotAnalyzer.BallotNeedsReview)
                                      .Join(locations, b => b.LocationGuid, l => l.LocationGuid,
                                            (b, location) => new { b, location })
                                      .OrderBy(x => x.b.ComputerCode)
                                      .ThenBy(x => x.b.BallotNumAtComputer)
                                      .Select(x => new
                    {
                        LocationId = x.location.C_RowId,
                        BallotId   = x.b.C_RowId,
                        Status     = BallotStatusEnum.TextFor(x.b.StatusCode),
                        Ballot     = multipleLocations ? $"{x.b.C_BallotCode} ({x.location.Name})" : x.b.C_BallotCode
                    });

                    return(new
                    {
                        NeedReview = needReview.Concat(needReview2).Distinct(),
                        _election.VotingMethods,
                        ResultsFinal = _analyzer.ResultSummaryFinal
                                       .GetPropertiesExcept(null, new[] { "ElectionGuid" }),
                        ResultsCalc =
                            _analyzer.ResultSummaries.First(rs => rs.ResultType == ResultType.Calculated)
                            .GetPropertiesExcept(null, new[] { "ElectionGuid" }),
                        ResultsManual = (_analyzer.ResultSummaries.FirstOrDefault(rs => rs.ResultType == ResultType.Manual) ??
                                         new ResultSummary()).GetPropertiesExcept(null, new[] { "ElectionGuid" }),
                        OnlineIssues = issues,
                        _election.OnlineCurrentlyOpen
                        //ResultsFinal =
                        //  resultSummaries.First(rs => rs.ResultType == ResultType.Final)
                        //    .GetPropertiesExcept(null, new[] { "ElectionGuid" }),
                    });
                }

                // show vote totals

                var persons = new PersonCacher(Db).AllForThisElection;

                var vResultInfos =
                    // TODO 2012-01-21 Glen Little: Could return fewer columns for non-tied results
                    _analyzer.Results // new ResultCacher(Db).AllForThisElection
                    .OrderBy(r => r.Rank)
                    .ToList()
                    .Select(r => new
                {
                    rid = r.C_RowId,
                    r.CloseToNext,
                    r.CloseToPrev,
                    r.ForceShowInOther,
                    r.IsTied,
                    r.IsTieResolved,
                    PersonName = PersonNameFor(persons, r),
                    r.Rank,
                    //ri.RankInExtra,
                    r.Section,
                    r.TieBreakCount,
                    r.TieBreakGroup,
                    r.TieBreakRequired,
                    r.VoteCount
                }).ToList();

                var ties = _analyzer.ResultTies //  new ResultTieCacher(Db).AllForThisElection
                           .OrderBy(rt => rt.TieBreakGroup)
                           .Select(rt => new
                {
                    rt.TieBreakGroup,
                    rt.NumInTie,
                    rt.NumToElect,
                    rt.TieBreakRequired,
                    rt.IsResolved
                }).ToList();

                //var spoiledVotesSummary = Db.vVoteInfoes.where
                return(new
                {
                    Votes = vResultInfos,
                    UserSession.CurrentElectionStatus,
                    Ties = ties,
                    NumToElect = _election.NumberToElect,
                    NumExtra = _election.NumberExtra,
                    // ShowCalledIn = _election.UseCallInButton,
                    // _election.CustomMethods,
                    _election.VotingMethods,
                    ShowOnline = _election.OnlineWhenOpen.HasValue,
                    ResultsManual =
                        (_analyzer.ResultSummaries.FirstOrDefault(rs => rs.ResultType == ResultType.Manual) ?? new ResultSummary())
                        .GetPropertiesExcept(null, new[] { "ElectionGuid" }),
                    ResultsCalc =
                        _analyzer.ResultSummaries.First(rs => rs.ResultType == ResultType.Calculated)
                        .GetPropertiesExcept(null, new[] { "ElectionGuid" }),
                    ResultsFinal =
                        _analyzer.ResultSummaries.First(rs => rs.ResultType == ResultType.Final)
                        .GetPropertiesExcept(null, new[] { "ElectionGuid" }),
                });
            }
            catch (Exception ex)
            {
                return(new
                {
                    Interrupted = true,
                    Msg = ex.GetAllMsgs("; ") + "\n" + ex.StackTrace
                });
            }
        }