|
|
|
@ -2,94 +2,80 @@ class ExerciseAnswersController < ApplicationController
|
|
|
|
|
before_action :require_login, :check_auth
|
|
|
|
|
before_action :get_exercise_question
|
|
|
|
|
include ExercisesHelper
|
|
|
|
|
|
|
|
|
|
# model validation error
|
|
|
|
|
rescue_from ActiveRecord::RecordInvalid do |ex|
|
|
|
|
|
render_error(ex.record.errors.full_messages.join(','))
|
|
|
|
|
end
|
|
|
|
|
# form validation error
|
|
|
|
|
rescue_from ActiveModel::ValidationError do |ex|
|
|
|
|
|
render_error(ex.model.errors.full_messages.join(','))
|
|
|
|
|
end
|
|
|
|
|
include ControllerRescueHandler
|
|
|
|
|
|
|
|
|
|
def create #每一次答案的点击,请求一次,实训题不在这里回答
|
|
|
|
|
begin
|
|
|
|
|
q_type = @exercise_question.question_type #试卷的类型
|
|
|
|
|
choice_id = params[:exercise_choice_id].present? ? params[:exercise_choice_id] : ""
|
|
|
|
|
answer_text = params[:answer_text].present? ? params[:answer_text].strip : "" #为字符串
|
|
|
|
|
if q_type < Exercise::SUBJECTIVE && (q_type != Exercise::MULTIPLE) && choice_id.blank?
|
|
|
|
|
normal_status(-1,"请选择序号")
|
|
|
|
|
else
|
|
|
|
|
ea = @exercise_question.exercise_answers.search_answer_users("user_id",current_user.id) #试卷的当前用户的答案
|
|
|
|
|
if q_type == Exercise::SINGLE || q_type == Exercise::JUDGMENT #选择题(单选)/判断题
|
|
|
|
|
if ea.exists?
|
|
|
|
|
ea.first.update!(exercise_choice_id: choice_id )
|
|
|
|
|
else
|
|
|
|
|
answer_option = {
|
|
|
|
|
:user_id => current_user.id,
|
|
|
|
|
:exercise_question_id => @exercise_question.id,
|
|
|
|
|
:exercise_choice_id => choice_id,
|
|
|
|
|
:answer_text => ""
|
|
|
|
|
}
|
|
|
|
|
ex_a = ExerciseAnswer.new(answer_option)
|
|
|
|
|
ex_a.save!
|
|
|
|
|
end
|
|
|
|
|
elsif q_type == Exercise::MULTIPLE #多选题的
|
|
|
|
|
choice_ids = params[:exercise_choice_id].present? ? params[:exercise_choice_id] : []
|
|
|
|
|
|
|
|
|
|
ea_ids = ea.pluck(:exercise_choice_id)
|
|
|
|
|
common_answer_ids = choice_ids & ea_ids #已经存在的试卷选项id
|
|
|
|
|
new_ids = (choice_ids - common_answer_ids).uniq # 新增的id
|
|
|
|
|
old_ids = (ea_ids - common_answer_ids).uniq #没有选择的,则删掉
|
|
|
|
|
if new_ids.size > 0
|
|
|
|
|
new_ids.each do |e|
|
|
|
|
|
answer_option = {
|
|
|
|
|
:user_id => current_user.id,
|
|
|
|
|
:exercise_question_id => @exercise_question.id,
|
|
|
|
|
:exercise_choice_id => e,
|
|
|
|
|
:answer_text => ""
|
|
|
|
|
}
|
|
|
|
|
ex_a = ExerciseAnswer.new(answer_option)
|
|
|
|
|
ex_a.save!
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
if old_ids.size > 0
|
|
|
|
|
ea_answer = ea.search_answer_users("exercise_choice_id",old_ids)
|
|
|
|
|
ea_answer.destroy_all
|
|
|
|
|
end
|
|
|
|
|
elsif q_type == Exercise::COMPLETION #填空题
|
|
|
|
|
answer_option = {
|
|
|
|
|
:user_id => current_user.id,
|
|
|
|
|
:exercise_question_id => @exercise_question.id,
|
|
|
|
|
:exercise_choice_id => choice_id,
|
|
|
|
|
:answer_text => answer_text
|
|
|
|
|
}
|
|
|
|
|
ea_answer = ea.search_answer_users("exercise_choice_id",choice_id)
|
|
|
|
|
if ea.present? && ea_answer.present?
|
|
|
|
|
ea_answer.update!(answer_option)
|
|
|
|
|
else
|
|
|
|
|
ex_new = ExerciseAnswer.new(answer_option)
|
|
|
|
|
ex_new.save!
|
|
|
|
|
end
|
|
|
|
|
elsif q_type == Exercise::SUBJECTIVE #简答题
|
|
|
|
|
q_type = @exercise_question.question_type #试卷的类型
|
|
|
|
|
choice_id = params[:exercise_choice_id].present? ? params[:exercise_choice_id] : ""
|
|
|
|
|
answer_text = params[:answer_text].present? ? params[:answer_text].strip : "" #为字符串
|
|
|
|
|
if q_type < Exercise::SUBJECTIVE && (q_type != Exercise::MULTIPLE) && choice_id.blank?
|
|
|
|
|
normal_status(-1,"请选择序号")
|
|
|
|
|
else
|
|
|
|
|
ea = @exercise_question.exercise_answers.search_answer_users("user_id",current_user.id) #试卷的当前用户的答案
|
|
|
|
|
if q_type == Exercise::SINGLE || q_type == Exercise::JUDGMENT #选择题(单选)/判断题
|
|
|
|
|
if ea.exists?
|
|
|
|
|
ea.first.update!(exercise_choice_id: choice_id )
|
|
|
|
|
else
|
|
|
|
|
answer_option = {
|
|
|
|
|
:user_id => current_user.id,
|
|
|
|
|
:exercise_question_id => @exercise_question.id
|
|
|
|
|
:user_id => current_user.id,
|
|
|
|
|
:exercise_question_id => @exercise_question.id,
|
|
|
|
|
:exercise_choice_id => choice_id,
|
|
|
|
|
:answer_text => ""
|
|
|
|
|
}
|
|
|
|
|
if ea.present? #已经回答了的,
|
|
|
|
|
ea.first.update!(answer_text: answer_text)
|
|
|
|
|
else
|
|
|
|
|
answer_option.merge!(answer_text:answer_text)
|
|
|
|
|
ex_a = ExerciseAnswer.new(answer_option)
|
|
|
|
|
ex_a.save!
|
|
|
|
|
end
|
|
|
|
|
elsif q_type == Exercise::MULTIPLE #多选题的
|
|
|
|
|
choice_ids = params[:exercise_choice_id].present? ? params[:exercise_choice_id] : []
|
|
|
|
|
|
|
|
|
|
ea_ids = ea.pluck(:exercise_choice_id)
|
|
|
|
|
common_answer_ids = choice_ids & ea_ids #已经存在的试卷选项id
|
|
|
|
|
new_ids = (choice_ids - common_answer_ids).uniq # 新增的id
|
|
|
|
|
old_ids = (ea_ids - common_answer_ids).uniq #没有选择的,则删掉
|
|
|
|
|
if new_ids.size > 0
|
|
|
|
|
new_ids.each do |e|
|
|
|
|
|
answer_option = {
|
|
|
|
|
:user_id => current_user.id,
|
|
|
|
|
:exercise_question_id => @exercise_question.id,
|
|
|
|
|
:exercise_choice_id => e,
|
|
|
|
|
:answer_text => ""
|
|
|
|
|
}
|
|
|
|
|
ex_a = ExerciseAnswer.new(answer_option)
|
|
|
|
|
ex_a.save!
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
normal_status(0,"回答成功")
|
|
|
|
|
if old_ids.size > 0
|
|
|
|
|
ea_answer = ea.search_answer_users("exercise_choice_id",old_ids)
|
|
|
|
|
ea_answer.destroy_all
|
|
|
|
|
end
|
|
|
|
|
elsif q_type == Exercise::COMPLETION #填空题
|
|
|
|
|
answer_option = {
|
|
|
|
|
:user_id => current_user.id,
|
|
|
|
|
:exercise_question_id => @exercise_question.id,
|
|
|
|
|
:exercise_choice_id => choice_id,
|
|
|
|
|
:answer_text => answer_text
|
|
|
|
|
}
|
|
|
|
|
ea_answer = ea.search_answer_users("exercise_choice_id",choice_id)
|
|
|
|
|
if ea.present? && ea_answer.present?
|
|
|
|
|
ea_answer.update!(answer_option)
|
|
|
|
|
else
|
|
|
|
|
ex_new = ExerciseAnswer.new(answer_option)
|
|
|
|
|
ex_new.save!
|
|
|
|
|
end
|
|
|
|
|
elsif q_type == Exercise::SUBJECTIVE #简答题
|
|
|
|
|
answer_option = {
|
|
|
|
|
:user_id => current_user.id,
|
|
|
|
|
:exercise_question_id => @exercise_question.id
|
|
|
|
|
}
|
|
|
|
|
if ea.present? #已经回答了的,
|
|
|
|
|
ea.first.update!(answer_text: answer_text)
|
|
|
|
|
else
|
|
|
|
|
answer_option.merge!(answer_text:answer_text)
|
|
|
|
|
ex_a = ExerciseAnswer.new(answer_option)
|
|
|
|
|
ex_a.save!
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
rescue Exception => e
|
|
|
|
|
uid_logger_error(e.message)
|
|
|
|
|
tip_exception("页面调用失败!")
|
|
|
|
|
raise ActiveRecord::Rollback
|
|
|
|
|
normal_status(0,"回答成功")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|