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"
15 before_filter :authenticate, :except => [:index, :login, :reminder,
16 :kiosk_ready, :sort_candidates]
19 if params[:election_id]
20 @election = Election.find(params[:election_id])
21 unless @election.authenticated?
22 @voter = OpenVoter.find(:all,
23 :conditions => ["session_id = ? and election_id = ?",
24 session.session_id, @election.id])[0]
26 @voter = OpenVoter.new unless @voter
28 @voter.election = @election
29 @voter.session_id = session.session_id
30 @password = "open." + @election.id.to_s
32 elsif params[:urlpassword]
33 password = params[:urlpassword]
35 if @voter = FullVoter.find(:all,
36 :conditions => [ "password = ?", password ] )[0]
37 @election = @voter.election
38 @password = @voter.password
42 if @voter and @election
43 # initialize things if the vote is blank
45 @voter.vote = Vote.new
49 @voter.vote.set_defaults! if @voter.vote.rankings.empty?
51 # if the election is now finished
52 if @election.enddate < Time.now
53 redirect_to :action => :results, :id => @password
55 @sidebar_content = render_to_string(:partial => 'vote_sidebar')
56 if @election.embeddable? and params[:embed] == "true"
57 # look for custom theme, and assign to instance variabels
59 if @election.embed_custom_string
60 @top_bar = SkinPicture.find(:first,
61 :conditions => ["filename = ?",
62 @election.embed_custom_string + "top_bar.png"])
63 @default_image = SkinPicture.find(:first,
64 :conditions => ["filename = ?",
65 @election.embed_custom_string + "default_image.png"])
66 @bg1 = SkinPicture.find(:first,
67 :conditions => ["filename = ?",
68 @election.embed_custom_string + "bg1.png"])
69 @bg2 = SkinPicture.find(:first,
70 :conditions => ["filename = ?",
71 @election.embed_custom_string + "bg2.png"])
72 @bottom_bar = SkinPicture.find(:first,
73 :conditions => ["filename = ?",
74 @election.embed_custom_string + "bottom_bar.png"])
76 render :template => 'embed/full_vote', :layout => 'embed'
78 render :action => 'full_vote'
85 if params[:vote] and params[:vote][:password]
86 redirect_to votepassword_url(:action => 'index',
87 :urlpassword => params[:vote][:password])
89 redirect_to :action => 'index'
94 @voter.vote.time = Time.now
102 if @voter.election.embeddable? and params[:embed] == "true" \
103 and @voter.election.early_results?
104 redirect_to :action => :results, :id => @password, :embed => 'true'
105 elsif not(@voter.election.verifiable) \
106 and @voter.election.kiosk and params[:kiosk] == "true"
107 redirect_to :action => "kiosk_ready", :id => @password, :kiosk => true
109 render :action => 'thanks'
115 voter_array= FullVoter.find(:all,
116 :conditions => ["email = ?", params[:email]])
117 voter_array.delete_if {|voter| voter.election.active == 0}
118 unless voter_array.empty?
119 VoterNotify.deliver_reminder(voter_array)
121 render :action => 'reminder_sent'
126 if @voter.election.early_results? \
127 or @voter.election.enddate < Time.now
129 @election = @voter.election
131 render_to_string(:partial => 'full_results_sidebar')
133 # look for custom theme, and assign to instance variabels for
135 if @election.embed_custom_string
136 @top_bar = SkinPicture.find(:first,
137 :conditions => ["filename = ?",
138 @election.embed_custom_string + "top_bar.png"])
139 @default_image = SkinPicture.find(:first,
140 :conditions => ["filename = ?",
141 @election.embed_custom_string + "default_image.png"])
142 @bg1 = SkinPicture.find(:first,
143 :conditions => ["filename = ?",
144 @election.embed_custom_string + "bg1.png"])
145 @bg2 = SkinPicture.find(:first,
146 :conditions => ["filename = ?",
147 @election.embed_custom_string + "bg2.png"])
148 @bottom_bar = SkinPicture.find(:first,
149 :conditions => ["filename = ?",
150 @election.embed_custom_string + "bottom_bar.png"])
152 if @election.embeddable? and params[:embed] == "true"
153 render :template => 'embed/results', :layout => 'embed'
155 render :template => 'common/results'
158 redirect_to :action => 'index'
163 @election = @voter.election
164 render :template => 'common/pref_tables_wrapper', :layout => 'basic'
168 @election = @voter.election
169 render :template => 'common/details'
176 redirect_to :action => 'index'
182 password = params[:id]
183 if password == "open"
184 election = Election.find(params[:format])
186 # if it's not actually open, lets redirect
187 if election.authenticated
188 redirect_to :action => 'index'
190 # otherwise, lets see if they've before
192 @voter = OpenVoter.find(:all,
193 :conditions => ["session_id = ? and election_id = ?",
194 session.session_id, election.id])[0]
196 # when (a) there is no voter or (b) when there is a voter but
197 # it's kiosk mode on the right page, rewrite with a blank voter
199 or (params[:action] == 'kiosk_ready' and election.kiosk)
200 @voter = OpenVoter.new unless @voter
203 # now that we have a voter (one way or another), set things
205 @voter.election = election
206 @voter.session_id = session.session_id
207 @password = "open." + election.id.to_s
211 @voter = FullVoter.find(:all,
212 :conditions => [ "password = ?", password ] )[0]
215 @password = @voter.password
217 redirect_to :Action => 'index'