Added a method, views, and corresponding route so that a user can track the
[selectricity] / app / controllers / quickvote_controller.rb
1 class QuickvoteController < ApplicationController
2   layout 'main'
3   require_dependency "quick_voter"
4   require_dependency "quick_vote"
5   require_dependency "vote"
6   require_dependency "election"
7   
8   #############################################################
9   # the following methods pertain to creating quickvotes
10   #############################################################
11
12   def create
13     if params[:quickvote]
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       # try to save, if it fails, show the page again (the flash should
21       # still be intact
22       if @quickvote.save
23         @quickvote = @quickvote.reload
24         render :action => 'success'
25       else
26         flash.keep(:candidate_names)
27       end 
28
29     else
30       # if we don't have a quickvote param, it means that the person
31       # here has not been hitting this page and we can clear any
32       # candidate_names list in the flash
33       flash.delete(:candidate_names) if flash.has_key?(:candidate_names)
34       @quickvote = QuickVote.new
35     end
36   end
37
38   def add_candidate
39     candidate_name = params[:ajax][:newcandidate]
40     unless candidate_name.strip.empty?
41       if flash.has_key?(:candidate_names) \
42         and flash[:candidate_names].instance_of?(Array) 
43         unless flash[:candidate_names].index(candidate_name)
44           flash[:candidate_names] << candidate_name
45         end
46      else
47        flash[:candidate_names] = [ candidate_name ]
48       end
49     end
50     flash.keep(:candidate_names)
51     render_partial 'candidate_list'
52   end
53  
54   #############################################################
55   # the following methods pertain to *voting* in the quickvotes
56   #############################################################
57
58   def index
59     @election = QuickVote.ident_to_quickvote(params[:ident])
60     # if the person has specified an election, we show them the voting
61     # page. otherwise, we redirect back to main the page
62     if @election
63       # look to see that the voter has been created and has voted in
64       # this election, and has confirmed their vote
65       @voter = QuickVoter.find(:all,
66         :conditions => ["session_id = ? and election_id = ?",
67                         session.session_id, @election.id])[0]
68
69       # if the voter has not voted we destroy them
70       if @voter and not @voter.voted?
71         @voter.destroy
72         @voter = nil
73       end
74
75       # if the voter does not exist or has has been destroyed, lets
76       # create a new one
77       unless @voter
78         # create a new voter and populate it
79         @voter = QuickVoter.new
80         @voter.election = @election
81         @voter.session_id = session.session_id
82               
83         # create new vote and make it the defaulted sorted list
84         @voter.vote = Vote.new
85               @voter.save
86               @voter.vote.set_defaults!
87               @voter.reload
88       end
89     else
90       redirect_to :controller => 'site'
91     end
92   end
93
94   def confirm
95     # we need the election to verify that we have the right voter
96     election = QuickVote.ident_to_quickvote(params[:ident])
97
98     # find out who the voter is for this election
99     @voter = QuickVoter.find(:all,
100       :conditions => ["session_id = ? and election_id = ?", 
101                       session.session_id, election.id])[0]
102   
103     if not @voter
104       # we have not seen this  voter before. something is wrong, try
105       # again
106       redirect_to quickvote_url( :ident => params[:ident] ) 
107       
108     elsif @voter.voted? 
109       # this person has already voted, we try again
110       flash[:notice] = "You have already voted!"
111       redirect_to quickvote_url( :ident => params[:ident] )
112       
113     else
114       # record the ip address for posterity
115       @voter.ipaddress = request.env["REMOTE_ADDR"]
116       @voter.save
117       
118       # save the time the vote was made for statistical use
119       @voter.vote.time = Time.now
120       
121       # toggle the confirmation bit      
122       @voter.vote.confirm!
123      
124       @voter.reload
125       render :action => 'thanks'
126     end
127   end
128  
129   def change
130     voter = QuickVoter.find(:all, :conditions => ["session_id = ?",
131                                                   session.session_id])[0]
132     voter.destroy
133     redirect_to quickvote_url( :ident => params[:ident] )
134   end
135
136   def sort_candidates
137     @vote = Vote.find(params[:id])
138
139     @vote.rankings.each do |ranking|
140       ranking.rank = params['rankings-list'].index(ranking.candidate.id.to_s) + 1
141       ranking.save
142     end
143     render :nothing => true
144   end
145                 
146   def mapvoters
147     @map = GMap.new("map_div_id") 
148     @map.control_init(:large_map => true, :map_type => true) 
149     center = nil
150
151     QuickVote.ident_to_quickvote(params[:id]).voters.each do |voter|
152       next unless voter.ipaddress
153
154       location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
155       next unless location.lng and location.lat
156
157       unless center
158         center = [location.lat, location.lng]
159         @map.center_zoom_init(center, 4)
160       end
161
162       marker = GMarker.new([location.lat,location.lng],
163                            :title => "Voter",
164                            :info_window => (voter.ipaddress or "unknown") \
165                                            + "   " + voter.vote.votestring)
166       @map.overlay_init(marker)
167     end
168   end
169
170   ###############################################################
171   # the following method pertains to displaying the results of a
172   # quickvote
173   ###############################################################
174
175   def results
176     unless @election = QuickVote.ident_to_quickvote(params[:ident])
177       flash[:notice] = "Cannot find quickvote #{params[:ident]}."
178       redirect_to :controller => 'site'
179       return
180     end
181     @results = @election.results
182     @candidates = {}
183     @election.candidates.each {|c| @candidates[c.id] = c}
184   end
185   
186   def my_quickvotes
187     @myqvs = QuickVote.find(:all, :conditions => ["quickuser = ?",
188                                 session.session_id])
189   end
190   
191 end
192

Benjamin Mako Hill || Want to submit a patch?