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
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.
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.
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/>.
19 class VoterController < ApplicationController
21 require_dependency "voter"
22 require_dependency "vote"
23 require_dependency "election"
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]
33 @voter = Voter.new unless @voter
35 @voter.election = @election
36 @voter.session_id = session.session_id
37 @password = "open." + @election.id.to_s
39 elsif params[:urlpassword]
40 password = params[:urlpassword]
42 if @voter = FullVoter.find(:all,
43 :conditions => [ "password = ?", password ] )[0]
44 @election = @voter.election
45 @password = @voter.password
49 if @voter and @election
50 # initialize things if the vote is blank
52 @voter.vote = Vote.new
56 @voter.vote.set_defaults! if @voter.vote.rankings.empty?
58 # if the election is now finished
59 if @election.enddate < Time.now
60 redirect_to :action => :results, :id => @password
62 @sidebar_content = render_to_string(:partial => 'vote_sidebar')
63 if @election.embeddable? and params[:embed] == "true"
64 #look for custom theme, and assign to instance variabels for widget use
65 if @election.embed_custom_string
66 @top_bar = SkinPicture.find(:first,
67 :conditions => ["filename = ?", @election.embed_custom_string + "top_bar.png"])
68 @default_image = SkinPicture.find(:first,
69 :conditions => ["filename = ?", @election.embed_custom_string + "default_image.png"])
70 @bg1 = SkinPicture.find(:first,
71 :conditions => ["filename = ?", @election.embed_custom_string + "bg1.png"])
72 @bg2 = SkinPicture.find(:first,
73 :conditions => ["filename = ?", @election.embed_custom_string + "bg2.png"])
74 @bottom_bar = SkinPicture.find(:first,
75 :conditions => ["filename = ?", @election.embed_custom_string + "bottom_bar.png"])
77 render :template => 'embed/full_vote', :layout => 'embed'
79 render :action => 'full_vote'
86 if params[:vote] and params[:vote][:password]
87 redirect_to votepassword_url( :action => 'index', :urlpassword => params[:vote][:password])
89 redirect_to :action => 'index'
95 @election = @voter.election
96 @results = @election.results
98 @election.candidates.each {|c| @candidates[c.id] = c}
99 @names = @election.names_by_id
100 render :template => 'common/pref_tables', :layout => 'basic'
102 redirect_to :action => 'index'
108 @election = @voter.election
109 @votes = @election.votes.select {|v| v.confirmed? }.randomize
110 @voters = @votes.collect {|v| v.voter}.randomize
111 render :action => 'details'
113 redirect_to :action => 'index'
119 @voter.vote.time = Time.now
123 redirect_to :action => 'index'
131 if @voter.election.embeddable? and params[:embed] == "true" \
132 and @voter.election.early_results?
133 redirect_to :action => :results, :id => @password, :embed => 'true'
135 render :action => 'thanks'
138 redirect_to :action => 'index'
144 voter_array= FullVoter.find(:all, :conditions => ["email = ?", params[:email]])
145 voter_array.delete_if {|voter| voter.election.active == 0}
146 unless voter_array.empty?
147 VoterNotify.deliver_reminder(voter_array)
149 render :action => 'reminder_sent'
155 (@voter.election.early_results? \
156 or @voter.election.enddate < Time.now)
158 @election = @voter.election
159 # compute and display results
161 @results = @election.results
163 @election.candidates.each {|c| @candidates[c.id] = c}
164 @names = @election.names_by_id
166 @sidebar_content = render_to_string(:partial => 'results_sidebar')
167 #look for custom theme, and assign to instance variabels for widget use
168 if @election.embed_custom_string
169 @top_bar = SkinPicture.find(:first,
170 :conditions => ["filename = ?", @election.embed_custom_string + "top_bar.png"])
171 @default_image = SkinPicture.find(:first,
172 :conditions => ["filename = ?", @election.embed_custom_string + "default_image.png"])
173 @bg1 = SkinPicture.find(:first,
174 :conditions => ["filename = ?", @election.embed_custom_string + "bg1.png"])
175 @bg2 = SkinPicture.find(:first,
176 :conditions => ["filename = ?", @election.embed_custom_string + "bg2.png"])
177 @bottom_bar = SkinPicture.find(:first,
178 :conditions => ["filename = ?", @election.embed_custom_string + "bottom_bar.png"])
180 if @election.embeddable? and params[:embed] == "true"
181 render :template => 'embed/results', :layout => 'embed'
183 render :action => 'results'
186 redirect_to :action => 'index'
192 password = params[:id]
193 if password == "open"
194 election = Election.find(params[:format])
195 unless election.authenticated?
196 @voter = Voter.find(:all,
197 :conditions => ["session_id = ? and election_id = ?",
198 session.session_id, election.id])[0]
199 @password = "open." + election.id.to_s
202 @voter = FullVoter.find(:all,
203 :conditions => [ "password = ?", password ] )[0]
204 @password = @voter.password