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 #Give registered users additional QuickVote functionality
21 @quickvote.user_id = session[:user][:id] if session[:user]
22 # try to save, if it fails, show the page again (the flash should
25 @quickvote = @quickvote.reload
26 render :action => 'success'
28 flash.keep(:candidate_names)
32 # if we don't have a quickvote param, it means that the person
33 # here has not been hitting this page and we can clear any
34 # candidate_names list in the flash
35 flash.delete(:candidate_names) if flash.has_key?(:candidate_names)
36 @quickvote = QuickVote.new
41 candidate_name = params[:ajax][:newcandidate]
42 unless candidate_name.strip.empty?
43 if flash.has_key?(:candidate_names) \
44 and flash[:candidate_names].instance_of?(Array)
45 unless flash[:candidate_names].index(candidate_name)
46 flash[:candidate_names] << candidate_name
49 flash[:candidate_names] = [ candidate_name ]
52 flash.keep(:candidate_names)
53 render_partial 'candidate_list'
56 #############################################################
57 # the following methods pertain to *voting* in the quickvotes
58 #############################################################
61 @election = QuickVote.ident_to_quickvote(params[:ident])
62 # if the person has specified an election, we show them the voting
63 # page. otherwise, we redirect back to main the page
65 # look to see that the voter has been created and has voted in
66 # this election, and has confirmed their vote
67 @voter = QuickVoter.find(:all,
68 :conditions => ["session_id = ? and election_id = ?",
69 session.session_id, @election.id])[0]
71 # if the voter has not voted we destroy them
72 if @voter and not @voter.voted?
77 # if the voter does not exist or has has been destroyed, lets
80 # create a new voter and populate it
81 @voter = QuickVoter.new
82 @voter.election = @election
83 @voter.session_id = session.session_id
85 # create new vote and make it the defaulted sorted list
86 @voter.vote = Vote.new
88 @voter.vote.set_defaults!
92 redirect_to :controller => 'site'
97 # we need the election to verify that we have the right voter
98 election = QuickVote.ident_to_quickvote(params[:ident])
100 # find out who the voter is for this election
101 @voter = QuickVoter.find(:all,
102 :conditions => ["session_id = ? and election_id = ?",
103 session.session_id, election.id])[0]
106 # we have not seen this voter before. something is wrong, try
108 redirect_to quickvote_url( :ident => params[:ident] )
111 # this person has already voted, we try again
112 flash[:notice] = "You have already voted!"
113 redirect_to quickvote_url( :ident => params[:ident] )
116 # record the ip address for posterity
117 @voter.ipaddress = request.env["REMOTE_ADDR"]
120 # save the time the vote was made for statistical use
121 @voter.vote.time = Time.now
123 # toggle the confirmation bit
127 render :action => 'thanks'
132 voter = QuickVoter.find(:all, :conditions => ["session_id = ?",
133 session.session_id])[0]
135 redirect_to quickvote_url( :ident => params[:ident] )
139 @vote = Vote.find(params[:id])
141 @vote.rankings.each do |ranking|
142 ranking.rank = params['rankings-list'].index(ranking.candidate.id.to_s) + 1
145 render :nothing => true
149 @map = GMap.new("map_div_id")
150 @map.control_init(:large_map => true, :map_type => true)
153 QuickVote.ident_to_quickvote(params[:id]).voters.each do |voter|
154 next unless voter.ipaddress
156 location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
157 next unless location.lng and location.lat
160 center = [location.lat, location.lng]
161 @map.center_zoom_init(center, 4)
164 marker = GMarker.new([location.lat,location.lng],
166 :info_window => (voter.ipaddress or "unknown") \
167 + " " + voter.vote.votestring)
168 @map.overlay_init(marker)
172 ###############################################################
173 # the following method pertains to displaying the results of a
175 ###############################################################
178 unless @election = QuickVote.ident_to_quickvote(params[:ident])
179 flash[:notice] = "Cannot find quickvote #{params[:ident]}."
180 redirect_to :controller => 'site'
183 if @election.viewable == 0 && @election.active == 1
184 render :action => 'not_viewable' and return
186 @results = @election.results
188 @election.candidates.each {|c| @candidates[c.id] = c}
192 @myqvs = QuickVote.find(:all, :conditions => ["quickuser = ?",