]> projects.mako.cc - selectricity/blob - app/controllers/voter_controller.rb
a912824ebd25f6baf1539b320077e86004613ff3
[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   def index
16     if params[:election_id]
17       @election = Election.find(params[:election_id])
18       unless @election.authenticated?
19         @voter = OpenVoter.find(:all,
20           :conditions => ["session_id = ? and election_id = ?",
21           session.session_id, @election.id])[0]
22      
23         unless @voter and not @voter.election.kiosk
24           @voter = OpenVoter.new
25         end
26
27         @voter.election = @election
28         @voter.session_id = session.session_id
29         @password = "open." + @election.id.to_s
30       end
31     elsif params[:urlpassword]
32       password = params[:urlpassword]
33
34       if @voter = FullVoter.find(:all,
35         :conditions => [ "password = ?", password ] )[0]
36         @election = @voter.election
37         @password = @voter.password
38       end
39     end
40
41     if @voter and @election
42       # initialize things if the vote is blank
43       if @voter.vote.nil?
44         @voter.vote = Vote.new 
45         @voter.save
46       end
47     
48       @voter.vote.set_defaults! if @voter.vote.rankings.empty?
49
50       # if the election is now finished 
51       if @election.enddate < Time.now
52         redirect_to :action => :results, :id => @password
53       else
54         @sidebar_content = render_to_string(:partial => 'vote_sidebar')
55         if @election.embeddable? and params[:embed] == "true"
56           #look for custom theme, and assign to instance variabels for widget use
57           if @election.embed_custom_string
58             @top_bar = SkinPicture.find(:first,
59             :conditions => ["filename = ?", @election.embed_custom_string + "top_bar.png"])
60             @default_image = SkinPicture.find(:first,
61             :conditions => ["filename = ?", @election.embed_custom_string + "default_image.png"])
62             @bg1 = SkinPicture.find(:first,
63             :conditions => ["filename = ?", @election.embed_custom_string + "bg1.png"])
64             @bg2 = SkinPicture.find(:first,
65             :conditions => ["filename = ?", @election.embed_custom_string + "bg2.png"])
66             @bottom_bar = SkinPicture.find(:first,
67             :conditions => ["filename = ?", @election.embed_custom_string + "bottom_bar.png"])
68           end
69           render :template => 'embed/full_vote', :layout => 'embed'
70         else
71           render :action => 'full_vote'
72         end
73       end
74     end
75   end
76
77   def login
78     if params[:vote] and params[:vote][:password]
79       redirect_to votepassword_url( :action => 'index', :urlpassword => params[:vote][:password])
80     else
81       redirect_to :action => 'index'
82     end
83   end
84   
85   def pref_tables
86     if authenticate
87       @election = @voter.election
88       @results = @election.results
89       @candidates = {}
90       @election.candidates.each {|c| @candidates[c.id] = c}
91       @names = @election.names_by_id
92       render :template => 'common/pref_tables', :layout => 'basic'
93     else
94       redirect_to :action => 'index'
95     end
96   end
97
98   def details
99     if authenticate
100       @election = @voter.election
101       @votes = @election.votes.select {|v| v.confirmed? }.shuffle
102       @voters = @votes.collect {|v| v.voter}.shuffle
103       render :action => 'details'
104     else
105       redirect_to :action => 'index'
106     end
107   end
108
109   def review
110     if authenticate
111       @voter.vote.time = Time.now
112       @voter.vote.save
113       @voter.reload
114     else
115       redirect_to :action => 'index'
116     end
117   end
118
119   def confirm
120     if authenticate
121       @voter.vote.confirm!
122
123       if @voter.election.embeddable? and params[:embed] == "true" \
124         and @voter.election.early_results?
125         redirect_to :action => :results, :id => @password, :embed => 'true'
126       elsif @voter.election.kiosk and params[:kiosk] = "true"
127         redirect_to :action => "kiosk_ready", :id => @password, :kiosk => true
128       else
129         render :action => 'thanks'
130       end
131     else
132       redirect_to :action => 'index'
133     end
134   end
135   
136   def reminder
137     if params[:email]
138       voter_array= FullVoter.find(:all, :conditions => ["email = ?", params[:email]])
139       voter_array.delete_if {|voter| voter.election.active == 0}
140       unless voter_array.empty?
141         VoterNotify.deliver_reminder(voter_array)
142       end
143       render :action => 'reminder_sent'
144     end
145   end
146   
147   def results
148     if authenticate and
149       (@voter.election.early_results? \
150        or @voter.election.enddate < Time.now)
151       
152       @election = @voter.election
153       # compute and display results
154
155       @results = @election.results
156       @candidates = {}
157       @election.candidates.each {|c| @candidates[c.id] = c}
158       @names = @election.names_by_id
159         
160       @sidebar_content = render_to_string(:partial => 'results_sidebar')
161       #look for custom theme, and assign to instance variabels for widget use
162       if @election.embed_custom_string
163         @top_bar = SkinPicture.find(:first,
164         :conditions => ["filename = ?", @election.embed_custom_string + "top_bar.png"])
165         @default_image = SkinPicture.find(:first,
166         :conditions => ["filename = ?", @election.embed_custom_string + "default_image.png"])
167         @bg1 = SkinPicture.find(:first,
168         :conditions => ["filename = ?", @election.embed_custom_string + "bg1.png"])
169         @bg2 = SkinPicture.find(:first,
170         :conditions => ["filename = ?", @election.embed_custom_string + "bg2.png"])
171         @bottom_bar = SkinPicture.find(:first,
172         :conditions => ["filename = ?", @election.embed_custom_string + "bottom_bar.png"])
173       end
174       if @election.embeddable? and params[:embed] == "true"
175         render :template => 'embed/results', :layout => 'embed'
176       else
177         render :action => 'results'
178       end
179     else
180       redirect_to :action => 'index'
181     end
182   end
183  
184   def kiosk_ready
185     if not authenticate
186       redirect_to :action => 'index', :kiosk => true
187     end
188   end
189
190   private
191   def authenticate
192     password = params[:id]
193     if password == "open"
194       election = Election.find(params[:format])
195       unless election.authenticated?
196         @voter = OpenVoter.find(:all,
197           :conditions => ["session_id = ? and election_id = ?",
198                           session.session_id, election.id])[0]
199         @password = "open." + election.id.to_s
200       end
201     else
202       @voter = FullVoter.find(:all,
203         :conditions => [ "password = ?", password ] )[0]
204       @password = @voter.password
205     end
206     @voter
207   end
208 end
209

Benjamin Mako Hill || Want to submit a patch?