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

Benjamin Mako Hill || Want to submit a patch?