<commit_before>class QuizRegrader::Submission

  attr_reader :submission, :question_regrades

  def initialize(hash)
    @submission        = hash.fetch(:submission)
    @question_regrades = hash.fetch(:question_regrades)
  end

  def regrade!
    return unless answers_to_grade.size > 0

    # regrade all previous versions
    submission.attempt_versions.each do |version|
      QuizRegrader::AttemptVersion.new(
        :version           => version,
        :question_regrades => question_regrades).regrade!
    end

    # save this version
    rescored_submission.save_with_versioning!
  end

  def rescored_submission
    previous_score = submission.score_before_regrade || submission.score
    submission.score += answers_to_grade.map(&:regrade!).inject(&:+)
    submission.score_before_regrade = previous_score
    submission.quiz_data = regraded_question_data
    submission
  end

  private

  def answers_to_grade
    @answers_to_grade ||= submitted_answers.map do |answer|
      QuizRegrader::Answer.new(answer, question_regrades[answer[:question_id]])
    end
  end

  def submitted_answers
    @submitted_answers ||= submission.submission_data.select do |answer|
      question_regrades[answer[:question_id]].present?
    end
  end

  def submitted_answer_ids
    @submitted_answer_ids ||= submitted_answers.map {|q| q[:question_id] }.to_set
  end

  def regraded_question_data
    submission.quiz_data.map do |question|
      id = question[:id]
      if submitted_answer_ids.include?(id)
        question.keep_if {|k, v| %w{id position published_at}.include?(k) }<commit_msg>fix issue regrading questions added to a quiz after a submission has been made<commit_after>class QuizRegrader::Submission

  attr_reader :submission, :question_regrades

  def initialize(hash)
    @submission        = hash.fetch(:submission)
    @question_regrades = hash.fetch(:question_regrades)
  end

  def regrade!
    return unless answers_to_grade.size > 0

    # regrade all previous versions
    submission.attempt_versions.each do |version|
      QuizRegrader::AttemptVersion.new(
        :version           => version,
        :question_regrades => question_regrades).regrade!
    end

    # save this version
    rescored_submission.save_with_versioning!
  end

  def rescored_submission
    previous_score = submission.score_before_regrade || submission.score
    submission.score += answers_to_grade.map(&:regrade!).inject(&:+) || 0
    submission.score_before_regrade = previous_score
    submission.quiz_data = regraded_question_data
    submission
  end

  private

  def answers_to_grade
    @answers_to_grade ||= submitted_answers.map do |answer|
      QuizRegrader::Answer.new(answer, question_regrades[answer[:question_id]])
    end
  end

  def submitted_answers
    @submitted_answers ||= submission.submission_data.select do |answer|
      question_regrades[answer[:question_id]].present?
    end
  end

  def submitted_answer_ids
    @submitted_answer_ids ||= submitted_answers.map {|q| q[:question_id] }.to_set
  end

  def regraded_question_data
    submission.quiz_data.map do |question|
      id = question[:id]
      if submitted_answer_ids.include?(id)
        question.keep_if {|k, v| %w{id position published_at}.include?(k) }
{'fraction_non_alphanumeric': 0.088, 'fraction_numerical': 0.0009230769230769231, 'mean_word_length': 3.9108761329305137, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1534529', 'n_tokens_mistral': 1042, 'n_tokens_neox': 1011, 'n_words': 198}