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

Benjamin Mako Hill || Want to submit a patch?