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 # try to save, if it fails, show the page again (the flash should
21 @quickvote = @quickvote.reload
22 render :action => 'success'
24 flash.keep(:candidate_names)
28 # if we don't have a quickvote param, it means that the person
29 # here has not been hitting this page and we can clear any
30 # candidate_names list in the flash
31 flash.delete(:candidate_names) if flash.has_key?(:candidate_names)
32 @quickvote = QuickVote.new
37 candidate_name = params[:ajax][:newcandidate]
38 unless candidate_name.strip.empty?
39 if flash.has_key?(:candidate_names) \
40 and flash[:candidate_names].instance_of?(Array)
41 unless flash[:candidate_names].index(candidate_name)
42 flash[:candidate_names] << candidate_name
45 flash[:candidate_names] = [ candidate_name ]
48 flash.keep(:candidate_names)
49 render_partial 'candidate_list'
52 #############################################################
53 # the following methods pertain to *voting* in the quickvotes
54 #############################################################
57 @election = QuickVote.ident_to_quickvote(params[:ident])
58 # if the person has specified an election, we show them the voting
59 # page. otherwise, we redirect back to main the page
61 # look to see that the voter has been created and has voted in
62 # this election, and has confirmed their vote
63 @voter = QuickVoter.find(:all,
64 :conditions => ["session_id = ? and election_id = ?",
65 session.session_id, @election.id])[0]
67 # if the voter has not voted we destroy them
68 if @voter and not @voter.voted?
73 # if the voter does not exist or has has been destroyed, lets
76 # create a new voter and populate it
77 @voter = QuickVoter.new
78 @voter.election = @election
79 @voter.session_id = session.session_id
81 # create new vote and make it the defaulted sorted list
82 @voter.vote = Vote.new
84 @voter.vote.set_defaults!
88 redirect_to :controller => 'site'
93 # we need the election to verify that we have the right voter
94 election = QuickVote.ident_to_quickvote(params[:ident])
96 # find out who the voter is for this election
97 @voter = QuickVoter.find(:all,
98 :conditions => ["session_id = ? and election_id = ?",
99 session.session_id, election.id])[0]
102 # we have not seen this voter before. something is wrong, try
104 redirect_to quickvote_url( :ident => params[:ident] )
107 # this person has already voted, we try again
108 flash[:notice] = "You have already voted!"
109 redirect_to quickvote_url( :ident => params[:ident] )
112 # record the ip address for posterity
113 @voter.ipaddress = request.env["REMOTE_ADDR"]
116 # save the time the vote was made for statistical use
117 @voter.vote.time = Time.now
119 # toggle the confirmation bit
123 render :action => 'thanks'
128 voter = QuickVoter.find(:all, :conditions => ["session_id = ?",
129 session.session_id])[0]
131 redirect_to quickvote_url( :ident => params[:ident] )
135 @vote = Vote.find(params[:id])
137 @vote.rankings.each do |ranking|
138 ranking.rank = params['rankings-list'].index(ranking.candidate.id.to_s) + 1
141 render :nothing => true
145 @map = GMap.new("map_div_id")
146 @map.control_init(:large_map => true, :map_type => true)
149 QuickVote.ident_to_quickvote(params[:id]).voters.each do |voter|
150 next unless voter.ipaddress
152 location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
153 next unless location.lng and location.lat
156 center = [location.lat, location.lng]
157 @map.center_zoom_init(center, 4)
160 marker = GMarker.new([location.lat,location.lng],
162 :info_window => (voter.ipaddress or "unknown") \
163 + " " + voter.vote.votestring)
164 @map.overlay_init(marker)
168 ###############################################################
169 # the following method pertains to displaying the results of a
171 ###############################################################
174 unless @election = QuickVote.ident_to_quickvote(params[:ident])
175 flash[:notice] = "Cannot find quickvote #{params[:ident]}."
176 redirect_to :controller => 'site'
179 @results = @election.results
181 @election.candidates.each {|c| @candidates[c.id] = c}