Skip to content

Commit

Permalink
dailybuild
Browse files Browse the repository at this point in the history
  • Loading branch information
shanghaichris committed Jul 15, 2012
1 parent 8db35a8 commit ec8ee82
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 42 deletions.
117 changes: 83 additions & 34 deletions app/controllers/charts_controller.rb
Expand Up @@ -5,7 +5,7 @@ def index
if params[:question_id] if params[:question_id]
id = params[:question_id] id = params[:question_id]
@question = Question.find(id) @question = Question.find(id)
@answers = get_answers(@question) @answers = Hash[get_answers(@question)]


end end
end end
Expand All @@ -22,28 +22,29 @@ def life
@question = Question.find(params[:original_question]) @question = Question.find(params[:original_question])
@s_options = params[:option] @s_options = params[:option]


@s = Hash.new
@chart_option = ""


debugger @chart_option = []
@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 @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 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] elsif params[:question_id]
@has_series = false @has_series = false
#show total #show total
id = params[:question_id] id = params[:question_id]
@question = Question.find(id) @question = Question.find(id)
@answers = get_answers(@question) @answers = Hash[get_answers(@question)]
else else
@has_series = nil @has_series = nil
end end
Expand All @@ -56,11 +57,25 @@ def get_options
end end




def get_answers(question,s_option_id=nil) def get_total_answer_with_special_options(question,s_option_id)
answers = Hash.new #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 = "" table_option = ""
if "01,02,03,04,05,06".include? s_option_id[0,2] if "01,02,03,04,05,06".include? s_option_id[0,2]
table_option = "T_1" table_option = "T_1"
Expand All @@ -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] elsif "28,29,30,31,32,33,34,35,36".include? s_option_id[0,2]
table_option = "T_4" table_option = "T_4"
end end
end

return table_option

end

def find_table_question(question)
table_question = ""
case question.id case question.id
when 1..6 when 1..6
question.options.each do |option| table_question = "T_1"
answers[option.option_id] = T_1.where("o_#{option.option_id} = ?",true).count
end

when 7..17 when 7..17
question.options.each do |option| table_question = "T_2"
answers[option.option_id] = T_2.where("o_#{option.option_id} = ?",true).count
end
when 18..26 when 18..26
question.options.each do |option| table_question = "T_3"
answers[option.option_id] = T_3.where("o_#{option.option_id} = ?",true).count
end
when 27..36 when 27..36
question.options.each do |option| table_question = "T_4"
answers[option.option_id] = T_4.where("o_#{option.option_id} = ?",true).count
end
end 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
end end
16 changes: 8 additions & 8 deletions app/views/charts/life.html.erb
Expand Up @@ -113,13 +113,13 @@
} }
}, },


series: [ series:
<% if @has_series %> <% if @chart_option %>
<%# @s:{"0302_name"=>"2 个", "0302_data"=>{"1001"=>395, ..."1002"=>581}, "0304_name"=>"4个", "0304_data"=>{"1001"=>395, ..."1002"=>581}} <%= raw @chart_option.to_json
%>
%>
<% else %> <% else %>
{ [{
name: 'Total', name: 'Total',
<% <%
data = [] data = []
Expand All @@ -128,9 +128,9 @@
end end
%> %>
data: <%= raw data.to_a %> data: <%= raw data.to_a %>
} }]
<% end %> <% end %>
]


}); });
}); });
Expand Down

0 comments on commit ec8ee82

Please sign in to comment.