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])
15 # store the candidate grabbed through ajax and stored in flash
16 @quickvote.candidate_names = flash[:candidate_names]
17 @quickvote.description=@quickvote.description
18 #record who created the quickvote so that person can monitor it easily
19 @quickvote.quickuser = session.session_id
20 # try to save, if it fails, show the page again (the flash should
23 @quickvote = @quickvote.reload
24 render :action => 'success'
26 flash.keep(:candidate_names)
30 # if we don't have a quickvote param, it means that the person
31 # here has not been hitting this page and we can clear any
32 # candidate_names list in the flash
33 flash.delete(:candidate_names) if flash.has_key?(:candidate_names)
34 @quickvote = QuickVote.new
39 candidate_name = params[:ajax][:newcandidate]
40 unless candidate_name.strip.empty?
41 if flash.has_key?(:candidate_names) \
42 and flash[:candidate_names].instance_of?(Array)
43 unless flash[:candidate_names].index(candidate_name)
44 flash[:candidate_names] << candidate_name
47 flash[:candidate_names] = [ candidate_name ]
50 flash.keep(:candidate_names)
51 render_partial 'candidate_list'
54 #############################################################
55 # the following methods pertain to *voting* in the quickvotes
56 #############################################################
59 @election = QuickVote.ident_to_quickvote(params[:ident])
60 # if the person has specified an election, we show them the voting
61 # page. otherwise, we redirect back to main the page
63 # look to see that the voter has been created and has voted in
64 # this election, and has confirmed their vote
65 @voter = QuickVoter.find(:all,
66 :conditions => ["session_id = ? and election_id = ?",
67 session.session_id, @election.id])[0]
69 # if the voter has not voted we destroy them
70 if @voter and not @voter.voted?
75 # if the voter does not exist or has has been destroyed, lets
78 # create a new voter and populate it
79 @voter = QuickVoter.new
80 @voter.election = @election
81 @voter.session_id = session.session_id
83 # create new vote and make it the defaulted sorted list
84 @voter.vote = Vote.new
86 @voter.vote.set_defaults!
90 redirect_to :controller => 'site'
95 # we need the election to verify that we have the right voter
96 election = QuickVote.ident_to_quickvote(params[:ident])
98 # find out who the voter is for this election
99 @voter = QuickVoter.find(:all,
100 :conditions => ["session_id = ? and election_id = ?",
101 session.session_id, election.id])[0]
104 # we have not seen this voter before. something is wrong, try
106 redirect_to quickvote_url( :ident => params[:ident] )
109 # this person has already voted, we try again
110 flash[:notice] = "You have already voted!"
111 redirect_to quickvote_url( :ident => params[:ident] )
114 # record the ip address for posterity
115 @voter.ipaddress = request.env["REMOTE_ADDR"]
118 # save the time the vote was made for statistical use
119 @voter.vote.time = Time.now
121 # toggle the confirmation bit
125 render :action => 'thanks'
130 voter = QuickVoter.find(:all, :conditions => ["session_id = ?",
131 session.session_id])[0]
133 redirect_to quickvote_url( :ident => params[:ident] )
137 @vote = Vote.find(params[:id])
139 @vote.rankings.each do |ranking|
140 ranking.rank = params['rankings-list'].index(ranking.candidate.id.to_s) + 1
143 render :nothing => true
147 @map = GMap.new("map_div_id")
148 @map.control_init(:large_map => true, :map_type => true)
151 QuickVote.ident_to_quickvote(params[:id]).voters.each do |voter|
152 next unless voter.ipaddress
154 location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
155 next unless location.lng and location.lat
158 center = [location.lat, location.lng]
159 @map.center_zoom_init(center, 4)
162 marker = GMarker.new([location.lat,location.lng],
164 :info_window => (voter.ipaddress or "unknown") \
165 + " " + voter.vote.votestring)
166 @map.overlay_init(marker)
170 ###############################################################
171 # the following method pertains to displaying the results of a
173 ###############################################################
176 unless @election = QuickVote.ident_to_quickvote(params[:ident])
177 flash[:notice] = "Cannot find quickvote #{params[:ident]}."
178 redirect_to :controller => 'site'
181 if @election.viewable == 0 && @election.active == 1
182 render :action => 'not_viewable' and return
184 @results = @election.results
186 @election.candidates.each {|c| @candidates[c.id] = c}
190 @myqvs = QuickVote.find(:all, :conditions => ["quickuser = ?",