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

Benjamin Mako Hill || Want to submit a patch?