app/controllers/surveyor_gui/reports_controller.rb
# the count for all responses, for all questions and answers
the count for all responses, for all questions and answers
the count for all responses for all questions and answers
def all_responses
    @chart = {}
    @show_section_titles = false 
    @sections = @survey.survey_sections
    @questions = Question.includes(:answers).where(survey_section_id: @sections.map(&:id), is_comment: false)
    @answers = Answer.where(question_id: @questions.map(&:id), is_comment: false)
    @response_sets = ResponseSet.where(survey: @survey)
    @responses = Response.where(answer_id: @answers.map(&:id)).order(:answer_id)
    response_counts = Response.where(response_set_id: @response_sets.map(&:id)).group(:question_id).order(:question_id).group(:answer_id).count(:id)
    @response_counts_by_q = response_counts.group_by { |k, _v| k[0] }
     response_counts_by_q.each do | k_question, v_answers |
      "QUESTION ID (key[0]): {k_question[0]}  answer ID (key[1]) = {k_question[1]}, count = {v_answers}"
     end
     but we can re-organize them so they are easier to work with:
    @question_answers_counts = {}
    @response_counts_by_q.each do | k_qid, v_ka_count |
      v_ka_count.each do | each_v |
        a_id = each_v.first[1]
        count = each_v.last
        @question_answers_counts.fetch(k_qid){ |ques_id| @question_answers_counts[ques_id] = {}}
        @question_answers_counts[k_qid].fetch(a_id){ |ans_id| @question_answers_counts[k_qid][ans_id] = count  }
      end
    end
     Now they are in this handy form:
      question_id => { answer_id => count}
     [ {305=>{1097=>435, 1098=>109, 1099=>56}},
       {306=>{1104=>35, 1102=>188, 1100=>53, 1103=>154, 1101=>166}},
       {307=>{1108=>55, 1106=>78, 1107=>95, 1105=>368}},
        {308=>{1110=>57, 1109=>371, 1111=>165}},
        {309=>{1125=>65, 1124=>130, 1123=>397}},
        {310=>{1128=>366, 1130=>75, 1127=>170, 1129=>236, 1126=>51}},
        {311=>{1133=>146, 1132=>246, 1131=>199}},
        {312=>
             {1121=>7,
              1113=>3,
              1116=>4,
              1120=>7,
              1117=>2,
              1122=>11,
              1119=>9,
              1114=>2,
              1115=>1,
              1112=>7,
              1118=>4}
         }
     ]
    @title = t('surveyor_gui.responses.all_responses_report.title')
     The following is just an ActiveRecord_Relation. it doesn't hit the db for data
     It will hit the db once to get the response count for ALL of the answers for ALL of these questions.
      (Then the info will be cached)
    all_answers = Answer.unscoped
                   .joins("LEFT OUTER JOIN responses ON responses.answer_id = answers.id")
                   .where(question_id: @questions.map(&:id))
                   .select("answers.question_id, answers.id, answers.text as text, answers.is_comment, count(responses.id) as answer_count")
                   .group("answers.question_id, answers.id, answers.text, answers.is_comment")
                   .order("answers.question_id, answers.id")
    set_chart_info(@questions, all_answers)
  end
def all_responses @chart = { } @show_section_titles = false @sections = @survey . survey_sections @questions = Question . includes ( :answers ) . where ( survey_section_id : @sections . map ( & :id ) , is_comment : false ) @answers = Answer . where ( question_id : @questions . map ( & :id ) , is_comment : false ) @response_sets = ResponseSet . where ( survey : @survey ) @responses = Response . where ( answer_id : @answers . map ( & :id ) ) . order ( :answer_id ) response_counts = Response . where ( response_set_id : @response_sets . map ( & :id ) ) . group ( :question_id ) . order ( :question_id ) . group ( :answer_id ) . count ( :id ) @response_counts_by_q = response_counts . group_by { | k , _v | k [ 0 ] } @question_answers_counts = { } @response_counts_by_q . each do | k_qid , v_ka_count | v_ka_count . each do | each_v | a_id = each_v . first [ 1 ] count = each_v . last @question_answers_counts . fetch ( k_qid ) { | ques_id | @question_answers_counts [ ques_id ] = { } } @question_answers_counts [ k_qid ] . fetch ( a_id ) { | ans_id | @question_answers_counts [ k_qid ] [ ans_id ] = count } end end @title = t ( 'surveyor_gui.responses.all_responses_report.title' ) all_answers = Answer . unscoped . joins ( "LEFT OUTER JOIN responses ON responses.answer_id = answers.id" ) . where ( question_id : @questions . map ( & :id ) ) . select ( "answers.question_id, answers.id, answers.text as text, answers.is_comment, count(responses.id) as answer_count" ) . group ( "answers.question_id, answers.id, answers.text, answers.is_comment" ) . order ( "answers.question_id, answers.id" ) set_chart_info ( @questions , all_answers ) end
the count for all responses, for all questions and answers
the count for all responses for all questions and answers
# FIXME - need to config this for the survey # this creates a nested Hash like this: #   [question_id, answer_id] => count # { #   [305, 1097]=>435, #   [305, 1098]=>109, #   [305, 1099]=>56, #   [306, 1104]=>35, #   [306, 1102]=>188, #   [306, 1100]=>53, #   [306, 1103]=>154,  ... # And we can access them in that form like this: # # response_counts_by_q.each do | k_question, v_answers | #  "QUESTION ID (key[0]): #{k_question[0]}  answer ID (key[1]) = #{k_question[1]}, count = #{v_answers}" # end # but we can re-organize them so they are easier to work with: # Now they are in this handy form: #  question_id => { answer_id => count} # # [ {305=>{1097=>435, 1098=>109, 1099=>56}}, #   {306=>{1104=>35, 1102=>188, 1100=>53, 1103=>154, 1101=>166}}, #   {307=>{1108=>55, 1106=>78, 1107=>95, 1105=>368}}, #    {308=>{1110=>57, 1109=>371, 1111=>165}}, #    {309=>{1125=>65, 1124=>130, 1123=>397}}, #    {310=>{1128=>366, 1130=>75, 1127=>170, 1129=>236, 1126=>51}}, #    {311=>{1133=>146, 1132=>246, 1131=>199}}, #    {312=> #         {1121=>7, #          1113=>3, #          1116=>4, #          1120=>7, #          1117=>2, #          1122=>11, #          1119=>9, #          1114=>2, #          1115=>1, #          1112=>7, #          1118=>4} #     } # ] # The following is just an ActiveRecord_Relation. it doesn't hit the db for data # It will hit the db once to get the response count for ALL of the answers for ALL of these questions. #  (Then the info will be cached)