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 @election = Election.new
20 render :action => 'general_information'
24 @election = Election.new(params[:election])
27 @election.user = session[:user]
28 @election.anonymous = 1
29 @election.startdate = Time.now
32 flash[:notice] = 'Election was successfully created.'
33 redirect_to :action => 'edit_candidates', :id => @election.id
35 render :action => 'general_information'
39 # add filter to verify that the person working on or looking at
40 # something is the owner
42 @election = Election.find(params[:id])
46 @election = Election.find(params[:id])
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
55 render :action => 'edit'
60 @election = Election.find(params[:id])
62 @election.voters.each do |voter|
63 email = VoterNotify.deliver_votestart(voter)
64 #render(:text => "<pre>" + email.encoded + "</pre>")
68 redirect_to :action => 'show', :id => @election.id
72 election = Election.find(params[:id])
73 if election.notices == 0
80 # methods fod display, adding, deleting, and manipulating candidate
81 # information for elections
82 ####################################################################
84 @election = Election.find( params[:id] )
88 @election = Election.find(params[:id])
89 @candidate = Candidate.new(params[:candidate])
90 @election.candidates << @candidate
93 @candidate = Candidate.new
94 redirect_to :action => 'edit_candidates', :id => @election.id
96 render :action => 'edit_candidates', :id => @election.id
101 candidate = Candidate.find( params[:id] )
105 def lessinfo_candidate
106 @show_details = false
107 @candidate = Candidate.find( params[:id] )
108 render :partial => 'candidate_line'
111 def moreinfo_candidate
113 @candidate = Candidate.find( params[:id] )
114 render :partial => 'candidate_line'
118 @candidate = Candidate.find( params[:id] )
119 @election = @candidate.election
123 @candidate = Candidate.find(params[:id])
124 @election = @candidate.election
126 if @candidate.update_attributes(params[:candidate])
127 redirect_to :action => 'edit_candidates', :id => @candidate.election.id
129 render :action => 'edit_candidate'
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' )
141 ## methods for displaying, adding, deleting, and manipulating voters
142 ## for a particular election
143 ####################################################################
149 @election = Election.find( params[:id] )
150 if params.has_key?( :raw_voter_list )
151 process_incoming_voters( params[:raw_voter_list] )
154 @raw_voter_list = RawVoterList.new
158 voter = Voter.find( params[:id] )
162 ## methods for computing and printing results
163 ####################################################################
165 @election = Election.find( params[:id] )
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}
174 @voteobj = CloneproofSSDVote.new(votes)
175 @resultobj = @voteobj.result
176 @winners = @resultobj.winners
178 @candidates_by_id = {}
179 @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
188 @election.voters. each do |voter|
189 if voter.vote and voter.vote.confirmed?
190 @voter_list << voter.email
191 @vote_list << voter.vote
196 @vote_list.sort! { |a,b| a.token <=> b.token }
200 ####################################################################
203 def process_incoming_voters(raw_voter_list)
204 incoming_voters = RawVoterList.new( raw_voter_list )
206 unless incoming_voters.entries.empty?
207 incoming_voters.each do |new_voter|
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
215 new_voter.contacted = 0
218 # the new voter should be in good shape. save add to the election
219 @election.voters << new_voter
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
229 def email_voter(email=nil)