<commit_before>class Exam < Assignment
  validates :related_assignment_id, :absence => true
  validate :set_exam_graders
  before_update :update_exam_submission_times

  def assign_attributes(attrs)
    super(attrs)
    self.available = self.due_date
    self.lateness_config = self.course.lateness_config
  end
  
  def set_exam_graders
    return true unless self.new_record?
    upload = @assignment_file_data
    if upload.nil?
      if self.assignment_upload.nil?
        self.errors.add(:base, "Exam questions file is missing")
        return false
      else
        return true
      end
    else
      begin
        questions = YAML.load(File.read(upload.tempfile))
        upload.rewind
      rescue Psych::SyntaxError => e
        self.errors.add(:base, "Could not parse the supplied file")
        return false
      end
    end
    if !questions.is_a? Array
      self.errors.add(:base, "Supplied file does not contain a list of questions")
      return false
    else
      @total_weight = 0
      def make_err(msg)
        self.errors.add(:base, msg)
      end
      def is_float(val)
        Float(val) rescue false
      end
      def is_int(val)
        Integer(val) rescue false
      end
      questions.each_with_index do |q, q_num|
        if q["parts"].is_a? Array
          q["parts"].each_with_index do |part, p_num|
            if !is_float(part["weight"])
              make_err "Question #{part['name']} has an invalid weight"
              next
            elsif !part["extra"]
              @total_weight += Float(part["weight"])
            end
          end
        elsif !is_float(q["weight"])
          make_err "Question #{q['name']} has an invalid weight"
          next
        elsif !q["extra"]
          @total_weight += Float(q["weight"])
        end
      end
      return false if self.errors.count > 0
      grader = self.graders.first
      if grader.nil?
        grader = Grader.new(type: "ExamGrader", assignment: self)
        self.graders << grader
      end
      grader.avail_score = @total_weight
      grader.order = self.graders.count + 1
    end
  end

  def update_exam_submission_times
    self.available = self.due_date # for exams, there's no window in which "the assignment is available"
    self.submissions.update_attributes({created_at: self.due_date - 1.minute,
                                        updated_at: self.due_date - 1.minute})
  end

  def questions
    qs = YAML.load(File.read(self.assignment_upload.submission_path))
    qs.each_with_index do |q, q_num|
      q["name"] = "Problem #{q_num + 1}" unless q["name"]<commit_msg>using the wrong method<commit_after>class Exam < Assignment
  validates :related_assignment_id, :absence => true
  validate :set_exam_graders
  before_update :update_exam_submission_times

  def assign_attributes(attrs)
    super(attrs)
    self.available = self.due_date
    self.lateness_config = self.course.lateness_config
  end
  
  def set_exam_graders
    return true unless self.new_record?
    upload = @assignment_file_data
    if upload.nil?
      if self.assignment_upload.nil?
        self.errors.add(:base, "Exam questions file is missing")
        return false
      else
        return true
      end
    else
      begin
        questions = YAML.load(File.read(upload.tempfile))
        upload.rewind
      rescue Psych::SyntaxError => e
        self.errors.add(:base, "Could not parse the supplied file")
        return false
      end
    end
    if !questions.is_a? Array
      self.errors.add(:base, "Supplied file does not contain a list of questions")
      return false
    else
      @total_weight = 0
      def make_err(msg)
        self.errors.add(:base, msg)
      end
      def is_float(val)
        Float(val) rescue false
      end
      def is_int(val)
        Integer(val) rescue false
      end
      questions.each_with_index do |q, q_num|
        if q["parts"].is_a? Array
          q["parts"].each_with_index do |part, p_num|
            if !is_float(part["weight"])
              make_err "Question #{part['name']} has an invalid weight"
              next
            elsif !part["extra"]
              @total_weight += Float(part["weight"])
            end
          end
        elsif !is_float(q["weight"])
          make_err "Question #{q['name']} has an invalid weight"
          next
        elsif !q["extra"]
          @total_weight += Float(q["weight"])
        end
      end
      return false if self.errors.count > 0
      grader = self.graders.first
      if grader.nil?
        grader = Grader.new(type: "ExamGrader", assignment: self)
        self.graders << grader
      end
      grader.avail_score = @total_weight
      grader.order = self.graders.count + 1
    end
  end

  def update_exam_submission_times
    self.available = self.due_date # for exams, there's no window in which "the assignment is available"
    self.submissions.update_all({created_at: self.due_date - 1.minute,
                                 updated_at: self.due_date - 1.minute})
  end

  def questions
    qs = YAML.load(File.read(self.assignment_upload.submission_path))
    qs.each_with_index do |q, q_num|
      q["name"] = "Problem #{q_num + 1}" unless q["name"]
{'fraction_non_alphanumeric': 0.09459719284752932, 'fraction_numerical': 0.0023072486060373007, 'mean_word_length': 2.2573575453976207, 'pattern_counts': {'":': 0, '<': 9, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7634231', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1581, 'n_words': 402}