added section mechanism

This commit is contained in:
Harry Bomrah 2018-01-03 16:36:00 +08:00
parent b6987db8aa
commit 2956f2a59c
12 changed files with 376 additions and 101 deletions

View File

@ -174,6 +174,34 @@ class Admin::SurveysController < OrbitAdminController
end
def set_sections
@sections = QuestionnaireSurvey.find(params[:id]).survey_sections
end
def add_section
@section = SurveySection.new
@survey = QuestionnaireSurvey.find(params[:id])
end
def edit_section
@section = SurveySection.find(params[:id])
@survey = @section.questionnaire_survey
end
def delete_section
end
def create_section
section = SurveySection.create(survey_section_params)
redirect_to set_sections_admin_survey_path(section.questionnaire_survey.id)
end
def update_section
section = SurveySection.find(params[:id])
section.update_attributes(survey_section_params)
redirect_to set_sections_admin_survey_path(section.questionnaire_survey.id)
end
def jump
@questions = @survey.survey_questions.all
@ -259,4 +287,8 @@ class Admin::SurveysController < OrbitAdminController
p
end
def survey_section_params
params.require(:survey_section).permit!
end
end

View File

@ -40,6 +40,7 @@ class QuestionnaireSurvey
has_many :survey_questions, :autosave => true, :dependent => :destroy
has_many :survey_answers, :dependent => :destroy
has_many :survey_sections, :dependent => :destroy
accepts_nested_attributes_for :survey_questions, :allow_destroy => true

View File

@ -0,0 +1,12 @@
class SurveySection
include Mongoid::Document
include Mongoid::Timestamps
field :section_title, localize: true
field :section_description, localize: true
field :order, type: Integer
field :start_question, type: Integer
field :end_question, type: Integer
belongs_to :questionnaire_survey
end

View File

@ -31,6 +31,7 @@
<li><a href="/admin/surveys/<%=survey.id.to_s%>/jump"><%= t('survey.jump') %></a></li>
<li><a href="/admin/surveys/<%=survey.id.to_s%>/set_answers"><%= t('survey.set_answers') %></a></li>
<li><a href="/admin/surveys/<%=survey.id.to_s%>/export?format=xlsx" data-survey-id="<%= survey.id.to_s %>" class="export-xls"><%= t('survey.export_csv') %></a></li>
<li><a href="/admin/surveys/<%=survey.id.to_s%>/set_sections"><%= t('survey.set_sections') %></a></li>
<li><a href="<%=page_for_survey(survey)%>?method=result&force_chart=true" target="_blank"><%= t('survey.chart') %></a></li>
<li><a href="#" class="delete text-error" rel="/admin/surveys/<%=survey.id.to_s%>"><%= t(:delete_) %></a></li>
<% end %>

View File

@ -0,0 +1,139 @@
<% content_for :page_specific_css do %>
<%= stylesheet_link_tag "lib/main-forms" %>
<%= stylesheet_link_tag "lib/wrap-nav" %>
<%= stylesheet_link_tag "lib/main-list" %>
<% end %>
<% content_for :page_specific_javascript do %>
<%= javascript_include_tag "jquery-ui-custom.js" %>
<% end %>
<div class="input-area">
<div class="control-group">
<label class="control-label muted" for=""><%= t('survey.title') %></label>
<div class="controls">
<div class="input-append">
<div class="tab-content">
<% @site_in_use_locales.each_with_index do |locale, i| %>
<div class="tab-pane fade <%= ( i == 0 ) ? "active in" : '' %>" id="title_<%= locale %>">
<%= f.fields_for :section_title_translations do |f| %>
<%= f.text_field locale, :value => (@section.section_title_translations[locale] rescue nil), :placeholder=>"#{t("survey.section_title")}" %>
<% end %>
</div>
<% end %>
</div>
<div class="btn-group" data-toggle="buttons-radio">
<% @site_in_use_locales.each_with_index do |locale, i| %>
<a class="btn <%= ( i == 0 ) ? "active" : '' %>" href="#title_<%= locale %>" data-toggle="tab"><%= t(locale.to_s) %></a>
<% end %>
</div>
</div>
</div>
</div>
<!-- Description -->
<div class="control-group">
<label class="control-label muted"><%= t('survey.section_description') %></label>
<div class="controls">
<div class="tab-content textarea-lang">
<div class="btn-group" data-toggle="buttons-radio">
<% @site_in_use_locales.each_with_index do |locale, i| %>
<a class="btn <%= ( i == 0 ) ? "active" : '' %>" href="#textarea_<%= locale %>_0" data-toggle="tab"><%= t(locale.to_s) %></a>
<% end %>
</div>
<% @site_in_use_locales.each_with_index do |locale, i| %>
<div class="tab-pane fade in <%= ( i == 0 ) ? "active" : '' %>" id="textarea_<%= locale %>_0">
<%= f.fields_for :section_description_translations do |f| %>
<%= f.text_area locale, :class=>'resizable', :value => (@section.section_description_translations[locale] rescue nil), :placeholder => "#{t("survey.section_description")}", :rows=>"3" %>
<% end %>
</div>
<% end %>
</div>
</div>
</div>
<div class="control-group">
<%= f.label :start_question, t("sruvey.start_question"), :class => "control-label muted" %>
<div class="controls">
<% if @section.new_record? %>
<% if @survey.survey_sections.count == 0 %>
<%= f.number_field :start_question, :min => 1, :max => (@survey.survey_questions.count - 1), :value => (@survey.survey_sections.count + 1) %>
<% else %>
<% min_value = @survey.survey_sections.last.end_question %>
<%= f.number_field :start_question, :min => (min_value + 1), :max => (@survey.survey_questions.count - 1), :value => (min_value + 1) %>
<% end %>
<% else %>
<% if @survey.survey_sections.count == 0 %>
<%= f.number_field :start_question, :min => 1, :max => (@survey.survey_questions.count - 1) %>
<% else %>
<% if @section.order > 0 %>
<% min_value = @survey.survey_sections.where(:order => @section.order - 1).first.end_question %>
<%= f.number_field :start_question, :min => (min_value + 1), :max => (@survey.survey_questions.count - 1) %>
<% else %>
<%= f.number_field :start_question, :min => 1, :max => (@survey.survey_questions.count - 1) %>
<% end %>
<% end %>
<% end %>
</div>
</div>
<div class="control-group">
<%= f.label :end_question, t("sruvey.end_question"), :class => "control-label muted" %>
<div class="controls">
<% if @section.new_record? %>
<% if @survey.survey_sections.count == 0 %>
<%= f.number_field :end_question, :min => (@survey.survey_sections.count + 1), :max => @survey.survey_questions.count, :value => (@survey.survey_sections.count + 2) %>
<% else %>
<% min_value = @survey.survey_sections.last.end_question %>
<%= f.number_field :end_question, :min => (min_value + 1), :max => @survey.survey_questions.count, :value => (min_value + 2) %>
<% end %>
<% else %>
<% if @survey.survey_sections.count == 0 %>
<%= f.number_field :end_question, :min => (@survey.survey_sections.count + 1), :max => @survey.survey_questions.count %>
<% else %>
<% if @section.order > 0 %>
<% min_value = @survey.survey_sections.where(:order => @section.order - 1).first.end_question %>
<%= f.number_field :end_question, :min => (min_value + 2), :max => @survey.survey_questions.count %>
<% else %>
<%= f.number_field :end_question, :min => (@survey.survey_sections.count + 1), :max => @survey.survey_questions.count %>
<% end %>
<% end %>
<% end %>
</div>
</div>
</div>
<div class="form-actions">
<%= f.hidden_field :questionnaire_survey_id, :value => @survey.id %>
<% if @section.new_record? %>
<%= f.hidden_field :order, :value => @survey.survey_sections.count %>
<% end %>
<%= f.submit t('save'), :class=>'btn btn-primary' %>
</div>
<script type="text/javascript">
var current_start_value = 1,
current_end_value = 1;
$("#survey_section_start_question").on("focus",function(){
current_start_value = parseInt($(this).val());
})
$("#survey_section_end_question").on("focus",function(){
current_end_value = parseInt($(this).val());
})
$("#survey_section_end_question").on("change",function(){
var number = parseInt($(this).val());
if(number > parseInt($(this).attr("max"))){
$(this).val(current_end_value);
return false;
}
})
$("#survey_section_start_question").on("change",function(){
var number = parseInt($(this).val()) + 1;
if((number - 1) > parseInt($(this).attr("max"))){
$(this).val(current_start_value);
return false;
}
$("#survey_section_end_question").attr("min", number);
if(parseInt($("#survey_section_end_question").val()) < number){
$("#survey_section_end_question").val(number);
}
})
</script>

View File

@ -0,0 +1,5 @@
<%= form_for @section, :url => {:action => "create_section"}, :html => {:class => "form-horizontal main-forms"} do |f| %>
<fieldset>
<%= render :partial => "section_form", locals: {:f => f} %>
</fieldset>
<% end %>

View File

@ -0,0 +1,5 @@
<%= form_for @section, :url => update_section_admin_survey_path(@section.id), :html => {:class => "form-horizontal main-forms"} do |f| %>
<fieldset>
<%= render :partial => "section_form", locals: {:f => f} %>
</fieldset>
<% end %>

View File

@ -0,0 +1,32 @@
<span id="index_table">
<table class="table main-list">
<thead>
<tr class="sort-header">
<th>Section Title</th>
<th>Section Description</th>
</tr>
</thead>
<tbody>
<% @sections.each do |section| %>
<tr>
<td>
<%= section.section_title %>
<div class="quick-edit">
<ul class="nav nav-pills">
<li><a href="<%= edit_section_admin_survey_path(section) %>"><%= t(:edit) %></a></li>
<li><a class="delete text-error" data-method="delete" data-confirm="Are you sure?" href="<%= delete_section_admin_survey_path(section) %>"><%= t(:delete_) %></a></li>
</ul>
</div>
</td>
<td><%= section.section_description %></td>
</tr>
<% end %>
</tbody>
</table>
</span>
<div class="bottomnav clearfix">
<div class="pull-right">
<a href="<%= add_section_admin_survey_path %>" class="btn btn-primary">Add Section</a>
</div>
</div>

View File

@ -5,12 +5,36 @@
data = action_data
@survey = data["survey"]
@questions = data["questions"]
@sections = @survey.survey_sections
headers = @survey.survey_sections.pluck(:start_question)
footers = @survey.survey_sections.pluck(:end_question)
@jump_tos_map = data["jump_tos_map"]
@answers = data["answers"]
answer_present = data['answer_present']
@redirect_url = data["redirect_url"]
%>
<style type="text/css">
.section-header{
border: 1px dashed #cecece;
padding-bottom: 20px;
margin-bottom: 20px;
}
.section-header .header-wrapper{
background-color: #cecece;
color: #5c5c5c;
height: 80px;
padding: 10px;
margin-bottom: 15px;
}
.section-header .header-wrapper .section-title{
margin-bottom: 5px;
font-size: 18px;
}
ol.o-question-list li.question-item{
margin-left: 50px;
}
</style>
<% if @survey.needs_login && current_user.nil? %>
<script type="text/javascript">
@ -42,108 +66,120 @@
</div>
<ol class="o-question-list">
<% @questions.each_with_index do |question, i| %>
<li class="question-item question-<%= question.id.to_s %> hide" data-index="<%= i %>" data-type="<%= question.type%>" data-custom="<%= question.custom_option.to_s %>">
<% label = question.title %>
<% if question.is_required %>
<span class="required-star">(*)</span>
<% end %>
<%= f.label question.id, label, :class => "o-question-title" %>
<% if !question.description.blank? %>
<div class="o-question-description">
<%= question.description %>
</div>
<% end %>
<% case question.type %>
<% when SurveyQuestion::Oneline %>
<%= f.text_field question.id, :required => question.is_required %>
<% when SurveyQuestion::Multiline %>
<%= f.text_area question.id, :rows => 5, :required => question.is_required %>
<% when SurveyQuestion::Radio %>
<ul class="o-question-group view-list">
<% question.survey_question_options.each do |option| %>
<li>
<%= f.radio_button question.id, option.id %>
<%= f.label "#{question.id}_#{option.id}", option.name, :class => 'o-question-option' %>
</li>
<% end %>
<% if question.custom_option %>
<li>
<%= f.radio_button question.id, 'custom_option' %>
<%= f.label "#{question.id}_custom_option", t('survey_question.use_custom_option') + ': ', :class => 'o-question-option' %>
<%= f.text_field "#{question.id}_custom_option" %>
</li>
<% end %>
</ul>
<% when SurveyQuestion::DateTime %>
<%= f.datetime_picker question.id, :no_label => true, :new_record => true, :format => question.datetime_type, :placeholder => question.datetime_type.upcase, :required => question.is_required %>
<% when SurveyQuestion::Check %>
<ul class="o-question-group view-list">
<%= f.fields_for "#{question.id}" do |cf| %>
<% question.survey_question_options.each do |option| %>
<li>
<%= cf.check_box option.id %>
<%= cf.label option.id, option.name, :class => 'o-question-option' %>
</li>
<% end %>
<% if question.custom_option %>
<li>
<%= cf.check_box 'custom_option' %>
<%= f.label "#{question.id}_custom_option", t('survey_question.use_custom_option') + ': ', :class => 'o-question-option' %>
<%= f.text_field "#{question.id}_custom_option" %>
</li>
<% end %>
<% end %>
</ul>
<% when SurveyQuestion::Select %>
<% options = question.survey_question_options.collect {|o| [ o.name, o.id ] } %>
<% if question.custom_option %>
<% options.push [t('survey_question.use_custom_option'), 'custom_option'] %>
<% end %>
<%= f.select question.id, options, {}, :class => 'o-question-group view-dropdown' %>
<% if question.custom_option %>
<%= f.text_field "#{question.id}_custom_option", :class => 'custom_option' %>
<% end %>
<% when SurveyQuestion::Radiogroup %>
<%= f.fields_for "#{question.id}" do |rgf| %>
<table class="o-question-group view-grid">
<thead>
<tr>
<th></th>
<% if question.custom_option %>
<th></th>
<% end %>
<% question.survey_question_radiogroups.each do |radiogroup| %>
<th>
<%= radiogroup.name %>
</th>
<% end %>
</tr>
</thead>
<tbody>
<% question.survey_question_options.each_with_index do |option, j| %>
<tr<%= (' class="odd"').html_safe if (j % 2 == 0) %>>
<th class="o-question-title">
<%= option.name %>
</th>
<% if question.custom_option %>
<%= f.fields_for "#{option.id}" do |oid| %>
<td>
<%= oid.text_field "custom_group_field" %>
</td>
<% end %>
<% end %>
<% question.survey_question_radiogroups.each do |radiogroup| %>
<td>
<%= rgf.radio_button option.id, radiogroup.id %>
</td>
<% end %>
</tr>
<% header_count = i + 1 %>
<% if headers.include?(header_count) %>
<div class="section-header">
<% section = @sections.where(:start_question => header_count).first %>
<div class="header-wrapper">
<div class="section-title"><%= section.section_title %></div>
<div class="section-description"><%= section.section_description %></div>
</div>
<% end %>
<li class="question-item question-<%= question.id.to_s %> hide" data-index="<%= i %>" data-type="<%= question.type%>" data-custom="<%= question.custom_option.to_s %>">
<% label = question.title %>
<% if question.is_required %>
<span class="required-star">(*)</span>
<% end %>
<%= f.label question.id, label, :class => "o-question-title" %>
<% if !question.description.blank? %>
<div class="o-question-description">
<%= question.description %>
</div>
<% end %>
</tbody>
</table>
<% end %>
<% end %>
</li>
<% case question.type %>
<% when SurveyQuestion::Oneline %>
<%= f.text_field question.id, :required => question.is_required %>
<% when SurveyQuestion::Multiline %>
<%= f.text_area question.id, :rows => 5, :required => question.is_required %>
<% when SurveyQuestion::Radio %>
<ul class="o-question-group view-list">
<% question.survey_question_options.each do |option| %>
<li>
<%= f.radio_button question.id, option.id %>
<%= f.label "#{question.id}_#{option.id}", option.name, :class => 'o-question-option' %>
</li>
<% end %>
<% if question.custom_option %>
<li>
<%= f.radio_button question.id, 'custom_option' %>
<%= f.label "#{question.id}_custom_option", t('survey_question.use_custom_option') + ': ', :class => 'o-question-option' %>
<%= f.text_field "#{question.id}_custom_option" %>
</li>
<% end %>
</ul>
<% when SurveyQuestion::DateTime %>
<%= f.datetime_picker question.id, :no_label => true, :new_record => true, :format => question.datetime_type, :placeholder => question.datetime_type.upcase, :required => question.is_required %>
<% when SurveyQuestion::Check %>
<ul class="o-question-group view-list">
<%= f.fields_for "#{question.id}" do |cf| %>
<% question.survey_question_options.each do |option| %>
<li>
<%= cf.check_box option.id %>
<%= cf.label option.id, option.name, :class => 'o-question-option' %>
</li>
<% end %>
<% if question.custom_option %>
<li>
<%= cf.check_box 'custom_option' %>
<%= f.label "#{question.id}_custom_option", t('survey_question.use_custom_option') + ': ', :class => 'o-question-option' %>
<%= f.text_field "#{question.id}_custom_option" %>
</li>
<% end %>
<% end %>
</ul>
<% when SurveyQuestion::Select %>
<% options = question.survey_question_options.collect {|o| [ o.name, o.id ] } %>
<% if question.custom_option %>
<% options.push [t('survey_question.use_custom_option'), 'custom_option'] %>
<% end %>
<%= f.select question.id, options, {}, :class => 'o-question-group view-dropdown' %>
<% if question.custom_option %>
<%= f.text_field "#{question.id}_custom_option", :class => 'custom_option' %>
<% end %>
<% when SurveyQuestion::Radiogroup %>
<%= f.fields_for "#{question.id}" do |rgf| %>
<table class="o-question-group view-grid">
<thead>
<tr>
<th></th>
<% if question.custom_option %>
<th></th>
<% end %>
<% question.survey_question_radiogroups.each do |radiogroup| %>
<th>
<%= radiogroup.name %>
</th>
<% end %>
</tr>
</thead>
<tbody>
<% question.survey_question_options.each_with_index do |option, j| %>
<tr <%= (' class="odd"').html_safe if (j % 2 == 0) %>>
<th class="o-question-title">
<%= option.name %>
</th>
<% if question.custom_option %>
<%= f.fields_for "#{option.id}" do |oid| %>
<td>
<%= oid.text_field "custom_group_field" %>
</td>
<% end %>
<% end %>
<% question.survey_question_radiogroups.each do |radiogroup| %>
<td>
<%= rgf.radio_button option.id, radiogroup.id %>
</td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<% end %>
<% end %>
</li>
<% if footers.include?(header_count) %>
</div>
<% end %>
<% end %>
</ol>
<div>

View File

@ -36,6 +36,9 @@ en:
export_csv: Export CSV
chart: Chart
set_answers: Set Answers
set_sections: Set Sections
section_title: Section Title
section_description: Section Description
duplicate_it: Duplicate It
view_result: View Result

View File

@ -25,6 +25,9 @@ zh_tw:
taken_by: Taken By
taken_date: Taken On
view_answers: View Answers
set_sections: Set Sections
section_title: Section Title
section_description: Section Description
criteria: Criteria
result_type: 前端結果表示方式
extern_link: 外部連結

View File

@ -12,6 +12,12 @@ Rails.application.routes.draw do
member do
get 'export'
get 'set_answers'
get 'set_sections'
get 'add_section'
post 'create_section'
delete 'delete_section'
get 'edit_section'
patch 'update_section'
get 'jump'
get 'duplicate_it'
get 'answer_sets'