fixed a large number of bugs in the software (see wiki) over a days work
[selectricity] / app / controllers / voter_controller.rb
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
4 #
5 # This program is free software. Please see the COPYING file for
6 # details.
7
8 class VoterController < ApplicationController
9   helper :sparklines
10   layout 'main'
11   require_dependency "voter"
12   require_dependency "vote"
13   require_dependency "election"
14
15   before_filter :authenticate, :except => [:index, :login, :reminder,
16                                            :kiosk_ready]
17
18   def index
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]
25      
26         @voter = OpenVoter.new unless @voter
27
28         @voter.election = @election
29         @voter.session_id = session.session_id
30         @password = "open." + @election.id.to_s
31       end
32     elsif params[:urlpassword]
33       password = params[:urlpassword]
34
35       if @voter = FullVoter.find(:all,
36         :conditions => [ "password = ?", password ] )[0]
37         @election = @voter.election
38         @password = @voter.password
39       end
40     end
41
42     if @voter and @election
43       # initialize things if the vote is blank
44       if @voter.vote.nil?
45         @voter.vote = Vote.new 
46         @voter.save
47       end
48     
49       @voter.vote.set_defaults! if @voter.vote.rankings.empty?
50
51       # if the election is now finished 
52       if @election.enddate < Time.now
53         redirect_to :action => :results, :id => @password
54       else
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"])
69           end
70           render :template => 'embed/full_vote', :layout => 'embed'
71         else
72           render :action => 'full_vote'
73         end
74       end
75     end
76   end
77
78   def login
79     if params[:vote] and params[:vote][:password]
80       redirect_to votepassword_url( :action => 'index', :urlpassword => params[:vote][:password])
81     else
82       redirect_to :action => 'index'
83     end
84   end
85   
86   def review
87     @voter.vote.time = Time.now
88     @voter.vote.save
89     @voter.reload
90   end
91
92   def confirm
93     @voter.vote.confirm!
94
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
101     else
102       render :action => 'thanks'
103     end
104   end
105   
106   def reminder
107     if params[:email]
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)
112       end
113       render :action => 'reminder_sent'
114     end
115   end
116   
117   def results
118     if @voter.election.early_results? \
119        or @voter.election.enddate < Time.now
120       
121       @election = @voter.election
122       @sidebar_content = render_to_string(:partial => 'full_results_sidebar')
123
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"])
136       end
137       if @election.embeddable? and params[:embed] == "true"
138         render :template => 'embed/results', :layout => 'embed'
139       else
140         render :template => 'common/results'
141       end
142     else
143       redirect_to :action => 'index'
144     end
145   end
146  
147   def pref_tables
148     @election = @voter.election
149     render :template => 'common/pref_tables_wrapper', :layout => 'basic'
150   end
151
152   def details
153     @election = @voter.election
154     render :template => 'common/details'
155   end
156
157   def kiosk_ready
158     reset_session
159
160     if not authenticate
161       redirect_to :action => 'index'
162     end
163   end
164
165   private
166   def authenticate
167     password = params[:id]
168     if password == "open"
169       election = Election.find(params[:format])
170
171       # if it's not actually open, lets redirect
172       if election.authenticated
173         redirect_to :action => 'index'
174       
175       # otherwise, lets see if they've before
176       else
177         @voter = OpenVoter.find(:all,
178           :conditions => ["session_id = ? and election_id = ?",
179                           session.session_id, election.id])[0]
180
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
183         if not(@voter) \
184           or (params[:action] == 'kiosk_ready' and election.kiosk)
185           @voter = OpenVoter.new unless @voter
186         end
187
188         # now that we have a voter (one way or another), set things
189         # right
190         @voter.election = election
191         @voter.session_id = session.session_id
192         @password = "open." + election.id.to_s
193       end
194
195     else
196       @voter = FullVoter.find(:all,
197         :conditions => [ "password = ?", password ] )[0]
198
199       if @voter
200         @password = @voter.password
201       else
202         redirect_to :Action => 'index'
203       end
204     end
205   end
206 end
207

Benjamin Mako Hill || Want to submit a patch?