1 class QuickvoteController < ApplicationController
8 #############################################################
9 # the following methods pertain to creating quickvotes
10 #############################################################
14 @quickvote = QuickVote.new(params[:quickvote])
16 # store the candidate grabbed through ajax and stored in flash
17 @quickvote.candidatelist = flash[:candlist]
18 @quickvote.description=CGI.escapeHTML(@quickvote.description)
19 # try to save, if it fails, show the page again (the flash should
22 @quickvote = @quickvote.reload
23 render :action => 'success'
29 # if we don't have a quickvote param, it means that the person
30 # here has not been hitting this page and we can clear any
31 # candlist in the flash
32 flash.delete(:candlist) if flash.has_key?(:candlist)
37 candidate_name = CGI.escapeHTML(params[:ajax][:newcandidate])
38 unless candidate_name.strip.empty?
39 if flash.has_key?(:candlist) and flash[:candlist].instance_of?(Array)
40 flash[:candlist] << candidate_name unless flash[:candlist].index(candidate_name)
42 flash[:candlist] = [ candidate_name ]
46 render_partial 'candidate_list'
49 #############################################################
50 # the following methods pertain to *voting* in the quickvotes
51 #############################################################
54 @election = QuickVote.ident_to_quickvote(params[:ident])
56 # if the person has specified an election, we show them the voting
57 # page. otherwise, we redirect back to main the page
60 # look to see that the voter has been created and has voted in
61 # this election, and has confirmed their vote
62 @voter = QuickVoter.find(:all, :conditions => ["session_id = ? and election_id = ?",
63 session.session_id, @election.id])[0]
65 # if the voter has not voted we destroy them
66 if @voter and not @voter.voted?
71 # if the voter does not exist or has has been destroyed, lets
74 # create a new voter and populate it
75 @voter = QuickVoter.new
76 @voter.election = @election
77 @voter.session_id = session.session_id
79 # create new vote and make it the defaulted sorted list
80 @voter.vote = Vote.new
82 @voter.vote.set_defaults!
86 redirect_to :controller => 'site'
91 # we need the election to verify that we have the right voter
92 election = QuickVote.ident_to_quickvote(params[:ident])
94 # find out who the voter is for this election
95 @voter = QuickVoter.find(:all, :conditions => ["session_id = ? and election_id = ?",
96 session.session_id, election.id])[0]
99 # we have not seen this voter before. something is wrong, try
101 redirect_to quickvote_url( :ident => params[:ident] )
104 # this person has already voted, we try again
105 flash[:notice] = "You have already voted!"
106 redirect_to quickvote_url( :ident => params[:ident] )
109 # record the ip address for posterity
110 @voter.ipaddress = request.env["REMOTE_ADDR"]
113 # save the time the vote was made for statistical use
114 @voter.vote.time = Time.now
116 # toggle the confirmation bit
120 render :action => 'thanks'
125 voter = QuickVoter.find(:all, :conditions => ["session_id = ?", session.session_id])[0]
127 redirect_to quickvote_url( :ident => params[:ident] )
131 @vote = Vote.find(params[:id])
133 @vote.rankings.each do |ranking|
134 ranking.rank = params['rankings-list'].index(ranking.candidate.id.to_s) + 1
137 render :nothing => true
141 @map = GMap.new("map_div_id")
142 @map.control_init(:large_map => true, :map_type => true)
144 QuickVote.ident_to_quickvote(params[:id]).voters.each do |voter|
145 next unless voter.ipaddress
146 location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
147 next unless location.lng and location.lat
149 center=[location.lat,location.lng]
150 @map.center_zoom_init(center,4)
152 marker = GMarker.new([location.lat,location.lng], :title => "Voter", :info_window => (voter.ipaddress or "unknown")+" "+voter.vote.votestring)
153 @map.overlay_init(marker)
156 ###############################################################
157 # the following method pertains to displaying the results of a
159 ###############################################################
162 @election = QuickVote.ident_to_quickvote(params[:ident])
165 @election.candidates.each {|c| @candidates[c.id] = c}