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

Benjamin Mako Hill || Want to submit a patch?