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

Benjamin Mako Hill || Want to submit a patch?