diff --git a/app/controllers/exercise_controller.rb b/app/controllers/exercise_controller.rb index 202752b7..c9ddb907 100644 --- a/app/controllers/exercise_controller.rb +++ b/app/controllers/exercise_controller.rb @@ -74,51 +74,49 @@ class ExerciseController < ApplicationController # end # end @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) + ActiveRecord::Base.transaction do + 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) + elsif @exercise_user.start_at.nil? + @exercise_user.update_attributes(:start_at => Time.now) 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) 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 - 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 - if @exercise_user.commit_status == 1 && setting_time.end_time > Time.now - if @exercise.time == -1 - @cancel_commit = true - else - @cancel_commit = (Time.now.to_i - @exercise_user.start_at.to_i) < @exercise.time.to_i * 60 + 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 + if @exercise_user.commit_status == 1 && setting_time.end_time > Time.now + if @exercise.time == -1 + @cancel_commit = true + else + @cancel_commit = (Time.now.to_i - @exercise_user.start_at.to_i) < @exercise.time.to_i * 60 + end end + + #score = calculate_student_score(@exercise, User.current) + #@exercise_user.update_attributes(:objective_score => score, :score => (score + (@exercise_user.subjective_score > 0 ? @exercise_user.subjective_score : 0))) + else + @exercise_user = ExerciseUser.where("user_id=? and exercise_id=?", params[:user_id], @exercise.id).first + @can_edit_excercise = false end - #score = calculate_student_score(@exercise, User.current) - #@exercise_user.update_attributes(:objective_score => score, :score => (score + (@exercise_user.subjective_score > 0 ? @exercise_user.subjective_score : 0))) - else - @exercise_user = ExerciseUser.where("user_id=? and exercise_id=?", params[:user_id], @exercise.id).first - @can_edit_excercise = false + @exercise_questions = @exercise.user_question_list @exercise_user.id end - @exercise_questions = @exercise.user_question_list @exercise_user.id # @percent = get_percent(@exercise,User.current) #@exercise_questions = @exercise.exercise_questions