1 # Selectricity: Voting Machinery for the Masses
2 # Copyright (C) 2007, 2008 Benjamin Mako Hill <mako@atdot.cc>
3 # Copyright (C) 2007 Massachusetts Institute of Technology
5 # This program is free software. Please see the COPYING file for
8 class VoterController < ApplicationController
11 require_dependency "voter"
12 require_dependency "vote"
13 require_dependency "election"
16 if params[:election_id]
17 @election = Election.find(params[:election_id])
18 unless @election.authenticated?
19 @voter = OpenVoter.find(:all,
20 :conditions => ["session_id = ? and election_id = ?",
21 session.session_id, @election.id])[0]
23 @voter = OpenVoter.new unless @voter
25 @voter.election = @election
26 @voter.session_id = session.session_id
27 @password = "open." + @election.id.to_s
29 elsif params[:urlpassword]
30 password = params[:urlpassword]
32 if @voter = FullVoter.find(:all,
33 :conditions => [ "password = ?", password ] )[0]
34 @election = @voter.election
35 @password = @voter.password
39 if @voter and @election
40 # initialize things if the vote is blank
42 @voter.vote = Vote.new
46 @voter.vote.set_defaults! if @voter.vote.rankings.empty?
48 # if the election is now finished
49 if @election.enddate < Time.now
50 redirect_to :action => :results, :id => @password
52 @sidebar_content = render_to_string(:partial => 'vote_sidebar')
53 if @election.embeddable? and params[:embed] == "true"
54 #look for custom theme, and assign to instance variabels for widget use
55 if @election.embed_custom_string
56 @top_bar = SkinPicture.find(:first,
57 :conditions => ["filename = ?", @election.embed_custom_string + "top_bar.png"])
58 @default_image = SkinPicture.find(:first,
59 :conditions => ["filename = ?", @election.embed_custom_string + "default_image.png"])
60 @bg1 = SkinPicture.find(:first,
61 :conditions => ["filename = ?", @election.embed_custom_string + "bg1.png"])
62 @bg2 = SkinPicture.find(:first,
63 :conditions => ["filename = ?", @election.embed_custom_string + "bg2.png"])
64 @bottom_bar = SkinPicture.find(:first,
65 :conditions => ["filename = ?", @election.embed_custom_string + "bottom_bar.png"])
67 render :template => 'embed/full_vote', :layout => 'embed'
69 render :action => 'full_vote'
76 if params[:vote] and params[:vote][:password]
77 redirect_to votepassword_url( :action => 'index', :urlpassword => params[:vote][:password])
79 redirect_to :action => 'index'
85 @election = @voter.election
86 @results = @election.results
88 @election.candidates.each {|c| @candidates[c.id] = c}
89 @names = @election.names_by_id
90 render :template => 'common/pref_tables', :layout => 'basic'
92 redirect_to :action => 'index'
98 @election = @voter.election
99 @votes = @election.votes.select {|v| v.confirmed? }.shuffle
100 @voters = @votes.collect {|v| v.voter}.shuffle
101 render :action => 'details'
103 redirect_to :action => 'index'
109 @voter.vote.time = Time.now
113 redirect_to :action => 'index'
121 if @voter.election.embeddable? and params[:embed] == "true" \
122 and @voter.election.early_results?
123 redirect_to :action => :results, :id => @password, :embed => 'true'
124 elsif @voter.election.kiosk and params[:kiosk] = "true"
125 redirect_to :action => "kiosk_ready", :id => @password, :kiosk => true
127 render :action => 'thanks'
130 redirect_to :action => 'index'
136 voter_array= FullVoter.find(:all, :conditions => ["email = ?", params[:email]])
137 voter_array.delete_if {|voter| voter.election.active == 0}
138 unless voter_array.empty?
139 VoterNotify.deliver_reminder(voter_array)
141 render :action => 'reminder_sent'
147 (@voter.election.early_results? \
148 or @voter.election.enddate < Time.now)
150 @election = @voter.election
151 # compute and display results
153 @results = @election.results
155 @election.candidates.each {|c| @candidates[c.id] = c}
156 @names = @election.names_by_id
158 @sidebar_content = render_to_string(:partial => 'results_sidebar')
159 #look for custom theme, and assign to instance variabels for widget use
160 if @election.embed_custom_string
161 @top_bar = SkinPicture.find(:first,
162 :conditions => ["filename = ?", @election.embed_custom_string + "top_bar.png"])
163 @default_image = SkinPicture.find(:first,
164 :conditions => ["filename = ?", @election.embed_custom_string + "default_image.png"])
165 @bg1 = SkinPicture.find(:first,
166 :conditions => ["filename = ?", @election.embed_custom_string + "bg1.png"])
167 @bg2 = SkinPicture.find(:first,
168 :conditions => ["filename = ?", @election.embed_custom_string + "bg2.png"])
169 @bottom_bar = SkinPicture.find(:first,
170 :conditions => ["filename = ?", @election.embed_custom_string + "bottom_bar.png"])
172 if @election.embeddable? and params[:embed] == "true"
173 render :template => 'embed/results', :layout => 'embed'
175 render :action => 'results'
178 redirect_to :action => 'index'
186 redirect_to :action => 'index'
192 password = params[:id]
193 if password == "open"
194 election = Election.find(params[:format])
196 # check to see if the person has voted before
197 unless election.authenticated?
198 @voter = OpenVoter.find(:all,
199 :conditions => ["session_id = ? and election_id = ?",
200 session.session_id, election.id])[0]
201 @password = "open." + election.id.to_s
204 # if it's ready for kiosk_mode, then we create and authenticate
205 unless @voter and params[:action] == 'kiosk_ready' \
208 # this is maybe not quite as a dry as it should be
209 @voter = OpenVoter.new unless @voter
211 @voter.election = election
212 @voter.session_id = session.session_id
213 @password = "open." + election.id.to_s
217 @voter = FullVoter.find(:all,
218 :conditions => [ "password = ?", password ] )[0]
219 @password = @voter.password