merged in changes from devel
[selectricity] / app / controllers / election_controller.rb
1 class ElectionController < ApplicationController
2   require_dependency "raw_voter_list"
3   require_dependency "voter"
4   require_dependency "vote"
5   require_dependency "candidate"
6   layout 'main'
7
8   ## methods for displaying, creating,
9   ## and manipulating election overview data
10   ####################################################################
11
12   def new
13     redirect_to :action => 'general_information'
14   end
15   
16   def general_information
17     @sidebar_content = render_to_string :partial => 'progress',
18                                         :locals => { :page => 'overview' }
19     @election = Election.new
20     render :action => 'general_information'
21   end
22   
23   def create_election
24     @election = Election.new(params[:election])
25     
26     # default options
27     @election.user = session[:user]
28     @election.anonymous = 1
29     @election.startdate = Time.now
30
31     if @election.save
32       flash[:notice] = 'Election was successfully created.'
33       redirect_to :action => 'edit_candidates', :id => @election.id
34     else
35       render :action => 'general_information'
36     end
37   end
38   
39   # add filter to verify that the person working on or looking at
40   # something is the owner
41   def edit
42     @election = Election.find(params[:id])
43   end
44
45   def show
46     @election = Election.find(params[:id])
47   end
48
49   def update
50     @election = Election.find(params[:id])
51     if @election.update_attributes(params[:election])
52       flash[:notice] = 'Election was successfully updated.'
53       redirect_to :action => 'show', :id => @election
54     else
55       render :action => 'edit'
56     end
57   end
58
59   def start_election
60     @election = Election.find(params[:id])
61     @election.voters.each do |voter|
62       voter.vote = Vote.new
63       email_voter voter
64     end
65
66     @election.activate!
67     redirect_to :action => 'show', :id => @election.id
68   end
69
70   # methods fod display, adding, deleting, and manipulating candidate
71   # information for elections
72   ####################################################################
73   def edit_candidates
74     @sidebar_content = render_to_string :partial => 'progress',
75                                         :locals => { :page => 'candidates' }
76     @election = Election.find( params[:id] )
77   end
78
79   def add_candidate
80     @election = Election.find(params[:id])
81     @candidate = Candidate.new(params[:candidate])
82     @election.candidates << @candidate
83
84     if @candidate.save
85       @candidate = Candidate.new
86       redirect_to :action => 'edit_candidates', :id => @election.id
87     else
88       render :action => 'edit_candidates', :id => @election.id
89     end
90   end
91   
92   def delete_candidate
93     candidate = Candidate.find( params[:id] )
94     candidate.destroy
95   end
96
97   def lessinfo_candidate
98     @show_details = false
99     @current_candidate = Candidate.find( params[:id] )
100     render :partial => 'candidate_line'
101   end
102
103   def moreinfo_candidate
104     @show_details = true
105     @current_candidate = Candidate.find( params[:id] )
106     render :partial => 'candidate_line'
107   end
108
109   def edit_candidate
110     @candidate = Candidate.find( params[:id] )
111     @election = @candidate.election
112   end
113
114   def update_candidate
115     @candidate = Candidate.find(params[:id])
116     @election = @candidate.election
117
118     if @candidate.update_attributes(params[:candidate])
119       redirect_to :action => 'edit_candidates', :id => @candidate.election.id
120     else
121       render :action => 'edit_candidate'
122     end
123   end
124
125   def candidate_picture
126     candidate = Candidate.find( params[:id] )
127     send_data( candidate.picture.data,
128                :filename => candidate.picture.filename,
129                :type => candidate.picture.filetype,
130                :disposition => 'inline' )
131   end
132
133   ## methods for displaying, adding, deleting, and manipulating voters
134   ## for a particular election
135   ####################################################################
136   def new_voters
137     edit_voters
138   end
139   
140   def edit_voters
141     @election = Election.find( params[:id] )
142     if params.has_key?( :raw_voter_list )
143       process_incoming_voters( params[:raw_voter_list] )
144     end
145     @raw_voter_list = RawVoterList.new
146   end
147   
148   def delete_voter
149     voter = Voter.find( params[:id] )
150     voter.destroy
151   end
152   
153   def remind_voter
154     voter_array= FullVoter.find(:all, :conditions => ["email = ?", params[:email]])
155     voter_array.delete_if {|voter| voter.election.active == 0}
156     unless voter_array.empty?
157       VoterNotify.deliver_reminder(voter_array)
158     end
159   end
160   
161   ## methods for computing and printing results
162   ####################################################################
163   def results
164     @election = Election.find( params[:id] )
165     votes = []
166
167     @election.voters.each do |voter|
168       if voter.vote and voter.vote.confirmed?
169         votes << voter.vote.rankings.sort.collect {|vote| vote.candidate_id}
170       end
171     end
172     
173     @voteobj = CloneproofSSDVote.new(votes)
174     @resultobj = @voteobj.result
175     @winners = @resultobj.winners
176     
177     @candidates_by_id = {}
178     @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
179   end
180   
181   def detailed_results
182    
183     self.results
184
185     @voter_list = []
186     @vote_list = []
187     @election.voters. each do |voter|
188       if voter.vote and voter.vote.confirmed?
189         @voter_list << voter.email
190               @vote_list << voter.vote
191       end
192     end
193
194     @vote_list.sort!
195     @vote_list.sort! { |a,b| a.token <=> b.token }
196   end
197
198   ## private methods
199   ####################################################################
200   private
201
202     def process_incoming_voters(raw_voter_list)
203       incoming_voters = RawVoterList.new( raw_voter_list )
204
205       unless incoming_voters.entries.empty?
206         incoming_voters.each do |new_voter|
207           new_voter.email.strip! # There's a trailing \r on all but the last in
208                                  # the list!
209           if new_voter.valid?
210             # the new voter should be in good shape. save add to the election
211             @election.voters << new_voter
212                   new_voter.save
213           end
214           # TODO: Can we do some kind of AJAX error message for the voter being invalid?
215         end
216         @election.save
217       end
218  
219       # reset the next time to have a the same default value for emailing
220       @raw_voter_list = RawVoterList.new
221       @raw_voter_list.email = incoming_voters.email
222     end
223
224     def email_voter(voter=nil)
225       if voter
226         VoterNotify.deliver_votestart(voter)
227         voter.contacted=1
228         voter.save
229       end
230     end
231
232 end

Benjamin Mako Hill || Want to submit a patch?