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

Benjamin Mako Hill || Want to submit a patch?