试卷加索引、更改算分规则

dev_jupyter
cxt 5 years ago
parent 3770b63a0a
commit 9ddd5f6f75

@ -26,6 +26,7 @@ class ExerciseAnswersController < ApplicationController
end end
elsif q_type == Exercise::MULTIPLE #多选题的 elsif q_type == Exercise::MULTIPLE #多选题的
choice_ids = params[:exercise_choice_id].present? ? params[:exercise_choice_id] : [] choice_ids = params[:exercise_choice_id].present? ? params[:exercise_choice_id] : []
question_choice_ids = @exercise_question.exercise_choices.pluck(:id)
ea_ids = ea.pluck(:exercise_choice_id) ea_ids = ea.pluck(:exercise_choice_id)
common_answer_ids = choice_ids & ea_ids #已经存在的试卷选项id common_answer_ids = choice_ids & ea_ids #已经存在的试卷选项id
@ -37,7 +38,8 @@ class ExerciseAnswersController < ApplicationController
:user_id => current_user.id, :user_id => current_user.id,
:exercise_question_id => @exercise_question.id, :exercise_question_id => @exercise_question.id,
:exercise_choice_id => e, :exercise_choice_id => e,
:answer_text => "" :answer_text => "",
:choice_index => question_choice_ids.index(e).to_i + 1 # choice的序号
} }
ex_a = ExerciseAnswer.new(answer_option) ex_a = ExerciseAnswer.new(answer_option)
ex_a.save! ex_a.save!
@ -52,7 +54,8 @@ class ExerciseAnswersController < ApplicationController
:user_id => current_user.id, :user_id => current_user.id,
:exercise_question_id => @exercise_question.id, :exercise_question_id => @exercise_question.id,
:exercise_choice_id => choice_id, :exercise_choice_id => choice_id,
:answer_text => answer_text :answer_text => answer_text,
:choice_index => choice_id
} }
ea_answer = ea.search_answer_users("exercise_choice_id",choice_id) ea_answer = ea.search_answer_users("exercise_choice_id",choice_id)
if ea.present? && ea_answer.present? if ea.present? && ea_answer.present?

@ -2,9 +2,11 @@ module ExerciseQuestionsHelper
def get_exercise_question_info(question,exercise,ex_user,ex_answerer_id) def get_exercise_question_info(question,exercise,ex_user,ex_answerer_id)
answered_content = [] answered_content = []
exercise_answers = question.exercise_answers.search_exercise_answer("user_id",ex_answerer_id) #试卷用户的回答 exercise_answers = question.exercise_answers.search_exercise_answer("user_id",ex_answerer_id) #试卷用户的回答
if question.question_type <= 2 if question.question_type == Exercise::SINGLE || question.question_type == Exercise::JUDGMENT
answered_content << exercise_answers.last&.exercise_choice_id if exercise_answers.present?
elsif question.question_type == Exercise::MULTIPLE
answered_content = exercise_answers.pluck(:exercise_choice_id) answered_content = exercise_answers.pluck(:exercise_choice_id)
elsif question.question_type == 3 elsif question.question_type == Exercise::COMPLETION
exercise_answers.each do |a| exercise_answers.each do |a|
u_answer = { u_answer = {
"choice_id": a.exercise_choice_id, "choice_id": a.exercise_choice_id,
@ -12,10 +14,10 @@ module ExerciseQuestionsHelper
} }
answered_content.push(u_answer) answered_content.push(u_answer)
end end
elsif question.question_type == 4 elsif question.question_type == Exercise::SUBJECTIVE
answered_content = exercise_answers.pluck(:answer_text) answered_content = exercise_answers.pluck(:answer_text)
end end
if question.question_type == 5 && ex_user.present? && ex_user.commit_status == 1 #存在实训题,且用户已提交了的,如果实训题只做了一半就关闭,则相当于不要了 if question.question_type == Exercise::PRACTICAL && ex_user.present? && ex_user.commit_status == 1 #存在实训题,且用户已提交了的,如果实训题只做了一半就关闭,则相当于不要了
if exercise.exercise_status == 3 #如果试卷已截止,则可以看到分数,否则不能查看分数 if exercise.exercise_status == 3 #如果试卷已截止,则可以看到分数,否则不能查看分数
shixun_type = 2 shixun_type = 2
else else

@ -16,8 +16,8 @@ module ExercisesHelper
end end
if q_type <= Exercise::JUDGMENT if q_type <= Exercise::JUDGMENT
if answers_content.present? #学生有回答时,分数已经全部存到exercise_answer 表,所以可以直接取第一个值 if answers_content.present? #学生有回答时,分数已经全部存到exercise_answer 表,多选题直接取第一个值,单选题和判断题选最后一个值(考虑并发)
ques_score = answers_content.first.score ques_score = q_type == Exercise::MULTIPLE ? answers_content.first.score : answers_content.last.score
ques_score = ques_score < 0 ? 0.0 : ques_score ques_score = ques_score < 0 ? 0.0 : ques_score
# answer_choice_array = [] # answer_choice_array = []
# answers_content.each do |a| # answers_content.each do |a|
@ -441,30 +441,42 @@ module ExercisesHelper
end end
if q.question_type <= 2 #为选择题或判断题时 if q.question_type <= 2 #为选择题或判断题时
if answers_content.present? #学生有回答时 if answers_content.present? #学生有回答时
answer_choice_array = [] if q.question_type == 0 || q.question_type == 2 ## 单选、判断题的算分与多选题分开计算
answers_content.each do |a| user_answer = answers_content.last.exercise_choice.choice_position
answer_choice_array.push(a.exercise_choice.choice_position) #学生答案的位置 standard_answer = q.exercise_standard_answers.first&.exercise_choice_id
end if standard_answer.present? && user_answer == standard_answer
user_answer_content = answer_choice_array.sort
standard_answer = q.exercise_standard_answers.pluck(:exercise_choice_id).sort #该问题的标准答案,可能有多个
#TODO: 旧版多选题的标准答案是放在一个里面的新版又做成了一个题有多个标准答案exercise_choice_id存放的是标准答案的位置..
if q.question_type == 1 && standard_answer.size == 1
standard_answer = standard_answer.first.to_s.split("").map(&:to_i).sort
end
if user_answer_content == standard_answer #答案一致,多选或单选才给分,答案不对不给分
if standard_answer.size > 0
q_score_1 = q.question_score q_score_1 = q.question_score
# q_score_1 = (q.question_score.to_f / standard_answer.count) #当多选答案正确时每个answer的分数均摊。 score1 = score1 + q.question_score
else else
q_score_1 = 0.0 q_score_1 = -1.0
end end
answers_content.update_all(:score => q_score_1) answers_content.last.update!(score: q_score_1)
score1 = score1 + q.question_score
else else
answers_content.update_all(:score => -1.0) answer_choice_array = []
score1 += 0.0 answers_content.each do |a|
answer_choice_array.push(a.exercise_choice.choice_position) #学生答案的位置
end
user_answer_content = answer_choice_array.sort
standard_answer = q.exercise_standard_answers.pluck(:exercise_choice_id).sort #该问题的标准答案,可能有多个
#TODO: 旧版多选题的标准答案是放在一个里面的新版又做成了一个题有多个标准答案exercise_choice_id存放的是标准答案的位置..
if q.question_type == 1 && standard_answer.size == 1
standard_answer = standard_answer.first.to_s.split("").map(&:to_i).sort
end
if user_answer_content == standard_answer #答案一致,多选或单选才给分,答案不对不给分
if standard_answer.size > 0
q_score_1 = q.question_score
# q_score_1 = (q.question_score.to_f / standard_answer.count) #当多选答案正确时每个answer的分数均摊。
else
q_score_1 = 0.0
end
answers_content.update_all(:score => q_score_1)
score1 = score1 + q.question_score
else
answers_content.update_all(:score => -1.0)
score1 += 0.0
end
end end
else else
score1 += 0.0 score1 += 0.0

@ -0,0 +1,17 @@
class AddChoiceIndexToExerciseAnswers < ActiveRecord::Migration[5.2]
def change
add_column :exercise_answers, :choice_index, :integer, default: 1
multi_questions = ExerciseQuestion.where(question_type: 1)
multi_questions.includes(:exercise_choices, :exercise_answers).find_each do |question|
exercise_answers = question.exercise_answers
exercise_answers.find_each do |answer|
choice_index = question.exercise_choices.pluck(:id).index(answer.exercise_choice_id).to_i + 1
answer.update_column('choice_index', choice_index)
end
puts "multi_questions: #{question.id}"
end
ExerciseAnswer.joins(:exercise_question).where(exercise_questions: {question_type: 3}).update_all("choice_index = exercise_choice_id")
end
end

@ -0,0 +1,13 @@
class AddChoiceIndexUniqIndexToExerciseAnswers < ActiveRecord::Migration[5.2]
def change
sql = %Q(delete from exercise_answers where (exercise_question_id, user_id, choice_index) in
(select * from (select exercise_question_id, user_id, choice_index from exercise_answers group by exercise_question_id, user_id, choice_index having count(*) > 1) a)
and id not in (select * from (select max(id) from exercise_answers group by exercise_question_id, user_id, choice_index having count(*) > 1 order by id) b))
ActiveRecord::Base.connection.execute sql
add_index :exercise_answers, [:exercise_question_id, :user_id, :choice_index], name: 'exercise_user_choice_index', unique: true
remove_index :exercise_answers, name: :exercise_choice_index
end
end
Loading…
Cancel
Save