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

Benjamin Mako Hill || Want to submit a patch?