1 class QuickvoteController < ApplicationController
3 require_dependency "quick_voter"
4 require_dependency "quick_vote"
5 require_dependency "vote"
6 require_dependency "election"
8 #############################################################
9 # the following methods pertain to creating quickvotes
10 #############################################################
14 @quickvote = QuickVote.new(params[:quickvote])
16 # check to see if any of the advanced options have been changed
17 new_qv = QuickVote.new
18 if @quickvote.election_method != new_qv.election_method \
19 or @quickvote.enddate.day != new_qv.enddate.day \
20 or @quickvote.viewable != new_qv.viewable \
21 or @quickvote.notices != new_qv.notices
26 show_advanced ||= false
30 # store the candidate grabbed through ajax and stored in flash
31 @quickvote.candidate_names = flash[:candidate_names]
32 @quickvote.description=@quickvote.description
34 #record who created the quickvote so that person can monitor it easily
35 @quickvote.quickuser = session.session_id
37 #Give registered users additional QuickVote functionality
38 @quickvote.user_id = session[:user][:id] if session[:user]
40 # try to save, if it fails, show the page again (the flash should
43 @quickvote = @quickvote.reload
44 # blank sidebar and show the success page
46 render :action => 'success'
49 @sidebar_content = render_to_string(:partial => 'create_sidebar',
50 :locals => {:show_advanced => show_advanced})
51 flash.keep(:candidate_names)
55 # if we don't have a quickvote param, it means that the person
56 # here has not been hitting this page and we can clear any
57 # candidate_names list in the flash
58 flash.delete(:candidate_names) if flash.has_key?(:candidate_names)
59 @quickvote = QuickVote.new
60 @sidebar_content = render_to_string(:partial => 'create_sidebar',
61 :locals => {:show_advanced => show_advanced})
67 candidate_name = params[:ajax][:newcandidate]
68 unless candidate_name.strip.empty?
69 if flash.has_key?(:candidate_names) \
70 and flash[:candidate_names].instance_of?(Array)
71 unless flash[:candidate_names].index(candidate_name)
72 flash[:candidate_names] << candidate_name
75 flash[:candidate_names] = [ candidate_name ]
78 flash.keep(:candidate_names)
79 render_partial 'candidate_list'
82 #############################################################
83 # the following methods pertain to *voting* in the quickvotes
84 #############################################################
87 @election = QuickVote.ident_to_quickvote(params[:ident])
88 # if the person has specified an election, we show them the voting
89 # page. otherwise, we redirect back to main the page
91 # look to see that the voter has been created and has voted in
92 # this election, and has confirmed their vote
93 @voter = QuickVoter.find(:all,
94 :conditions => ["session_id = ? and election_id = ?",
95 session.session_id, @election.id])[0]
97 # if the voter has not voted we destroy them
98 if @voter and not @voter.voted?
103 # if the voter does not exist or has has been destroyed, lets
106 # create a new voter and populate it
107 @voter = QuickVoter.new
108 @voter.election = @election
109 @voter.session_id = session.session_id
111 # create new vote and make it the defaulted sorted list
112 @voter.vote = Vote.new
114 @voter.vote.set_defaults!
118 redirect_to :controller => 'site'
123 # we need the election to verify that we have the right voter
124 election = QuickVote.ident_to_quickvote(params[:ident])
126 # find out who the voter is for this election
127 @voter = QuickVoter.find(:all,
128 :conditions => ["session_id = ? and election_id = ?",
129 session.session_id, election.id])[0]
132 # we have not seen this voter before. something is wrong, try
134 redirect_to quickvote_url( :ident => params[:ident] )
137 # this person has already voted, we try again
138 flash[:notice] = "You have already voted!"
139 redirect_to quickvote_url( :ident => params[:ident] )
142 # record the ip address for posterity
143 @voter.ipaddress = request.env["REMOTE_ADDR"]
146 # save the time the vote was made for statistical use
147 @voter.vote.time = Time.now
149 # toggle the confirmation bit
153 render :action => 'thanks'
158 voter = QuickVoter.find(:all, :conditions => ["session_id = ?",
159 session.session_id])[0]
161 redirect_to quickvote_url( :ident => params[:ident] )
165 @map = GMap.new("map_div_id")
166 @map.control_init(:large_map => true, :map_type => true)
168 @election=QuickVote.ident_to_quickvote(params[:id])
169 @election.voters.each do |voter|
170 next unless voter.ipaddress
171 location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
172 next unless location.lng and location.lat
175 center = [location.lat, location.lng]
176 @map.center_zoom_init(center, 4)
179 marker = GMarker.new([location.lat,location.lng],
181 :info_window => (voter.ipaddress or "unknown") \
182 + " " + voter.vote.votestring)
183 @map.overlay_init(marker)
187 ###############################################################
188 # the following method pertains to displaying the results of a
190 ###############################################################
193 unless @election = QuickVote.ident_to_quickvote(params[:ident])
194 flash[:notice] = "Cannot find quickvote #{params[:ident]}."
195 redirect_to :controller => 'site'
198 if @election.viewable == 0 && @election.active == 1
199 render :action => 'not_viewable' and return
201 @results = @election.results
203 @election.candidates.each {|c| @candidates[c.id] = c}
204 @names = @election.names_by_id
205 @sidebar_content = render_to_string :partial => 'results_sidebar'
209 @myqvs = QuickVote.find(:all, :conditions => ["quickuser = ?",