试卷随机题的唯一性控制

dev_local
cxt 6 years ago
parent bf88d3d53c
commit 74ebb896a1

@ -76,15 +76,16 @@ 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
ActiveRecord::Base.transaction do
@exercise_user = ExerciseUser.where("user_id=? and exercise_id=?", User.current.id, @exercise.id).first @exercise_user = ExerciseUser.where("user_id=? and exercise_id=?", User.current.id, @exercise.id).first
if @exercise_user.nil? if @exercise_user.nil?
@exercise_user = ExerciseUser.create(:user_id => User.current.id, :exercise_id => @exercise.id, :start_at => Time.now, :status => false, :commit_status => 0) @exercise_user = ExerciseUser.create(:user_id => User.current.id, :exercise_id => @exercise.id, :start_at => Time.now, :status => false, :commit_status => 0)
if @exercise.exercise_level_settings.where("num != exercise_questions_count").count > 0 if @exercise.exercise_level_settings.where("num != exercise_questions_count").size > 0
@exercise.create_user_question_list(@exercise_user.id) @exercise.create_user_question_list(@exercise_user.id)
end end
# @exercise_user = ExerciseUser.where("user_id=? and exercise_id=?", User.current.id, @exercise.id).first # @exercise_user = ExerciseUser.where("user_id=? and exercise_id=?", User.current.id, @exercise.id).first
elsif @exercise_user.start_at.nil? elsif @exercise_user.start_at.nil?
if @exercise.exercise_level_settings.where("num != exercise_questions_count").count > 0 if @exercise.exercise_level_settings.where("num != exercise_questions_count").size > 0
@exercise.create_user_question_list(@exercise_user.id) @exercise.create_user_question_list(@exercise_user.id)
end end
@exercise_user.update_attributes(:start_at => Time.now) @exercise_user.update_attributes(:start_at => Time.now)
@ -100,6 +101,7 @@ class ExerciseController < ApplicationController
@exercise_user.update_attributes(:subjective_score => 0) @exercise_user.update_attributes(:subjective_score => 0)
end end
end end
end
member = @exercise.course.members.where(:user_id => User.current.id).first if @exercise_user.commit_status == 1 member = @exercise.course.members.where(:user_id => User.current.id).first if @exercise_user.commit_status == 1
setting_time = exercise_group_setting @exercise, member.try(:course_group) if @exercise_user.commit_status == 1 setting_time = exercise_group_setting @exercise, member.try(:course_group) if @exercise_user.commit_status == 1

@ -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