Merge head
[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     
64     @election.voters.each do |voter|
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   def change_notices
74     election = Election.find(params[:id])
75     if election.notices == 0
76       election.notices = 1 
77     else
78       election.notices = 0
79     end
80   end
81
82   # methods fod display, adding, deleting, and manipulating candidate
83   # information for elections
84   ####################################################################
85   def edit_candidates
86     @sidebar_content = render_to_string :partial => 'progress',
87                                         :locals => { :page => 'candidates' }
88     @election = Election.find( params[:id] )
89   end
90
91   def add_candidate
92     @election = Election.find(params[:id])
93     @candidate = Candidate.new(params[:candidate])
94     @election.candidates << @candidate
95
96     if @candidate.save
97       @candidate = Candidate.new
98       redirect_to :action => 'edit_candidates', :id => @election.id
99     else
100       render :action => 'edit_candidates', :id => @election.id
101     end
102   end
103   
104   def delete_candidate
105     candidate = Candidate.find( params[:id] )
106     candidate.destroy
107   end
108
109   def lessinfo_candidate
110     @show_details = false
111     @candidate = Candidate.find( params[:id] )
112     render :partial => 'candidate_line'
113   end
114
115   def moreinfo_candidate
116     @show_details = true
117     @candidate = Candidate.find( params[:id] )
118     render :partial => 'candidate_line'
119   end
120
121   def edit_candidate
122     @candidate = Candidate.find( params[:id] )
123     @election = @candidate.election
124   end
125
126   def update_candidate
127     @candidate = Candidate.find(params[:id])
128     @election = @candidate.election
129
130     if @candidate.update_attributes(params[:candidate])
131       redirect_to :action => 'edit_candidates', :id => @candidate.election.id
132     else
133       render :action => 'edit_candidate'
134     end
135   end
136
137   def candidate_picture
138     candidate = Candidate.find( params[:id] )
139     send_data( candidate.picture.data,
140                :filename => candidate.picture.filename,
141                :type => candidate.picture.filetype,
142                :disposition => 'inline' )
143   end
144
145   ## methods for displaying, adding, deleting, and manipulating voters
146   ## for a particular election
147   ####################################################################
148   def new_voters
149     edit_voters
150   end
151   
152   def edit_voters
153     @election = Election.find( params[:id] )
154     if params.has_key?( :raw_voter_list )
155       process_incoming_voters( params[:raw_voter_list] )
156     end
157
158     @raw_voter_list = RawVoterList.new
159   end
160   
161   def delete_voter
162     voter = Voter.find( params[:id] )
163     voter.destroy
164   end
165   
166   ## methods for computing and printing results
167   ####################################################################
168   def results
169     @election = Election.find( params[:id] )
170     votes = []
171
172     @election.voters.each do |voter|
173       if voter.vote and voter.vote.confirmed?
174         votes << voter.vote.rankings.sort.collect {|vote| vote.candidate_id}
175       end
176     end
177     
178     @voteobj = CloneproofSSDVote.new(votes)
179     @resultobj = @voteobj.result
180     @winners = @resultobj.winners
181     
182     @candidates_by_id = {}
183     @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
184   end
185   
186   def detailed_results
187    
188     self.results
189
190     @voter_list = []
191     @vote_list = []
192     @election.voters. each do |voter|
193       if voter.vote and voter.vote.confirmed?
194         @voter_list << voter.email
195         @vote_list << voter.vote
196       end
197     end
198
199     @vote_list.sort!
200     @vote_list.sort! { |a,b| a.token <=> b.token }
201   end
202
203   ## private methods
204   ####################################################################
205   private
206
207     def process_incoming_voters(raw_voter_list)
208       incoming_voters = RawVoterList.new( raw_voter_list )
209
210       unless incoming_voters.entries.empty?
211         incoming_voters.each do |new_voter|
212
213           if incoming_voters.email == 0
214             new_voter.contacted = 1
215                 elsif incoming_voters.email == 1
216                   email_voter( new_voter )
217             new_voter.contacted = 1
218                 else
219                   new_voter.contacted = 0
220           end
221         
222           # the new voter should be in good shape. save add to the election
223           @election.voters << new_voter
224                 new_voter.save
225         end
226       end
227  
228       # reset the next time to have a the same default value for emailing
229       @raw_voter_list = RawVoterList.new
230       @raw_voter_list.email = incoming_voters.email
231     end
232
233     def email_voter(email=nil)
234       if email
235         
236       end
237     end
238
239 end

Benjamin Mako Hill || Want to submit a patch?