From 2956f2a59cbfe93e23e08a121a8c042ee37b9548 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Wed, 3 Jan 2018 16:36:00 +0800 Subject: [PATCH] added section mechanism --- app/controllers/admin/surveys_controller.rb | 32 +++ app/models/questionnaire_survey.rb | 1 + app/models/survey_section.rb | 12 + app/views/admin/surveys/_index.html.erb | 1 + .../admin/surveys/_section_form.html.erb | 139 ++++++++++ app/views/admin/surveys/add_section.html.erb | 5 + app/views/admin/surveys/edit_section.html.erb | 5 + app/views/admin/surveys/set_sections.html.erb | 32 +++ app/views/surveys/show.html.erb | 238 ++++++++++-------- config/locales/en.yml | 3 + config/locales/zh_tw.yml | 3 + config/routes.rb | 6 + 12 files changed, 376 insertions(+), 101 deletions(-) create mode 100644 app/models/survey_section.rb create mode 100644 app/views/admin/surveys/_section_form.html.erb create mode 100644 app/views/admin/surveys/add_section.html.erb create mode 100644 app/views/admin/surveys/edit_section.html.erb create mode 100644 app/views/admin/surveys/set_sections.html.erb diff --git a/app/controllers/admin/surveys_controller.rb b/app/controllers/admin/surveys_controller.rb index 680df17..364c999 100644 --- a/app/controllers/admin/surveys_controller.rb +++ b/app/controllers/admin/surveys_controller.rb @@ -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 @@ -258,5 +286,9 @@ class Admin::SurveysController < OrbitAdminController end p end + + def survey_section_params + params.require(:survey_section).permit! + end end \ No newline at end of file diff --git a/app/models/questionnaire_survey.rb b/app/models/questionnaire_survey.rb index b850590..be7e58f 100644 --- a/app/models/questionnaire_survey.rb +++ b/app/models/questionnaire_survey.rb @@ -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 diff --git a/app/models/survey_section.rb b/app/models/survey_section.rb new file mode 100644 index 0000000..38d39e8 --- /dev/null +++ b/app/models/survey_section.rb @@ -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 \ No newline at end of file diff --git a/app/views/admin/surveys/_index.html.erb b/app/views/admin/surveys/_index.html.erb index 7cd4742..072d720 100644 --- a/app/views/admin/surveys/_index.html.erb +++ b/app/views/admin/surveys/_index.html.erb @@ -31,6 +31,7 @@
  • <%= t('survey.jump') %>
  • <%= t('survey.set_answers') %>
  • <%= t('survey.export_csv') %>
  • +
  • <%= t('survey.set_sections') %>
  • <%= t('survey.chart') %>
  • <%= t(:delete_) %>
  • <% end %> diff --git a/app/views/admin/surveys/_section_form.html.erb b/app/views/admin/surveys/_section_form.html.erb new file mode 100644 index 0000000..025fbf1 --- /dev/null +++ b/app/views/admin/surveys/_section_form.html.erb @@ -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 %> +
    +
    + +
    +
    +
    + <% @site_in_use_locales.each_with_index do |locale, i| %> +
    " 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 %> +
    + <% end %> +
    +
    + <% @site_in_use_locales.each_with_index do |locale, i| %> + " href="#title_<%= locale %>" data-toggle="tab"><%= t(locale.to_s) %> + <% end %> +
    +
    +
    +
    + + +
    + +
    +
    +
    + <% @site_in_use_locales.each_with_index do |locale, i| %> + " href="#textarea_<%= locale %>_0" data-toggle="tab"><%= t(locale.to_s) %> + <% end %> +
    + <% @site_in_use_locales.each_with_index do |locale, i| %> +
    " 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 %> +
    + <% end %> +
    +
    +
    + +
    + <%= f.label :start_question, t("sruvey.start_question"), :class => "control-label muted" %> +
    + <% 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 %> +
    +
    + +
    + <%= f.label :end_question, t("sruvey.end_question"), :class => "control-label muted" %> +
    + <% 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 %> +
    +
    +
    +
    + <%= 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' %> +
    + + + diff --git a/app/views/admin/surveys/add_section.html.erb b/app/views/admin/surveys/add_section.html.erb new file mode 100644 index 0000000..cd3d8e3 --- /dev/null +++ b/app/views/admin/surveys/add_section.html.erb @@ -0,0 +1,5 @@ +<%= form_for @section, :url => {:action => "create_section"}, :html => {:class => "form-horizontal main-forms"} do |f| %> +
    + <%= render :partial => "section_form", locals: {:f => f} %> +
    +<% end %> \ No newline at end of file diff --git a/app/views/admin/surveys/edit_section.html.erb b/app/views/admin/surveys/edit_section.html.erb new file mode 100644 index 0000000..5b4eef9 --- /dev/null +++ b/app/views/admin/surveys/edit_section.html.erb @@ -0,0 +1,5 @@ +<%= form_for @section, :url => update_section_admin_survey_path(@section.id), :html => {:class => "form-horizontal main-forms"} do |f| %> +
    + <%= render :partial => "section_form", locals: {:f => f} %> +
    +<% end %> \ No newline at end of file diff --git a/app/views/admin/surveys/set_sections.html.erb b/app/views/admin/surveys/set_sections.html.erb new file mode 100644 index 0000000..9e613eb --- /dev/null +++ b/app/views/admin/surveys/set_sections.html.erb @@ -0,0 +1,32 @@ + + + + + + + + + + <% @sections.each do |section| %> + + + + + <% end %> + +
    Section TitleSection Description
    + <%= section.section_title %> + + <%= section.section_description %>
    +
    + +
    + +
    \ No newline at end of file diff --git a/app/views/surveys/show.html.erb b/app/views/surveys/show.html.erb index e3d2748..af2abf2 100644 --- a/app/views/surveys/show.html.erb +++ b/app/views/surveys/show.html.erb @@ -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"] %> + <% if @survey.needs_login && current_user.nil? %>