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

Benjamin Mako Hill || Want to submit a patch?