bug fix and add feature

This commit is contained in:
Lukas 2013-05-11 13:52:27 +08:00 committed by iCross
parent 515185d87c
commit b659c7021d
9 changed files with 54 additions and 59 deletions

View File

@ -114,11 +114,6 @@ class Panel::Survey::BackEnd::SurveysController < OrbitBackendController
end end
end end
def chart
@survey = ::Survey.find(params[:id])
@chart_data, @survey_questions, @survey_answers = @survey.generate_chart_data
end
def set_answers def set_answers
@survey = ::Survey.find(params[:id]) @survey = ::Survey.find(params[:id])
end end

View File

@ -85,6 +85,7 @@ class Panel::Survey::FrontEnd::SurveysController < OrbitWidgetController
def result def result
redirect_standalone result_panel_survey_front_end_survey_path(params[:id], :standalone => 'true') do redirect_standalone result_panel_survey_front_end_survey_path(params[:id], :standalone => 'true') do
@survey = ::Survey.find params[:id] @survey = ::Survey.find params[:id]
@survey.result_type = ::Survey::ResultChart if params[:force_chart]
case @survey.result_type case @survey.result_type
when ::Survey::ResultChart when ::Survey::ResultChart
@chart_data, @survey_questions, @survey_answers = @survey.generate_chart_data @chart_data, @survey_questions, @survey_answers = @survey.generate_chart_data

View File

@ -75,6 +75,17 @@ class Survey
end end
end end
end end
when ::SurveyQuestion::Radiogroup
chart_data[qid] = {}
answers = survey_answers.each do |answer|
if answer[qid]
answer[qid].each do |option, group|
chart_data[qid][option] ||= {}
chart_data[qid][option][group] ||= 0
chart_data[qid][option][group] += 1
end
end
end
end end
end end

View File

@ -15,7 +15,7 @@
<li><%= link_to t('survey.set_answers'), set_answers_panel_survey_back_end_survey_path(survey, :page => params[:page]) %></li> <li><%= link_to t('survey.set_answers'), set_answers_panel_survey_back_end_survey_path(survey, :page => params[:page]) %></li>
<li><%= link_to t('survey.answers_list'), panel_survey_back_end_survey_answers_path(survey, :page => params[:page]) %></li> <li><%= link_to t('survey.answers_list'), panel_survey_back_end_survey_answers_path(survey, :page => params[:page]) %></li>
<li><%= link_to t('survey.export_csv'), export_panel_survey_back_end_survey_path(survey, :format => :csv), :target => '_blank' %></li> <li><%= link_to t('survey.export_csv'), export_panel_survey_back_end_survey_path(survey, :format => :csv), :target => '_blank' %></li>
<li><%= link_to t('survey.chart'), chart_panel_survey_back_end_survey_path(survey, :page => params[:page]) %></li> <li><%= link_to t('survey.chart'), result_panel_survey_front_end_survey_path(survey, :force_chart => true, :standalone => true), :target => '_blank' %></li>
<li><%= link_to t(:delete_), panel_survey_back_end_survey_path(survey), :confirm => t('sure?'), :method => :delete, :remote => true %></li> <li><%= link_to t(:delete_), panel_survey_back_end_survey_path(survey), :confirm => t('sure?'), :method => :delete, :remote => true %></li>
<% end -%> <% end -%>
</ul> </ul>

View File

@ -1,40 +0,0 @@
<h1><%= @survey.title %> <%= t('survey.chart') %></h1>
<% @survey_questions.each do |question| %>
<h3><%= question.title %></h3>
<% case question.type %>
<% when ::SurveyQuestion::Radio, ::SurveyQuestion::Check, ::SurveyQuestion::Select %>
<div id="question_chart_<%= question.id.to_s %>" style="width: 600px; height: 300px;"></div>
<% else %>
<p><%= t('survey_question.have_not_chart') %></p>
<% end %>
<% end %>
<div class="form-actions form-fixed">
<%= link_to t('back'), get_go_back, :class=>"btn" %>
</div>
<% content_for :page_specific_javascript do %>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(function(){
<% @survey_questions.each do |question| %>
<% qid = question.id.to_s %>
<% case question.type %>
<% when ::SurveyQuestion::Radio, ::SurveyQuestion::Check, ::SurveyQuestion::Select %>
var data_<%= qid %> = google.visualization.arrayToDataTable([
['<%= t("survey_question.option") %>', ''],
<% @chart_data[qid].each do |option, count| %>
['<%= option %>', <%= count %>],
<% end %>
]);
var chart_<%= qid %> = new google.visualization.PieChart(document.getElementById('question_chart_<%= qid %>'));
chart_<%= qid %>.draw(data_<%= qid %>, {title: '<%= question.title %>'});
<% end %>
<% end %>
});
</script>
<% end %>

View File

@ -7,9 +7,10 @@
<% qid = question.id %> <% qid = question.id %>
<% case question.type %> <% case question.type %>
<% when ::SurveyQuestion::Check %> <% when ::SurveyQuestion::Check %>
<%= answer[qid].join(', ') %> <%= ( answer[qid] || [] ).join(', ') %>
<% when ::SurveyQuestion::Radiogroup %> <% when ::SurveyQuestion::Radiogroup %>
<%= answer[qid].map{|key, value| key + ': ' + value}.join('; ') unless answer[qid].blank? %> <% options = Hash[question.survey_question_options.collect{|o| [ o.id.to_s, o.name ] }] %>
<%= ( answer[qid] || {} ).map{|key, value| ( options[key] || key ) + ': ' + value}.join('; ') unless answer[qid].blank? %>
<% else %> <% else %>
<%= answer[qid] %> <%= answer[qid] %>
<% end %> <% end %>

View File

@ -2,16 +2,32 @@
<% when ::Survey::ResultChart %> <% when ::Survey::ResultChart %>
<h1><%= @survey.title %> <%= t('survey.chart') %></h1> <h1><%= @survey.title %> <%= t('survey.chart') %></h1>
<div>
<section> <section>
<div class="o-question">
<div class="o-question-description">
<%= t 'survey.results_count' %>: <%= @survey_questions.count %>
</div>
<ol class="o-question-list">
<% @survey_questions.each do |question| %> <% @survey_questions.each do |question| %>
<% case question.type %> <% case question.type %>
<% when ::SurveyQuestion::Radio, ::SurveyQuestion::Check, ::SurveyQuestion::Select %> <% when ::SurveyQuestion::Radio, ::SurveyQuestion::Check, ::SurveyQuestion::Select %>
<li>
<div id="question_chart_<%= question.id.to_s %>" style="width: 600px; height: 300px;"></div> <div id="question_chart_<%= question.id.to_s %>" style="width: 600px; height: 300px;"></div>
</li>
<% when ::SurveyQuestion::Radiogroup %>
<% question.survey_question_options.each do |option| %>
<li>
<div id="question_chart_<%= question.id.to_s %>_<%= option.id.to_s %>" style="width: 600px; height: 300px;"></div>
</li>
<% end %>
<% end %> <% end %>
<% end %> <% end %>
</ol>
</div>
</section> </section>
</div>
<script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript"> <script type="text/javascript">
@ -30,6 +46,18 @@
]); ]);
var chart_<%= qid %> = new google.visualization.PieChart(document.getElementById('question_chart_<%= qid %>')); var chart_<%= qid %> = new google.visualization.PieChart(document.getElementById('question_chart_<%= qid %>'));
chart_<%= qid %>.draw(data_<%= qid %>, {title: '<%= question.title %>'}); chart_<%= qid %>.draw(data_<%= qid %>, {title: '<%= question.title %>'});
<% when ::SurveyQuestion::Radiogroup %>
<% options = Hash[question.survey_question_options.collect{|o| [ o.id.to_s, o.name ] }] %>
<% @chart_data[qid].each do |option, groups| %>
var data_<%= qid %>_<%= option %> = google.visualization.arrayToDataTable([
['<%= t("survey_question.option") %>', ''],
<% groups.each do |group, count| %>
['<%= group %>', <%= count %>],
<% end %>
]);
var chart_<%= qid %>_<%= option %> = new google.visualization.PieChart(document.getElementById('question_chart_<%= qid %>_<%= option %>'));
chart_<%= qid %>_<%= option %>.draw(data_<%= qid %>_<%= option %>, {title: '<%= question.title %> - <%= options[option] %>'});
<% end %>
<% end %> <% end %>
<% end %> <% end %>
}); });

View File

@ -9,7 +9,7 @@ zh_tw:
start_deadline: 開始/結束時間 start_deadline: 開始/結束時間
no_postdate: 沒有開始時間 no_postdate: 沒有開始時間
no_deadline: 沒有結束時間 no_deadline: 沒有結束時間
results_count: 回覆 results_count: 填寫人
update_user: 最後更新者 update_user: 最後更新者
question: 問題 question: 問題

View File

@ -9,7 +9,6 @@ Rails.application.routes.draw do
end end
member do member do
get 'export' get 'export'
get 'chart'
get 'set_answers' get 'set_answers'
get 'jump' get 'jump'
get 'duplicate_it' get 'duplicate_it'