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.candidatelist = flash[:candlist]
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'
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 # candlist in the flash
31 flash.delete(:candlist) if flash.has_key?(:candlist)
36 candidate_name = params[:ajax][:newcandidate]
37 unless candidate_name.strip.empty?
38 if flash.has_key?(:candlist) and flash[:candlist].instance_of?(Array)
39 flash[:candlist] << candidate_name unless flash[:candlist].index(candidate_name)
41 flash[:candlist] = [ candidate_name ]
45 render_partial 'candidate_list'
48 #############################################################
49 # the following methods pertain to *voting* in the quickvotes
50 #############################################################
53 @election = QuickVote.ident_to_quickvote(params[:ident])
54 # if the person has specified an election, we show them the voting
55 # page. otherwise, we redirect back to main the page
57 # look to see that the voter has been created and has voted in
58 # this election, and has confirmed their vote
59 @voter = QuickVoter.find(:all, :conditions => ["session_id = ? and election_id = ?",
60 session.session_id, @election.id])[0]
62 # if the voter has not voted we destroy them
63 if @voter and not @voter.voted?
68 # if the voter does not exist or has has been destroyed, lets
71 # create a new voter and populate it
72 @voter = QuickVoter.new
73 @voter.election = @election
74 @voter.session_id = session.session_id
76 # create new vote and make it the defaulted sorted list
77 @voter.vote = Vote.new
79 @voter.vote.set_defaults!
83 redirect_to :controller => 'site'
88 # we need the election to verify that we have the right voter
89 election = QuickVote.ident_to_quickvote(params[:ident])
91 # find out who the voter is for this election
92 @voter = QuickVoter.find(:all, :conditions => ["session_id = ? and election_id = ?",
93 session.session_id, election.id])[0]
96 # we have not seen this voter before. something is wrong, try
98 redirect_to quickvote_url( :ident => params[:ident] )
101 # this person has already voted, we try again
102 flash[:notice] = "You have already voted!"
103 redirect_to quickvote_url( :ident => params[:ident] )
106 # record the ip address for posterity
107 @voter.ipaddress = request.env["REMOTE_ADDR"]
110 # save the time the vote was made for statistical use
111 @voter.vote.time = Time.now
113 # toggle the confirmation bit
117 render :action => 'thanks'
122 voter = QuickVoter.find(:all, :conditions => ["session_id = ?", session.session_id])[0]
124 redirect_to quickvote_url( :ident => params[:ident] )
128 @vote = Vote.find(params[:id])
130 @vote.rankings.each do |ranking|
131 ranking.rank = params['rankings-list'].index(ranking.candidate.id.to_s) + 1
134 render :nothing => true
138 @map = GMap.new("map_div_id")
139 @map.control_init(:large_map => true, :map_type => true)
141 QuickVote.ident_to_quickvote(params[:id]).voters.each do |voter|
142 next unless voter.ipaddress
143 location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
144 next unless location.lng and location.lat
146 center=[location.lat,location.lng]
147 @map.center_zoom_init(center,4)
149 marker = GMarker.new([location.lat,location.lng], :title => "Voter", :info_window => (voter.ipaddress or "unknown")+" "+voter.vote.votestring)
150 @map.overlay_init(marker)
153 ###############################################################
154 # the following method pertains to displaying the results of a
156 ###############################################################
159 unless @election = QuickVote.ident_to_quickvote(params[:ident])
160 flash[:notice] = "Cannot find quickvote #{params[:ident]}."
161 redirect_to :controller => 'site'
164 @results = @election.results
166 @election.candidates.each {|c| @candidates[c.id] = c}