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

Benjamin Mako Hill || Want to submit a patch?