From c828fe7acee4152c8b02417651b5c6798ed1bdfb Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Sat, 29 Oct 2016 17:03:54 +0530 Subject: [PATCH] added qustions span and ranges --- app/controllers/surveys_controller.rb | 10 +++++ app/models/survey_answer.rb | 1 + app/views/admin/surveys/set_answers.html.erb | 44 +++++++++++++++----- app/views/surveys/answer_success.html.erb | 10 ++++- 4 files changed, 52 insertions(+), 13 deletions(-) diff --git a/app/controllers/surveys_controller.rb b/app/controllers/surveys_controller.rb index 2ee2c43..83e13c7 100644 --- a/app/controllers/surveys_controller.rb +++ b/app/controllers/surveys_controller.rb @@ -102,6 +102,7 @@ class SurveysController < ApplicationController @survey = QuestionnaireSurvey.find_by(:uid=>uid) total = 0 + individual_total = [] answer = params[:answer] @answer_model = @survey.survey_answers.new @answer_model.user = current_user.id if !current_user.nil? @@ -120,11 +121,13 @@ class SurveysController < ApplicationController @answer_model[qid] = opt.name p = (opt.points == nil ? 0 : opt.points) rescue 0 total = total + p + individual_total << p end end when SurveyQuestion::Check @answer_model[qid] = [] if answer[qid] + t = 0 answer[qid].each do |oid, value| if value.to_i != 0 if question.custom_option && oid == 'custom_option' @@ -134,33 +137,40 @@ class SurveysController < ApplicationController @answer_model[qid].push opt.name p = (opt.points == nil ? 0 : opt.points) rescue 0 total = total + p + t = t + p end end end + individual_total << p end when SurveyQuestion::Radiogroup @answer_model[qid] = {} options = Hash[question.survey_question_options.collect{|o| [ o.id.to_s, (o.points.nil? ? 0 : o.points) ] }] radiogroups = Hash[question.survey_question_radiogroups.collect{|rg| [ rg.id.to_s, rg.name] }] if answer[qid] + t = 0 answer[qid].each do |oid, value| unless value.blank? @answer_model[qid][oid] = radiogroups[value] total = total + options[oid] + t = t + options[oid] else if question.is_required @answer_model.errors.add question.title, t('survey_question.required_error') end end end + individual_total << t end when SurveyQuestion::Oneline, SurveyQuestion::Multiline @answer_model[qid] = answer[qid] + individual_total << 0 end end end if @answer_model.errors.empty? @answer_model.scored_points = total + @answer_model.individual_total = individual_total @answer_model.save! redirect_to OrbitHelper.url_to_show(@survey.to_param) + "?method=answer_success&ans=#{@answer_model.id.to_s}" else diff --git a/app/models/survey_answer.rb b/app/models/survey_answer.rb index f44dd96..245a85b 100644 --- a/app/models/survey_answer.rb +++ b/app/models/survey_answer.rb @@ -4,6 +4,7 @@ class SurveyAnswer field :user, type: BSON::ObjectId field :scored_points, type: Integer, :default => 0 + field :individual_total, type: Array, :default => [] belongs_to :questionnaire_survey diff --git a/app/views/admin/surveys/set_answers.html.erb b/app/views/admin/surveys/set_answers.html.erb index cab3619..7fb68fc 100644 --- a/app/views/admin/surveys/set_answers.html.erb +++ b/app/views/admin/surveys/set_answers.html.erb @@ -2,6 +2,14 @@ <%= 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" %> @@ -67,23 +75,37 @@
<% if @survey.result_criteria.blank? %> -
- - +
+
+ + +
+
+ +
+
<% else %> <% @survey.result_criteria.each_with_index do |criteria, idx| %> -
- - +
+
+ + +
+
+ + <% if idx != 0 %> <% end %> -
+
+ +
+
<% end %> <% end %>
@@ -108,7 +130,7 @@ $(".selectable[data-type=" + selectedOption.value + "]").removeClass("hide"); } var index = <%= @survey.result_criteria.count == 0 ? 1 : @survey.result_criteria.count + 1 %>, - html = '
Save to add text
'; + html = '
Save to add text
'; $("#add-criteria").on("click",function(){ var newhtml = html.replace(/{index}/g,index); @@ -119,7 +141,7 @@ }) $(document).on("click", ".delete-critera" ,function(){ - $(this).parent().remove(); + $(this).parent().parent().remove(); return false; }) diff --git a/app/views/surveys/answer_success.html.erb b/app/views/surveys/answer_success.html.erb index 8ec082e..59da084 100644 --- a/app/views/surveys/answer_success.html.erb +++ b/app/views/surveys/answer_success.html.erb @@ -7,8 +7,14 @@ <% if @survey.result_type == 3 %>

Your total score is <%= @answer_model.scored_points %>

<% @survey.result_criteria.each do |criteria| %> - <% if (criteria[0].to_i..criteria[1].to_i).cover?(@answer_model.scored_points) %> -
<%= criteria[2].html_safe %>
+ <% + total_criteria_score = 0 + (criteria["questions"][0].to_i..criteria["questions"][1].to_i).each do |x| + total_criteria_score = (total_criteria_score + @answer_model.individual_total[x].to_i) rescue 0 + end + %> + <% if (criteria["range"][0].to_i..criteria["range"][1].to_i).cover?(total_criteria_score) %> +
<%= criteria["msg"].html_safe %>
<% end %> <% end %> <% else %>