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

Benjamin Mako Hill || Want to submit a patch?