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
22 require_dependency "voter"
23 require_dependency "vote"
24 require_dependency "election"
27 if params[:election_id]
28 @election = Election.find(params[:election_id])
29 unless @election.authenticated?
30 @voter = Voter.find(:all,
31 :conditions => ["session_id = ? and election_id = ?",
32 session.session_id, @election.id])[0]
34 @voter = Voter.new unless @voter
36 @voter.election = @election
37 @voter.session_id = session.session_id
38 @password = "open." + @election.id.to_s
40 elsif params[:urlpassword]
41 password = params[:urlpassword]
43 if @voter = FullVoter.find(:all,
44 :conditions => [ "password = ?", password ] )[0]
45 @election = @voter.election
46 @password = @voter.password
50 if @voter and @election
51 # initialize things if the vote is blank
53 @voter.vote = Vote.new
57 @voter.vote.set_defaults! if @voter.vote.rankings.empty?
59 # if the election is now finished
60 if @election.enddate < Time.now
61 redirect_to :action => :results, :id => @password
63 @sidebar_content = render_to_string(:partial => 'vote_sidebar')
64 if @election.embeddable? and params[:embed] == "true"
65 #look for custom theme, and assign to instance variabels for widget use
66 if @election.embed_custom_string
67 @top_bar = SkinPicture.find(:first,
68 :conditions => ["filename = ?", @election.embed_custom_string + "top_bar.png"])
69 @default_image = SkinPicture.find(:first,
70 :conditions => ["filename = ?", @election.embed_custom_string + "default_image.png"])
71 @bg1 = SkinPicture.find(:first,
72 :conditions => ["filename = ?", @election.embed_custom_string + "bg1.png"])
73 @bg2 = SkinPicture.find(:first,
74 :conditions => ["filename = ?", @election.embed_custom_string + "bg2.png"])
75 @bottom_bar = SkinPicture.find(:first,
76 :conditions => ["filename = ?", @election.embed_custom_string + "bottom_bar.png"])
78 render :template => 'embed/full_vote', :layout => 'embed'
80 render :action => 'full_vote'
87 if params[:vote] and params[:vote][:password]
88 redirect_to votepassword_url( :action => 'index', :urlpassword => params[:vote][:password])
90 redirect_to :action => 'index'
96 @election = @voter.election
97 @results = @election.results
99 @election.candidates.each {|c| @candidates[c.id] = c}
100 @names = @election.names_by_id
101 render :template => 'common/pref_tables', :layout => 'basic'
103 redirect_to :action => 'index'
109 @election = @voter.election
110 @votes = @election.votes.select {|v| v.confirmed? }.shuffle
111 @voters = @votes.collect {|v| v.voter}.shuffle
112 render :action => 'details'
114 redirect_to :action => 'index'
120 @voter.vote.time = Time.now
124 redirect_to :action => 'index'
132 if @voter.election.embeddable? and params[:embed] == "true" \
133 and @voter.election.early_results?
134 redirect_to :action => :results, :id => @password, :embed => 'true'
136 render :action => 'thanks'
139 redirect_to :action => 'index'
145 voter_array= FullVoter.find(:all, :conditions => ["email = ?", params[:email]])
146 voter_array.delete_if {|voter| voter.election.active == 0}
147 unless voter_array.empty?
148 VoterNotify.deliver_reminder(voter_array)
150 render :action => 'reminder_sent'
156 (@voter.election.early_results? \
157 or @voter.election.enddate < Time.now)
159 @election = @voter.election
160 # compute and display results
162 @results = @election.results
164 @election.candidates.each {|c| @candidates[c.id] = c}
165 @names = @election.names_by_id
167 @sidebar_content = render_to_string(:partial => 'results_sidebar')
168 #look for custom theme, and assign to instance variabels for widget use
169 if @election.embed_custom_string
170 @top_bar = SkinPicture.find(:first,
171 :conditions => ["filename = ?", @election.embed_custom_string + "top_bar.png"])
172 @default_image = SkinPicture.find(:first,
173 :conditions => ["filename = ?", @election.embed_custom_string + "default_image.png"])
174 @bg1 = SkinPicture.find(:first,
175 :conditions => ["filename = ?", @election.embed_custom_string + "bg1.png"])
176 @bg2 = SkinPicture.find(:first,
177 :conditions => ["filename = ?", @election.embed_custom_string + "bg2.png"])
178 @bottom_bar = SkinPicture.find(:first,
179 :conditions => ["filename = ?", @election.embed_custom_string + "bottom_bar.png"])
181 if @election.embeddable? and params[:embed] == "true"
182 render :template => 'embed/results', :layout => 'embed'
184 render :action => 'results'
187 redirect_to :action => 'index'
193 password = params[:id]
194 if password == "open"
195 election = Election.find(params[:format])
196 unless election.authenticated?
197 @voter = Voter.find(:all,
198 :conditions => ["session_id = ? and election_id = ?",
199 session.session_id, election.id])[0]
200 @password = "open." + election.id.to_s
203 @voter = FullVoter.find(:all,
204 :conditions => [ "password = ?", password ] )[0]
205 @password = @voter.password