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

Benjamin Mako Hill || Want to submit a patch?