a0549d40d510fabb9abd71e223a528a29879be48
[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 destroy
52     election = Election.find(params[:id]).destroy
53     redirect_to :action => 'list'
54   end
55
56   # methods fod display, adding, deleting, and manipulating candidate
57   # information for elections
58   ####################################################################
59   def edit_candidates
60     @election = Election.find( params[:id] )
61   end
62
63   def add_candidate
64     @election = Election.find(params[:id])
65     @candidate = Candidate.new(params[:candidate])
66     
67     if @candidate.save
68       @election.candidates << @candidate
69       @candidate = Candidate.new
70       redirect_to :action => 'edit_candidates', :id => @election.id
71     else
72       render :action => 'edit_candidates', :id => @election.id
73     end
74   end
75   
76   def delete_candidate
77     candidate = Candidate.find( params[:id] )
78     candidate.destroy
79   end
80
81   def lessinfo_candidate
82     @show_details = false
83     @candidate = Candidate.find( params[:id] )
84     render :partial => 'candidate_line'
85   end
86
87   def moreinfo_candidate
88     @show_details = true
89     @candidate = Candidate.find( params[:id] )
90     render :partial => 'candidate_line'
91   end
92
93   def edit_candidate
94     @candidate = Candidate.find( params[:id] )
95     @election = @candidate.election
96   end
97
98   def update_candidate
99     @candidate = Candidate.find(params[:id])
100
101     if @candidate.update_attributes(params[:candidate])
102       flash[:notice] = 'Candidate information was successfully updated.'
103       redirect_to :action => 'edit_candidates', :id => @candidate.election
104     else
105       render :action => 'edit_candidates'
106     end
107   end
108
109   def candidate_picture
110     candidate = Candidate.find( params[:id] )
111     send_data( candidate.picture_data,
112                :filename => candidate.picture_filename,
113                :type => candidate.picture_type,
114                :disposition => 'inline' )
115   end
116
117   ## methods for displaying, adding, deleting, and manipulating voters
118   ## for a particular election
119   ####################################################################
120   def new_voters
121     @election = Election.find( params[:id] )
122     if params.has_key?[:raw_voter_list]
123       process_incoming_voters( params[:raw_voter_list] )
124     end
125     @raw_voter_list = RawVoterList.new
126
127   end
128   
129   def edit_voters
130     @election = Election.find( params[:id] )
131     if params.has_key?( :raw_voter_list )
132       process_incoming_voters( params[:raw_voter_list] )
133     end
134
135     @raw_voter_list = RawVoterList.new
136   end
137   
138   def delete_voter
139     voter = Voter.find( params[:id] )
140     voter.destroy
141   end
142   
143   ## methods for computing and printing results
144   ####################################################################
145   def results
146     @election = Election.find( params[:id] )
147     votes = []
148
149     @election.voters.each do |voter|
150       if voter.vote and voter.vote.confirmed?
151         votes << voter.vote.rankings.sort.collect {|vote| vote.candidate_id}
152       end
153     end
154     
155     @voteobj = CloneproofSSDVote.new(votes)
156     @resultobj = @voteobj.result
157     @winners = @resultobj.winners
158     
159     @candidates_by_id = {}
160     @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
161   end
162   
163   def detailed_results
164    
165     self.results
166
167     @voter_list = []
168     @vote_list = []
169     @election.voters. each do |voter|
170       if voter.vote and voter.vote.confirmed?
171         @voter_list << voter.email
172         @vote_list << voter.vote
173       end
174     end
175
176     @vote_list.sort!
177     @vote_list.sort! { |a,b| a.token <=> b.token }
178   end
179
180   ## private methods
181   ####################################################################
182   private
183
184     def process_incoming_voters(raw_voter_list)
185       incoming_voters = RawVoterList.new( raw_voter_list )
186
187       unless incoming_voters.entries.empty?
188         incoming_voters.each do |new_voter|
189
190           if incoming_voters.email == 0
191             new_voter.contacted = 1
192           elsif incoming_voters.email == 1
193             email_voter( new_voter )
194             new_voter.contacted = 1
195           else
196             new_voter.contacted = 0
197           end
198         
199           # the new voter should be in good shape. save add to the election
200           new_voter.save
201           @election.voters << new_voter
202         end
203       end
204  
205       # reset the next time to have a the same default value for emailing
206       @raw_voter_list = RawVoterList.new
207       @raw_voter_list.email = incoming_voters.email
208     end
209
210     def email_voter
211     end
212
213 end

Benjamin Mako Hill || Want to submit a patch?