|
|
|
@ -178,6 +178,7 @@ class ExerciseQuestionsController < ApplicationController
|
|
|
|
|
def update
|
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
|
begin
|
|
|
|
|
standard_answer_change = false
|
|
|
|
|
# 更新试卷题目的内容
|
|
|
|
|
question_options = {
|
|
|
|
|
:question_title => params[:question_title],
|
|
|
|
@ -227,96 +228,103 @@ class ExerciseQuestionsController < ApplicationController
|
|
|
|
|
if standard_answer.present?
|
|
|
|
|
if @exercise_question.question_type <= Exercise::JUDGMENT #选择题/判断题,标准答案为一个或多个
|
|
|
|
|
exercise_standard_choices = @exercise_answers_array.pluck(:exercise_choice_id) #问题以前的全部标准答案选项位置
|
|
|
|
|
common_standard_choices = standard_answer & exercise_standard_choices # 传入的标准答案的选项位置和以前的并集,即表示不用做更改的
|
|
|
|
|
old_left_standard_choices = exercise_standard_choices - common_standard_choices # 以前的差集共同的,剩余的表示需要删掉
|
|
|
|
|
new_left_standard_choices = standard_answer - common_standard_choices # 传入的标准答案差集共同的,剩余的表示需要新建
|
|
|
|
|
if old_left_standard_choices.count > 0
|
|
|
|
|
@exercise_answers_array.standard_by_ids(old_left_standard_choices).destroy_all
|
|
|
|
|
end
|
|
|
|
|
if new_left_standard_choices.count > 0 #新建标准答案
|
|
|
|
|
new_left_standard_choices.each do |s|
|
|
|
|
|
standard_option = {
|
|
|
|
|
:exercise_question_id => @exercise_question.id,
|
|
|
|
|
:exercise_choice_id => s.to_i #即为选择的位置参数
|
|
|
|
|
}
|
|
|
|
|
question_standard_answer = ExerciseStandardAnswer.new(standard_option)
|
|
|
|
|
question_standard_answer.save
|
|
|
|
|
if exercise_standard_choices.sort != standard_answer.sort #表示答案有更改的
|
|
|
|
|
standard_answer_change = true
|
|
|
|
|
common_standard_choices = standard_answer & exercise_standard_choices # 传入的标准答案的选项位置和以前的并集,即表示不用做更改的
|
|
|
|
|
old_left_standard_choices = exercise_standard_choices - common_standard_choices # 以前的差集共同的,剩余的表示需要删掉
|
|
|
|
|
new_left_standard_choices = standard_answer - common_standard_choices # 传入的标准答案差集共同的,剩余的表示需要新建
|
|
|
|
|
if old_left_standard_choices.count > 0
|
|
|
|
|
@exercise_answers_array.standard_by_ids(old_left_standard_choices).destroy_all
|
|
|
|
|
end
|
|
|
|
|
if new_left_standard_choices.count > 0 #新建标准答案
|
|
|
|
|
new_left_standard_choices.each do |s|
|
|
|
|
|
standard_option = {
|
|
|
|
|
:exercise_question_id => @exercise_question.id,
|
|
|
|
|
:exercise_choice_id => s.to_i #即为选择的位置参数
|
|
|
|
|
}
|
|
|
|
|
question_standard_answer = ExerciseStandardAnswer.new(standard_option)
|
|
|
|
|
question_standard_answer.save
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
if standard_answer.count > 1 && @exercise_question.question_type == Exercise::SINGLE #当标准答案数大于1,且不为多选时,修改为多选
|
|
|
|
|
@exercise_question.update_attribute("question_type",Exercise::MULTIPLE)
|
|
|
|
|
elsif standard_answer.count == 1 && @exercise_question.question_type == Exercise::MULTIPLE
|
|
|
|
|
@exercise_question.update_attribute("question_type",Exercise::SINGLE)
|
|
|
|
|
end
|
|
|
|
|
if standard_answer.count > 1 && @exercise_question.question_type == Exercise::SINGLE #当标准答案数大于1,且不为多选时,修改为多选
|
|
|
|
|
@exercise_question.update_attribute("question_type",Exercise::MULTIPLE)
|
|
|
|
|
elsif standard_answer.count == 1 && @exercise_question.question_type == Exercise::MULTIPLE
|
|
|
|
|
@exercise_question.update_attribute("question_type",Exercise::SINGLE)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
elsif @exercise_question.question_type == Exercise::COMPLETION #填空题
|
|
|
|
|
old_ex_answer = @exercise_question.exercise_standard_answers #当前问题的全部标准答案
|
|
|
|
|
old_ex_answer_choice_ids = old_ex_answer.pluck(:exercise_choice_id).uniq #全部的答案数组序号
|
|
|
|
|
new_ex_answer_choice_ids = standard_answer.map {|a| a[:choice_id]}.uniq #新传入的答案数组序号
|
|
|
|
|
|
|
|
|
|
#删除多余的选项
|
|
|
|
|
if old_ex_answer_choice_ids.count > new_ex_answer_choice_ids.count #有减少的填空
|
|
|
|
|
delete_ex_answer_choice_ids = old_ex_answer_choice_ids - new_ex_answer_choice_ids
|
|
|
|
|
old_ex_answer.standard_by_ids(delete_ex_answer_choice_ids).destroy_all
|
|
|
|
|
end
|
|
|
|
|
standard_answer.each do |aa|
|
|
|
|
|
null_choice_id = aa[:choice_id]
|
|
|
|
|
null_choice_text = aa[:answer_text]
|
|
|
|
|
null_choice_text_count = null_choice_text.count #当前传入的答案数量
|
|
|
|
|
null_choice_text_count_array = (1..null_choice_text_count).to_a
|
|
|
|
|
old_ex_answer_choice_texts = old_ex_answer.pluck(:answer_text).uniq.sort
|
|
|
|
|
new_ex_answer_choice_texts = standard_answer.pluck(:answer_text).sum.uniq.sort
|
|
|
|
|
if old_ex_answer_choice_texts != new_ex_answer_choice_texts #填空题标准答案有更改时,才会更新标准答案
|
|
|
|
|
new_ex_answer_choice_ids = standard_answer.map {|a| a[:choice_id]}.uniq #新传入的答案数组序号
|
|
|
|
|
old_ex_answer_choice_ids = old_ex_answer.pluck(:exercise_choice_id).uniq #全部的答案数组序号
|
|
|
|
|
standard_answer_change = true
|
|
|
|
|
#删除多余的选项
|
|
|
|
|
if old_ex_answer_choice_ids.count > new_ex_answer_choice_ids.count #有减少的填空
|
|
|
|
|
delete_ex_answer_choice_ids = old_ex_answer_choice_ids - new_ex_answer_choice_ids
|
|
|
|
|
old_ex_answer.standard_by_ids(delete_ex_answer_choice_ids).destroy_all
|
|
|
|
|
end
|
|
|
|
|
standard_answer.each do |aa|
|
|
|
|
|
null_choice_id = aa[:choice_id]
|
|
|
|
|
null_choice_text = aa[:answer_text]
|
|
|
|
|
null_choice_text_count = null_choice_text.count #当前传入的答案数量
|
|
|
|
|
null_choice_text_count_array = (1..null_choice_text_count).to_a
|
|
|
|
|
|
|
|
|
|
ex_answer_pre = old_ex_answer.standard_by_ids(null_choice_id) #当前问题的全部答案
|
|
|
|
|
ex_answer_pre_count = ex_answer_pre.count
|
|
|
|
|
ex_answer_pre_count_array = (1..ex_answer_pre_count).to_a
|
|
|
|
|
ex_answer_pre = old_ex_answer.standard_by_ids(null_choice_id) #当前问题的全部答案
|
|
|
|
|
ex_answer_pre_count = ex_answer_pre.count
|
|
|
|
|
ex_answer_pre_count_array = (1..ex_answer_pre_count).to_a
|
|
|
|
|
|
|
|
|
|
if old_ex_answer_choice_ids.include?(null_choice_id) #以前的填空题答案包含有现在的填空序号
|
|
|
|
|
if null_choice_text_count >= ex_answer_pre_count
|
|
|
|
|
new_add_choice = null_choice_text_count_array - ex_answer_pre_count_array
|
|
|
|
|
ex_answer_pre_count_array.each do |n|
|
|
|
|
|
standard_option = {
|
|
|
|
|
:exercise_question_id => @exercise_question.id,
|
|
|
|
|
:exercise_choice_id => null_choice_id,
|
|
|
|
|
:answer_text => null_choice_text[n-1]
|
|
|
|
|
}
|
|
|
|
|
ex_answer_pre[n-1].update(standard_option)
|
|
|
|
|
end
|
|
|
|
|
if new_add_choice.count > 0 #表示有新增的
|
|
|
|
|
new_add_choice.each do |i|
|
|
|
|
|
if old_ex_answer_choice_ids.include?(null_choice_id) #以前的填空题答案包含有现在的填空序号
|
|
|
|
|
if null_choice_text_count >= ex_answer_pre_count
|
|
|
|
|
new_add_choice = null_choice_text_count_array - ex_answer_pre_count_array
|
|
|
|
|
ex_answer_pre_count_array.each do |n|
|
|
|
|
|
standard_option = {
|
|
|
|
|
:exercise_question_id => @exercise_question.id,
|
|
|
|
|
:exercise_choice_id => null_choice_id,
|
|
|
|
|
:answer_text => null_choice_text[i-1]
|
|
|
|
|
:answer_text => null_choice_text[n-1]
|
|
|
|
|
}
|
|
|
|
|
question_standard_answer = ExerciseStandardAnswer.new(standard_option)
|
|
|
|
|
question_standard_answer.save
|
|
|
|
|
ex_answer_pre[n-1].update(standard_option)
|
|
|
|
|
end
|
|
|
|
|
if new_add_choice.count > 0 #表示有新增的
|
|
|
|
|
new_add_choice.each do |i|
|
|
|
|
|
standard_option = {
|
|
|
|
|
:exercise_question_id => @exercise_question.id,
|
|
|
|
|
:exercise_choice_id => null_choice_id,
|
|
|
|
|
:answer_text => null_choice_text[i-1]
|
|
|
|
|
}
|
|
|
|
|
question_standard_answer = ExerciseStandardAnswer.new(standard_option)
|
|
|
|
|
question_standard_answer.save
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
new_delete_choice = ex_answer_pre_count_array - null_choice_text_count_array
|
|
|
|
|
null_choice_text.each_with_index do |n,index|
|
|
|
|
|
standard_option = {
|
|
|
|
|
:exercise_question_id => @exercise_question.id,
|
|
|
|
|
:exercise_choice_id => null_choice_id,
|
|
|
|
|
:answer_text => n
|
|
|
|
|
}
|
|
|
|
|
ex_answer_pre[index].update(standard_option)
|
|
|
|
|
end
|
|
|
|
|
if new_delete_choice.count > 0 #表示填空题的答案有删减的
|
|
|
|
|
new_delete_choice.each do |d|
|
|
|
|
|
ex_answer_pre[d-1].destroy
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
new_delete_choice = ex_answer_pre_count_array - null_choice_text_count_array
|
|
|
|
|
null_choice_text.each_with_index do |n,index|
|
|
|
|
|
null_choice_text.each do |n|
|
|
|
|
|
standard_option = {
|
|
|
|
|
:exercise_question_id => @exercise_question.id,
|
|
|
|
|
:exercise_choice_id => null_choice_id,
|
|
|
|
|
:answer_text => n
|
|
|
|
|
}
|
|
|
|
|
ex_answer_pre[index].update(standard_option)
|
|
|
|
|
end
|
|
|
|
|
if new_delete_choice.count > 0 #表示填空题的答案有删减的
|
|
|
|
|
new_delete_choice.each do |d|
|
|
|
|
|
ex_answer_pre[d-1].destroy
|
|
|
|
|
end
|
|
|
|
|
question_standard_answer = ExerciseStandardAnswer.new(standard_option)
|
|
|
|
|
question_standard_answer.save
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
null_choice_text.each do |n|
|
|
|
|
|
standard_option = {
|
|
|
|
|
:exercise_question_id => @exercise_question.id,
|
|
|
|
|
:exercise_choice_id => null_choice_id,
|
|
|
|
|
:answer_text => n
|
|
|
|
|
}
|
|
|
|
|
question_standard_answer = ExerciseStandardAnswer.new(standard_option)
|
|
|
|
|
question_standard_answer.save
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
@ -348,20 +356,27 @@ class ExerciseQuestionsController < ApplicationController
|
|
|
|
|
|
|
|
|
|
#当试卷已发布时(试卷的总状态),当标准答案修改时,如有已提交的学生,需重新计算分数.
|
|
|
|
|
|
|
|
|
|
if @exercise.exercise_status >= Exercise::PUBLISHED
|
|
|
|
|
if standard_answer_change && @exercise.exercise_status >= Exercise::PUBLISHED
|
|
|
|
|
ex_users_committed = @exercise.exercise_users.exercise_user_committed
|
|
|
|
|
if ex_users_committed.size > 0
|
|
|
|
|
ex_users_committed.each do |ex_user|
|
|
|
|
|
user = ex_user.user
|
|
|
|
|
objective_score = calculate_student_score(@exercise,user)[:total_score]
|
|
|
|
|
subjective_score = ex_user.subjective_score
|
|
|
|
|
total_score_subjective_score = subjective_score < 0.0 ? 0.0 : subjective_score
|
|
|
|
|
total_score = objective_score + total_score_subjective_score
|
|
|
|
|
ex_user.update_attributes(objective_score:objective_score,score:total_score)
|
|
|
|
|
update_objective_score = update_single_score(@exercise_question,ex_user.user_id,standard_answer)
|
|
|
|
|
if update_objective_score != 0
|
|
|
|
|
objective_score = ex_user.objective_score
|
|
|
|
|
subjective_score = ex_user.subjective_score
|
|
|
|
|
total_score_subjective_score = subjective_score < 0.0 ? 0.0 : subjective_score
|
|
|
|
|
total_score = objective_score + update_objective_score + total_score_subjective_score
|
|
|
|
|
ex_user.update_attributes(objective_score:objective_score,score:total_score)
|
|
|
|
|
end
|
|
|
|
|
# user = ex_user.user
|
|
|
|
|
# objective_score = calculate_student_score(@exercise,user)[:total_score]
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
normal_status(0,"试卷更新成功,因标准答案修改,需重新计算学生成绩!")
|
|
|
|
|
else
|
|
|
|
|
normal_status(0,"试卷更新成功!")
|
|
|
|
|
end
|
|
|
|
|
normal_status(0,"试卷更新成功!")
|
|
|
|
|
|
|
|
|
|
rescue Exception => e
|
|
|
|
|
uid_logger_error(e.message)
|
|
|
|
|
tip_exception("页面调用失败!")
|
|
|
|
|