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'
127 (@voter.election.early_results? \
128 or @voter.election.enddate < Time.now)
130 @election = @voter.election
132 render_to_string(:partial => 'full_results_sidebar')
134 # look for custom theme, and assign to instance variabels for
136 if @election.embed_custom_string
137 @top_bar = SkinPicture.find(:first,
138 :conditions => ["filename = ?",
139 @election.embed_custom_string + "top_bar.png"])
140 @default_image = SkinPicture.find(:first,
141 :conditions => ["filename = ?",
142 @election.embed_custom_string + "default_image.png"])
143 @bg1 = SkinPicture.find(:first,
144 :conditions => ["filename = ?",
145 @election.embed_custom_string + "bg1.png"])
146 @bg2 = SkinPicture.find(:first,
147 :conditions => ["filename = ?",
148 @election.embed_custom_string + "bg2.png"])
149 @bottom_bar = SkinPicture.find(:first,
150 :conditions => ["filename = ?",
151 @election.embed_custom_string + "bottom_bar.png"])
153 if @election.embeddable? and params[:embed] == "true"
154 render :template => 'embed/results', :layout => 'embed'
156 render :template => 'common/results'
159 redirect_to :action => 'index'
164 @election = @voter.election
165 render :template => 'common/pref_tables_wrapper', :layout => 'basic'
169 @election = @voter.election
170 render :template => 'common/details'
177 redirect_to :action => 'index'
183 password = params[:id]
184 if password == "open"
185 election = Election.find(params[:format])
187 # if it's not actually open, lets redirect
188 if election.authenticated
189 redirect_to :action => 'index'
191 # otherwise, lets see if they've before
193 @voter = OpenVoter.find(:all,
194 :conditions => ["session_id = ? and election_id = ?",
195 session.session_id, election.id])[0]
197 # when (a) there is no voter or (b) when there is a voter but
198 # it's kiosk mode on the right page, rewrite with a blank voter
199 if ((not @voter) and (election.enddate < Time.now)) \
200 or (params[:action] == 'kiosk_ready' and election.kiosk)
201 @voter = OpenVoter.new unless @voter
204 # now that we have a voter (one way or another), set things
206 @voter.election = election
207 @voter.session_id = session.session_id
208 @password = "open." + election.id.to_s
212 @voter = FullVoter.find(:all,
213 :conditions => [ "password = ?", password ] )[0]
216 @password = @voter.password
218 redirect_to :Action => 'index'