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])
16 # check to see if any of the advanced options have been changed
17 new_qv = QuickVote.new
18 if @quickvote.election_method != new_qv.election_method \
19 or @quickvote.enddate.day != new_qv.enddate.day \
20 or @quickvote.viewable != new_qv.viewable \
21 or @quickvote.notices != new_qv.notices
26 show_advanced ||= false
29 @sidebar_content = render_to_string(:partial => 'create_sidebar',
30 :locals => {:show_advanced => show_advanced})
34 # store the candidate grabbed through ajax and stored in flash
35 @quickvote.candidate_names = flash[:candidate_names]
36 @quickvote.description=@quickvote.description
38 #record who created the quickvote so that person can monitor it easily
39 @quickvote.quickuser = session.session_id
41 #Give registered users additional QuickVote functionality
42 @quickvote.user_id = session[:user][:id] if session[:user]
44 # try to save, if it fails, show the page again (the flash should
47 @quickvote = @quickvote.reload
48 # blank sidebar and show the success page
50 render :action => 'success'
52 flash.keep(:candidate_names)
56 # if we don't have a quickvote param, it means that the person
57 # here has not been hitting this page and we can clear any
58 # candidate_names list in the flash
59 flash.delete(:candidate_names) if flash.has_key?(:candidate_names)
60 @quickvote = QuickVote.new
66 candidate_name = params[:ajax][:newcandidate]
67 unless candidate_name.strip.empty?
68 if flash.has_key?(:candidate_names) \
69 and flash[:candidate_names].instance_of?(Array)
70 unless flash[:candidate_names].index(candidate_name)
71 flash[:candidate_names] << candidate_name
74 flash[:candidate_names] = [ candidate_name ]
77 flash.keep(:candidate_names)
78 render_partial 'candidate_list'
81 #############################################################
82 # the following methods pertain to *voting* in the quickvotes
83 #############################################################
86 @election = QuickVote.ident_to_quickvote(params[:ident])
87 # if the person has specified an election, we show them the voting
88 # page. otherwise, we redirect back to main the page
90 # look to see that the voter has been created and has voted in
91 # this election, and has confirmed their vote
92 @voter = QuickVoter.find(:all,
93 :conditions => ["session_id = ? and election_id = ?",
94 session.session_id, @election.id])[0]
96 # if the voter has not voted we destroy them
97 if @voter and not @voter.voted?
102 # if the voter does not exist or has has been destroyed, lets
105 # create a new voter and populate it
106 @voter = QuickVoter.new
107 @voter.election = @election
108 @voter.session_id = session.session_id
110 # create new vote and make it the defaulted sorted list
111 @voter.vote = Vote.new
113 @voter.vote.set_defaults!
117 redirect_to :controller => 'site'
122 # we need the election to verify that we have the right voter
123 election = QuickVote.ident_to_quickvote(params[:ident])
125 # find out who the voter is for this election
126 @voter = QuickVoter.find(:all,
127 :conditions => ["session_id = ? and election_id = ?",
128 session.session_id, election.id])[0]
131 # we have not seen this voter before. something is wrong, try
133 redirect_to quickvote_url( :ident => params[:ident] )
136 # this person has already voted, we try again
137 flash[:notice] = "You have already voted!"
138 redirect_to quickvote_url( :ident => params[:ident] )
141 # record the ip address for posterity
142 @voter.ipaddress = request.env["REMOTE_ADDR"]
145 # save the time the vote was made for statistical use
146 @voter.vote.time = Time.now
148 # toggle the confirmation bit
152 render :action => 'thanks'
157 voter = QuickVoter.find(:all, :conditions => ["session_id = ?",
158 session.session_id])[0]
160 redirect_to quickvote_url( :ident => params[:ident] )
164 @vote = Vote.find(params[:id])
166 @vote.rankings.each do |ranking|
167 ranking.rank = params['rankings-list'].index(ranking.candidate.id.to_s) + 1
170 render :nothing => true
174 @map = GMap.new("map_div_id")
175 @map.control_init(:large_map => true, :map_type => true)
178 QuickVote.ident_to_quickvote(params[:id]).voters.each do |voter|
179 next unless voter.ipaddress
181 location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
182 next unless location.lng and location.lat
185 center = [location.lat, location.lng]
186 @map.center_zoom_init(center, 4)
189 marker = GMarker.new([location.lat,location.lng],
191 :info_window => (voter.ipaddress or "unknown") \
192 + " " + voter.vote.votestring)
193 @map.overlay_init(marker)
197 ###############################################################
198 # the following method pertains to displaying the results of a
200 ###############################################################
203 unless @election = QuickVote.ident_to_quickvote(params[:ident])
204 flash[:notice] = "Cannot find quickvote #{params[:ident]}."
205 redirect_to :controller => 'site'
208 if @election.viewable == 0 && @election.active == 1
209 render :action => 'not_viewable' and return
211 @results = @election.results
213 @election.candidates.each {|c| @candidates[c.id] = c}
217 @myqvs = QuickVote.find(:all, :conditions => ["quickuser = ?",