Commit e8499507622fb172f4398d818142128cb030aa1d

Authored by Dhruv Kapadia
1 parent 3b9ccf62

Changes to support new maps, scatter plot on AOI

app/controllers/questions_controller.rb
@@ -66,8 +66,9 @@ class QuestionsController < InheritedResources::Base @@ -66,8 +66,9 @@ class QuestionsController < InheritedResources::Base
66 66
67 # we sometimes request a question when no prompt is displayed 67 # we sometimes request a question when no prompt is displayed
68 # TODO It would be a good idea to find these places and treat them like barebones 68 # TODO It would be a good idea to find these places and treat them like barebones
69 - if visitor_identifier != "" 69 + if !visitor_identifier.blank?
70 @a = current_user.record_appearance(visitor_identifier, @p) 70 @a = current_user.record_appearance(visitor_identifier, @p)
  71 + logger.info("creating appearance!")
71 else 72 else
72 @a = nil 73 @a = nil
73 end 74 end
@@ -150,19 +151,52 @@ class QuestionsController < InheritedResources::Base @@ -150,19 +151,52 @@ class QuestionsController < InheritedResources::Base
150 # export_format = params[:export_format] #CSV always now, could expand to xml later 151 # export_format = params[:export_format] #CSV always now, could expand to xml later
151 end 152 end
152 153
153 - def num_votes_by_visitor_id 154 + def object_info_by_visitor_id
  155 +
  156 + object_type = params[:object_type]
154 @question = current_user.questions.find(params[:id]) 157 @question = current_user.questions.find(params[:id])
155 158
156 - votes_by_visitor_id= Vote.all(:select => 'visitors.identifier as thevi, count(*) as the_votes_count',  
157 - :joins => :voter,  
158 - :conditions => {:question_id => @question.id },  
159 - :group => "voter_id")  
160 -  
161 visitor_id_hash = {} 159 visitor_id_hash = {}
  160 + if object_type == "votes"
  161 + votes_by_visitor_id= Vote.all(:select => 'visitors.identifier as thevi, count(*) as the_votes_count',
  162 + :joins => :voter,
  163 + :conditions => {:question_id => @question.id },
  164 + :group => "voter_id")
162 165
163 - votes_by_visitor_id.each do |visitor|  
164 - visitor_id_hash[visitor.thevi] = visitor.the_votes_count  
165 - visitor_id_hash[visitor.thevi] = visitor.the_votes_count 166 +
  167 + votes_by_visitor_id.each do |visitor|
  168 + visitor_id_hash[visitor.thevi] = visitor.the_votes_count
  169 + end
  170 + elsif object_type == "uploaded_ideas"
  171 +
  172 + uploaded_ideas_by_visitor_id = @question.choices.find(:all, :select => 'creator_id, count(*) as ideas_count',
  173 + :joins => [:item],
  174 + :conditions => "items.creator_id != #{@question.creator_id}",
  175 + :group => 'creator_id')
  176 +
  177 + uploaded_ideas_by_visitor_id.each do |visitor|
  178 + v = Visitor.find(visitor.creator_id, :select => 'identifier')
  179 +
  180 + logger.info(v.identifier)
  181 +
  182 + visitor_id_hash[v.identifier] = visitor.ideas_count
  183 + end
  184 +
  185 + logger.info(visitor_id_hash.inspect)
  186 + elsif object_type == "bounces"
  187 +
  188 + possible_bounces = @question.appearances.count(:group => :voter_id, :having => 'count_all = 1')
  189 + possible_bounce_ids = possible_bounces.inject([]){|list, (k,v)| list << k}
  190 +
  191 + voted_at_least_once = @question.votes.find(:all, :select => :voter_id, :conditions => {:voter_id => possible_bounce_ids})
  192 + voted_at_least_once_ids = voted_at_least_once.inject([]){|list, v| list << v.voter_id}
  193 +
  194 + bounces = possible_bounce_ids - voted_at_least_once_ids
  195 +
  196 + bounces.each do |visitor_id|
  197 + v = Visitor.find(visitor_id, :select => 'identifier')
  198 + visitor_id_hash[v.identifier] = 1
  199 + end
166 end 200 end
167 respond_to do |format| 201 respond_to do |format|
168 format.xml{ render :xml => visitor_id_hash.to_xml and return} 202 format.xml{ render :xml => visitor_id_hash.to_xml and return}
app/models/question.rb
@@ -18,6 +18,7 @@ class Question &lt; ActiveRecord::Base @@ -18,6 +18,7 @@ class Question &lt; ActiveRecord::Base
18 end 18 end
19 has_many :votes 19 has_many :votes
20 has_many :densities 20 has_many :densities
  21 + has_many :appearances
21 22
22 #comment out to run bt import script! 23 #comment out to run bt import script!
23 after_save :ensure_at_least_two_choices 24 after_save :ensure_at_least_two_choices
config/routes.rb
@@ -3,7 +3,7 @@ ActionController::Routing::Routes.draw do |map| @@ -3,7 +3,7 @@ ActionController::Routing::Routes.draw do |map|
3 map.resources :densities 3 map.resources :densities
4 map.resources :visitors, :collection => {:votes_by_session_ids => :post} 4 map.resources :visitors, :collection => {:votes_by_session_ids => :post}
5 map.resources :questions, :member => { :object_info_totals_by_date => :get, 5 map.resources :questions, :member => { :object_info_totals_by_date => :get,
6 - :num_votes_by_visitor_id => :get, 6 + :object_info_by_visitor_id => :get,
7 :export => :post, 7 :export => :post,
8 :set_autoactivate_ideas_from_abroad => :put, 8 :set_autoactivate_ideas_from_abroad => :put,
9 :activate => :put, 9 :activate => :put,