From fff49b4783dcf618384260c1648e3f9bc7582864 Mon Sep 17 00:00:00 2001 From: Dhruv Kapadia Date: Tue, 9 Mar 2010 15:42:20 -0500 Subject: [PATCH] Changes to support viewing site wide data --- app/controllers/questions_controller.rb | 54 +++++++++++++++++++++++++++++++++++++++++++++++------- config/routes.rb | 10 +++++++++- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index 883c172..b9dcb2f 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -102,25 +102,42 @@ class QuestionsController < InheritedResources::Base def num_votes_by_visitor_id @question = current_user.questions.find(params[:id]) - hash = Vote.count(:conditions => "question_id = #{@question.id}", :group => "voter_id") + + votes_by_visitor_id= Vote.all(:select => 'visitors.identifier as thevi, count(*) as the_votes_count', + :joins => :voter, + :conditions => {:question_id => @question.id }, + :group => "voter_id") + visitor_id_hash = {} - hash.each do |visitor_id, num_votes| - visitor = Visitor.find(visitor_id) - visitor_id_hash[visitor.identifier] = num_votes + + votes_by_visitor_id.each do |visitor| + visitor_id_hash[visitor.thevi] = visitor.the_votes_count + visitor_id_hash[visitor.thevi] = visitor.the_votes_count end respond_to do |format| format.xml{ render :xml => visitor_id_hash.to_xml and return} end end - def object_info_totals_by_date - authenticate + def all_num_votes_by_visitor_id + votes_by_visitor_id= Vote.all(:select => 'visitors.identifier as thevi, count(*) as the_votes_count', + :joins => :voter, + :group => "voter_id") + visitor_id_hash = {} + votes_by_visitor_id.each do |visitor| + visitor_id_hash[visitor.thevi] = visitor.the_votes_count + visitor_id_hash[visitor.thevi] = visitor.the_votes_count + end + respond_to do |format| + format.xml{ render :xml => visitor_id_hash.to_xml and return} + end + end + def object_info_totals_by_date object_type = params[:object_type] @question = current_user.questions.find(params[:id]) if object_type == 'votes' - # eventually allow for users to specify type of export through params[:type] hash = Vote.count(:conditions => "question_id = #{@question.id}", :group => "date(created_at)") elsif object_type == 'user_submitted_ideas' hash = Choice.count(:include => 'item', @@ -140,6 +157,29 @@ class QuestionsController < InheritedResources::Base format.xml { render :xml => hash.to_xml and return} end end + + def all_object_info_totals_by_date + object_type = params[:object_type] + + if object_type == 'votes' + hash = Vote.count(:group => "date(created_at)") + elsif object_type == 'user_submitted_ideas' + hash = Choice.count(:include => ['item', 'question'], + :conditions => "items.creator_id <> questions.creator_id", + :group => "date(choices.created_at)") + elsif object_type == 'user_sessions' + result = Vote.find(:all, :select => 'date(created_at) as date, voter_id, count(*) as vote_count', + :group => 'date(created_at), voter_id') + hash = Hash.new(0) + result.each do |r| + hash[r.date]+=1 + end + end + + respond_to do |format| + format.xml { render :xml => hash.to_xml and return} + end + end protected def export_votes diff --git a/config/routes.rb b/config/routes.rb index 38a6afa..fa735ad 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,15 @@ ActionController::Routing::Routes.draw do |map| #map.resources :clicks map.resources :visitors, :collection => {:votes_by_session_ids => :post} - map.resources :questions, :member => { :object_info_totals_by_date => :get, :num_votes_by_visitor_id => :get, :export => :post, :set_autoactivate_ideas_from_abroad => :put, :activate => :put, :suspend => :put}, :collection => {:recent_votes_by_question_id => :get} do |question| + map.resources :questions, :member => { :object_info_totals_by_date => :get, + :num_votes_by_visitor_id => :get, + :export => :post, + :set_autoactivate_ideas_from_abroad => :put, + :activate => :put, + :suspend => :put}, + :collection => {:all_num_votes_by_visitor_id => :get, + :all_object_info_totals_by_date => :get, + :recent_votes_by_question_id => :get} do |question| question.resources :items question.resources :prompts, :member => {:vote_left => :post, :vote_right => :post, :skip => :post, :vote => :post}, :collection => {:single => :get, :index => :get} -- libgit2 0.21.2