cdc045da05c3e680d425918a7c0670685590eff5
[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: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Affero General Public License for more details.
14 #
15 # You should have received a copy of the GNU Affero General Public
16 # License along with this program.  If not, see
17 # <http://www.gnu.org/licenses/>.
18
19 class VoterController < ApplicationController
20   layout 'main'
21   require_dependency "voter"
22   require_dependency "vote"
23   require_dependency "election"
24
25   def index
26     if params[:election_id]
27       @election = Election.find(params[:election_id])
28       unless @election.authenticated?
29         @voter = Voter.find(:all,
30           :conditions => ["session_id = ? and election_id = ?",
31           session.session_id, @election.id])[0]
32       
33         @voter = Voter.new unless @voter
34
35         @voter.election = @election
36         @voter.session_id = session.session_id
37         @password = "open." + @election.id.to_s
38       end
39     elsif params[:urlpassword]
40       password = params[:urlpassword]
41
42       if @voter = FullVoter.find(:all,
43         :conditions => [ "password = ?", password ] )[0]
44         @election = @voter.election
45         @password = @voter.password
46       end
47     end
48
49     if @voter and @election
50       # initialize things if the vote is blank
51       if @voter.vote.nil?
52         @voter.vote = Vote.new 
53         @voter.save
54       end
55       
56       @voter.vote.set_defaults! if @voter.vote.rankings.empty?
57
58       # if the election is now finished 
59       if @election.enddate < Time.now
60         redirect_to :action => :results, :id => @password
61       else
62         @sidebar_content = render_to_string(:partial => 'vote_sidebar')
63         if @election.embeddable? and params[:embed] == "true"
64           render :template => 'embed/full_vote', :layout => 'embed'
65         else
66           render :action => 'full_vote'
67         end
68       end
69     end
70   end
71
72   def login
73     if params[:vote] and params[:vote][:password]
74       redirect_to votepassword_url( :action => 'index', :urlpassword => params[:vote][:password])
75     else
76       redirect_to :action => 'index'
77     end
78   end
79   
80   def pref_tables
81     if authenticate
82       @election = @voter.election
83       @results = @election.results
84       @candidates = {}
85       @election.candidates.each {|c| @candidates[c.id] = c}
86       @names = @election.names_by_id
87       render :template => 'common/pref_tables', :layout => 'basic'
88     else
89       redirect_to :action => 'index'
90     end
91   end
92
93   def details
94     if authenticate
95       @election = @voter.election
96       @votes = @election.votes.select {|v| v.confirmed? }.randomize
97       @voters = @votes.collect {|v| v.voter}.randomize
98       render :action => 'details'
99     else
100       redirect_to :action => 'index'
101     end
102   end
103
104   def review
105     if authenticate
106       @voter.vote.time = Time.now
107       @voter.vote.save
108       @voter.reload
109     else
110       redirect_to :action => 'index'
111     end
112   end
113
114   def confirm
115     if authenticate
116       @voter.vote.confirm!
117
118       if @voter.election.embeddable? and params[:embed] == "true" \
119         and @voter.election.early_results?
120         redirect_to :action => :results, :id => @password, :embed => 'true'
121       else
122         render :action => 'thanks'
123       end
124     else
125       redirect_to :action => 'index'
126     end
127   end
128   
129   def reminder
130     if params[:email]
131       voter_array= FullVoter.find(:all, :conditions => ["email = ?", params[:email]])
132       voter_array.delete_if {|voter| voter.election.active == 0}
133       unless voter_array.empty?
134         VoterNotify.deliver_reminder(voter_array)
135       end
136       render :action => 'reminder_sent'
137     end
138   end
139   
140   def results
141     if authenticate and
142       (@voter.election.early_results? \
143        or @voter.election.enddate < Time.now)
144       
145       @election = @voter.election
146       # compute and display results
147
148       @results = @election.results
149       @candidates = {}
150       @election.candidates.each {|c| @candidates[c.id] = c}
151       @names = @election.names_by_id
152         
153       @sidebar_content = render_to_string(:partial => 'results_sidebar')
154       if @election.embeddable? and params[:embed] == "true"
155         render :template => 'embed/results', :layout => 'embed'
156       else
157         render :action => 'results'
158       end
159     else
160       redirect_to :action => 'index'
161     end
162   end
163   
164   private
165   def authenticate
166     password = params[:id]
167     if password == "open"
168       election = Election.find(params[:format])
169       unless election.authenticated?
170         @voter = Voter.find(:all,
171           :conditions => ["session_id = ? and election_id = ?",
172                           session.session_id, election.id])[0]
173         @password = "open." + election.id.to_s
174       end
175     else
176       @voter = FullVoter.find(:all,
177         :conditions => [ "password = ?", password ] )[0]
178       @password = @voter.password
179     end
180     @voter
181   end
182 end
183

Benjamin Mako Hill || Want to submit a patch?