From 7c511361ff6856f7b7de057e7d3a1fc897257079 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Mon, 30 Nov 2015 18:11:58 +0800 Subject: [PATCH] now redirects to answer set if already taken. --- app/controllers/admin/surveys_controller.rb | 8 ++++++-- app/controllers/surveys_controller.rb | 18 ++++++++++++++---- app/views/admin/surveys/answer_set.html.erb | 2 ++ app/views/surveys/show.html.erb | 7 ++++++- app/views/surveys/show_data.html.erb | 8 +++++++- 5 files changed, 35 insertions(+), 8 deletions(-) diff --git a/app/controllers/admin/surveys_controller.rb b/app/controllers/admin/surveys_controller.rb index f498174..22a0999 100644 --- a/app/controllers/admin/surveys_controller.rb +++ b/app/controllers/admin/surveys_controller.rb @@ -25,8 +25,12 @@ class Admin::SurveysController < OrbitAdminController 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) + if can_edit_or_delete?(@survey) + @table_fields = ['survey.taken_by', 'survey.taken_date'] + @survey_answers = @survey.survey_answers.desc(:created_at).page(params[:page]).per(15) + else + render_401 + end end def answer_set diff --git a/app/controllers/surveys_controller.rb b/app/controllers/surveys_controller.rb index 6a77fae..4f1405f 100644 --- a/app/controllers/surveys_controller.rb +++ b/app/controllers/surveys_controller.rb @@ -5,8 +5,9 @@ class SurveysController < ApplicationController def index questionnairesurveys = QuestionnaireSurvey.any_of( {:postdate.lte => Time.now} ).desc(:postdate).page(OrbitHelper.params[:page_no]).per(OrbitHelper.page_data_count) - + current_user = OrbitHelper.current_user surveys = questionnairesurveys.collect do |a| + answer_present = (current_user.nil? ? false : (a.survey_answers.where(:user => current_user.id).count > 0 ? true : false)) if !OrbitHelper.params[:page_no].blank? title = ('' + a.title + '').html_safe @@ -18,7 +19,8 @@ class SurveysController < ApplicationController "title" => title, "time_range" => QuestionnaireSurvey.time_range(a), "write" => QuestionnaireSurvey.write(a), - "result" => QuestionnaireSurvey.result(a) + "result" => QuestionnaireSurvey.result(a), + "answer_present" => answer_present } end { @@ -40,9 +42,13 @@ class SurveysController < ApplicationController params = OrbitHelper.params survey = QuestionnaireSurvey.find_by(uid: params[:uid]) - + current_user = OrbitHelper.current_user + answer_present = (current_user.nil? ? false : (survey.survey_answers.where(:user => current_user.id).count > 0 ? true : false)) + link_to_answer_set = (answer_present ? "/admin/surveys/#{survey.survey_answers.where(:user => current_user.id).first.id.to_s}/answer_set" : "") { 'survey' => survey, + 'answer_present' => answer_present, + 'link_to_answer_set' => link_to_answer_set, 'time_range' => QuestionnaireSurvey.time_range(survey) } @@ -66,7 +72,9 @@ class SurveysController < ApplicationController else questions = survey.survey_questions.all.entries - + current_user = OrbitHelper.current_user + answer_present = (current_user.nil? ? false : (survey.survey_answers.where(:user => current_user.id).count > 0 ? true : false)) + link_to_answer_set = (answer_present ? "/admin/surveys/#{survey.survey_answers.where(:user => current_user.id).first.id.to_s}/answer_set" : "") if survey.jump_mode answers = Hash[questions.collect{ |o| [o.id.to_s, o.id.to_s] }] jumpable_questions = questions.select{ |q| q.jumpable? && q.can_set_jump? } @@ -75,8 +83,10 @@ class SurveysController < ApplicationController { 'survey' => survey, 'redirect_url' => '', + 'link_to_answer_set' => link_to_answer_set, 'questions' => questions, 'answers' => answers, + 'answer_present' => answer_present, 'jump_tos_map' => jump_tos_map } diff --git a/app/views/admin/surveys/answer_set.html.erb b/app/views/admin/surveys/answer_set.html.erb index 68f5ddd..47b351e 100644 --- a/app/views/admin/surveys/answer_set.html.erb +++ b/app/views/admin/surveys/answer_set.html.erb @@ -80,9 +80,11 @@ <% end %> + <% if can_edit_or_delete?(@survey) %>
Back to answer sets
+ <% end %> diff --git a/app/views/surveys/show.html.erb b/app/views/surveys/show.html.erb index d64549b..3a01758 100644 --- a/app/views/surveys/show.html.erb +++ b/app/views/surveys/show.html.erb @@ -6,7 +6,7 @@ @questions = data["questions"] @jump_tos_map = data["jump_tos_map"] @answers = data["answers"] - + answer_present = data['answer_present'] @redirect_url = data["redirect_url"] %> @@ -15,6 +15,11 @@ +<% elsif answer_present %> + <% end %> diff --git a/app/views/surveys/show_data.html.erb b/app/views/surveys/show_data.html.erb index 2591f83..4d4acc1 100644 --- a/app/views/surveys/show_data.html.erb +++ b/app/views/surveys/show_data.html.erb @@ -4,6 +4,8 @@ data = action_data @survey = data["survey"] @time_range = data["time_range"] + answer_present = data["answer_present"] + link_to_answer_set = data["link_to_answer_set"] %>
@@ -31,5 +33,9 @@ <% else %> <%= link_to t('back'), page_for_survey, :class => 'btn btn-primary' %> <% end %> -<%= link_to "Take survey", OrbitHelper.url_to_show(@survey.to_param), :class => 'btn btn-primary' %> +<% if !answer_present %> + <%= link_to "Take survey", OrbitHelper.url_to_show(@survey.to_param), :class => 'btn btn-primary' %> +<% else %> + <%= link_to "View answers", link_to_answer_set, :class => "btn btn-primary" %> +<% end %>