Link to source code more prominently.
[selectricity-live] / 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, :sort_candidates]
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
58           # for widget use
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"])
75           end
76           render :template => 'embed/full_vote', :layout => 'embed'
77         else
78           render :action => 'full_vote'
79         end
80       end
81     end
82   end
83
84   def login
85     if params[:vote] and params[:vote][:password]
86       redirect_to votepassword_url(:action => 'index',
87         :urlpassword => params[:vote][:password])
88     else
89       redirect_to :action => 'index'
90     end
91   end
92   
93   def review
94     @voter.vote.time = Time.now
95     @voter.vote.save
96     @voter.reload
97   end
98
99   def confirm
100     @voter.vote.confirm!
101
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
108     else
109       render :action => 'thanks'
110     end
111   end
112   
113   def reminder
114     if params[:email]
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)
120       end
121       render :action => 'reminder_sent'
122     end
123   end
124   
125   def results
126     if @voter.election.early_results? \
127        or @voter.election.enddate < Time.now
128       
129       @election = @voter.election
130       @sidebar_content = \
131         render_to_string(:partial => 'full_results_sidebar')
132
133       # look for custom theme, and assign to instance variabels for
134       # widget use
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"])
151       end
152       if @election.embeddable? and params[:embed] == "true"
153         render :template => 'embed/results', :layout => 'embed'
154       else
155         render :template => 'common/results'
156       end
157     else
158       redirect_to :action => 'index'
159     end
160   end
161  
162   def pref_tables
163     @election = @voter.election
164     render :template => 'common/pref_tables_wrapper', :layout => 'basic'
165   end
166
167   def details
168     @election = @voter.election
169     render :template => 'common/details'
170   end
171
172   def kiosk_ready
173     reset_session
174
175     if not authenticate
176       redirect_to :action => 'index'
177     end
178   end
179
180   private
181   def authenticate
182     password = params[:id]
183     if password == "open"
184       election = Election.find(params[:format])
185
186       # if it's not actually open, lets redirect
187       if election.authenticated
188         redirect_to :action => 'index'
189       
190       # otherwise, lets see if they've before
191       else
192         @voter = OpenVoter.find(:all,
193           :conditions => ["session_id = ? and election_id = ?",
194                           session.session_id, election.id])[0]
195
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
198         if not(@voter) \
199           or (params[:action] == 'kiosk_ready' and election.kiosk)
200           @voter = OpenVoter.new unless @voter
201         end
202
203         # now that we have a voter (one way or another), set things
204         # right
205         @voter.election = election
206         @voter.session_id = session.session_id
207         @password = "open." + election.id.to_s
208       end
209
210     else
211       @voter = FullVoter.find(:all,
212         :conditions => [ "password = ?", password ] )[0]
213
214       if @voter
215         @password = @voter.password
216       else
217         redirect_to :Action => 'index'
218       end
219     end
220   end
221 end
222

Benjamin Mako Hill || Want to submit a patch?