From 74ebb896a1a39cbd20c10e47d749df2741160a01 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Wed, 15 May 2019 14:04:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=95=E5=8D=B7=E9=9A=8F=E6=9C=BA=E9=A2=98?= =?UTF-8?q?=E7=9A=84=E5=94=AF=E4=B8=80=E6=80=A7=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/exercise_controller.rb | 42 ++++++++++--------- app/models/exercise_user_question.rb | 2 + ...6070751_migrate_exercise_user_questions.rb | 30 ------------- ...5070751_migrate_exercise_user_questions.rb | 23 ++++++++++ 4 files changed, 47 insertions(+), 50 deletions(-) delete mode 100644 db/migrate/20190506070751_migrate_exercise_user_questions.rb create mode 100644 db/migrate/20190515070751_migrate_exercise_user_questions.rb diff --git a/app/controllers/exercise_controller.rb b/app/controllers/exercise_controller.rb index 202752b7..56c8c4ed 100755 --- a/app/controllers/exercise_controller.rb +++ b/app/controllers/exercise_controller.rb @@ -76,28 +76,30 @@ class ExerciseController < ApplicationController @is_teacher = User.current.allowed_to?(:as_teacher,@course) || User.current.admin? unless @is_teacher - @exercise_user = ExerciseUser.where("user_id=? and exercise_id=?", User.current.id, @exercise.id).first - 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) - if @exercise.exercise_level_settings.where("num != exercise_questions_count").count > 0 - @exercise.create_user_question_list(@exercise_user.id) - end - # @exercise_user = ExerciseUser.where("user_id=? and exercise_id=?", User.current.id, @exercise.id).first - elsif @exercise_user.start_at.nil? - if @exercise.exercise_level_settings.where("num != exercise_questions_count").count > 0 - @exercise.create_user_question_list(@exercise_user.id) + ActiveRecord::Base.transaction do + @exercise_user = ExerciseUser.where("user_id=? and exercise_id=?", User.current.id, @exercise.id).first + 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) + if @exercise.exercise_level_settings.where("num != exercise_questions_count").size > 0 + @exercise.create_user_question_list(@exercise_user.id) + end + # @exercise_user = ExerciseUser.where("user_id=? and exercise_id=?", User.current.id, @exercise.id).first + elsif @exercise_user.start_at.nil? + 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 - @exercise_user.update_attributes(:start_at => Time.now) - end - @can_edit_excercise = can_edit_exercise @exercise, @exercise_user - 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) - @exercise_user.update_attributes(:status => true, :end_at => time, :commit_status => 1) - 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))) - 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) + @can_edit_excercise = can_edit_exercise @exercise, @exercise_user + 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) + @exercise_user.update_attributes(:status => true, :end_at => time, :commit_status => 1) + 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))) + 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) + end end end diff --git a/app/models/exercise_user_question.rb b/app/models/exercise_user_question.rb index 136a187f..ab8c5f84 100755 --- a/app/models/exercise_user_question.rb +++ b/app/models/exercise_user_question.rb @@ -3,4 +3,6 @@ class ExerciseUserQuestion < ActiveRecord::Base belongs_to :exercise_user belongs_to :exercise_question # attr_accessible :title, :body + + validates_uniqueness_of :exercise_user_id, :scope => [:exercise_question_id] end diff --git a/db/migrate/20190506070751_migrate_exercise_user_questions.rb b/db/migrate/20190506070751_migrate_exercise_user_questions.rb deleted file mode 100644 index 3417cf67..00000000 --- a/db/migrate/20190506070751_migrate_exercise_user_questions.rb +++ /dev/null @@ -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 diff --git a/db/migrate/20190515070751_migrate_exercise_user_questions.rb b/db/migrate/20190515070751_migrate_exercise_user_questions.rb new file mode 100644 index 00000000..75f3c29b --- /dev/null +++ b/db/migrate/20190515070751_migrate_exercise_user_questions.rb @@ -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