]> projects.mako.cc - selectricity/blob - app/controllers/voter_controller.rb
ac57f1b6d7d9d2fa6895f4f3ce35e001bfbf93e7
[selectricity] / app / controllers / voter_controller.rb
1 class VoterController < ApplicationController
2   layout 'main'
3   require_dependency "voter"
4   require_dependency "vote"
5   require_dependency "election"
6
7   def index
8     if params[:urlpassword]
9       password = params[:urlpassword]
10
11       if @voter = FullVoter.find(:all,
12         :conditions => [ "password = ?", password ] )[0]
13
14         @voter.vote = Vote.new if @voter.vote.nil?
15         @voter.vote.set_defaults! if @voter.vote.rankings.empty?
16
17         @election = @voter.election
18         
19         # if the election is now finished 
20         if @election.enddate < Time.now
21           # compute and display results
22     
23           @results = @election.results
24           @candidates = {}
25           @election.candidates.each {|c| @candidates[c.id] = c}
26           @names = @election.names_by_id
27           
28           @sidebar_content = render_to_string(:partial => 'results_sidebar')
29           render :action => 'results'
30         else
31           @sidebar_content = render_to_string(:partial => 'vote_sidebar')
32           render :action => 'full_vote'
33         end
34       elsif params[:urlpassword] 
35         redirect_to :action => 'index'
36       end
37     end
38   end
39
40   def login
41     if params[:vote] and params[:vote][:password]
42       redirect_to votepassword_url( :action => 'index', :urlpassword => params[:vote][:password])
43     else
44       redirect_to :action => 'index'
45     end
46   end
47   
48   def pref_tables
49     if authenticate
50       @election = @voter.election
51       @results = @election.results
52       @candidates = {}
53       @election.candidates.each {|c| @candidates[c.id] = c}
54       @names = @election.names_by_id
55       render :template => 'common/pref_tables', :layout => 'basic'
56     else
57       redirect_to :action => 'index'
58     end
59   end
60
61   def details
62     if authenticate
63       @election = @voter.election
64       @votes = @election.votes.select {|v| v.confirmed? }.randomize
65       @voters = @votes.collect {|v| v.voter}.randomize
66       render :action => 'details'
67     else
68       redirect_to :action => 'index'
69     end
70   end
71
72   def review
73     if authenticate
74       @voter.vote.time = Time.now
75       @voter.vote.save
76       @voter.reload
77     else
78       redirect_to :action => 'index'
79     end
80   end
81
82   def confirm
83     if authenticate
84       @voter.vote.confirm!
85       render :action => 'thanks'
86     else
87       redirect_to :action => 'index'
88     end
89   end
90   
91   def reminder
92     if params[:email]
93       voter_array= FullVoter.find(:all, :conditions => ["email = ?", params[:email]])
94       voter_array.delete_if {|voter| voter.election.active == 0}
95       unless voter_array.empty?
96         VoterNotify.deliver_reminder(voter_array)
97       end
98       render :action => 'reminder_sent'
99     end
100   end
101   
102   
103   private
104   def authenticate
105     password = params[:id]
106     @voter = FullVoter.find(:all, :conditions => [ "password = ?", password ] )[0]
107   end
108 end
109

Benjamin Mako Hill || Want to submit a patch?