public PollEntry CreatePollEntry(PollPartRecord poll) { if (poll == null) { throw new ArgumentNullException("poll"); } return(new PollEntry { Poll = poll, NumberOfChoices = _pollService.GetChoices(poll.Id).Count(), IsChecked = false }); }
public PollResultViewModel GetPollWithResult(int id) { PollPartRecord poll = GetPoll(id); PollResultViewModel model = new PollResultViewModel { Question = poll.Question, Shown = poll.Shown, Choices = GetChoices(id).Select(c => new PollResultEntry { Choice = c, Result = GetResult(c.Id) }).ToList() }; return(model); }
public void TryVote(int pollId, string user, string fingerprint, IEnumerable <PollChoiceRecord> choices) { if (choices == null) { throw new ArgumentNullException("choices"); } PollPartRecord poll = _pollRepository.Get(pollId); //var existingVotesFromUser = _voteRepository.Fetch(exv => exv.PollPartRecord.Id == pollId && exv.Username == user); //foreach (var choice in choices) //{ // foreach(var exv in existingVotesFromUser) // { // if (choice.Id == exv.PollChoiceRecord.Id) // { // return; // } // } //} foreach (var choice in choices) { if (_voteRepository.Count(v => v.PollPartRecord.Id == pollId && v.Username == user) < poll.MaxVotes) { PollVoteRecord vote = new PollVoteRecord { Fingerprint = fingerprint, PollChoiceRecord = choice, PollPartRecord = poll, Username = user, VoteDateUtc = DateTime.Now }; _voteRepository.Create(vote); var result = _resultRepository.Get(r => r.PollChoiceRecord.Id == choice.Id); result.Count++; _resultRepository.Update(result); } else { // Poke a finger at the controller // Services.Notifier.Information(T("How Da Der!")); } } }
public void CreatePoll(PollPart poll) { if (poll == null) { throw new ArgumentNullException("poll"); } var p = new PollPartRecord { ContentItemRecord = poll.Record.ContentItemRecord, Question = poll.Question, OpenDateUtc = poll.OpenDateUtc, CloseDateUtc = poll.CloseDateUtc, MaxVotes = poll.MaxVotes, Shown = poll.IsShown, MusicPoll = poll.IsMusicPoll, ShowVotingUI = poll.ShowVotingUI }; _pollRepository.Create(p); }
public IQueryable <PollChoiceRecord> GetChoices(int pollId) { PollPartRecord poll = GetPoll(pollId); var iQueryableChoices = _choiceRepository.Fetch(c => c.PollPartRecord.Id == pollId).OrderBy(c => c.Id).AsQueryable <PollChoiceRecord>(); // If poll is a moosic poll, return a randomized list of choices here, rather than the ordered list. pollId !=0 is checked // for the case of a new poll create from the dashboard, where the poll id will be zero, and poll will be null. if (poll != null) { if (pollId != 0 && poll.MusicPoll) { // randomize once per username var currentUser = _orchardServices.WorkContext.CurrentUser; string user = currentUser == null ? "Anonymous" : currentUser.UserName; var random = new Random(user.GetHashCode()); // randomize every time method is called // var random = new Random(); var shuffledChoices = iQueryableChoices.OrderBy(x => random.Next()); return(shuffledChoices); } } return(iQueryableChoices); }
public bool CanVote(int pollId, string user, string fingerprint) { if (user == "Anonymous") { // return _voteRepository.Count(v => v.PollPartRecord.Id == pollId && v.Fingerprint == fingerprint) == 0; return(false); } else { // if MaxVotes isn't reached for user, let user vote. PollPartRecord poll = _pollRepository.Get(pollId); if (_voteRepository.Count(v => v.PollPartRecord.Id == pollId && v.Username == user) < poll.MaxVotes) { return(true); } else { return(false); } // statement will return false if one vote is already given // return _voteRepository.Count(v => v.PollPartRecord.Id == pollId && v.Username == user) == 0; } }
public void EditPoll(PollPart part, PollViewModel model) { if (part == null) { throw new ArgumentNullException("part"); } if (model == null) { throw new ArgumentNullException("model"); } var poll = new PollPartRecord { Id = part.Record.ContentItemRecord.Id, Question = model.Question, OpenDateUtc = model.Open, CloseDateUtc = model.Close, MaxVotes = model.MaxVotes, Shown = model.Shown, MusicPoll = model.MusicPoll, ShowVotingUI = model.ShowVotingUI, ContentItemRecord = part.Record.ContentItemRecord }; UpdatePoll(poll); foreach (var entry in model.Choices) { switch (entry.Action) { case "Alter": var choice = new PollChoiceRecord { Id = entry.Choice.Id, Answer = entry.Choice.Answer, PollPartRecord = part.Record }; UpdateChoice(choice); break; case "Create": if (entry.Choice.Answer != null && entry.Choice.Answer.Length > 0) { choice = new PollChoiceRecord { Answer = entry.Choice.Answer, PollPartRecord = part.Record }; CreateChoice(choice); } break; } } var choices = GetChoices(poll.Id).ToList(); var newChoices = model.Choices.Select(c => choices.FirstOrDefault(x => x.Id == c.Choice.Id || x.Answer == c.Choice.Answer)); var toDelete = choices.Except(newChoices); foreach (var choice in toDelete) { DeleteChoice(choice); } }
public void UpdatePoll(PollPartRecord poll) { _pollRepository.Update(poll); }