Class: SurveyorGui::ReportsController

Inherits:
ApplicationController show all
Includes:
ReportPreviewWrapper
Defined in:
app/controllers/surveyor_gui/reports_controller.rb

Instance Method Summary collapse

Methods included from ReportPreviewWrapper

#wrap_in_transaction

Instance Method Details

#generate_report(survey_id, test) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/controllers/surveyor_gui/reports_controller.rb', line 43

def generate_report(survey_id, test)
  questions = Question.joins(:survey_section).where('survey_sections.survey_id = ? and is_comment = ?', survey_id, false)
# multiple_choice_responses = Response.joins(:response_set, :answer).where('survey_id = ? and test_data = ?',survey_id,test).group('responses.question_id','answers.id','answers.text').select('responses.question_id, answers.id, answers.text as text, count(*) as answer_count').order('responses.question_id','answers.id')
 
# multiple_choice_responses = Answer.unscoped.joins(:question=>:survey_section).includes(:responses=>:response_set).where('survey_sections.survey_id=? and (response_sets.test_data=? or response_sets.test_data is null)',survey_id,test).group('answers.question_id','answers.id','answers.text').select('answers.question_id, answers.id, answers.text as text, count(*) as answer_count').order('answers.question_id','answers.id')

# multiple_choice_responses = Answer.unscoped.find(:all,
# :joins => "LEFT JOIN responses ON responses.answer_id = answers.id",
# :select => "answers.question_id, answers.id, answers.text as text, count(answers.*) as answer_count",
# :group => "answers.question_id, answers.id, answers.text",
# :order => "answers.question_id, answers.id")

  multiple_choice_responses = Answer.unscoped.joins("LEFT OUTER JOIN responses ON responses.answer_id = answers.id
LEFT OUTER JOIN response_sets ON response_sets.id = responses.response_set_id").
                                              joins(:question=>:survey_section).
                                              where('survey_sections.survey_id=? and (response_sets.test_data=? or response_sets.test_data is null)',survey_id,test).
                                              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")

  single_choice_responses = Response.joins(:response_set).where('survey_id = ? and test_data = ?',survey_id,test).select('responses.question_id, responses.answer_id,
responses.float_value,
responses.integer_value,
responses.datetime_value, 
responses.string_value')
  @chart = {}
  colors = ['#4572A7', '#AA4643', '#89A54E', '#80699B', '#3D96AE', '#DB843D', '#92A8CD', '#A47D7C', '#B5CA92']
  questions.each do |q|
    if [:grid_one, :grid_any].include? q.question_type_id
        generate_stacked_bar_chart(q, multiple_choice_responses, colors)
    elsif q.question_type_id == :grid_dropdown
      q.question_group.questions.where(is_comment: false).each do |question|
          generate_grid_dropdown_bar_chart(question, multiple_choice_responses, colors)
        end
    elsif q.pick == 'one'
        generate_pie_chart(q, multiple_choice_responses)
    elsif q.pick == 'any'
        generate_bar_chart(q, multiple_choice_responses, colors)
    elsif [:number,:date,:datetime,:time].include? q.question_type_id
        generate_histogram_chart(q, single_choice_responses)
    end
  end
end

#previewObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/surveyor_gui/reports_controller.rb', line 11

def preview
  response_qty = 5 
  user_ids = response_qty.times.map{|i| -1*i}
  @title = "Preview Report for "+response_qty.to_s+" randomized responses"
  @survey = Survey.find(params[:survey_id])
  user_ids.each do |user_id|
    @response_set = ResponseSet.create(survey: @survey, user_id: user_id, test_data: true)
    ReportResponseGenerator.new(@survey).generate_1_result_set(@response_set)
  end
  @response_sets = ResponseSet.where(survey_id: @survey.id, test_data: true).where('user_id in (?)', user_ids)
  @responses = Response.joins(:response_set, :answer).where('user_id in (?) and survey_id = ? and test_data = ? and answers.is_comment = ?',user_ids,params[:survey_id],true, false)
  if (!@survey)
    flash[:notice] = "Survey/Questionnnaire not found."
    redirect_to :back
  end
  generate_report(params[:survey_id], true)
  render :show    
end

#showObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/surveyor_gui/reports_controller.rb', line 30

def show
  @survey = Survey.find(params[:id])
  @response_sets = ResponseSet.where(survey_id: @survey.id, test_data: false)
  @responses = Response.joins(:response_set, :answer).where('survey_id = ? and test_data = ? and answers.is_comment=?',@survey.id,false, false)  
  @title = "Show report for #{@survey.title}"
  if @responses.count > 0
    generate_report(@survey.id, false)
  else
    flash[:error] = "No responses have been collected for this survey"
    redirect_to surveyforms_path 
  end
end