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,
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 for widget use
58 if @election.embed_custom_string
59 @top_bar = SkinPicture.find(:first,
60 :conditions => ["filename = ?", @election.embed_custom_string + "top_bar.png"])
61 @default_image = SkinPicture.find(:first,
62 :conditions => ["filename = ?", @election.embed_custom_string + "default_image.png"])
63 @bg1 = SkinPicture.find(:first,
64 :conditions => ["filename = ?", @election.embed_custom_string + "bg1.png"])
65 @bg2 = SkinPicture.find(:first,
66 :conditions => ["filename = ?", @election.embed_custom_string + "bg2.png"])
67 @bottom_bar = SkinPicture.find(:first,
68 :conditions => ["filename = ?", @election.embed_custom_string + "bottom_bar.png"])
70 render :template => 'embed/full_vote', :layout => 'embed'
72 render :action => 'full_vote'
79 if params[:vote] and params[:vote][:password]
80 redirect_to votepassword_url( :action => 'index', :urlpassword => params[:vote][:password])
82 redirect_to :action => 'index'
87 @voter.vote.time = Time.now
95 if @voter.election.embeddable? and params[:embed] == "true" \
96 and @voter.election.early_results?
97 redirect_to :action => :results, :id => @password, :embed => 'true'
98 elsif not(@voter.election.verifiable) \
99 and @voter.election.kiosk and params[:kiosk] == "true"
100 redirect_to :action => "kiosk_ready", :id => @password, :kiosk => true
102 render :action => 'thanks'
108 voter_array= FullVoter.find(:all, :conditions => ["email = ?", params[:email]])
109 voter_array.delete_if {|voter| voter.election.active == 0}
110 unless voter_array.empty?
111 VoterNotify.deliver_reminder(voter_array)
113 render :action => 'reminder_sent'
118 if @voter.election.early_results? \
119 or @voter.election.enddate < Time.now
121 @election = @voter.election
122 @sidebar_content = render_to_string(:partial => 'full_results_sidebar')
124 #look for custom theme, and assign to instance variabels for widget use
125 if @election.embed_custom_string
126 @top_bar = SkinPicture.find(:first,
127 :conditions => ["filename = ?", @election.embed_custom_string + "top_bar.png"])
128 @default_image = SkinPicture.find(:first,
129 :conditions => ["filename = ?", @election.embed_custom_string + "default_image.png"])
130 @bg1 = SkinPicture.find(:first,
131 :conditions => ["filename = ?", @election.embed_custom_string + "bg1.png"])
132 @bg2 = SkinPicture.find(:first,
133 :conditions => ["filename = ?", @election.embed_custom_string + "bg2.png"])
134 @bottom_bar = SkinPicture.find(:first,
135 :conditions => ["filename = ?", @election.embed_custom_string + "bottom_bar.png"])
137 if @election.embeddable? and params[:embed] == "true"
138 render :template => 'embed/results', :layout => 'embed'
140 render :template => 'common/results'
143 redirect_to :action => 'index'
148 @election = @voter.election
149 render :template => 'common/pref_tables_wrapper', :layout => 'basic'
153 @election = @voter.election
154 render :template => 'common/details'
161 redirect_to :action => 'index'
167 password = params[:id]
168 if password == "open"
169 election = Election.find(params[:format])
171 # if it's not actually open, lets redirect
172 if election.authenticated
173 redirect_to :action => 'index'
175 # otherwise, lets see if they've before
177 @voter = OpenVoter.find(:all,
178 :conditions => ["session_id = ? and election_id = ?",
179 session.session_id, election.id])[0]
181 # when (a) there is no voter or (b) when there is a voter but
182 # it's kiosk mode on the right page, rewrite witha blank voter
184 or (params[:action] == 'kiosk_ready' and election.kiosk)
185 @voter = OpenVoter.new unless @voter
188 # now that we have a voter (one way or another), set things
190 @voter.election = election
191 @voter.session_id = session.session_id
192 @password = "open." + election.id.to_s
196 @voter = FullVoter.find(:all,
197 :conditions => [ "password = ?", password ] )[0]
200 @password = @voter.password
202 redirect_to :Action => 'index'