Merge branch 'dev_local' of https://bdgit.educoder.net/Hjqreturn/pgfqe6ch8 into dev_local

dev_local
daiao 6 years ago
commit 1d39f457fd

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

@ -43,11 +43,11 @@ class Exercise < ActiveRecord::Base
end end
def user_question_list exercise_user_id def user_question_list exercise_user_id
if self.exercise_user_questions.where(exercise_user_id: exercise_user_id).count == 0 if self.exercise_user_questions.where(exercise_user_id: exercise_user_id).size == 0
self.exercise_questions self.exercise_questions
else else
self.exercise_questions.where(id: self.exercise_user_questions.where(exercise_user_id: exercise_user_id). self.exercise_questions.where(id: self.exercise_user_questions.where(exercise_user_id: exercise_user_id).
pluck(:exercise_question_id)) limit(self.questions_count).pluck(:exercise_question_id))
end end
end end
@ -55,6 +55,10 @@ class Exercise < ActiveRecord::Base
self.exercise_level_settings.where(level: level).first self.exercise_level_settings.where(level: level).first
end end
def questions_count
self.exercise_questions.where("question_type != 5").size + self.shixun_question_count
end
def shixun_question_count def shixun_question_count
self.exercise_level_settings.sum(:num) self.exercise_level_settings.sum(:num)
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 > 1000")
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