试卷随机题的唯一性控制

dev_local
cxt 6 years ago
parent bf88d3d53c
commit 74ebb896a1

@ -76,28 +76,30 @@ class ExerciseController < ApplicationController
@is_teacher = User.current.allowed_to?(:as_teacher,@course) || User.current.admin? @is_teacher = User.current.allowed_to?(:as_teacher,@course) || User.current.admin?
unless @is_teacher unless @is_teacher
@exercise_user = ExerciseUser.where("user_id=? and exercise_id=?", User.current.id, @exercise.id).first ActiveRecord::Base.transaction do
if @exercise_user.nil? @exercise_user = ExerciseUser.where("user_id=? and exercise_id=?", User.current.id, @exercise.id).first
@exercise_user = ExerciseUser.create(:user_id => User.current.id, :exercise_id => @exercise.id, :start_at => Time.now, :status => false, :commit_status => 0) if @exercise_user.nil?
if @exercise.exercise_level_settings.where("num != exercise_questions_count").count > 0 @exercise_user = ExerciseUser.create(:user_id => User.current.id, :exercise_id => @exercise.id, :start_at => Time.now, :status => false, :commit_status => 0)
@exercise.create_user_question_list(@exercise_user.id) if @exercise.exercise_level_settings.where("num != exercise_questions_count").size > 0
end @exercise.create_user_question_list(@exercise_user.id)
# @exercise_user = ExerciseUser.where("user_id=? and exercise_id=?", User.current.id, @exercise.id).first end
elsif @exercise_user.start_at.nil? # @exercise_user = ExerciseUser.where("user_id=? and exercise_id=?", User.current.id, @exercise.id).first
if @exercise.exercise_level_settings.where("num != exercise_questions_count").count > 0 elsif @exercise_user.start_at.nil?
@exercise.create_user_question_list(@exercise_user.id) if @exercise.exercise_level_settings.where("num != exercise_questions_count").size > 0
@exercise.create_user_question_list(@exercise_user.id)
end
@exercise_user.update_attributes(:start_at => Time.now)
end end
@exercise_user.update_attributes(:start_at => Time.now)
end
@can_edit_excercise = can_edit_exercise @exercise, @exercise_user @can_edit_excercise = can_edit_exercise @exercise, @exercise_user
if !@can_edit_excercise && !@exercise_user.status if !@can_edit_excercise && !@exercise_user.status
time = (@exercise_user.start_at.to_i + @exercise.time.to_i * 60) > @exercise.end_time.to_i ? @exercise.end_time : Time.at(@exercise_user.start_at.to_i + @exercise.time.to_i * 60) time = (@exercise_user.start_at.to_i + @exercise.time.to_i * 60) > @exercise.end_time.to_i ? @exercise.end_time : Time.at(@exercise_user.start_at.to_i + @exercise.time.to_i * 60)
@exercise_user.update_attributes(:status => true, :end_at => time, :commit_status => 1) @exercise_user.update_attributes(:status => true, :end_at => time, :commit_status => 1)
s_score = calculate_student_score(@exercise, @exercise_user.user) s_score = calculate_student_score(@exercise, @exercise_user.user)
@exercise_user.update_attributes(:objective_score => s_score, :score => (s_score + (@exercise_user.subjective_score && @exercise_user.subjective_score > 0 ? @exercise_user.subjective_score : 0))) @exercise_user.update_attributes(:objective_score => s_score, :score => (s_score + (@exercise_user.subjective_score && @exercise_user.subjective_score > 0 ? @exercise_user.subjective_score : 0)))
if @exercise_user.user.exercise_answer.where(:exercise_question_id => @exercise.exercise_questions.where(:question_type => 4).map(&:id)).empty? if @exercise_user.user.exercise_answer.where(:exercise_question_id => @exercise.exercise_questions.where(:question_type => 4).map(&:id)).empty?
@exercise_user.update_attributes(:subjective_score => 0) @exercise_user.update_attributes(:subjective_score => 0)
end
end end
end end

@ -3,4 +3,6 @@ class ExerciseUserQuestion < ActiveRecord::Base
belongs_to :exercise_user belongs_to :exercise_user
belongs_to :exercise_question belongs_to :exercise_question
# attr_accessible :title, :body # attr_accessible :title, :body
validates_uniqueness_of :exercise_user_id, :scope => [:exercise_question_id]
end end

@ -1,30 +0,0 @@
class MigrateExerciseUserQuestions < ActiveRecord::Migration
def up
exercise = Exercise.where(:id => 1031).first
if exercise.present?
exercise.exercise_users.each do |exercise_user|
# 分配的实训题数量多于6个的撤销发布过的学生会有12个题则删除前六个题
if exercise_user.exercise_user_questions.where(:exercise_id => exercise.id).count > 6
exercise_user.exercise_user_questions[0..5].each do |question|
question.destroy
end
end
# 分数超过一百分的需要重新计算得分
if exercise_user.objective_score > 100.0
score = 0
ExerciseUserQuestion.where(exercise_user_id: exercise_user.id, exercise_id: exercise.id).each do |question|
question.exercise_shixun_challenges.each do |exercise_cha|
score += exercise_cha.exercise_shixun_answers.where(user_id => exercise_user.user_id).try(:score).to_f
end
end
exercise_user.update_attributes(score: score, objective_score: score)
end
end
end
end
def down
end
end

@ -0,0 +1,23 @@
class MigrateExerciseUserQuestions < ActiveRecord::Migration
def up
exercises = Exercise.where("id > 900")
exercises.each do |exercise|
if exercise.exercise_level_settings.where("num != exercise_questions_count").size > 0
exercise.exercise_users.each do |exercise_user|
user_question_count = exercise_user.exercise_user_questions.size
if user_question_count > exercise.questions_count
exercise_user.exercise_user_questions.
where(id: exercise_user.exercise_user_questions[exercise.questions_count...user_question_count].map(&:id)).delete_all
end
end
end
end
add_index :exercise_user_questions, [:exercise_user_id, :exercise_question_id], unique: true,
name: "index_exercise_user_id_and_exercise_question_id"
end
def down
end
end
Loading…
Cancel
Save