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

Benjamin Mako Hill || Want to submit a patch?