+class QuickvoteController < ApplicationController
+ layout 'vb'
+ model :quick_voter
+ model :vote
+ model :election
+
+ def index
+ @election = QuickVote.find_all(["name = ?", params[:votename]])[0]
+
+ @voter = QuickVoter.find_all(["session_id = ? and election_id = ?",
+ session.session_id, @election.id])[0]
+ unless @voter
+ @voter = QuickVoter.new
+ @voter.election = Election.find_all( [ "name = ?", params[:votename] ] )[0]
+ end
+ end
+
+ def create
+ if params[:quickvote]
+ @quickvote = QuickVote.new(params[:quickvote])
+ if @quickvote.reviewed? and @quickvote.save
+ @quickvote = @quickvote.reload
+ render :action => 'success'
+ end
+ end
+ end
+
+ def change
+ voter = QuickVoter.find_all(["session_id = ?", session.session_id])[0]
+ voter.destroy
+ redirect_to quickvote_url( :votename => params[:votename] )
+ end
+
+ def confirm
+ election = QuickVote.find_all(["name = ?", params[:votename]])[0]
+
+ if QuickVoter.find_all(["session_id = ? and election_id = ?",
+ session.session_id, election.id])[0]
+ flash[:notice] = "You have already voted!"
+ redirect_to quickvote_url( :votename => params[:votename] )
+ else
+ @voter = QuickVoter.new()
+ @voter.election = election
+ @voter.session_id = session.session_id
+ @voter.save
+ @voter.reload
+
+ @voter.vote = Vote.new
+ @voter.vote.votestring = params[:vote][:votestring]
+ @voter.vote.confirm!
+ render :action => 'thanks'
+ end
+ end
+
+ def results
+ @election = Election.find_all( ["name = ?", params[:votename]] )[0]
+
+ preference_tally = []
+ plurality_tally = []
+ approval_tally = []
+ @election.voters.each do |voter|
+ plurality_tally << voter.vote.rankings.sort[0].candidate.id
+ approval_tally << voter.vote.rankings.sort[0..1].collect {|ranking| ranking.candidate.id}
+ preference_tally << voter.vote.rankings.sort.collect {|ranking| ranking.candidate.id}
+ end
+
+ @plurality_result = PluralityVote.new(plurality_tally).result
+ @approval_result = ApprovalVote.new(approval_tally).result
+ @condorcet_result = CloneproofSSDVote.new(preference_tally).result
+ @ssd_result = PureCondorcetVote.new(preference_tally).result
+ @borda_result = BordaVote.new(preference_tally).result
+ @runoff_result = InstantRunoffVote.new(preference_tally).result
+
+ @candidates = {}
+ @election.candidates.each {|c| @candidates[c.id] = c}
+ end
+
+end