fixed a large number of bugs in the software (see wiki) over a days work
[selectricity-live] / app / controllers / quickvote_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. Please see the COPYING file for
6 # details.
7
8 class QuickvoteController < ApplicationController
9   helper :sparklines
10   layout 'main'
11   require_dependency "quick_voter"
12   require_dependency "quick_vote"
13   require_dependency "vote"
14   require_dependency "election"
15   
16   #############################################################
17   # the following methods pertain to creating quickvotes
18   #############################################################
19
20   def create
21     if params[:quickvote]
22       @quickvote = QuickVote.new(params[:quickvote])
23
24       # check to see if any of the advanced options have been changed
25       new_qv = QuickVote.new
26       if @quickvote.election_method != new_qv.election_method \
27         or @quickvote.enddate.day != new_qv.enddate.day \
28         or @quickvote.viewable != new_qv.viewable \
29         or @quickvote.notices != new_qv.notices
30         show_advanced = true
31       end
32     end
33
34     show_advanced ||= false
35
36     if params[:quickvote]
37
38       # store the candidate grabbed through ajax and stored in flash
39       @quickvote.candidate_names = flash[:candidate_names]
40       @quickvote.description=@quickvote.description
41
42       #record who created the quickvote so that person can monitor it easily
43       @quickvote.quickuser = session.session_id
44
45       #Give registered users additional QuickVote functionality 
46       @quickvote.user_id = session[:user][:id] if session[:user]
47       @quickvote.create_candidates
48
49       # try to save, if it fails, show the page again (the flash should
50       # still be intact
51       if @quickvote.save
52         @quickvote = @quickvote.reload
53         # blank sidebar and show the success page
54         @sidebar_content = ''
55         render :action => 'success'
56       else
57         # render the sidebar
58         @sidebar_content = render_to_string(:partial => 'create_sidebar',
59           :locals => {:show_advanced => show_advanced})
60         flash.keep(:candidate_names)
61       end 
62
63     else
64       # if we don't have a quickvote param, it means that the person
65       # here has not been hitting this page and we can clear any
66       # candidate_names list in the flash
67       flash.delete(:candidate_names) if flash.has_key?(:candidate_names)
68       @quickvote = QuickVote.new
69       @sidebar_content = render_to_string(:partial => 'create_sidebar',
70         :locals => {:show_advanced => show_advanced})
71     end
72
73   end
74
75   def add_candidate
76     candidate_name = params[:ajax][:newcandidate]
77     unless candidate_name.strip.empty?
78       if flash.has_key?(:candidate_names) \
79         and flash[:candidate_names].instance_of?(Array) 
80         unless flash[:candidate_names].index(candidate_name)
81           flash[:candidate_names] << candidate_name
82         end
83      else
84        flash[:candidate_names] = [ candidate_name ]
85       end
86     end
87     flash.keep(:candidate_names)
88     render :partial => 'candidate_list'
89   end
90  
91   #############################################################
92   # the following methods pertain to *voting* in the quickvotes
93   #############################################################
94
95   def index
96     @election = QuickVote.ident_to_quickvote(params[:ident])
97     # if the person has specified an election, we show them the voting
98     # page. otherwise, we redirect back to main the page
99     if @election
100       # look to see that the voter has been created and has voted in
101       # this election, and has confirmed their vote
102       @voter = QuickVoter.find(:all,
103         :conditions => ["session_id = ? and election_id = ?",
104                         session.session_id, @election.id])[0]
105
106       # if the voter has not voted we destroy them
107       if @voter and not @voter.voted?
108         @voter.destroy
109         @voter = nil
110       end
111
112       # if the voter does not exist or has has been destroyed, lets
113       # create a new one
114       unless @voter
115         # create a new voter and populate it
116         @voter = QuickVoter.new
117         @voter.election = @election
118         @voter.session_id = session.session_id
119               
120         # create new vote and make it the defaulted sorted list
121         @voter.vote = Vote.new
122               @voter.save
123               @voter.vote.set_defaults!
124               @voter.reload
125       end
126     else
127       redirect_to :controller => 'front'
128     end
129   end
130
131   def confirm
132     
133     # we need the election to verify that we have the right voter
134     election = QuickVote.ident_to_quickvote(params[:ident])
135
136     # find out who the voter is for this election
137     @voter = QuickVoter.find(:all,
138       :conditions => ["session_id = ? and election_id = ?", 
139                       session.session_id, election.id])[0]
140    
141     if not @voter
142       # we have not seen this  voter before. something is wrong, try
143       # again
144       redirect_to quickvote_url( :ident => params[:ident] ) 
145       
146     elsif @voter.voted? 
147       # this person has already voted, we try again
148       flash[:notice] = "You have already voted!"
149       redirect_to quickvote_url( :ident => params[:ident] )
150       
151     else
152       
153       # record the ip address for posterity
154       @voter.ipaddress = request.env["REMOTE_ADDR"]
155       @voter.save
156       
157       # toggle the confirmation bit      
158       @voter.vote.confirm!
159      
160       @voter.reload
161       render :action => 'thanks'
162     end
163   end
164  
165   def change
166     voter = QuickVoter.find(:all, :conditions => ["session_id = ?",
167                                                   session.session_id])[0]
168     voter.destroy
169     redirect_to quickvote_url( :ident => params[:ident] )
170   end
171                 
172   def list_voters
173     @map = GMap.new("map_div_id") 
174     @map.control_init(:large_map => true, :map_type => true) 
175     center = nil
176     @election=QuickVote.ident_to_quickvote(params[:id])
177     @election.voters.each do |voter|
178       next unless voter.ipaddress
179       location=nil
180       if defined? Cache and location=Cache.get("GEO:#{voter.ipaddress}")
181       elsif defined? Cache
182         location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
183         Cache.set "GEO:#{voter.ipaddress}", location
184       else
185         location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
186       end
187       next unless location.lng and location.lat
188
189       unless center
190         center = [location.lat, location.lng]
191         @map.center_zoom_init(center, 4)
192       end
193
194       marker = GMarker.new([location.lat,location.lng],
195                            :title => "Voter",
196                            :info_window => (voter.ipaddress or "unknown"))
197       @map.overlay_init(marker)
198     end
199   end
200
201   ###############################################################
202   # the following method pertains to displaying the results of a
203   # quickvote
204   ###############################################################
205
206   def results
207     unless @election = QuickVote.ident_to_quickvote(params[:ident])
208       flash[:notice] = "Cannot find quickvote #{params[:ident]}."
209       redirect_to :controller => 'front'
210       return
211     end
212     if @election.viewable == 0 && @election.active == 1
213       render :action => 'not_viewable' and return
214     end
215     @results = @election.results
216     @candidates = {}
217     @election.candidates.each {|c| @candidates[c.id] = c}
218     @names = @election.names_by_id
219     @sidebar_content = render_to_string :partial => 'results_sidebar'
220   end
221   
222   def my_quickvotes
223     @myqvs = QuickVote.find(:all, :conditions => ["quickuser = ?",
224                                 session.session_id])
225   end
226   
227 end
228

Benjamin Mako Hill || Want to submit a patch?