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 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 def jump
@questions = @survey.survey_questions.all @questions = @survey.survey_questions.all
@ -259,4 +287,8 @@ class Admin::SurveysController < OrbitAdminController
p p
end end
def survey_section_params
params.require(:survey_section).permit!
end
end end

View File

@ -40,6 +40,7 @@ class QuestionnaireSurvey
has_many :survey_questions, :autosave => true, :dependent => :destroy has_many :survey_questions, :autosave => true, :dependent => :destroy
has_many :survey_answers, :dependent => :destroy has_many :survey_answers, :dependent => :destroy
has_many :survey_sections, :dependent => :destroy
accepts_nested_attributes_for :survey_questions, :allow_destroy => true 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%>/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%>/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%>/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="<%=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> <li><a href="#" class="delete text-error" rel="/admin/surveys/<%=survey.id.to_s%>"><%= t(:delete_) %></a></li>
<% end %> <% 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 data = action_data
@survey = data["survey"] @survey = data["survey"]
@questions = data["questions"] @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"] @jump_tos_map = data["jump_tos_map"]
@answers = data["answers"] @answers = data["answers"]
answer_present = data['answer_present'] answer_present = data['answer_present']
@redirect_url = data["redirect_url"] @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? %> <% if @survey.needs_login && current_user.nil? %>
<script type="text/javascript"> <script type="text/javascript">
@ -42,6 +66,15 @@
</div> </div>
<ol class="o-question-list"> <ol class="o-question-list">
<% @questions.each_with_index do |question, i| %> <% @questions.each_with_index do |question, i| %>
<% 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 %>"> <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 %> <% label = question.title %>
<% if question.is_required %> <% if question.is_required %>
@ -121,7 +154,7 @@
</thead> </thead>
<tbody> <tbody>
<% question.survey_question_options.each_with_index do |option, j| %> <% question.survey_question_options.each_with_index do |option, j| %>
<tr<%= (' class="odd"').html_safe if (j % 2 == 0) %>> <tr <%= (' class="odd"').html_safe if (j % 2 == 0) %>>
<th class="o-question-title"> <th class="o-question-title">
<%= option.name %> <%= option.name %>
</th> </th>
@ -144,6 +177,9 @@
<% end %> <% end %>
<% end %> <% end %>
</li> </li>
<% if footers.include?(header_count) %>
</div>
<% end %>
<% end %> <% end %>
</ol> </ol>
<div> <div>

View File

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

View File

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

View File

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