1 class ElectionController < ApplicationController
2 require_dependency "raw_voter_list"
3 require_dependency "voter"
4 require_dependency "vote"
5 require_dependency "candidate"
8 #before_filter :login_required
10 ## methods for displaying, creating,
11 ## and manipulating election overview data
12 ####################################################################
15 redirect_to :action => 'general_information'
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'
26 @election = Election.new(params[:election])
29 @election.user = session[:user]
30 @election.anonymous = 1
31 @election.startdate = Time.now
34 flash[:notice] = 'Election was successfully created.'
35 redirect_to :action => 'edit_candidates', :id => @election.id
37 render :action => 'general_information'
41 # add filter to verify that the person working on or looking at
42 # something is the owner
44 @election = Election.find(params[:id])
48 @election = Election.find(params[:id])
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
57 render :action => 'edit'
62 @election = Election.find(params[:id])
64 @election.voters.each do |voter|
65 email = VoterNotify.deliver_votestart(voter)
66 #render(:text => "<pre>" + email.encoded + "</pre>")
70 redirect_to :action => 'show', :id => @election.id
74 election = Election.find(params[:id])
75 if election.notices == 0
82 # methods fod display, adding, deleting, and manipulating candidate
83 # information for elections
84 ####################################################################
86 @sidebar_content = render_to_string :partial => 'progress',
87 :locals => { :page => 'candidates' }
88 @election = Election.find( params[:id] )
92 @election = Election.find(params[:id])
93 @candidate = Candidate.new(params[:candidate])
94 @election.candidates << @candidate
97 @candidate = Candidate.new
98 redirect_to :action => 'edit_candidates', :id => @election.id
100 render :action => 'edit_candidates', :id => @election.id
105 candidate = Candidate.find( params[:id] )
109 def lessinfo_candidate
110 @show_details = false
111 @candidate = Candidate.find( params[:id] )
112 render :partial => 'candidate_line'
115 def moreinfo_candidate
117 @candidate = Candidate.find( params[:id] )
118 render :partial => 'candidate_line'
122 @candidate = Candidate.find( params[:id] )
123 @election = @candidate.election
127 @candidate = Candidate.find(params[:id])
128 @election = @candidate.election
130 if @candidate.update_attributes(params[:candidate])
131 redirect_to :action => 'edit_candidates', :id => @candidate.election.id
133 render :action => 'edit_candidate'
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' )
145 ## methods for displaying, adding, deleting, and manipulating voters
146 ## for a particular election
147 ####################################################################
153 @election = Election.find( params[:id] )
154 if params.has_key?( :raw_voter_list )
155 process_incoming_voters( params[:raw_voter_list] )
158 @raw_voter_list = RawVoterList.new
162 voter = Voter.find( params[:id] )
166 ## methods for computing and printing results
167 ####################################################################
169 @election = Election.find( params[:id] )
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}
178 @voteobj = CloneproofSSDVote.new(votes)
179 @resultobj = @voteobj.result
180 @winners = @resultobj.winners
182 @candidates_by_id = {}
183 @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
192 @election.voters. each do |voter|
193 if voter.vote and voter.vote.confirmed?
194 @voter_list << voter.email
195 @vote_list << voter.vote
200 @vote_list.sort! { |a,b| a.token <=> b.token }
204 ####################################################################
207 def process_incoming_voters(raw_voter_list)
208 incoming_voters = RawVoterList.new( raw_voter_list )
210 unless incoming_voters.entries.empty?
211 incoming_voters.each do |new_voter|
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
219 new_voter.contacted = 0
222 # the new voter should be in good shape. save add to the election
223 @election.voters << new_voter
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
233 def email_voter(email=nil)