From 19cc1e2fb35d78eaae2d7130c5f28d2385bb0907 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Fri, 27 Nov 2015 19:55:47 +0800 Subject: [PATCH] added functionality to take survey only after login and also can see the results of each user --- app/controllers/admin/surveys_controller.rb | 13 ++- app/controllers/surveys_controller.rb | 1 + app/models/questionnaire_survey.rb | 1 + app/models/survey_answer.rb | 2 + app/views/admin/surveys/_form.html.erb | 13 ++- app/views/admin/surveys/_index.html.erb | 8 +- app/views/admin/surveys/answer_set.html.erb | 101 +++++++++++++++++++ app/views/admin/surveys/answer_sets.html.erb | 30 ++++++ app/views/surveys/show.html.erb | 9 +- app/views/surveys/show_data.html.erb | 3 +- config/locales/en.yml | 4 + config/locales/zh_tw.yml | 5 +- config/routes.rb | 2 + 13 files changed, 185 insertions(+), 7 deletions(-) create mode 100644 app/views/admin/surveys/answer_set.html.erb create mode 100644 app/views/admin/surveys/answer_sets.html.erb diff --git a/app/controllers/admin/surveys_controller.rb b/app/controllers/admin/surveys_controller.rb index 1bef1ce..f498174 100644 --- a/app/controllers/admin/surveys_controller.rb +++ b/app/controllers/admin/surveys_controller.rb @@ -2,7 +2,7 @@ class Admin::SurveysController < OrbitAdminController include Admin::SurveysHelper before_action ->(module_app = @app_title) { set_variables module_app } - before_action :set_survey, only: [:edit, :destroy, :export, :set_answers, :jump, :duplicate_it] + before_action :set_survey, only: [:edit, :destroy, :export, :set_answers, :jump, :duplicate_it, :answer_sets] def initialize super @@ -24,6 +24,17 @@ class Admin::SurveysController < OrbitAdminController end end + def answer_sets + @table_fields = ['survey.taken_by', 'survey.taken_date'] + @survey_answers = @survey.survey_answers.desc(:created_at).page(params[:page]).per(15) + end + + def answer_set + @survey_answer = SurveyAnswer.find(params["id"]) rescue nil + @survey = @survey_answer.questionnaire_survey if !@survey_answer.nil? + @user = @survey_answer.user.nil? ? nil : (User.find(@survey_answer.user) rescue nil) + end + def new @survey = QuestionnaireSurvey.new @primary_locale = I18n.locale.to_s diff --git a/app/controllers/surveys_controller.rb b/app/controllers/surveys_controller.rb index 5a11136..6a77fae 100644 --- a/app/controllers/surveys_controller.rb +++ b/app/controllers/surveys_controller.rb @@ -91,6 +91,7 @@ class SurveysController < ApplicationController answer = params[:answer] @answer_model = @survey.survey_answers.new + @answer_model.user = current_user.id if !current_user.nil? @survey.survey_questions.each do |question| qid = question.id.to_s if question.is_required && answer[qid].blank? && !@survey.jump_mode #&& (! @survey.jump_mode || ( @survey.jump_mode && question.jumpable? ) ) diff --git a/app/models/questionnaire_survey.rb b/app/models/questionnaire_survey.rb index f04fa8c..d906ba2 100644 --- a/app/models/questionnaire_survey.rb +++ b/app/models/questionnaire_survey.rb @@ -21,6 +21,7 @@ class QuestionnaireSurvey field :deadline, :type => DateTime field :is_hidden, :type => Boolean, :default => false + field :needs_login, :type => Boolean, :default => false field :result_type, :type => Integer, :default => 0 field :extern_link diff --git a/app/models/survey_answer.rb b/app/models/survey_answer.rb index 794f7c2..e032ce0 100644 --- a/app/models/survey_answer.rb +++ b/app/models/survey_answer.rb @@ -2,6 +2,8 @@ class SurveyAnswer include Mongoid::Document include Mongoid::Timestamps + field :user, type: BSON::ObjectId + belongs_to :questionnaire_survey end \ No newline at end of file diff --git a/app/views/admin/surveys/_form.html.erb b/app/views/admin/surveys/_form.html.erb index 379c199..b214ace 100644 --- a/app/views/admin/surveys/_form.html.erb +++ b/app/views/admin/surveys/_form.html.erb @@ -69,16 +69,25 @@
- <%= f.datetime_picker :postdate, :no_label => true %> + <%= f.datetime_picker :postdate, :value => @survey.postdate, :new_record => @survey.new_record? %>
- <%= f.datetime_picker :deadline, :no_label => true %> + <%= f.datetime_picker :deadline, :value => @survey.deadline, :new_record => @survey.new_record? %>
+ +
+ <%= f.label :needs_login, t("survey.needs_login"), :class=>"control-label muted" %> +
+ +
+
diff --git a/app/views/admin/surveys/_index.html.erb b/app/views/admin/surveys/_index.html.erb index 21b7974..19e4135 100644 --- a/app/views/admin/surveys/_index.html.erb +++ b/app/views/admin/surveys/_index.html.erb @@ -39,7 +39,13 @@ <%= (survey.postdate) ? (format_value survey.postdate) : t(:no_deadline) %> <%= (survey.deadline) ? (format_value survey.deadline) : t(:no_deadline) %> - <%= survey.survey_answers.count %> + + <% if survey.needs_login %> + <%= survey.survey_answers.count %> + <% else %> + <%= survey.survey_answers.count %> + <% end %> + <%= survey.update_user.user_name rescue ''%> <% end %> diff --git a/app/views/admin/surveys/answer_set.html.erb b/app/views/admin/surveys/answer_set.html.erb new file mode 100644 index 0000000..68f5ddd --- /dev/null +++ b/app/views/admin/surveys/answer_set.html.erb @@ -0,0 +1,101 @@ +<% content_for :page_specific_css do %> + +<% end %> + +
+
+

<%= @survey.title %>

+ <% if !@user.nil? %> +
<%= @user.name %>
+ <% end %> +
+
+ <% @survey.survey_questions.each_with_index do |sq, index| %> +
+
+ Q<%= (index + 1).to_s %> <%= sq.title %> +
+
+ A + <% case sq.type %> + <% when SurveyQuestion::Radio, SurveyQuestion::Select %> + <%= @survey_answer[sq.id.to_s] rescue "" %> + <% when SurveyQuestion::Check %> + <%= @survey_answer[sq.id.to_s].join(", ") rescue "" %> + <% when SurveyQuestion::Radiogroup %> +
+ <% sq.survey_question_options.each do |sqo| %> + <% if !@survey_answer[sq.id.to_s].nil? %> + <%= sqo.name %> : <%= @survey_answer[sq.id.to_s][sqo.id.to_s] %>
+ <% end %> + <% end %> +
+ <% when SurveyQuestion::Oneline, SurveyQuestion::Multiline %> + <%= @survey_answer[sq.id.to_s] rescue "" %> + <% end %> +
+
+ <% end %> +
+ +
+ + + + + + + + + + + + + + + diff --git a/app/views/admin/surveys/answer_sets.html.erb b/app/views/admin/surveys/answer_sets.html.erb new file mode 100644 index 0000000..027f2a8 --- /dev/null +++ b/app/views/admin/surveys/answer_sets.html.erb @@ -0,0 +1,30 @@ +

<%= @survey.title %>

+ + + + <% @table_fields.each do |f| %> + <%= thead(f) %> + <% end %> + + + + + <% @survey_answers.each do |sa| %> + + <% user = sa.user.nil? ? nil : (User.find(sa.user) rescue nil) %> + <% if !user.nil? %> + + <% else %> + + <% end %> + + + + <% end %> + +
<%= user.name %> <%= sa.created_at.strftime("%y-%M-%d %h:%m") rescue nil %><%= t("survey.view_answers") %>
+ <%= + content_tag :div, class: "bottomnav clearfix" do + content_tag :div, paginate(@survey_answers), class: "pagination pagination-centered" + end +%> \ No newline at end of file diff --git a/app/views/surveys/show.html.erb b/app/views/surveys/show.html.erb index 4dbaaf6..d64549b 100644 --- a/app/views/surveys/show.html.erb +++ b/app/views/surveys/show.html.erb @@ -11,6 +11,13 @@ %> +<% if @survey.needs_login && current_user.nil? %> + +<% end %> + + <% if !@redirect_url.blank? %>