1 class QuickvoteController < ApplicationController
8 #############################################################
9 # the following methods pertain to creating quickvotes
10 #############################################################
15 @quickvote = QuickVote.new(params[:quickvote])
17 # store the candidate grabbed through ajax and stored in flash
18 @quickvote.candidatelist = flash[:candlist]
20 # try to save, if it fails, show the page again (the flash should
23 @quickvote = @quickvote.reload
24 render :action => 'success'
30 # if we don't have a quickvote param, it means that the person
31 # here has not been hitting this page and we can clear any
32 # candlist in the flash
33 flash.delete(:candlist) if flash.has_key?(:candlist)
38 candidate_name = params[:ajax][:newcandidate]
39 if flash.has_key?(:candlist) and flash[:candlist].instance_of?(Array)
40 flash[:candlist] << candidate_name
42 flash[:candlist] = [ candidate_name ]
45 render_partial 'candidate_list'
48 #############################################################
49 # the following methods pertain to *voting* in the quickvotes
50 #############################################################
53 @election = QuickVote.find_all(["name = ?", params[:votename]])[0]
55 # if the person has specified an election, we show them the voting
56 # page. otherwise, we redirect back to main the page
59 # look to see that the voter has been created and has voted in
60 # this election, and has confirmed their vote
61 @voter = QuickVoter.find_all(["session_id = ? and election_id = ?",
62 session.session_id, @election.id])[0]
64 # if the voter has not voted we destroy them
65 if @voter and not @voter.voted?
70 # if the voter does not exist or has has been destroyed, lets
73 # create a new voter and populate it
74 @voter = QuickVoter.new
75 @voter.election = QuickVote.find_all( [ "name = ?", params[:votename] ] )[0]
76 @voter.session_id = session.session_id
78 # create new vote and make it the defaulted sorted list
79 @voter.vote = Vote.new
81 @voter.vote.set_defaults!
85 redirect_to :controller => 'site'
90 # we need the election to verify that we have the right voter
91 election = QuickVote.find_all( [ "name = ?", params[:votename] ] )[0]
93 # find out who the voter is for this election
94 @voter = QuickVoter.find_all(["session_id = ? and election_id = ?",
95 session.session_id, election.id])[0]
98 # we have not seen this voter before. something is wrong, try
100 redirect_to quickvote_url( :votename => params[:votename] )
103 # this person has already voted, we try again
104 flash[:notice] = "You have already voted!"
105 redirect_to quickvote_url( :votename => params[:votename] )
108 # record the ip address for posterity
109 @voter.ipaddress = request.env["HTTP_X_FORWARDED_FOR"]
112 # toggle the confirmation bit
115 render :action => 'thanks'
120 voter = QuickVoter.find_all(["session_id = ?", session.session_id])[0]
122 redirect_to quickvote_url( :votename => params[:votename] )
126 @vote = Vote.find(params[:id])
128 @vote.rankings.each do |ranking|
129 ranking.rank = params['rankings-list'].index(ranking.candidate.id.to_s) + 1
132 render :nothing => true
136 ###############################################################
137 # the following method pertains to displaying the results of a
139 ###############################################################
142 @election = QuickVote.find_all( ["name = ?", params[:votename]] )[0]
144 # initalize the tallies to empty arrays
145 preference_tally = Array.new
146 plurality_tally = Array.new
147 approval_tally = Array.new
149 @election.voters.each do |voter|
150 # skip if the voter has not voted or has an unconfirmed vote
151 next unless voter.voted?
153 plurality_tally << voter.vote.rankings.sort[0].candidate.id
154 approval_tally << voter.vote.rankings.sort[0..1].collect \
155 { |ranking| ranking.candidate.id }
156 preference_tally << voter.vote.rankings.sort.collect \
157 { |ranking| ranking.candidate.id }
160 @plurality_result = PluralityVote.new(plurality_tally).result
161 @approval_result = ApprovalVote.new(approval_tally).result
162 @condorcet_result = PureCondorcetVote.new(preference_tally).result
163 @ssd_result = CloneproofSSDVote.new(preference_tally).result
164 @borda_result = BordaVote.new(preference_tally).result
165 #@runoff_result = InstantRunoffVote.new(preference_tally).result
166 #@runoff_results = PluralityVote.new(preference_tally).result
170 @election.candidates.each {|c| @candidates[c.id] = c}