<commit_before># frozen_string_literal: true

# == Schema Information
#
# Table name: activities
#
#  id                         :integer          not null, primary key
#  data                       :jsonb
#  description                :text
#  flags                      :string           default([]), not null, is an Array
#  name                       :string
#  repeatable                 :boolean          default(TRUE)
#  supporting_info            :string
#  uid                        :string           not null
#  created_at                 :datetime
#  updated_at                 :datetime
#  activity_classification_id :integer
#  follow_up_activity_id      :integer
#  raw_score_id               :integer
#  standard_id                :integer
#  topic_id                   :integer
#
# Indexes
#
#  index_activities_on_activity_classification_id  (activity_classification_id)
#  index_activities_on_raw_score_id                (raw_score_id)
#  index_activities_on_topic_id                    (topic_id)
#  index_activities_on_uid                         (uid) UNIQUE
#
# Foreign Keys
#
#  fk_rails_...  (raw_score_id => raw_scores.id)
#  fk_rails_...  (standard_id => standards.id)
#
class Activity < ApplicationRecord
  include Flags
  include Uid

  validate :data_must_be_hash

  has_many :skill_group_activities
  has_many :skill_groups, through: :skill_group_activities
  has_many :skills, through: :skill_groups

  has_and_belongs_to_many :unit_templates

  belongs_to :classification, class_name: 'ActivityClassification', foreign_key: 'activity_classification_id'
  belongs_to :standard
  belongs_to :raw_score

  has_one :standard_level, through: :standard

  belongs_to :follow_up_activity, class_name: "Activity", foreign_key: "follow_up_activity_id"

  has_many :unit_activities, dependent: :destroy
  has_many :units, through: :unit_activities
  has_many :classroom_units, through: :units
  has_many :classrooms, through: :classroom_units
  has_many :recommendations, dependent: :destroy
  has_many :activity_category_activities, dependent: :destroy
  has_many :activity_categories, through: :activity_category_activities
  has_many :content_partner_activities, dependent: :destroy
  has_many :content_partners, :through => :content_partner_activities
  has_many :teacher_saved_activities, dependent: :destroy
  has_many :teachers, through: :teacher_saved_activities, foreign_key: 'teacher_id'
  has_many :activity_topics, dependent: :destroy
  has_many :topics, through: :activity_topics
  before_create :flag_as_beta, unless: :flags?
  after_commit :clear_activity_search_cache
  after_save :update_evidence_child_title, if: :update_evidence_title?

  delegate :form_url, to: :classification, prefix: true

  scope :production, lambda {
    where(<<-SQL, :production)
      activities.flags = '{}' OR ? = ANY (activities.flags)
    SQL
  }

  PRODUCTION = 'production'
  GAMMA = 'gamma'
  BETA = 'beta'
  ALPHA = 'alpha'
  ARCHIVED = 'archived'

  scope :gamma_user, -> { where("'#{GAMMA}' = ANY(activities.flags) OR '#{PRODUCTION}' = ANY(activities.flags)")}
  scope :beta_user, -> { where("'#{BETA}' = ANY(activities.flags) OR '#{GAMMA}' = ANY(activities.flags) OR '#{PRODUCTION}' = ANY(activities.flags)")}
  scope :alpha_user, -> { where("'#{ALPHA}' = ANY(activities.flags) OR '#{BETA}' = ANY(activities.flags) OR '#{GAMMA}' = ANY(activities.flags) OR '#{PRODUCTION}' = ANY(activities.flags)")}
  scope :not_archived, -> {where("'#{ARCHIVED}' != ANY(activities.flags)")}

  scope :with_classification, -> { includes(:classification).joins(:classification) }

  # only Grammar (2), Connect (5), and Diagnostic (4) Activities contain questions
  # the other two, Proofreader and Lesson, contain passages and other data, not questions
  ACTIVITY_TYPES_WITH_QUESTIONS = [2,4,5]

  STARTER_DIAGNOSTIC_ACTIVITY_ID = 1663
  INTERMEDIATE_DIAGNOSTIC_ACTIVITY_ID = 1668
  ADVANCED_DIAGNOSTIC_ACTIVITY_ID = 1678
  ELL_STARTER_DIAGNOSTIC_ACTIVITY_ID = 1161
  ELL_INTERMEDIATE_DIAGNOSTIC_ACTIVITY_ID = 1568
  ELL_ADVANCED_DIAGNOSTIC_ACTIVITY_ID = 1590
  PRE_TEST_DIAGNOSTIC_IDS = [STARTER_DIAGNOSTIC_ACTIVITY_ID, INTERMEDIATE_DIAGNOSTIC_ACTIVITY_ID, ADVANCED_DIAGNOSTIC_ACTIVITY_ID, ELL_STARTER_DIAGNOSTIC_ACTIVITY_ID, ELL_INTERMEDIATE_DIAGNOSTIC_ACTIVITY_ID, ELL_ADVANCED_DIAGNOSTIC_ACTIVITY_ID]

  def self.diagnostic_activity_ids
    ActivityClassification.find_by_key('diagnostic')&.activities&.pluck(:id) || []
  end

  def self.activity_with_recommendations_ids
    Rails.cache.fetch('all_recommendation_activity_ids', expires_in: 24.hours) do
      Recommendation.all.map(&:activity_id).uniq
    end
  end

  def self.find_by_id_or_uid(arg)
    find_by(uid: arg)  || find(arg)
  end
<commit_msg>Remove redundant scope (#9126)<commit_after># frozen_string_literal: true

# == Schema Information
#
# Table name: activities
#
#  id                         :integer          not null, primary key
#  data                       :jsonb
#  description                :text
#  flags                      :string           default([]), not null, is an Array
#  name                       :string
#  repeatable                 :boolean          default(TRUE)
#  supporting_info            :string
#  uid                        :string           not null
#  created_at                 :datetime
#  updated_at                 :datetime
#  activity_classification_id :integer
#  follow_up_activity_id      :integer
#  raw_score_id               :integer
#  standard_id                :integer
#  topic_id                   :integer
#
# Indexes
#
#  index_activities_on_activity_classification_id  (activity_classification_id)
#  index_activities_on_raw_score_id                (raw_score_id)
#  index_activities_on_topic_id                    (topic_id)
#  index_activities_on_uid                         (uid) UNIQUE
#
# Foreign Keys
#
#  fk_rails_...  (raw_score_id => raw_scores.id)
#  fk_rails_...  (standard_id => standards.id)
#
class Activity < ApplicationRecord
  include Flags
  include Uid

  validate :data_must_be_hash

  has_many :skill_group_activities
  has_many :skill_groups, through: :skill_group_activities
  has_many :skills, through: :skill_groups

  has_and_belongs_to_many :unit_templates

  belongs_to :classification, class_name: 'ActivityClassification', foreign_key: 'activity_classification_id'
  belongs_to :standard
  belongs_to :raw_score

  has_one :standard_level, through: :standard

  belongs_to :follow_up_activity, class_name: "Activity", foreign_key: "follow_up_activity_id"

  has_many :unit_activities, dependent: :destroy
  has_many :units, through: :unit_activities
  has_many :classroom_units, through: :units
  has_many :classrooms, through: :classroom_units
  has_many :recommendations, dependent: :destroy
  has_many :activity_category_activities, dependent: :destroy
  has_many :activity_categories, through: :activity_category_activities
  has_many :content_partner_activities, dependent: :destroy
  has_many :content_partners, :through => :content_partner_activities
  has_many :teacher_saved_activities, dependent: :destroy
  has_many :teachers, through: :teacher_saved_activities, foreign_key: 'teacher_id'
  has_many :activity_topics, dependent: :destroy
  has_many :topics, through: :activity_topics
  before_create :flag_as_beta, unless: :flags?
  after_commit :clear_activity_search_cache
  after_save :update_evidence_child_title, if: :update_evidence_title?

  delegate :form_url, to: :classification, prefix: true

  scope :production, lambda {
    where(<<-SQL, :production)
      activities.flags = '{}' OR ? = ANY (activities.flags)
    SQL
  }

  PRODUCTION = 'production'
  GAMMA = 'gamma'
  BETA = 'beta'
  ALPHA = 'alpha'
  ARCHIVED = 'archived'

  scope :gamma_user, -> { where("'#{GAMMA}' = ANY(activities.flags) OR '#{PRODUCTION}' = ANY(activities.flags)")}
  scope :beta_user, -> { where("'#{BETA}' = ANY(activities.flags) OR '#{GAMMA}' = ANY(activities.flags) OR '#{PRODUCTION}' = ANY(activities.flags)")}
  scope :alpha_user, -> { where("'#{ALPHA}' = ANY(activities.flags) OR '#{BETA}' = ANY(activities.flags) OR '#{GAMMA}' = ANY(activities.flags) OR '#{PRODUCTION}' = ANY(activities.flags)")}

  scope :with_classification, -> { includes(:classification).joins(:classification) }

  # only Grammar (2), Connect (5), and Diagnostic (4) Activities contain questions
  # the other two, Proofreader and Lesson, contain passages and other data, not questions
  ACTIVITY_TYPES_WITH_QUESTIONS = [2,4,5]

  STARTER_DIAGNOSTIC_ACTIVITY_ID = 1663
  INTERMEDIATE_DIAGNOSTIC_ACTIVITY_ID = 1668
  ADVANCED_DIAGNOSTIC_ACTIVITY_ID = 1678
  ELL_STARTER_DIAGNOSTIC_ACTIVITY_ID = 1161
  ELL_INTERMEDIATE_DIAGNOSTIC_ACTIVITY_ID = 1568
  ELL_ADVANCED_DIAGNOSTIC_ACTIVITY_ID = 1590
  PRE_TEST_DIAGNOSTIC_IDS = [STARTER_DIAGNOSTIC_ACTIVITY_ID, INTERMEDIATE_DIAGNOSTIC_ACTIVITY_ID, ADVANCED_DIAGNOSTIC_ACTIVITY_ID, ELL_STARTER_DIAGNOSTIC_ACTIVITY_ID, ELL_INTERMEDIATE_DIAGNOSTIC_ACTIVITY_ID, ELL_ADVANCED_DIAGNOSTIC_ACTIVITY_ID]

  def self.diagnostic_activity_ids
    ActivityClassification.find_by_key('diagnostic')&.activities&.pluck(:id) || []
  end

  def self.activity_with_recommendations_ids
    Rails.cache.fetch('all_recommendation_activity_ids', expires_in: 24.hours) do
      Recommendation.all.map(&:activity_id).uniq
    end
  end

  def self.find_by_id_or_uid(arg)
    find_by(uid: arg)  || find(arg)
  end

{'fraction_non_alphanumeric': 0.09536494405966968, 'fraction_numerical': 0.007245604688332445, 'mean_word_length': 3.966137566137566, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 18, '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': '6258225', 'n_tokens_mistral': 3303, 'n_tokens_neox': 3153, 'n_words': 601}