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]
48 # try to save, if it fails, show the page again (the flash should
51 @quickvote = @quickvote.reload
52 # blank sidebar and show the success page
54 render :action => 'success'
57 @sidebar_content = render_to_string(:partial => 'create_sidebar',
58 :locals => {:show_advanced => show_advanced})
59 flash.keep(:candidate_names)
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})
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
83 flash[:candidate_names] = [ candidate_name ]
86 flash.keep(:candidate_names)
87 render :partial => 'candidate_list'
90 #############################################################
91 # the following methods pertain to *voting* in the quickvotes
92 #############################################################
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
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]
105 # if the voter has not voted we destroy them
106 if @voter and not @voter.voted?
111 # if the voter does not exist or has has been destroyed, lets
114 # create a new voter and populate it
115 @voter = QuickVoter.new
116 @voter.election = @election
117 @voter.session_id = session.session_id
119 # create new vote and make it the defaulted sorted list
120 @voter.vote = Vote.new
122 @voter.vote.set_defaults!
126 redirect_to :controller => 'front'
132 # we need the election to verify that we have the right voter
133 election = QuickVote.ident_to_quickvote(params[:ident])
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]
141 # we have not seen this voter before. something is wrong, try
143 redirect_to quickvote_url( :ident => params[:ident] )
146 # this person has already voted, we try again
147 flash[:notice] = "You have already voted!"
148 redirect_to quickvote_url( :ident => params[:ident] )
152 # record the ip address for posterity
153 @voter.ipaddress = request.env["REMOTE_ADDR"]
156 # toggle the confirmation bit
160 render :action => 'thanks'
165 voter = QuickVoter.find(:all, :conditions => ["session_id = ?",
166 session.session_id])[0]
168 redirect_to quickvote_url( :ident => params[:ident] )
172 @map = GMap.new("map_div_id")
173 @map.control_init(:large_map => true, :map_type => true)
175 @election=QuickVote.ident_to_quickvote(params[:id])
176 @election.voters.each do |voter|
177 next unless voter.ipaddress
179 if defined? Cache and location=Cache.get("GEO:#{voter.ipaddress}")
181 location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
182 Cache.set "GEO:#{voter.ipaddress}", location
184 location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
186 next unless location.lng and location.lat
189 center = [location.lat, location.lng]
190 @map.center_zoom_init(center, 4)
193 marker = GMarker.new([location.lat,location.lng],
195 :info_window => (voter.ipaddress or "unknown"))
196 @map.overlay_init(marker)
200 ###############################################################
201 # the following method pertains to displaying the results of a
203 ###############################################################
206 unless @election = QuickVote.ident_to_quickvote(params[:ident])
207 flash[:notice] = "Cannot find quickvote #{params[:ident]}."
208 redirect_to :controller => 'front'
211 if @election.viewable == 0 && @election.active == 1
212 render :action => 'not_viewable' and return
214 @results = @election.results
216 @election.candidates.each {|c| @candidates[c.id] = c}
217 @names = @election.names_by_id
218 @sidebar_content = render_to_string :partial => 'results_sidebar'
222 @myqvs = QuickVote.find(:all, :conditions => ["quickuser = ?",