Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

dailybuild

  • Loading branch information...
commit ec8ee82b0faba974f14f24e054dd4e183980afa4 1 parent 8db35a8
chris authored
117 app/controllers/charts_controller.rb
@@ -5,7 +5,7 @@ def index
5 5 if params[:question_id]
6 6 id = params[:question_id]
7 7 @question = Question.find(id)
8   - @answers = get_answers(@question)
  8 + @answers = Hash[get_answers(@question)]
9 9
10 10 end
11 11 end
@@ -22,28 +22,29 @@ def life
22 22 @question = Question.find(params[:original_question])
23 23 @s_options = params[:option]
24 24
25   - @s = Hash.new
26   - @chart_option = ""
27 25
28   - debugger
29   - @s_options.order("id").each do |s_option|
30   - @s["#{s_option.first}_name"] = Option.find_by_option_id(s_option.first).option # for series name
31   - @s["#{s_option.first}_data"] = get_answers(@question,s_option.first) #get etc. "0302" option as series
  26 + @chart_option = []
  27 +
  28 +
  29 + @s_options.each do |s_option|
  30 + @s = Hash.new
  31 + @s["name"] = Option.find_by_option_id(s_option.first).option # for series name
  32 + @s["data"] = get_answers(@question,s_option.first).to_a #get etc. "0302" option as series
  33 +
  34 + @chart_option << @s
32 35
33   - @chart_option += "{name:'#{Option.find_by_option_id(s_option.first).option}',data:[]},"
34 36 end
35 37
36   - #@s:{"0302_name"=>"2 个", "0302_data"=>{"1001"=>395, ..."1002"=>581}, "0304_name"=>"4个", "0304_data"=>{"1001"=>395, ..."1002"=>581}}
  38 + #@s:{"name"=>"2 个", "data"=>{"1001"=>395, ..."1002"=>581}}
  39 + #@chart_option:[{"name"=>"option1", "data"=>[1, 2, 3]}, {"name"=>"option2", "data"=>[1, 2, 3]}]
37 40
38 41
39   -
40   -
41 42 elsif params[:question_id]
42 43 @has_series = false
43 44 #show total
44 45 id = params[:question_id]
45 46 @question = Question.find(id)
46   - @answers = get_answers(@question)
  47 + @answers = Hash[get_answers(@question)]
47 48 else
48 49 @has_series = nil
49 50 end
@@ -56,11 +57,25 @@ def get_options
56 57 end
57 58
58 59
59   - def get_answers(question,s_option_id=nil)
60   - answers = Hash.new
  60 + def get_total_answer_with_special_options(question,s_option_id)
  61 + #initialize
  62 + table_question = find_table_question(question)
  63 + table_option = find_table_option(s_option_id)
  64 + total_answer_option_count = 0
  65 +
  66 +
  67 + question.options.each do |option|
  68 + answers_with_option = get_option_count(table_question,table_option,option,s_option_id)
  69 +
  70 + #means: T_1.joins("inner join t_2s on t_1s.o_id = t_2s.o_id").where("t_1s.o_0101 = ? and t_2s.o_0802 = ?",true,true).count
  71 + total_answer_option_count += answers_with_option
  72 + end
  73 +
  74 + return total_answer_option_count
  75 + end
  76 +
  77 + def find_table_option(s_option_id)
61 78
62   - #if has another options to add series
63   - if s_option_id
64 79 table_option = ""
65 80 if "01,02,03,04,05,06".include? s_option_id[0,2]
66 81 table_option = "T_1"
@@ -71,31 +86,65 @@ def get_answers(question,s_option_id=nil)
71 86 elsif "28,29,30,31,32,33,34,35,36".include? s_option_id[0,2]
72 87 table_option = "T_4"
73 88 end
74   - end
75   -
76   -
  89 +
  90 + return table_option
  91 + end
  92 +
  93 + def find_table_question(question)
  94 + table_question = ""
77 95 case question.id
78 96 when 1..6
79   - question.options.each do |option|
80   - answers[option.option_id] = T_1.where("o_#{option.option_id} = ?",true).count
81   - end
82   -
  97 + table_question = "T_1"
83 98 when 7..17
84   - question.options.each do |option|
85   - answers[option.option_id] = T_2.where("o_#{option.option_id} = ?",true).count
86   - end
  99 + table_question = "T_2"
87 100 when 18..26
88   - question.options.each do |option|
89   - answers[option.option_id] = T_3.where("o_#{option.option_id} = ?",true).count
90   - end
  101 + table_question = "T_3"
91 102 when 27..36
92   - question.options.each do |option|
93   - answers[option.option_id] = T_4.where("o_#{option.option_id} = ?",true).count
94   - end
  103 + table_question = "T_4"
95 104 end
  105 + return table_question
  106 + end
  107 +
  108 + def get_answers(question,s_option_id=nil)
  109 + answers = Hash.new
96 110
97   -
  111 +
  112 + #if has another options to add series
  113 + table_option = find_table_option(s_option_id) if s_option_id
  114 +
  115 +
  116 + #question.id means which table the question's options are
  117 + #s_option_id means which table the additional series options are
  118 + #so we should query the data joins 2 different tables or 1 same table.
  119 + table_question = find_table_question(question)
  120 +
  121 + answer_option_count = get_total_answer_with_special_options(question,s_option_id) if s_option_id #initial the count of total answers with special addtional option
  122 +
  123 + question.options.each do |option|
  124 +
  125 + if s_option_id
  126 + answers_with_option = get_option_count(table_question,table_option,option,s_option_id) #get the option count
  127 +
  128 + answers[option.option_id] = (answers_with_option.to_f/answer_option_count.to_f*100).round(2) #got percent
  129 + else
  130 +
  131 + answers[option.option_id] = eval "#{table_question}.where('o_#{option.option_id} = ?',true).count"
  132 + end
  133 + end
98 134
99   - return answers
  135 + return answers.to_a
  136 + end
  137 +
  138 +
  139 + def get_option_count(table_question,table_option,option,s_option_id)
  140 + if table_question == table_option
  141 + answers_with_option = eval "#{table_question}.where('#{table_question.downcase.pluralize}.o_#{option.option_id} = ? and #{table_option.downcase.pluralize}.o_#{s_option_id} = ?',true,true).count" #count of this option with addtional option
  142 +
  143 + else
  144 + answers_with_option = eval "#{table_question}.joins('inner join #{table_option.downcase.pluralize} on #{table_question.downcase.pluralize}.o_id = #{table_option.downcase.pluralize}.o_id').where('#{table_question.downcase.pluralize}.o_#{option.option_id} = ? and #{table_option.downcase.pluralize}.o_#{s_option_id} = ?',true,true).count"
  145 +
  146 + #means: T_1.joins("inner join t_2s on t_1s.o_id = t_2s.o_id").where("t_1s.o_0101 = ? and t_2s.o_0802 = ?",true,true).count
  147 + end
  148 + return answers_with_option
100 149 end
101 150 end
16 app/views/charts/life.html.erb
@@ -113,13 +113,13 @@
113 113 }
114 114 },
115 115
116   - series: [
117   - <% if @has_series %>
118   - <%# @s:{"0302_name"=>"2 个", "0302_data"=>{"1001"=>395, ..."1002"=>581}, "0304_name"=>"4个", "0304_data"=>{"1001"=>395, ..."1002"=>581}}
119   -
120   - %>
  116 + series:
  117 + <% if @chart_option %>
  118 + <%= raw @chart_option.to_json
  119 + %>
  120 +
121 121 <% else %>
122   - {
  122 + [{
123 123 name: 'Total',
124 124 <%
125 125 data = []
@@ -128,9 +128,9 @@
128 128 end
129 129 %>
130 130 data: <%= raw data.to_a %>
131   - }
  131 + }]
132 132 <% end %>
133   - ]
  133 +
134 134
135 135 });
136 136 });

0 comments on commit ec8ee82

Please sign in to comment.
Something went wrong with that request. Please try again.