<commit_before>class ClassroomActivity < ActiveRecord::Base
  include CheckboxCallback
  include ::NewRelic::Agent

  belongs_to :classroom
  belongs_to :activity
  belongs_to :unit, touch: true
  has_one :topic, through: :activity
  has_many :activity_sessions

  default_scope { where(visible: true) }
  scope :with_topic, ->(tid) { joins(:topic).where(topics: {id: tid}) }

  validate :not_duplicate, :on => :create

  validates_uniqueness_of :pinned, scope: :classroom_id,
    if: Proc.new { |ca| ca.pinned == true }

  before_validation :check_pinned
  after_create :assign_to_students, :lock_if_lesson
  after_save :teacher_checkbox, :assign_to_students, :hide_appropriate_activity_sessions, :update_lessons_cache

  def assigned_students
    User.where(id: assigned_student_ids)
  end

  def assign_follow_up_lesson(locked=true)
    extant_ca = ClassroomActivity.find_by(classroom_id: self.classroom_id,
                                          activity_id: self.activity.follow_up_activity_id,
                                          unit_id: self.unit_id)
    if !self.activity.follow_up_activity_id
      return false
    elsif extant_ca
      extant_ca.update(locked: false)
      return extant_ca
    end
    follow_up = ClassroomActivity.create(classroom_id: self.classroom_id,
                             activity_id: self.activity.follow_up_activity_id,
                             unit_id: self.unit_id,
                             visible: true,
                             locked: locked,
                             assigned_student_ids: self.assigned_student_ids )
    follow_up
  end


  def due_date_string= val
    self.due_date = Date.strptime(val, Time::DATE_FORMATS[:quill_default])
  end

  def due_date_string
    due_date.try(:to_formatted_s, :quill_default)
  end

  def mark_all_activity_sessions_complete
    ActivitySession.unscoped.where(classroom_activity_id: self.id).update_all(state: 'finished')
  end

  def session_for user
    ass = ActivitySession.unscoped.where(classroom_activity: self, user: user, activity: activity).includes(:activity).order(created_at: :asc)
    if ass.any?
      if ass.any? { |as| as.is_final_score }
        keeper = ass.find { |as| as.is_final_score}
      # the next two cases should not be necessary to handle
      # since the highest score should always have .is_final_score
      # and only one should be started at a time,
      # but due to some data confusion we're going to leave it in for now
      elsif ass.any? { |as| as.state == 'finished' }
        keeper = ass.find_all { |as| as.state == 'finished' }.sort_by { |as| as.percentage }.first
      elsif ass.any? { |as| as.state == 'started' }
        keeper = ass.find_all { |as| as.state == 'started' }.sort_by { |as| as.updated_at }.last
      else
        keeper = ass.sort_by { |as| as.updated_at }.last
      end
      keeper.update(visible: true)
      return keeper
    else
      activity_sessions.create(user: user, activity: activity)
    end

    # if as.save
    #
    # else
    #   if as.errors[""]
    #     begin
    #<commit_msg>Give the session a percentage of 1 upon completion.<commit_after>class ClassroomActivity < ActiveRecord::Base
  include CheckboxCallback
  include ::NewRelic::Agent

  belongs_to :classroom
  belongs_to :activity
  belongs_to :unit, touch: true
  has_one :topic, through: :activity
  has_many :activity_sessions

  default_scope { where(visible: true) }
  scope :with_topic, ->(tid) { joins(:topic).where(topics: {id: tid}) }

  validate :not_duplicate, :on => :create

  validates_uniqueness_of :pinned, scope: :classroom_id,
    if: Proc.new { |ca| ca.pinned == true }

  before_validation :check_pinned
  after_create :assign_to_students, :lock_if_lesson
  after_save :teacher_checkbox, :assign_to_students, :hide_appropriate_activity_sessions, :update_lessons_cache

  def assigned_students
    User.where(id: assigned_student_ids)
  end

  def assign_follow_up_lesson(locked=true)
    extant_ca = ClassroomActivity.find_by(classroom_id: self.classroom_id,
                                          activity_id: self.activity.follow_up_activity_id,
                                          unit_id: self.unit_id)
    if !self.activity.follow_up_activity_id
      return false
    elsif extant_ca
      extant_ca.update(locked: false)
      return extant_ca
    end
    follow_up = ClassroomActivity.create(classroom_id: self.classroom_id,
                             activity_id: self.activity.follow_up_activity_id,
                             unit_id: self.unit_id,
                             visible: true,
                             locked: locked,
                             assigned_student_ids: self.assigned_student_ids )
    follow_up
  end


  def due_date_string= val
    self.due_date = Date.strptime(val, Time::DATE_FORMATS[:quill_default])
  end

  def due_date_string
    due_date.try(:to_formatted_s, :quill_default)
  end

  def mark_all_activity_sessions_complete
    ActivitySession.unscoped.where(classroom_activity_id: self.id).update_all(state: 'finished', percentage: 1)
  end

  def session_for user
    ass = ActivitySession.unscoped.where(classroom_activity: self, user: user, activity: activity).includes(:activity).order(created_at: :asc)
    if ass.any?
      if ass.any? { |as| as.is_final_score }
        keeper = ass.find { |as| as.is_final_score}
      # the next two cases should not be necessary to handle
      # since the highest score should always have .is_final_score
      # and only one should be started at a time,
      # but due to some data confusion we're going to leave it in for now
      elsif ass.any? { |as| as.state == 'finished' }
        keeper = ass.find_all { |as| as.state == 'finished' }.sort_by { |as| as.percentage }.first
      elsif ass.any? { |as| as.state == 'started' }
        keeper = ass.find_all { |as| as.state == 'started' }.sort_by { |as| as.updated_at }.last
      else
        keeper = ass.sort_by { |as| as.updated_at }.last
      end
      keeper.update(visible: true)
      return keeper
    else
      activity_sessions.create(user: user, activity: activity)
    end

    # if as.save
    #
    # else
    #   if as.errors[""]
    #     begin
    #
{'fraction_non_alphanumeric': 0.09073390075477758, 'fraction_numerical': 0.0003211819495744339, 'mean_word_length': 2.7338129496402876, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1533867', 'n_tokens_mistral': 1994, 'n_tokens_neox': 1944, 'n_words': 458}