class MigrateExerciseScoreTask
  include ExercisesHelper
  include GitHelper

  def calculate exercise_id, user_login=nil
    exercise = Exercise.find_by(id: exercise_id)
    if exercise.present?
      if user_login.present?
        ex_users = exercise.exercise_users.where(commit_status: 1, user_id: User.where(login: user_login).pluck(:id))
      else
        ex_users = exercise.exercise_users.where(commit_status: 1)
      end

      ex_users.each do |ex_user|
        puts ex_user.id
        calculate_result = calculate_student_score(exercise, ex_user, ex_user.end_at)
        s_score = calculate_result[:total_score]
        total_score_subjective_score = ex_user.subjective_score < 0.0 ? 0.0 : ex_user.subjective_score
        total_score = s_score + total_score_subjective_score

        commit_option = {
          :objective_score => s_score,
          :score => total_score,
        }
        ex_user.update_attributes(commit_option)
      end
    end
  end
end