Merge jdong
[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
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
22         show_advanced = true
23       end
24     end
25
26     show_advanced ||= false
27
28     if params[:quickvote]
29
30       # store the candidate grabbed through ajax and stored in flash
31       @quickvote.candidate_names = flash[:candidate_names]
32       @quickvote.description=@quickvote.description
33
34       #record who created the quickvote so that person can monitor it easily
35       @quickvote.quickuser = session.session_id
36
37       #Give registered users additional QuickVote functionality 
38       @quickvote.user_id = session[:user][:id] if session[:user]
39
40       # try to save, if it fails, show the page again (the flash should
41       # still be intact
42       if @quickvote.save
43         @quickvote = @quickvote.reload
44         # blank sidebar and show the success page
45         @sidebar_content = ''
46         render :action => 'success'
47       else
48         # render the sidebar
49         @sidebar_content = render_to_string(:partial => 'create_sidebar',
50           :locals => {:show_advanced => show_advanced})
51         flash.keep(:candidate_names)
52       end 
53
54     else
55       # if we don't have a quickvote param, it means that the person
56       # here has not been hitting this page and we can clear any
57       # candidate_names list in the flash
58       flash.delete(:candidate_names) if flash.has_key?(:candidate_names)
59       @quickvote = QuickVote.new
60       @sidebar_content = render_to_string(:partial => 'create_sidebar',
61         :locals => {:show_advanced => show_advanced})
62     end
63
64   end
65
66   def add_candidate
67     candidate_name = params[:ajax][:newcandidate]
68     unless candidate_name.strip.empty?
69       if flash.has_key?(:candidate_names) \
70         and flash[:candidate_names].instance_of?(Array) 
71         unless flash[:candidate_names].index(candidate_name)
72           flash[:candidate_names] << candidate_name
73         end
74      else
75        flash[:candidate_names] = [ candidate_name ]
76       end
77     end
78     flash.keep(:candidate_names)
79     render_partial 'candidate_list'
80   end
81  
82   #############################################################
83   # the following methods pertain to *voting* in the quickvotes
84   #############################################################
85
86   def index
87     @election = QuickVote.ident_to_quickvote(params[:ident])
88     # if the person has specified an election, we show them the voting
89     # page. otherwise, we redirect back to main the page
90     if @election
91       # look to see that the voter has been created and has voted in
92       # this election, and has confirmed their vote
93       @voter = QuickVoter.find(:all,
94         :conditions => ["session_id = ? and election_id = ?",
95                         session.session_id, @election.id])[0]
96
97       # if the voter has not voted we destroy them
98       if @voter and not @voter.voted?
99         @voter.destroy
100         @voter = nil
101       end
102
103       # if the voter does not exist or has has been destroyed, lets
104       # create a new one
105       unless @voter
106         # create a new voter and populate it
107         @voter = QuickVoter.new
108         @voter.election = @election
109         @voter.session_id = session.session_id
110               
111         # create new vote and make it the defaulted sorted list
112         @voter.vote = Vote.new
113               @voter.save
114               @voter.vote.set_defaults!
115               @voter.reload
116       end
117     else
118       redirect_to :controller => 'site'
119     end
120   end
121
122   def confirm
123     # we need the election to verify that we have the right voter
124     election = QuickVote.ident_to_quickvote(params[:ident])
125
126     # find out who the voter is for this election
127     @voter = QuickVoter.find(:all,
128       :conditions => ["session_id = ? and election_id = ?", 
129                       session.session_id, election.id])[0]
130   
131     if not @voter
132       # we have not seen this  voter before. something is wrong, try
133       # again
134       redirect_to quickvote_url( :ident => params[:ident] ) 
135       
136     elsif @voter.voted? 
137       # this person has already voted, we try again
138       flash[:notice] = "You have already voted!"
139       redirect_to quickvote_url( :ident => params[:ident] )
140       
141     else
142       # record the ip address for posterity
143       @voter.ipaddress = request.env["REMOTE_ADDR"]
144       @voter.save
145       
146       # save the time the vote was made for statistical use
147       @voter.vote.time = Time.now
148       
149       # toggle the confirmation bit      
150       @voter.vote.confirm!
151      
152       @voter.reload
153       render :action => 'thanks'
154     end
155   end
156  
157   def change
158     voter = QuickVoter.find(:all, :conditions => ["session_id = ?",
159                                                   session.session_id])[0]
160     voter.destroy
161     redirect_to quickvote_url( :ident => params[:ident] )
162   end
163                 
164   def list_voters
165     @map = GMap.new("map_div_id") 
166     @map.control_init(:large_map => true, :map_type => true) 
167     center = nil
168     @election=QuickVote.ident_to_quickvote(params[:id])
169     @election.voters.each do |voter|
170       next unless voter.ipaddress
171       location=nil
172       if Cache and location=Cache.get("GEO:#{voter.ipaddress}")
173       elsif Cache
174         location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
175         Cache.set "GEO:#{voter.ipaddress}", location
176       else
177         location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
178       end
179       next unless location.lng and location.lat
180
181       unless center
182         center = [location.lat, location.lng]
183         @map.center_zoom_init(center, 4)
184       end
185
186       marker = GMarker.new([location.lat,location.lng],
187                            :title => "Voter",
188                            :info_window => (voter.ipaddress or "unknown") \
189                                            + "   " + voter.vote.votestring)
190       @map.overlay_init(marker)
191     end
192   end
193
194   ###############################################################
195   # the following method pertains to displaying the results of a
196   # quickvote
197   ###############################################################
198
199   def results
200     unless @election = QuickVote.ident_to_quickvote(params[:ident])
201       flash[:notice] = "Cannot find quickvote #{params[:ident]}."
202       redirect_to :controller => 'site'
203       return
204     end
205     if @election.viewable == 0 && @election.active == 1
206       render :action => 'not_viewable' and return
207     end
208     @results = @election.results
209     @candidates = {}
210     @election.candidates.each {|c| @candidates[c.id] = c}
211     @names = @election.names_by_id
212     @sidebar_content = render_to_string :partial => 'results_sidebar'
213   end
214   
215   def my_quickvotes
216     @myqvs = QuickVote.find(:all, :conditions => ["quickuser = ?",
217                                 session.session_id])
218   end
219   
220 end
221

Benjamin Mako Hill || Want to submit a patch?