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
5 # This program is free software. Please see the COPYING file for
8 class QuickvoteController < ApplicationController
11 require_dependency "quick_voter"
12 require_dependency "quick_vote"
13 require_dependency "vote"
14 require_dependency "election"
16 #############################################################
17 # the following methods pertain to creating quickvotes
18 #############################################################
22 @quickvote = QuickVote.new(params[:quickvote])
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
34 show_advanced ||= false
38 # store the candidate grabbed through ajax and stored in flash
39 @quickvote.candidate_names = flash[:candidate_names]
40 @quickvote.description=@quickvote.description
42 #record who created the quickvote so that person can monitor it easily
43 @quickvote.quickuser = session.session_id
45 #Give registered users additional QuickVote functionality
46 @quickvote.user_id = session[:user][:id] if session[:user]
47 @quickvote.create_candidates
49 # try to save, if it fails, show the page again (the flash should
52 @quickvote = @quickvote.reload
53 # blank sidebar and show the success page
55 render :action => 'success'
58 @sidebar_content = render_to_string(:partial => 'create_sidebar',
59 :locals => {:show_advanced => show_advanced})
60 flash.keep(:candidate_names)
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})
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
84 flash[:candidate_names] = [ candidate_name ]
87 flash.keep(:candidate_names)
88 render :partial => 'candidate_list'
91 #############################################################
92 # the following methods pertain to *voting* in the quickvotes
93 #############################################################
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
101 # if the election is over, redirect to the the results page
102 unless @election.active?
103 redirect_to quickaction_url(:ident => params[:ident],
104 :action => 'results')
107 # look to see that the voter has been created and has voted in
108 # this election, and has confirmed their vote
109 @voter = QuickVoter.find(:all,
110 :conditions => ["session_id = ? and election_id = ?",
111 session.session_id, @election.id])[0]
113 # if the voter has not voted we destroy them
114 if @voter and not @voter.voted?
119 # if the voter does not exist or has has been destroyed, lets
122 # create a new voter and populate it
123 @voter = QuickVoter.new
124 @voter.election = @election
125 @voter.session_id = session.session_id
127 # create new vote and make it the defaulted sorted list
128 @voter.vote = Vote.new
130 @voter.vote.set_defaults!
134 redirect_to :controller => 'front'
140 # we need the election to verify that we have the right voter
141 election = QuickVote.ident_to_quickvote(params[:ident])
143 # find out who the voter is for this election
144 @voter = QuickVoter.find(:all,
145 :conditions => ["session_id = ? and election_id = ?",
146 session.session_id, election.id])[0]
149 # we have not seen this voter before. something is wrong, try
151 redirect_to quickvote_url( :ident => params[:ident] )
154 # this person has already voted, we try again
155 flash[:notice] = "You have already voted!"
156 redirect_to quickvote_url( :ident => params[:ident] )
160 # record the ip address for posterity
161 @voter.ipaddress = request.env["REMOTE_ADDR"]
164 # toggle the confirmation bit
168 render :action => 'thanks'
173 voter = QuickVoter.find(:all, :conditions => ["session_id = ?",
174 session.session_id])[0]
176 redirect_to quickvote_url( :ident => params[:ident] )
180 @map = GMap.new("map_div_id")
181 @map.control_init(:large_map => true, :map_type => true)
183 @election=QuickVote.ident_to_quickvote(params[:id])
184 @election.voters.each do |voter|
185 next unless voter.ipaddress
187 if defined? Cache and location=Cache.get("GEO:#{voter.ipaddress}")
189 location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
190 Cache.set "GEO:#{voter.ipaddress}", location
192 location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
194 next unless location.lng and location.lat
197 center = [location.lat, location.lng]
198 @map.center_zoom_init(center, 4)
201 marker = GMarker.new([location.lat,location.lng],
203 :info_window => (voter.ipaddress or "unknown"))
204 @map.overlay_init(marker)
208 ###############################################################
209 # the following method pertains to displaying the results of a
211 ###############################################################
214 unless @election = QuickVote.ident_to_quickvote(params[:ident])
215 flash[:notice] = "Cannot find quickvote #{params[:ident]}."
216 redirect_to :controller => 'front'
219 if @election.viewable == 0 && @election.active == 1
220 render :action => 'not_viewable' and return
222 @results = @election.results
224 @election.candidates.each {|c| @candidates[c.id] = c}
225 @names = @election.names_by_id
226 @sidebar_content = render_to_string :partial => 'results_sidebar'
230 @myqvs = QuickVote.find(:all, :conditions => ["quickuser = ?",