module ExerciseQuestionsHelper
  def get_exercise_question_info(question,exercise,ex_user,ex_answerer_id)
    answered_content = []
    exercise_answers = question.exercise_answers.search_exercise_answer("user_id",ex_answerer_id)  #试卷用户的回答
    if question.question_type == Exercise::SINGLE || question.question_type == Exercise::JUDGMENT
      answered_content << exercise_answers.last&.exercise_choice_id if exercise_answers.present?
    elsif question.question_type == Exercise::MULTIPLE
      answered_content = exercise_answers.pluck(:exercise_choice_id)
    elsif question.question_type == Exercise::COMPLETION
      exercise_answers.each do |a|
        u_answer = {
            "choice_id": a.exercise_choice_id,
            "answer_text": a.answer_text
        }
        answered_content.push(u_answer)
      end
    elsif question.question_type == Exercise::SUBJECTIVE || question.question_type == Exercise::PROGRAM
      answered_content = exercise_answers.pluck(:answer_text)
    end
    if (question.question_type == Exercise::PRACTICAL || question.question_type == Exercise::PROGRAM) && ex_user.present? && ex_user.commited? #存在实训题，且用户已提交了的，如果实训题只做了一半就关闭，则相当于不要了
      if exercise.exercise_status == 3  #如果试卷已截止，则可以看到分数，否则不能查看分数
        shixun_type = 2
      else
        shixun_type =1
      end
    else
      shixun_type = 0
    end
    {
        "answered_content":answered_content,
        "shixun_type":shixun_type
    }
  end

  def shixun_game_scores(challenge,ex_answerer,shixun_type,question_id)
    game_score = challenge.question_score
    game_answers = challenge.exercise_shixun_answers.where(user_id:ex_answerer.id,exercise_question_id:question_id)
    if shixun_type == 2 && game_answers.present?  #试卷已截止，用户才可以查看答案
      s_score = game_answers.first.score
    else
      s_score = nil
    end
    games = ex_answerer.games.ch_games(challenge.challenge_id).uniq
    {
        "games":games,
        "s_score":s_score,
        "game_score":game_score
    }
  end

  def program_passed_code user_codes
    user_codes.where(status: 0).first&.code || user_codes.where.not(status: 0).last&.code
  end
end
