change result chart to year seperated.
Add color recover to default feature.
change consent checkbox to required.
This commit is contained in:
BoHung Chiu 2021-11-14 12:59:03 +08:00
parent 9b6e011286
commit cc4131c05e
10 changed files with 336 additions and 271 deletions

View File

@ -234,6 +234,9 @@ class Admin::SurveysController < OrbitAdminController
end
def delete_section
@section = SurveySection.find(params[:id])
@section.destroy
redirect_to :back
end
def create_section

View File

@ -300,7 +300,9 @@ class SurveysController < ApplicationController
end
end
@answer_model.save!
params[:url] = params[:show_page_url]
show_page = (OrbitHelper.page.nil? rescue true) ? Page.where(:page_id=>params["page_id"]).first : OrbitHelper.page
show_page_url = show_page.get_url rescue show_page.url
params[:url] = show_page_url
OrbitHelper.set_params(params,current_user)
redirect_to OrbitHelper.url_to_show(@survey.to_param) + "?method=answer_success&ans=#{@answer_model.id.to_s}"
else

View File

@ -67,17 +67,17 @@ class QuestionnaireSurvey
end
def check_need_update
if self.result_criteria_changed?
old_colors = self.result_criteria_was.map{|c| c["color"].blank? ? '#ffffff' : c["color"]}
new_colors = self.result_criteria.map{|c| c["color"].blank? ? '#ffffff' : c["color"]}
old_colors = self.result_criteria_was.map{|c| c["color"].blank? ? '#ffffff' : c["color"]} rescue []
new_colors = self.result_criteria.map{|c| c["color"].blank? ? '#ffffff' : c["color"]} rescue []
new_colors_uniq_count = new_colors.uniq.count
self.need_assign_color = (new_colors_uniq_count > 1)
if old_colors != new_colors
self.need_update_color = true
elsif new_colors_uniq_count > 1
old_questions = self.result_criteria_was.map{|c| c["questions"]}
old_range = self.result_criteria_was.map{|c| c["range"]}
new_questions = self.result_criteria.map{|c| c["questions"]}
new_range = self.result_criteria.map{|c| c["range"]}
old_questions = self.result_criteria_was.map{|c| c["questions"]} rescue []
old_range = self.result_criteria_was.map{|c| c["range"]} rescue []
new_questions = self.result_criteria.map{|c| c["questions"]} rescue []
new_range = self.result_criteria.map{|c| c["range"]} rescue []
if old_questions != new_questions || old_range != new_range
self.need_update_color = true
end
@ -193,6 +193,7 @@ class QuestionnaireSurvey
if self.enable_consent_feature
answers[i]["agree"] = (answer.consent_used ? agree_trans : disagree_trans)
end
answers[i]["scored_points"] = answer.scored_points
end
SurveysHelper.set_locale(I18n.locale)
survey_questions.each do |question|

View File

@ -57,7 +57,7 @@
<% @survey_answers.options = {} %>
<div style="clear: both;"></div>
<style type="text/css">
#tooltip{
.tooltip{
position: absolute;
background: #ffffff;
z-index: 1;
@ -65,16 +65,13 @@
padding: 2px;
display: none;
font-weight: bold;
font-size: 1em;
opacity: 1;
}
.flot_wrapper{
position: relative;
}
</style>
<div class="flot_wrapper">
<span id="tooltip"></span>
<div class="legend-container"></div>
<div id="resultchart-container" class="flot-placeholder"></div>
</div>
<%= javascript_include_tag "survey/jquery.colorhelpers.js" %>
<%= javascript_include_tag "survey/jquery.canvaswrapper.js" %>
<%= javascript_include_tag "survey/jquery.flot_3.0.js" %>
@ -93,9 +90,25 @@
font-weight: bold;
}
</style>
<% @survey_answers = @survey_answers.order_by(:created_at=>1) %>
<% last_year = @survey_answers.last.created_at.year %>
<% first_year = @survey_answers.first.created_at.year %>
<% all_years = (first_year..last_year).to_a.reverse %>
<% all_years.each do |year|%>
<% start_d = DateTime.parse("#{year}/1/1 #{Time.zone}") %>
<% end_d = start_d.end_of_year %>
<% survey_answers_sep = @survey_answers.where(:created_at.gte=>start_d).and(:created_at.lte=>end_d) %>
<% next if survey_answers_sep.count == 0 %>
<div class="flot_wrapper" id="<%=year%>_block">
<h4 style="text-align: center;"><%= year %></h4>
<span class="tooltip"></span>
<div class="legend-container"></div>
<div class="flot-placeholder"></div>
</div>
<script>
(function(){
<% if @survey.need_assign_color %>
<% data = @survey_answers.order_by(:created_at=>1).group_by(&:color).map{|k,sas| [k, sas.map{|sa| [sa.created_at.to_i * 1000 , sa.scored_points ]} ] }.to_h %>
<% data = survey_answers_sep.group_by(&:color).map{|k,sas| [k, sas.map{|sa| [sa.created_at.to_i * 1000 , sa.scored_points ]} ] }.to_h %>
var min_time = <%= data.map{|k,d| d[0][0] rescue 0}.min %>;
var max_time = <%= data.map{|k,d| d[-1][0] rescue 0}.max %>;
<% all_data = data.values.flatten(1) %>
@ -103,7 +116,7 @@
var min_y = <%= y_data.min.to_i %>;
var max_y = <%= y_data.max.to_i %>;
<% else %>
<% data = @survey_answers.order_by(:created_at=>1).map{|sa| [sa.created_at.to_i * 1000 , sa.scored_points ]} %>
<% data = survey_answers_sep.order_by(:created_at=>1).map{|sa| [sa.created_at.to_i * 1000 , sa.scored_points ]} %>
var min_time = <%= data[0][0] rescue 0 %>;
var max_time = <%= data[-1][0] rescue 0 %>;
var min_y = <%= data.map{|a| a[1]}.min.to_i %>;
@ -209,24 +222,36 @@
<% end %>
$(document).ready(function(){
var chart_height = $(window).outerWidth(true)*0.2;
$("#resultchart-container").height(chart_height).plot(chart_data,options);
$("#<%=year%>_block .flot-placeholder").height(chart_height).plot(chart_data,options);
})
$(window).resize(function(){
var chart_height = $(window).outerWidth(true)*0.2;
$("#<%=year%>_block .flot-placeholder").height(chart_height).plot(chart_data,options);
})
})();
</script>
<% end %>
<script>
$(document).ready(function(){
$(".flot-placeholder").bind("plothover", function (event, pos, item) {
if (!pos.x || !pos.y) {
return;
}
if (!item) {
$("#tooltip").hide();
$(this).siblings(".tooltip").hide();
}
});
$(".flot-placeholder").bind("plotclick", function (event, pos, item) {
if (!pos.x || !pos.y) {
if (!pos.x || !pos.y || item == null) {
return;
}
var tooltip = $(this).siblings(".tooltip");
console.log(tooltip)
var x = item.datapoint[0].toFixed(2),
y = item.datapoint[1].toFixed(0);
var date = new moment(Math.round(x));
var tp = "(" + date.format("yyyy/MM/DD HH:mm") + ")"
$("#tooltip").html("<%=t("survey.type.0")%>: "+y+"<br>"+tp);
tooltip.html("<%=t("survey.type.0")%>: "+y+"<br>"+tp);
var left = 0;
var content_width = tp.length*8.2
if (content_width+item.pageX+5>$(window).width()){
@ -235,14 +260,10 @@
left = item.pageX - content_width / 2;
}
var offset = $(this).parents(".flot_wrapper").eq(0).offset();
$("#tooltip").css({top: item.pageY+5-offset.top, left: left-offset.left})
tooltip.css({top: item.pageY+5-offset.top, left: left-offset.left})
.fadeIn(200);
});
})
$(window).resize(function(){
var chart_height = $(window).outerWidth(true)*0.2;
$("#resultchart-container").height(chart_height).plot(chart_data,options);
})
</script>
<% else %>
<table class="table main-list">
@ -254,6 +275,8 @@
</tr>
</thead>
<tbody>
<% @is_answer_list = true if !@answer_repeat%>
<% answers_nil = @survey_answers.nil? %>
<% (@survey_answer_groups || @survey_answers).each do |sa| %>
<tr>
<% user = sa.user.nil? ? nil : (User.find(sa.user) rescue nil) %>
@ -268,6 +291,7 @@
<a class="btn btn-primary" href="/admin/surveys/<%= sa.id.to_s %>/answer_list"><%= t("survey.view") %>(<%=sa.survey_answer_ids.count%>)</a>
<button class="btn btn-primary export-xls" data-href="/admin/surveys/<%= sa.id.to_s %>/export_answers"><%= t("survey.export") %></button>
<% else %>
<% sa = sa.survey_answers.last if answers_nil %>
<% if @survey.result_type == QuestionnaireSurvey::ResultCriteria %>
<% tmp_msgs = []
answer_model_attrs = sa.attributes

View File

@ -9,6 +9,9 @@
border-bottom: 2px solid;
padding-bottom: 15px;
}
input.color_input{
height: 2em;
}
</style>
<% end %>
<% content_for :page_specific_javascript do %>
@ -115,7 +118,8 @@
<% end %>
</div>
<div class="criteria" style="margin-left:0px;display:block;margin-bottom:50px;">
<label style="padding-left:0px;" for="" class="radio inline"> Color : <input type="color" name="questionnaire_survey[result_criteria][<%= idx %>][color]" value="<%=criteria["color"].blank? ? "#ffffff" : criteria["color"] %>"></label>
<label style="padding-left:0px;" for="" class="radio inline"> Color : <input class="color_input" type="color" name="questionnaire_survey[result_criteria][<%= idx %>][color]" value="<%=criteria["color"].blank? ? "#ffffff" : criteria["color"] %>"></label>
<button type="button" class="btn btn-primary btn-small recover_to_default"><%=I18n.t("survey.recover_to_default")%></button>
</div>
<div class="criteria">
<label for="" class="radio inline">
@ -151,7 +155,7 @@
<% [0,1].each do |i| %>
html += '<option value="<%=i%>"><%=t("survey.type.#{i}")%></option>'
<% end %>
html += '</select></label></div><div data-critera-number="criteria_{index}" class="criteria" style="width:800px;"><label style="padding-left:0px;" for="" class="radio inline"> From : <input name="questionnaire_survey[result_criteria][{index}][range][]" class="span1" max="<%= @survey.total_points %>" type="number"></label><label for="" class="radio inline"> To : <input name="questionnaire_survey[result_criteria][{index}][range][]" class="span1" max="<%= @survey.total_points %>" type="number"></label><a href="" style="margin-left:5px;" class="btn btn-primary delete-critera"><i class="icon icon-trash"></i></a><span class="radio inline">Save to add text</span></div><div class="criteria" style="margin-left:0px;display:block;"><label style="padding-left:0px;" for="" class="radio inline"> Color : <input type="color" name="questionnaire_survey[result_criteria][{index}][color]" value="#ffffff"></label></div></div>';
html += '</select></label></div><div data-critera-number="criteria_{index}" class="criteria" style="width:800px;"><label style="padding-left:0px;" for="" class="radio inline"> From : <input name="questionnaire_survey[result_criteria][{index}][range][]" class="span1" max="<%= @survey.total_points %>" type="number"></label><label for="" class="radio inline"> To : <input name="questionnaire_survey[result_criteria][{index}][range][]" class="span1" max="<%= @survey.total_points %>" type="number"></label><a href="" style="margin-left:5px;" class="btn btn-primary delete-critera"><i class="icon icon-trash"></i></a><span class="radio inline">Save to add text</span></div><div class="criteria" style="margin-left:0px;display:block;"><label style="padding-left:0px;" for="" class="radio inline"> Color : <input class="color_input" type="color" name="questionnaire_survey[result_criteria][{index}][color]" value="#ffffff"></label><button type="button" class="btn btn-primary btn-small recover_to_default"><%=I18n.t("survey.recover_to_default")%></button></div></div>';
$("#add-criteria").on("click",function(){
var newhtml = html.replace(/{index}/g,index);
@ -160,7 +164,9 @@
$("#criterias").append(newhtml);
return false;
})
$(document).on("click", ".recover_to_default", function(){
$(this).siblings("label").find(".color_input").val("#ffffff");
})
$(document).on("click", ".delete-critera" ,function(){
$(this).parent().parent().remove();
return false;

View File

@ -10,6 +10,8 @@ wb.add_worksheet(name: title) do |sheet|
row << survey.consent_contents
row2 << ""
end
row << I18n.t("survey.type.0")
row2 << ""
survey_questions.each_with_index do |question, i|
qnum = question.title.match(/^\d+./).nil? ? "#{i+1}. " : ""
start_col = row2.count

View File

@ -37,9 +37,10 @@
</div>
<% end %>
<% if type == "result_chart" %>
<% @survey_answers.options = {} %>
<div style="clear: both;"></div>
<style type="text/css">
#tooltip{
.tooltip{
position: absolute;
background: #ffffff;
z-index: 1;
@ -47,13 +48,13 @@
padding: 2px;
display: none;
font-weight: bold;
font-size: 1em;
opacity: 1;
}
.flot_wrapper{
position: relative;
}
</style>
<div class="flot_wrapper">
<span id="tooltip"></span>
<div class="legend-container"></div>
<div id="resultchart-container" class="flot-placeholder"></div>
</div>
<%= javascript_include_tag "survey/jquery.colorhelpers.js" %>
<%= javascript_include_tag "survey/jquery.canvaswrapper.js" %>
<%= javascript_include_tag "survey/jquery.flot_3.0.js" %>
@ -71,13 +72,26 @@
.axisLabels {
font-weight: bold;
}
.flot_wrapper{
position: relative;
}
</style>
<% @survey_answers = @survey_answers.order_by(:created_at=>1) %>
<% last_year = @survey_answers.last.created_at.year %>
<% first_year = @survey_answers.first.created_at.year %>
<% all_years = (first_year..last_year).to_a.reverse %>
<% all_years.each do |year|%>
<% start_d = DateTime.parse("#{year}/1/1 #{Time.zone}") %>
<% end_d = start_d.end_of_year %>
<% survey_answers_sep = @survey_answers.where(:created_at.gte=>start_d).and(:created_at.lte=>end_d) %>
<% next if survey_answers_sep.count == 0 %>
<div class="flot_wrapper" id="<%=year%>_block">
<h4 style="text-align: center;"><%= year %></h4>
<span class="tooltip"></span>
<div class="legend-container"></div>
<div class="flot-placeholder"></div>
</div>
<script>
(function(){
<% if @survey.need_assign_color %>
<% data = @survey_answers.order_by(:created_at=>1).group_by(&:color).map{|k,sas| [k, sas.map{|sa| [sa.created_at.to_i * 1000 , sa.scored_points ]} ] }.to_h %>
<% data = survey_answers_sep.group_by(&:color).map{|k,sas| [k, sas.map{|sa| [sa.created_at.to_i * 1000 , sa.scored_points ]} ] }.to_h %>
var min_time = <%= data.map{|k,d| d[0][0] rescue 0}.min %>;
var max_time = <%= data.map{|k,d| d[-1][0] rescue 0}.max %>;
<% all_data = data.values.flatten(1) %>
@ -85,7 +99,7 @@
var min_y = <%= y_data.min.to_i %>;
var max_y = <%= y_data.max.to_i %>;
<% else %>
<% data = @survey_answers.order_by(:created_at=>1).map{|sa| [sa.created_at.to_i * 1000 , sa.scored_points ]} %>
<% data = survey_answers_sep.order_by(:created_at=>1).map{|sa| [sa.created_at.to_i * 1000 , sa.scored_points ]} %>
var min_time = <%= data[0][0] rescue 0 %>;
var max_time = <%= data[-1][0] rescue 0 %>;
var min_y = <%= data.map{|a| a[1]}.min.to_i %>;
@ -191,24 +205,36 @@
<% end %>
$(document).ready(function(){
var chart_height = $(window).outerWidth(true)*0.2;
$("#resultchart-container").height(chart_height).plot(chart_data,options);
$("#<%=year%>_block .flot-placeholder").height(chart_height).plot(chart_data,options);
})
$(window).resize(function(){
var chart_height = $(window).outerWidth(true)*0.2;
$("#<%=year%>_block .flot-placeholder").height(chart_height).plot(chart_data,options);
})
})();
</script>
<% end %>
<script>
$(document).ready(function(){
$(".flot-placeholder").bind("plothover", function (event, pos, item) {
if (!pos.x || !pos.y) {
return;
}
if (!item) {
$("#tooltip").hide();
$(this).siblings(".tooltip").hide();
}
});
$(".flot-placeholder").bind("plotclick", function (event, pos, item) {
if (!pos.x || !pos.y) {
if (!pos.x || !pos.y || item == null) {
return;
}
var tooltip = $(this).siblings(".tooltip");
console.log(tooltip)
var x = item.datapoint[0].toFixed(2),
y = item.datapoint[1].toFixed(0);
var date = new moment(Math.round(x));
var tp = "(" + date.format("yyyy/MM/DD HH:mm") + ")"
$("#tooltip").html("<%=t("survey.type.0")%>: "+y+"<br>"+tp);
tooltip.html("<%=t("survey.type.0")%>: "+y+"<br>"+tp);
var left = 0;
var content_width = tp.length*8.2
if (content_width+item.pageX+5>$(window).width()){
@ -217,15 +243,10 @@
left = item.pageX - content_width / 2;
}
var offset = $(this).parents(".flot_wrapper").eq(0).offset();
$("#tooltip").css({top: item.pageY+5-offset.top, left: left-offset.left})
tooltip.css({top: item.pageY+5-offset.top, left: left-offset.left})
.fadeIn(200);
});
})
$(window).resize(function(){
var chart_height = $(window).outerWidth(true)*0.2;
$("#resultchart-container").height(chart_height).plot(chart_data,options);
})
</script>
<% else %>
<table class="table main-list">
@ -237,6 +258,8 @@
</tr>
</thead>
<tbody>
<% @is_answer_list = true if !@answer_repeat%>
<% answers_nil = @survey_answers.nil? %>
<% (@survey_answer_groups || @survey_answers).each do |sa| %>
<tr>
<% user = sa.user.nil? ? nil : (User.find(sa.user) rescue nil) %>
@ -251,6 +274,7 @@
<a class="btn btn-primary" href="/admin/surveys/<%= sa.id.to_s %>/answer_list" title="<%= t("survey.view") %>"><%= t("survey.view") %>(<%=sa.survey_answer_ids.count%>)</a>
<button class="btn btn-primary export-xls" data-href="/admin/surveys/<%= sa.id.to_s %>/export_answers" title="<%= t("survey.export") %>"><%= t("survey.export") %></button>
<% else %>
<% sa = sa.survey_answers.last if answers_nil %>
<% if @survey.result_type == QuestionnaireSurvey::ResultCriteria %>
<% tmp_msgs = []
answer_model_attrs = sa.attributes

View File

@ -92,9 +92,10 @@
</div>
<ol class="o-question-list">
<% if @survey.enable_consent_feature %>
<div class="survey_consent_contents o-question-title">
<label for="survey_consent_used"><%= f.check_box :consent_used , {:id=>"survey_consent_used", :title=>t('survey.agree')} %><span style="margin-left: 0.2em;"><%= @survey.consent_contents.to_s.html_safe %></span></label>
</div>
<li class="survey_consent_contents question-item ">
<span class="required-star">(*)</span>
<label for="survey_consent_used" class="o-question-title"><%= f.check_box :consent_used , {:id=>"survey_consent_used", :title=>t('survey.agree'), :required=>"required"} %><span style="margin-left: 0.2em;"><%= @survey.consent_contents.to_s.html_safe %></span></label>
</li>
<% end %>
<% @questions.each_with_index do |question, i| %>
<% header_count = i + 1 %>

View File

@ -2,6 +2,7 @@ en:
module_name:
survey: Survey
survey:
recover_to_default: Recover to default
'true': 'Yes'
'false': 'No'
consent_to_be_used_for_research: "Consent answer to be used for research"

View File

@ -4,6 +4,7 @@ zh_tw:
survey: 問卷調查
survey:
recover_to_default: 恢復成預設
'true': 已勾選
'false': 未勾選
consent_to_be_used_for_research: "同意填答結果被用於研究"