1 class QuickvoteController < ApplicationController
8 #############################################################
9 # the following methods pertain to creating quickvotes
10 #############################################################
14 @quickvote = QuickVote.new(params[:quickvote])
16 # store the candidate grabbed through ajax and stored in flash
17 @quickvote.candidatelist = flash[:candlist]
19 # try to save, if it fails, show the page again (the flash should
22 @quickvote = @quickvote.reload
23 render :action => 'success'
29 # if we don't have a quickvote param, it means that the person
30 # here has not been hitting this page and we can clear any
31 # candlist in the flash
32 flash.delete(:candlist) if flash.has_key?(:candlist)
37 candidate_name = params[:ajax][:newcandidate]
38 if flash.has_key?(:candlist) and flash[:candlist].instance_of?(Array)
39 flash[:candlist] << candidate_name
41 flash[:candlist] = [ candidate_name ]
44 render_partial 'candidate_list'
47 #############################################################
48 # the following methods pertain to *voting* in the quickvotes
49 #############################################################
52 @election = QuickVote.find_all(["name = ?", params[:votename]])[0]
54 # if the person has specified an election, we show them the voting
55 # page. otherwise, we redirect back to main the page
58 # look to see that the voter has been created and has voted in
59 # this election, and has confirmed their vote
60 @voter = QuickVoter.find_all(["session_id = ? and election_id = ?",
61 session.session_id, @election.id])[0]
63 # if the voter has not voted we destroy them
64 if @voter and not @voter.voted?
69 # if the voter does not exist or has has been destroyed, lets
72 # create a new voter and populate it
73 @voter = QuickVoter.new
74 @voter.election = QuickVote.find_all( [ "name = ?", params[:votename] ] )[0]
75 @voter.session_id = session.session_id
77 # create new vote and make it the defaulted sorted list
78 @voter.vote = Vote.new
80 @voter.vote.set_defaults!
84 redirect_to :controller => 'site'
89 # we need the election to verify that we have the right voter
90 election = QuickVote.find_all( [ "name = ?", params[:votename] ] )[0]
92 # find out who the voter is for this election
93 @voter = QuickVoter.find_all(["session_id = ? and election_id = ?",
94 session.session_id, election.id])[0]
97 # we have not seen this voter before. something is wrong, try
99 redirect_to quickvote_url( :votename => params[:votename] )
102 # this person has already voted, we try again
103 flash[:notice] = "You have already voted!"
104 redirect_to quickvote_url( :votename => params[:votename] )
107 # record the ip address for posterity
108 @voter.ipaddress = request.env["BLAH"]
111 # save the time the vote was made for statistical use, it doesn't
112 #work here unless I use a method that will save it to the db
113 @voter.vote.time = Time.now
115 # toggle the confirmation bit
119 render :action => 'thanks'
124 voter = QuickVoter.find_all(["session_id = ?", session.session_id])[0]
126 redirect_to quickvote_url( :votename => params[:votename] )
130 @vote = Vote.find(params[:id])
132 @vote.rankings.each do |ranking|
133 ranking.rank = params['rankings-list'].index(ranking.candidate.id.to_s) + 1
136 render :nothing => true
140 ###############################################################
141 # the following method pertains to displaying the results of a
143 ###############################################################
146 @election = QuickVote.find_all( ["name = ?", params[:votename]] )[0]
148 # initalize the tallies to empty arrays
149 preference_tally = Array.new
150 plurality_tally = Array.new
151 approval_tally = Array.new
153 @election.voters.each do |voter|
154 # skip if the voter has not voted or has an unconfirmed vote
155 next unless voter.voted?
157 plurality_tally << voter.vote.rankings.sort[0].candidate.id
158 approval_tally << voter.vote.rankings.sort[0..1].collect \
159 { |ranking| ranking.candidate.id }
160 preference_tally << voter.vote.rankings.sort.collect \
161 { |ranking| ranking.candidate.id }
164 @plurality_result = PluralityVote.new(plurality_tally).result
165 @approval_result = ApprovalVote.new(approval_tally).result
166 @condorcet_result = PureCondorcetVote.new(preference_tally).result
167 @ssd_result = CloneproofSSDVote.new(preference_tally).result
168 @borda_result = BordaVote.new(preference_tally).result
169 #@runoff_result = InstantRunoffVote.new(preference_tally).result
170 #@runoff_results = PluralityVote.new(preference_tally).result
174 @election.candidates.each {|c| @candidates[c.id] = c}