问卷调整

dev_forge
cxt 6 years ago
parent f3e1548686
commit d53823223e

@ -6,119 +6,117 @@ class PollVotesController < ApplicationController
def create #每一次答案的点击,请求一次 def create #每一次答案的点击,请求一次
ActiveRecord::Base.transaction do begin
begin question_votes = @poll_question.poll_votes
question_votes = @poll_question.poll_votes question_type = @poll_question.question_type
question_type = @poll_question.question_type question_answer_id = params[:poll_answer_id] ? params[:poll_answer_id] : -1 #该答案的id
question_answer_id = params[:poll_answer_id] ? params[:poll_answer_id] : -1 #该答案的id question_answer_text = params[:vote_text].present? ? params[:vote_text] : nil #其他选项的内容
question_answer_text = params[:vote_text].present? ? params[:vote_text] : nil #其他选项的内容 user_votes = question_votes.find_current_vote("user_id",current_user.id) #当前用户的答案,可能有多个
user_votes = question_votes.find_current_vote("user_id",current_user.id) #当前用户的答案,可能有多个 # 当前用户的当前答案,如果已存在,当再次点击的时候,取消答案,即删除该答案
# 当前用户的当前答案,如果已存在,当再次点击的时候,取消答案,即删除该答案 current_vote_text = nil
current_vote_text = nil
# if user_votes.find_vote_text.present? # if user_votes.find_vote_text.present?
# current_vote_text = user_votes.find_vote_text.first # current_vote_text = user_votes.find_vote_text.first
# end # end
vote_answer_params = { vote_answer_params = {
:user_id => current_user.id, :user_id => current_user.id,
:poll_question_id => @poll_question.id, :poll_question_id => @poll_question.id,
:poll_answer_id => question_answer_id, :poll_answer_id => question_answer_id,
:vote_text => question_answer_text :vote_text => question_answer_text
} }
#begin #begin
if question_type == 1 if question_type == 1
if user_votes.present? #用户曾经回答过的,答案选择不一样,否则新建 if user_votes.present? #用户曾经回答过的,答案选择不一样,否则新建
current_user_answer = user_votes.first current_user_answer = user_votes.first
if current_user_answer&.poll_answer_id != question_answer_id #如果说更换了答案,则以前的答案删除,并新建记录 if current_user_answer&.poll_answer_id != question_answer_id #如果说更换了答案,则以前的答案删除,并新建记录
current_user_answer.destroy current_user_answer.destroy
PollVote.create(vote_answer_params) PollVote.create(vote_answer_params)
else else
if question_answer_text.present? if question_answer_text.present?
current_user_answer.update_attribute("vote_text", question_answer_text) current_user_answer.update_attribute("vote_text", question_answer_text)
end
end end
else
PollVote.create(vote_answer_params)
end end
elsif question_type == 2 #多选题的话答案应该是1个以上 else
question_answer_ids = params[:poll_answer_id] ? params[:poll_answer_id] : [] #该答案的id PollVote.create(vote_answer_params)
if question_answer_ids.present? end
if question_answer_text.present? #有文字输入,但是不存在其他选项的 elsif question_type == 2 #多选题的话答案应该是1个以上
ques_vote_id = question_answer_ids.map(&:to_i).max question_answer_ids = params[:poll_answer_id] ? params[:poll_answer_id] : [] #该答案的id
if user_votes.find_vote_text.present? if question_answer_ids.present?
current_vote_text = user_votes.find_vote_text.first if question_answer_text.present? #有文字输入,但是不存在其他选项的
current_vote_text.update_attribute("vote_text", question_answer_text) ques_vote_id = question_answer_ids.map(&:to_i).max
else if user_votes.find_vote_text.present?
answer_option = { current_vote_text = user_votes.find_vote_text.first
:user_id => current_user.id, current_vote_text.update_attribute("vote_text", question_answer_text)
:poll_question_id => @poll_question.id, else
:poll_answer_id => ques_vote_id, answer_option = {
:vote_text => question_answer_text :user_id => current_user.id,
} :poll_question_id => @poll_question.id,
PollVote.create(answer_option) :poll_answer_id => ques_vote_id,
end :vote_text => question_answer_text
# if current_vote_text.present? #已有其他输入文字的选项 }
# current_vote_text.update_attribute("vote_text", question_answer_text) PollVote.create(answer_option)
# else
#
# end
end end
# if current_vote_text.present? #已有其他输入文字的选项
# current_vote_text.update_attribute("vote_text", question_answer_text)
# else
#
# end
end
ea_ids = user_votes.pluck(:poll_answer_id) ea_ids = user_votes.pluck(:poll_answer_id)
common_answer_ids = question_answer_ids & ea_ids #已经存在的试卷选项id common_answer_ids = question_answer_ids & ea_ids #已经存在的试卷选项id
new_ids = question_answer_ids - common_answer_ids # 新增的id new_ids = question_answer_ids - common_answer_ids # 新增的id
old_ids = ea_ids - common_answer_ids #没有选择的,则删掉 old_ids = ea_ids - common_answer_ids #没有选择的,则删掉
if new_ids.size > 0 if new_ids.size > 0
new_ids.each do |e| new_ids.each do |e|
answer_option = { answer_option = {
:user_id => current_user.id, :user_id => current_user.id,
:poll_question_id => @poll_question.id, :poll_question_id => @poll_question.id,
:poll_answer_id => e, :poll_answer_id => e,
:vote_text => nil :vote_text => nil
} }
ex_a = PollVote.new(answer_option) ex_a = PollVote.new(answer_option)
ex_a.save! ex_a.save!
end
end
if old_ids.size > 0
ea_answer = user_votes.find_current_vote("poll_answer_id",old_ids)
ea_answer.destroy_all
end end
else
user_votes.destroy_all
end end
else #主观题的输入 if old_ids.size > 0
# current_vote_text = user_votes.find_vote_text ea_answer = user_votes.find_current_vote("poll_answer_id",old_ids)
if user_votes.present? ea_answer.destroy_all
user_votes.first.update_attribute("vote_text", question_answer_text)
# if question_answer_text.present?
# user_votes.first.update_attribute("vote_text", question_answer_text)
# else
# user_votes.destroy_all
# end
else
PollVote.create(vote_answer_params)
end end
else
user_votes.destroy_all
end end
@current_question_number = @poll_question.question_number else #主观题的输入
@current_question_necessary = @poll_question.is_necessary # current_vote_text = user_votes.find_vote_text
#问答记录存在,且有值,才会有返回值。 if user_votes.present?
@current_question_status = 0 user_votes.first.update_attribute("vote_text", question_answer_text)
new_user_votes = question_votes.where(user_id: current_user.id) # if question_answer_text.present?
if new_user_votes.present? # user_votes.first.update_attribute("vote_text", question_answer_text)
vote_answer_id = new_user_votes.pluck(:poll_answer_id).reject(&:blank?).size # else
vote_text_count = new_user_votes.pluck(:vote_text).reject(&:blank?).size # user_votes.destroy_all
if vote_text_count > 0 || vote_answer_id > 0 # end
@current_question_status = 1 else
end PollVote.create(vote_answer_params)
end
end
@current_question_number = @poll_question.question_number
@current_question_necessary = @poll_question.is_necessary
#问答记录存在,且有值,才会有返回值。
@current_question_status = 0
new_user_votes = question_votes.where(user_id: current_user.id)
if new_user_votes.present?
vote_answer_id = new_user_votes.pluck(:poll_answer_id).reject(&:blank?).size
vote_text_count = new_user_votes.pluck(:vote_text).reject(&:blank?).size
if vote_text_count > 0 || vote_answer_id > 0
@current_question_status = 1
end end
rescue Exception => e
uid_logger_error(e.message)
tip_exception("页面调用失败!")
raise ActiveRecord::Rollback
end end
rescue Exception => e
uid_logger_error(e.message)
tip_exception("页面调用失败!")
raise ActiveRecord::Rollback
end end
end end

Loading…
Cancel
Save