Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

dailybuild

  • Loading branch information...
commit ec8ee82b0faba974f14f24e054dd4e183980afa4 1 parent 8db35a8
@shanghaichris authored
View
117 app/controllers/charts_controller.rb
@@ -5,7 +5,7 @@ def index
if params[:question_id]
id = params[:question_id]
@question = Question.find(id)
- @answers = get_answers(@question)
+ @answers = Hash[get_answers(@question)]
end
end
@@ -22,28 +22,29 @@ def life
@question = Question.find(params[:original_question])
@s_options = params[:option]
- @s = Hash.new
- @chart_option = ""
- debugger
- @s_options.order("id").each do |s_option|
- @s["#{s_option.first}_name"] = Option.find_by_option_id(s_option.first).option # for series name
- @s["#{s_option.first}_data"] = get_answers(@question,s_option.first) #get etc. "0302" option as series
+ @chart_option = []
+
+
+ @s_options.each do |s_option|
+ @s = Hash.new
+ @s["name"] = Option.find_by_option_id(s_option.first).option # for series name
+ @s["data"] = get_answers(@question,s_option.first).to_a #get etc. "0302" option as series
+
+ @chart_option << @s
- @chart_option += "{name:'#{Option.find_by_option_id(s_option.first).option}',data:[]},"
end
- #@s:{"0302_name"=>"2 个", "0302_data"=>{"1001"=>395, ..."1002"=>581}, "0304_name"=>"4个", "0304_data"=>{"1001"=>395, ..."1002"=>581}}
+ #@s:{"name"=>"2 个", "data"=>{"1001"=>395, ..."1002"=>581}}
+ #@chart_option:[{"name"=>"option1", "data"=>[1, 2, 3]}, {"name"=>"option2", "data"=>[1, 2, 3]}]
-
-
elsif params[:question_id]
@has_series = false
#show total
id = params[:question_id]
@question = Question.find(id)
- @answers = get_answers(@question)
+ @answers = Hash[get_answers(@question)]
else
@has_series = nil
end
@@ -56,11 +57,25 @@ def get_options
end
- def get_answers(question,s_option_id=nil)
- answers = Hash.new
+ def get_total_answer_with_special_options(question,s_option_id)
+ #initialize
+ table_question = find_table_question(question)
+ table_option = find_table_option(s_option_id)
+ total_answer_option_count = 0
+
+
+ question.options.each do |option|
+ answers_with_option = get_option_count(table_question,table_option,option,s_option_id)
+
+ #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
+ total_answer_option_count += answers_with_option
+ end
+
+ return total_answer_option_count
+ end
+
+ def find_table_option(s_option_id)
- #if has another options to add series
- if s_option_id
table_option = ""
if "01,02,03,04,05,06".include? s_option_id[0,2]
table_option = "T_1"
@@ -71,31 +86,65 @@ def get_answers(question,s_option_id=nil)
elsif "28,29,30,31,32,33,34,35,36".include? s_option_id[0,2]
table_option = "T_4"
end
- end
-
-
+
+ return table_option
+ end
+
+ def find_table_question(question)
+ table_question = ""
case question.id
when 1..6
- question.options.each do |option|
- answers[option.option_id] = T_1.where("o_#{option.option_id} = ?",true).count
- end
-
+ table_question = "T_1"
when 7..17
- question.options.each do |option|
- answers[option.option_id] = T_2.where("o_#{option.option_id} = ?",true).count
- end
+ table_question = "T_2"
when 18..26
- question.options.each do |option|
- answers[option.option_id] = T_3.where("o_#{option.option_id} = ?",true).count
- end
+ table_question = "T_3"
when 27..36
- question.options.each do |option|
- answers[option.option_id] = T_4.where("o_#{option.option_id} = ?",true).count
- end
+ table_question = "T_4"
end
+ return table_question
+ end
+
+ def get_answers(question,s_option_id=nil)
+ answers = Hash.new
-
+
+ #if has another options to add series
+ table_option = find_table_option(s_option_id) if s_option_id
+
+
+ #question.id means which table the question's options are
+ #s_option_id means which table the additional series options are
+ #so we should query the data joins 2 different tables or 1 same table.
+ table_question = find_table_question(question)
+
+ 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
+
+ question.options.each do |option|
+
+ if s_option_id
+ answers_with_option = get_option_count(table_question,table_option,option,s_option_id) #get the option count
+
+ answers[option.option_id] = (answers_with_option.to_f/answer_option_count.to_f*100).round(2) #got percent
+ else
+
+ answers[option.option_id] = eval "#{table_question}.where('o_#{option.option_id} = ?',true).count"
+ end
+ end
- return answers
+ return answers.to_a
+ end
+
+
+ def get_option_count(table_question,table_option,option,s_option_id)
+ if table_question == table_option
+ 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
+
+ else
+ 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"
+
+ #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
+ end
+ return answers_with_option
end
end
View
16 app/views/charts/life.html.erb
@@ -113,13 +113,13 @@
}
},
- series: [
- <% if @has_series %>
- <%# @s:{"0302_name"=>"2 个", "0302_data"=>{"1001"=>395, ..."1002"=>581}, "0304_name"=>"4个", "0304_data"=>{"1001"=>395, ..."1002"=>581}}
-
- %>
+ series:
+ <% if @chart_option %>
+ <%= raw @chart_option.to_json
+ %>
+
<% else %>
- {
+ [{
name: 'Total',
<%
data = []
@@ -128,9 +128,9 @@
end
%>
data: <%= raw data.to_a %>
- }
+ }]
<% end %>
- ]
+
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.