]> projects.mako.cc - selectricity/blob - app/controllers/voter_controller.rb
Added funcitionality so user can change the email address associated with their accou...
[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, :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 authenticate and
127       (@voter.election.early_results? \
128        or @voter.election.enddate < Time.now)
129       
130       @election = @voter.election
131       @sidebar_content = \
132         render_to_string(:partial => 'full_results_sidebar')
133
134       # look for custom theme, and assign to instance variabels for
135       # widget use
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"])
152       end
153       if @election.embeddable? and params[:embed] == "true"
154         render :template => 'embed/results', :layout => 'embed'
155       else
156         render :template => 'common/results'
157       end
158     else
159       redirect_to :action => 'index'
160     end
161   end
162  
163   def pref_tables
164     @election = @voter.election
165     render :template => 'common/pref_tables_wrapper', :layout => 'basic'
166   end
167
168   def details
169     @election = @voter.election
170     render :template => 'common/details'
171   end
172
173   def kiosk_ready
174     reset_session
175
176     if not authenticate
177       redirect_to :action => 'index'
178     end
179   end
180
181   private
182   def authenticate
183     password = params[:id]
184     if password == "open"
185       election = Election.find(params[:format])
186
187       # if it's not actually open, lets redirect
188       if election.authenticated
189         redirect_to :action => 'index'
190       
191       # otherwise, lets see if they've before
192       else
193         @voter = OpenVoter.find(:all,
194           :conditions => ["session_id = ? and election_id = ?",
195                           session.session_id, election.id])[0]
196
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
202         end
203
204         # now that we have a voter (one way or another), set things
205         # right
206         @voter.election = election
207         @voter.session_id = session.session_id
208         @password = "open." + election.id.to_s
209       end
210
211     else
212       @voter = FullVoter.find(:all,
213         :conditions => [ "password = ?", password ] )[0]
214
215       if @voter
216         @password = @voter.password
217       else
218         redirect_to :Action => 'index'
219       end
220     end
221   end
222 end
223

Benjamin Mako Hill || Want to submit a patch?