]> projects.mako.cc - selectricity-live/blob - app/controllers/election_controller.rb
The account summary page has been updated, though the number of items displayed is...
[selectricity-live] / app / controllers / election_controller.rb
1 # Selectricity: Voting Machinery for the Masses
2 # Copyright (C) 2007, 2008 Benjamin Mako Hill <mako@atdot.cc>
3 # Copyright (C) 2007 Massachusetts Institute of Technology
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Affero General Public License for more details.
14 #
15 # You should have received a copy of the GNU Affero General Public
16 # License along with this program.  If not, see
17 # <http://www.gnu.org/licenses/>.
18
19 class ElectionController < ApplicationController
20   require_dependency "raw_voter_list"
21   require_dependency "voter"
22   require_dependency "vote"
23   require_dependency "candidate"
24   layout 'main'
25
26   ## methods for displaying, creating,
27   ## and manipulating election overview data
28   ####################################################################
29
30   def new
31     redirect_to :action => 'general_information'
32   end
33   
34   def general_information
35     @sidebar_content = render_to_string :partial => 'progress',
36                                         :locals => { :page => 'overview' }
37     @election = Election.new
38     render :action => 'general_information'
39   end
40   
41   def create_election
42     @election = Election.new(params[:election])
43     
44     # default options
45     @election.user = session[:user]
46     @election.anonymous = 1
47     @election.startdate = Time.now
48     
49     holder = create_theme_hash
50     unless holder.values.all? {|v| v.has_value?("")}
51       token_generator = UniqueTokenGenerator.new( 16 )
52       @election.embed_custom_string = token_generator.token
53       add_theme(@election.embed_custom_string)
54     end
55     
56     if @election.save
57       flash[:notice] = 'Election was successfully created.'
58       redirect_to :action => 'edit_candidates', :id => @election.id
59     else
60       render :action => 'general_information'
61     end
62   end
63   
64   def create_theme_hash
65     target = Hash.new
66     params.each do |k,v|
67       target[k] = v if k=="top_bar" or k=="default_image" or k=="bg1" \
68                     or k=="bg2" or k=="bottom_bar"
69     end
70     return target
71   end
72   
73   # TODO add filter to verify that the person working on or looking at
74   # something is the owner
75   def edit_general_information
76     @election = Election.find(params[:id])
77   end
78   
79   def update_general_information
80     @election = Election.find(params[:id])
81     
82     holder = create_theme_hash
83     unless holder.values.all? {|v| v.has_value?("")}
84       unless @election.embed_custom_string
85         token_generator = UniqueTokenGenerator.new( 16 )
86         @election.embed_custom_string = token_generator.token
87       end
88       
89       add_theme(@election.embed_custom_string)
90     end
91     
92     if @election.update_attributes(params[:election])
93       flash[:notice] = 'Election was successfully updated.'
94       redirect_to :action => 'show', :id => @election
95     else
96       render :action => 'edit'
97     end
98   end
99   
100   #Takes care of uploading custom images 
101   #unnecessarily long, how can I compress?
102   def add_theme(prefix)
103     holder = create_theme_hash
104     unless params[:top_bar][:uploaded_data].to_s.empty?
105       previous = SkinPicture.find(:first,
106       :conditions => ["filename = ?", @election.embed_custom_string + "top_bar.png"])
107       if previous
108         previous.destroy
109       end
110       top_bar = SkinPicture.new(params[:top_bar])
111       top_bar.filename = prefix + "top_bar." + params[:top_bar][:uploaded_data].content_type[6..-2]
112       top_bar.save
113     end
114     unless params[:default_image][:uploaded_data].to_s.empty?
115       previous = SkinPicture.find(:first,
116       :conditions => ["filename = ?", @election.embed_custom_string + "default_image.png"])
117       if previous
118         previous.destroy
119       end
120       default_image = SkinPicture.new(params[:default_image])
121       default_image.filename = prefix + "default_image." + params[:default_image][:uploaded_data].content_type[6..-2]
122       default_image.save
123     end
124     unless params[:bg1][:uploaded_data].to_s.empty?
125       previous = SkinPicture.find(:first,
126       :conditions => ["filename = ?", @election.embed_custom_string + "bg1.png"])
127       if previous
128         previous.destroy
129       end
130       bg1 = SkinPicture.new(params[:bg1])  
131       bg1.filename = prefix + "bg1." + params[:bg1][:uploaded_data].content_type[6..-2]
132       bg1.save
133     end
134     unless params[:bg2][:uploaded_data].to_s.empty?
135       previous = SkinPicture.find(:first,
136       :conditions => ["filename = ?", @election.embed_custom_string + "bg2.png"])
137       if previous
138         previous.destroy
139       end
140       bg2 = SkinPicture.new(params[:bg2]) 
141       bg2.filename = prefix + "bg2." + params[:bg2][:uploaded_data].content_type[6..-2]
142       bg2.save
143     end
144     unless params[:bottom_bar][:uploaded_data].to_s.empty?
145       previous = SkinPicture.find(:first,
146       :conditions => ["filename = ?", @election.embed_custom_string + "bottom_bar.png"])
147       if previous
148         previous.destroy
149       end
150       bottom_bar = SkinPicture.new(params[:bottom_bar])
151       bottom_bar.filename = prefix + "bottom_bar." + params[:bottom_bar][:uploaded_data].content_type[6..-2]
152       bottom_bar.save
153     end
154         
155   end
156   
157   def show
158     @sidebar_content = render_to_string :partial => 'progress',
159                                         :locals => { :page => 'review' }
160
161     @election = Election.find(params[:id])
162     if @election.type == QuickVote
163       redirect_to(:controller => 'quickvote', :action => 'index', :ident => @election.id)
164     end
165       
166   end
167
168   def start_election
169     @election = Election.find(params[:id])
170     
171     @election.voters.each do |voter|
172       voter.vote = Vote.new
173       email_voter voter unless voter.email.nil?
174     end
175
176     @election.activate!
177     redirect_to :action => 'show', :id => @election.id
178   end
179
180   # methods fod display, adding, deleting, and manipulating candidate
181   # information for elections
182   ####################################################################
183   def edit_candidates
184     @sidebar_content = render_to_string :partial => 'progress',
185                                         :locals => { :page => 'candidates' }
186     @election = Election.find( params[:id] )
187   end
188
189   def add_candidate
190     @election = Election.find(params[:id])
191     @candidate = Candidate.new(params[:candidate])
192     @election.candidates << @candidate
193
194     if @candidate.save
195       # check to see if they've uploaded a picture
196       if params[:picture][:uploaded_data]
197         picture = Picture.new(params[:picture])
198         @candidate.picture = picture if picture.save
199       end
200
201       @candidate = Candidate.new
202       redirect_to :action => 'edit_candidates', :id => @election.id
203     else
204       render :action => 'edit_candidates', :id => @election.id
205     end
206   end
207   
208   def delete_candidate
209     candidate = Candidate.find( params[:id] )
210     candidate.destroy
211   end
212
213   def candidate_picture
214     candidate = Candidate.find( params[:id] )
215     send_data( candidate.picture.data,
216                :filename => candidate.picture.filename,
217                :type => candidate.picture.filetype,
218                :disposition => 'inline' )
219   end
220
221   ## methods for displaying, adding, deleting, and manipulating voters
222   ## for a particular election
223   ####################################################################
224   def new_voters
225     redirect_to :action => 'edit_voters', :id => params[:id]
226   end
227   
228   def edit_voters
229     @sidebar_content = render_to_string :partial => 'progress',
230                                         :locals => { :page => 'voters' }
231
232     @election = Election.find( params[:id] )
233     if params.has_key?( :raw_voter_list )
234       process_incoming_voters( params[:raw_voter_list] )
235     end
236     @raw_voter_list = RawVoterList.new
237   end
238   
239   def delete_voter
240     voter = Voter.find( params[:id] )
241     voter.destroy
242   end
243
244   def toggle_authenticated
245     @election = Election.find(params[:id])
246     if params[:authenticated] == "1"
247       @election.authenticated = true
248     else
249       @election.authenticated = false
250     end
251     @election.save
252   end
253   
254   ## methods for computing and printing results
255   ####################################################################
256   def results
257     @election = Election.find( params[:id] )
258     votes = []
259     
260     @election.voters.each do |voter|
261       if voter.vote and voter.vote.confirmed?
262         votes << voter.vote.rankings.sort.collect {|vote| vote.candidate_id}
263       end
264     end
265     
266     @voteobj = CloneproofSSDVote.new(votes)
267     @resultobj = @voteobj.result
268     @winners = @resultobj.winners
269     
270     @candidates_by_id = {}
271     @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
272     
273   end
274   
275   def detailed_results
276    
277     self.results
278
279     @voter_list = []
280     @vote_list = []
281     
282     @election.voters.each do |voter|
283       if voter.vote and voter.vote.confirmed?
284         @voter_list << voter.email
285               @vote_list << voter.vote
286       end
287     end
288
289     @vote_list.sort!
290     @vote_list.sort! { |a,b| a.token <=> b.token }
291   end
292
293   ## private methods
294   ####################################################################
295   private
296
297     def process_incoming_voters(raw_voter_list)
298       incoming_voters = RawVoterList.new( raw_voter_list )
299
300       unless incoming_voters.entries.empty?
301         incoming_voters.each do |new_voter|
302           new_voter.email.strip! # There's a trailing \r on all but the last in
303                                  # the list!
304           if new_voter.valid?
305             # the new voter should be in good shape. save add to the election
306             @election.voters << new_voter
307                   new_voter.save
308           end
309           # TODO: Can we do some kind of AJAX error message for the voter being invalid?
310         end
311         @election.save
312       end
313  
314       # reset the next time to have a the same default value for emailing
315       @raw_voter_list = RawVoterList.new
316       @raw_voter_list.email = incoming_voters.email
317     end
318
319     def email_voter(voter=nil)
320       if voter
321         VoterNotify.deliver_votestart(voter)
322         voter.contacted=1
323         voter.save
324       end
325     end
326
327 end

Benjamin Mako Hill || Want to submit a patch?