diff --git a/app/controllers/admin/surveys_controller.rb b/app/controllers/admin/surveys_controller.rb index e12db27..81f20c4 100644 --- a/app/controllers/admin/surveys_controller.rb +++ b/app/controllers/admin/surveys_controller.rb @@ -84,7 +84,7 @@ class Admin::SurveysController < OrbitAdminController total = 0 @survey.survey_questions.each do |ques| ques.survey_question_options.each do |opt| - p = (opt.points == nil ? 0 : opt.points) rescue 0 + p = (opt.points == nil ? 0 : opt.points) * ques.weight rescue 0 total = total + p end end @@ -118,15 +118,20 @@ class Admin::SurveysController < OrbitAdminController total = 0 @survey.survey_questions.each do |ques| ques.survey_question_options.each do |opt| - p = (opt.points == nil ? 0 : opt.points) rescue 0 + p = (opt.points == nil ? 0 : opt.points) * ques.weight rescue 0 total = total + p end end @survey.total_points = total respond_to do |format| if @survey.update_attributes(survey_params) - @survey.update_answer_score - @survey.update_total_weight + Thread.new do + @survey.update_answer_score + @survey.update_total_weight + if @survey.need_assign_color && @survey.need_update_color + @survey.update_answers_color + end + end if params[:et] == "result" format.html { redirect_to(set_answers_admin_survey_path(@survey.id)) } else @@ -262,7 +267,6 @@ class Admin::SurveysController < OrbitAdminController format.xml { render :xml => @survey, :status => :created, :location => @new_survey } end end - private def set_survey diff --git a/app/controllers/surveys_controller.rb b/app/controllers/surveys_controller.rb index a8e1939..e5c3a26 100644 --- a/app/controllers/surveys_controller.rb +++ b/app/controllers/surveys_controller.rb @@ -257,6 +257,48 @@ class SurveysController < ApplicationController @answer_model.scored_points = total @answer_model.individual_total = individual_total @answer_model.consent_used = answer[:consent_used] + if @survey.need_assign_color + tmp_result_criteria = @survey.result_criteria.map{|c| {"q"=>c["questions"].map{|q| q.to_i},"r"=>c["range"].map{|r| r.to_i},"c"=>c["color"].blank? ? '#ffffff' : c["color"],"t"=>c["type"].to_i}} + survey_questions_count = @survey.survey_questions.count + weight_relations = @survey.survey_questions.map{|q| [q.id.to_s,(q.weight.nil? ? 1 : q.weight)]}.to_h + if tmp_result_criteria.select{|criteria| (criteria["q"][0] != 1 || criteria["q"][1] != survey_questions_count rescue true)}.count != 0 + answer_model_attrs = answer + tmp_result_criteria.each do |c| + total_weight = 0 + range = (c["r"].count != 2 ? [0,100] : c["r"]) + questions = c["q"].map{|q| (q == 0 ? 0 : q - 1)} + total = 0 + questions.each do |x| + if c["t"] == 1 + k = weight_relations.keys[x] + if k && answer_model_attrs.has_key?(k) + total_weight += weight_relations[k] + end + end + total += a.individual_total[x].to_i + end + if c["t"] == 0 + if (range[0]..range[1]).cover?(total) + @answer_model.color = c["color"] + break + end + else + avg = (total / total_weight rescue 0) + if (range[0]..range[1]).cover?(avg) + @answer_model.color = c["color"] + break + end + end + end + else + tmp_result_criteria.each do |c| + if (c["r"][0]..c["r"][1]).cover?(@answer_model.scored_points) + @answer_model.color = c["color"] + break + end + end + end + end @answer_model.save! params[:url] = params[:show_page_url] OrbitHelper.set_params(params,current_user) diff --git a/app/models/questionnaire_survey.rb b/app/models/questionnaire_survey.rb index 6a658d6..7f0ccf5 100644 --- a/app/models/questionnaire_survey.rb +++ b/app/models/questionnaire_survey.rb @@ -12,6 +12,8 @@ class QuestionnaireSurvey include OrbitCategory::Categorizable scope :can_display, ->{where(is_hidden: false)} + field :need_assign_color, :type => Boolean, :default => false + field :need_update_color, :type => Boolean, :default => false field :enable_consent_feature, :type => Boolean, :default => false field :consent_contents, :type => String, :default => "", :localize => true field :already_fix_data, :type => Boolean, :default => false @@ -51,7 +53,7 @@ class QuestionnaireSurvey accepts_nested_attributes_for :survey_questions, :allow_destroy => true - before_save :check_deadline#, :update_avliable_language + before_save :check_deadline, :check_need_update #, :update_avliable_language has_many :survey_paginations, :autosave => true, :dependent => :destroy accepts_nested_attributes_for :survey_paginations, :allow_destroy => true @@ -63,6 +65,71 @@ class QuestionnaireSurvey self.updated_at = DateTime.now end end + def check_need_update + if self.result_criteria_changed? + old_colors = self.result_criteria_was.map{|c| c["color"].blank? ? '#ffffff' : c["color"]} + new_colors = self.result_criteria.map{|c| c["color"].blank? ? '#ffffff' : c["color"]} + new_colors_uniq_count = new_colors.uniq.count + self.need_assign_color = (new_colors_uniq_count > 1) + if old_colors != new_colors + self.need_update_color = true + elsif new_colors_uniq_count > 1 + old_questions = self.result_criteria_was.map{|c| c["questions"]} + old_range = self.result_criteria_was.map{|c| c["range"]} + new_questions = self.result_criteria.map{|c| c["questions"]} + new_range = self.result_criteria.map{|c| c["range"]} + if old_questions != new_questions || old_range != new_range + self.need_update_color = true + end + end + end + end + def update_answers_color + puts "Updating answer's color" + tmp_result_criteria = self.result_criteria.map{|c| {"q"=>c["questions"].map{|q| q.to_i},"r"=>c["range"].map{|r| r.to_i},"c"=>c["color"].blank? ? '#ffffff' : c["color"],"t"=>c["type"].to_i}} + survey_questions_count = self.survey_questions.count + weight_relations = self.survey_questions.map{|q| [q.id.to_s,(q.weight.nil? ? 1 : q.weight)]}.to_h + if tmp_result_criteria.select{|criteria| (criteria["q"][0] != 1 || criteria["q"][1] != survey_questions_count rescue true)}.count != 0 + self.survey_answers.each do |a| + answer_model_attrs = a.attributes + tmp_result_criteria.each do |c| + total_weight = 0 + range = (c["r"].count != 2 ? [0,100] : c["r"]) + questions = c["q"].map{|q| (q == 0 ? 0 : q - 1)} + total = 0 + questions.each do |x| + if c["t"] == 1 + k = weight_relations.keys[x] + if k && answer_model_attrs.has_key?(k) + total_weight += weight_relations[k] + end + end + total += a.individual_total[x].to_i + end + if c["t"] == 0 + if (range[0]..range[1]).cover?(total) + a.color = c["color"] + a.save + break + end + else + avg = (total / total_weight rescue 0) + if (range[0]..range[1]).cover?(avg) + a.color = c["color"] + a.save + break + end + end + end + end + else + tmp_result_criteria.each do |c| + range = (c["r"].count != 2 ? [0,100] : c["r"]) + self.survey_answers.where(:scored_points.gte=>c["r"][0]).and(:scored_points.lte=>c["r"][1]).update_all(:color=>c["c"]) + end + end + self.update(:need_update_color=>false) + end def get_answer_repeat self.needs_login && self.answer_repeat end @@ -120,7 +187,7 @@ class QuestionnaireSurvey answers =(0...survey_answers.count).map{{}} survey_answers.each_with_index do |answer,i| answers[i]["name"] = User.find(answer.user).member_name rescue "" - answers[i]["agree"] = I18n.t("survey.#{answer.consent_used}") + answers[i]["agree"] = I18n.t("self.#{answer.consent_used}") end SurveysHelper.set_locale(I18n.locale) survey_questions.each do |question| @@ -378,6 +445,7 @@ class QuestionnaireSurvey end def update_answer_score if self.survey_questions.where(:need_update_score => true).count != 0 + self.update(:need_update_color=>true) puts "Updating answer's scores" tmp_weights = [] tmp_score_data = self.survey_questions.map do |question| diff --git a/app/models/survey_answer.rb b/app/models/survey_answer.rb index bf27829..25b7f6e 100644 --- a/app/models/survey_answer.rb +++ b/app/models/survey_answer.rb @@ -9,6 +9,7 @@ class SurveyAnswer field :avg_points, type: Integer field :individual_total, type: Array, :default => [] field :select_question, type: Array, :default => [] + field :color, type: String belongs_to :questionnaire_survey after_create do if self.questionnaire_survey diff --git a/app/views/admin/surveys/answer_sets.html.erb b/app/views/admin/surveys/answer_sets.html.erb index 11cf521..19a57b6 100644 --- a/app/views/admin/surveys/answer_sets.html.erb +++ b/app/views/admin/surveys/answer_sets.html.erb @@ -93,11 +93,21 @@ }