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
71 # methods fod display, adding, deleting, and manipulating candidate
72 # information for elections
73 ####################################################################
75 @election = Election.find( params[:id] )
79 @election = Election.find(params[:id])
80 @candidate = Candidate.new(params[:candidate])
81 @election.candidates << @candidate
84 @candidate = Candidate.new
85 redirect_to :action => 'edit_candidates', :id => @election.id
87 render :action => 'edit_candidates', :id => @election.id
92 candidate = Candidate.find( params[:id] )
96 def lessinfo_candidate
98 @candidate = Candidate.find( params[:id] )
99 render :partial => 'candidate_line'
102 def moreinfo_candidate
104 @candidate = Candidate.find( params[:id] )
105 render :partial => 'candidate_line'
109 @candidate = Candidate.find( params[:id] )
110 @election = @candidate.election
114 @candidate = Candidate.find(params[:id])
115 @election = @candidate.election
117 if @candidate.update_attributes(params[:candidate])
118 redirect_to :action => 'edit_candidates', :id => @candidate.election.id
120 render :action => 'edit_candidate'
124 def candidate_picture
125 candidate = Candidate.find( params[:id] )
126 send_data( candidate.picture.data,
127 :filename => candidate.picture.filename,
128 :type => candidate.picture.filetype,
129 :disposition => 'inline' )
132 ## methods for displaying, adding, deleting, and manipulating voters
133 ## for a particular election
134 ####################################################################
140 @election = Election.find( params[:id] )
141 if params.has_key?( :raw_voter_list )
142 process_incoming_voters( params[:raw_voter_list] )
145 @raw_voter_list = RawVoterList.new
149 voter = Voter.find( params[:id] )
153 ## methods for computing and printing results
154 ####################################################################
156 @election = Election.find( params[:id] )
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}
165 @voteobj = CloneproofSSDVote.new(votes)
166 @resultobj = @voteobj.result
167 @winners = @resultobj.winners
169 @candidates_by_id = {}
170 @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
179 @election.voters. each do |voter|
180 if voter.vote and voter.vote.confirmed?
181 @voter_list << voter.email
182 @vote_list << voter.vote
187 @vote_list.sort! { |a,b| a.token <=> b.token }
191 ####################################################################
194 def process_incoming_voters(raw_voter_list)
195 incoming_voters = RawVoterList.new( raw_voter_list )
197 unless incoming_voters.entries.empty?
198 incoming_voters.each do |new_voter|
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
206 new_voter.contacted = 0
209 # the new voter should be in good shape. save add to the election
210 @election.voters << new_voter
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
220 def email_voter(email=nil)