Merge head
[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 sort_candidates
165     @vote = Vote.find(params[:id])
166
167     @vote.rankings.each do |ranking|
168       ranking.rank = params['rankings-list'].index(ranking.candidate.id.to_s) + 1
169       ranking.save
170     end
171     render :nothing => true
172   end
173                 
174   def mapvoters
175     @map = GMap.new("map_div_id") 
176     @map.control_init(:large_map => true, :map_type => true) 
177     center = nil
178
179     QuickVote.ident_to_quickvote(params[:id]).voters.each do |voter|
180       next unless voter.ipaddress
181
182       location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
183       next unless location.lng and location.lat
184
185       unless center
186         center = [location.lat, location.lng]
187         @map.center_zoom_init(center, 4)
188       end
189
190       marker = GMarker.new([location.lat,location.lng],
191                            :title => "Voter",
192                            :info_window => (voter.ipaddress or "unknown") \
193                                            + "   " + voter.vote.votestring)
194       @map.overlay_init(marker)
195     end
196   end
197
198   ###############################################################
199   # the following method pertains to displaying the results of a
200   # quickvote
201   ###############################################################
202
203   def results
204     unless @election = QuickVote.ident_to_quickvote(params[:ident])
205       flash[:notice] = "Cannot find quickvote #{params[:ident]}."
206       redirect_to :controller => 'site'
207       return
208     end
209     if @election.viewable == 0 && @election.active == 1
210       render :action => 'not_viewable' and return
211     end
212     @results = @election.results
213     @candidates = {}
214     @election.candidates.each {|c| @candidates[c.id] = c}
215   end
216   
217   def my_quickvotes
218     @myqvs = QuickVote.find(:all, :conditions => ["quickuser = ?",
219                                 session.session_id])
220   end
221   
222 end
223

Benjamin Mako Hill || Want to submit a patch?