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 render :template => 'embed/full_vote', :layout => 'embed'
66 render :action => 'full_vote'
73 if params[:vote] and params[:vote][:password]
74 redirect_to votepassword_url( :action => 'index', :urlpassword => params[:vote][:password])
76 redirect_to :action => 'index'
82 @election = @voter.election
83 @results = @election.results
85 @election.candidates.each {|c| @candidates[c.id] = c}
86 @names = @election.names_by_id
87 render :template => 'common/pref_tables', :layout => 'basic'
89 redirect_to :action => 'index'
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'
100 redirect_to :action => 'index'
106 @voter.vote.time = Time.now
110 redirect_to :action => 'index'
118 if @voter.election.embeddable? and params[:embed] == "true" \
119 and @voter.election.early_results?
120 redirect_to :action => :results, :id => @password, :embed => 'true'
122 render :action => 'thanks'
125 redirect_to :action => 'index'
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)
136 render :action => 'reminder_sent'
142 (@voter.election.early_results? \
143 or @voter.election.enddate < Time.now)
145 @election = @voter.election
146 # compute and display results
148 @results = @election.results
150 @election.candidates.each {|c| @candidates[c.id] = c}
151 @names = @election.names_by_id
153 @sidebar_content = render_to_string(:partial => 'results_sidebar')
154 if @election.embeddable? and params[:embed] == "true"
155 render :template => 'embed/results', :layout => 'embed'
157 render :action => 'results'
160 redirect_to :action => 'index'
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
176 @voter = FullVoter.find(:all,
177 :conditions => [ "password = ?", password ] )[0]
178 @password = @voter.password