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. Please see the COPYING file for
8 class VoterController < ApplicationController
11 require_dependency "voter"
12 require_dependency "vote"
13 require_dependency "election"
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]
23 @voter = OpenVoter.new unless @voter
25 @voter.election = @election
26 @voter.session_id = session.session_id
27 @password = "open." + @election.id.to_s
29 elsif params[:urlpassword]
30 password = params[:urlpassword]
32 if @voter = FullVoter.find(:all,
33 :conditions => [ "password = ?", password ] )[0]
34 @election = @voter.election
35 @password = @voter.password
39 if @voter and @election
40 # initialize things if the vote is blank
42 @voter.vote = Vote.new
46 @voter.vote.set_defaults! if @voter.vote.rankings.empty?
48 # if the election is now finished
49 if @election.enddate < Time.now
50 redirect_to :action => :results, :id => @password
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"])
67 render :template => 'embed/full_vote', :layout => 'embed'
69 render :action => 'full_vote'
76 if params[:vote] and params[:vote][:password]
77 redirect_to votepassword_url( :action => 'index', :urlpassword => params[:vote][:password])
79 redirect_to :action => 'index'
85 @election = @voter.election
86 @results = @election.results
88 @election.candidates.each {|c| @candidates[c.id] = c}
89 @names = @election.names_by_id
90 render :template => 'common/pref_tables', :layout => 'basic'
92 redirect_to :action => 'index'
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'
103 redirect_to :action => 'index'
109 @voter.vote.time = Time.now
113 redirect_to :action => 'index'
121 if @voter.election.embeddable? and params[:embed] == "true" \
122 and @voter.election.early_results?
123 redirect_to :action => :results, :id => @password, :embed => 'true'
125 render :action => 'thanks'
128 redirect_to :action => 'index'
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)
139 render :action => 'reminder_sent'
145 (@voter.election.early_results? \
146 or @voter.election.enddate < Time.now)
148 @election = @voter.election
149 # compute and display results
151 @results = @election.results
153 @election.candidates.each {|c| @candidates[c.id] = c}
154 @names = @election.names_by_id
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"])
170 if @election.embeddable? and params[:embed] == "true"
171 render :template => 'embed/results', :layout => 'embed'
173 render :action => 'results'
176 redirect_to :action => 'index'
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
192 @voter = FullVoter.find(:all,
193 :conditions => [ "password = ?", password ] )[0]
194 @password = @voter.password