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
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]
106 # if the voter has not voted we destroy them
107 if @voter and not @voter.voted?
112 # if the voter does not exist or has has been destroyed, lets
115 # create a new voter and populate it
116 @voter = QuickVoter.new
117 @voter.election = @election
118 @voter.session_id = session.session_id
120 # create new vote and make it the defaulted sorted list
121 @voter.vote = Vote.new
123 @voter.vote.set_defaults!
127 redirect_to :controller => 'front'
133 # we need the election to verify that we have the right voter
134 election = QuickVote.ident_to_quickvote(params[:ident])
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]
142 # we have not seen this voter before. something is wrong, try
144 redirect_to quickvote_url( :ident => params[:ident] )
147 # this person has already voted, we try again
148 flash[:notice] = "You have already voted!"
149 redirect_to quickvote_url( :ident => params[:ident] )
153 # record the ip address for posterity
154 @voter.ipaddress = request.env["REMOTE_ADDR"]
157 # toggle the confirmation bit
161 render :action => 'thanks'
166 voter = QuickVoter.find(:all, :conditions => ["session_id = ?",
167 session.session_id])[0]
169 redirect_to quickvote_url( :ident => params[:ident] )
173 @map = GMap.new("map_div_id")
174 @map.control_init(:large_map => true, :map_type => true)
176 @election=QuickVote.ident_to_quickvote(params[:id])
177 @election.voters.each do |voter|
178 next unless voter.ipaddress
180 if defined? Cache and location=Cache.get("GEO:#{voter.ipaddress}")
182 location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
183 Cache.set "GEO:#{voter.ipaddress}", location
185 location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
187 next unless location.lng and location.lat
190 center = [location.lat, location.lng]
191 @map.center_zoom_init(center, 4)
194 marker = GMarker.new([location.lat,location.lng],
196 :info_window => (voter.ipaddress or "unknown"))
197 @map.overlay_init(marker)
201 ###############################################################
202 # the following method pertains to displaying the results of a
204 ###############################################################
207 unless @election = QuickVote.ident_to_quickvote(params[:ident])
208 flash[:notice] = "Cannot find quickvote #{params[:ident]}."
209 redirect_to :controller => 'front'
212 if @election.viewable == 0 && @election.active == 1
213 render :action => 'not_viewable' and return
215 @results = @election.results
217 @election.candidates.each {|c| @candidates[c.id] = c}
218 @names = @election.names_by_id
219 @sidebar_content = render_to_string :partial => 'results_sidebar'
223 @myqvs = QuickVote.find(:all, :conditions => ["quickuser = ?",