Skip to content
Snippets Groups Projects
learning_object.rb 3.94 KiB
Newer Older
# == Schema Information
#
# Table name: learning_objects
#
#  id                     :integer          not null, primary key
#  id_dspace              :integer
#  name                   :string
#  author                 :string
#  description            :text
#  published_at           :datetime
#  score                  :float            default("0.0")
#  school_level           :integer
#  metadata               :jsonb            default("{}")
#  keywords               :text
#  publisher_id           :integer
#  publisher_type         :string
#  language_id            :integer
#  object_type_id         :integer
#  created_at             :datetime         not null
#  updated_at             :datetime         not null
#  views_count            :integer          default("0")
#  downloads_count        :integer          default("0")
#  likes_count            :integer          default("0")
#  shares_count           :integer          default("0")
#  state                  :string           default("published")
#  thumbnail_file_name    :string
#  thumbnail_content_type :string
#  thumbnail_file_size    :integer
#  thumbnail_updated_at   :datetime
#  attachment_id          :integer
#

class LearningObject < ActiveRecord::Base
  include Metadatable
  include Reviewable
  include Sociable
  include Stateful
  include Scoreable
  include Thumbnailable
  has_many :collection_items, as: :collectionable
  has_many :collections, through: :collection_items
  has_many :complaints, as: :complaintable
  has_many :attachments, class_name: 'LearningObject::Attachment', autosave: true # autosave to allow import
  belongs_to :publisher, polymorphic: true
  belongs_to :language
  belongs_to :attachment, class_name: 'LearningObject::Attachment'
Giovanne Marcelo's avatar
Giovanne Marcelo committed
  validates_presence_of :name, :publisher, :object_type, :language, :author
Matheus Agio Nerone's avatar
Matheus Agio Nerone committed
  validates :id_dspace, presence: true, uniqueness: true, unless: :is_draft?
Mateus Rambo Strey's avatar
Mateus Rambo Strey committed
  default_scope { includes(:object_type, :attachment, :attachments).order(score: :desc) }
  scope :missing_thumbnail, ->() { where(thumbnail_file_name: nil) }
  searchkick language: 'brazilian', match: :word_start, searchable: [:name, :description, :author, :object_type], callbacks: :async
Mateus Rambo Strey's avatar
Mateus Rambo Strey committed
  def search_data
    source = (!publisher.nil? && publisher.is_a?(Institution)) ? publisher.name : nil
    type = object_type.try(:name)
        name: name,
        description: description,
        author: author,
Israel Barreto Sant'Anna's avatar
Israel Barreto Sant'Anna committed
        object_type: type,
        score: score,
        published_at: published_at,
Giovanne Marcelo's avatar
Giovanne Marcelo committed
        tags: tags.map(&:name),
Israel Barreto Sant'Anna's avatar
Israel Barreto Sant'Anna committed
        source: source,
Giovanne Marcelo's avatar
Giovanne Marcelo committed
        state: state
  def categories
    get_metadata_value_of 'dc.subject.category'
  end
  def default_attachment
    return attachment unless attachment.nil?
    unless attachments.blank?
      at = attachments.where(bundle_name: 'ORIGINAL').first
      at = attachments.first if at.nil?
      update(attachment: at)
      return attachment
    end
    nil
  def default_thumbnail
    return thumbnail unless thumbnail.blank?
    return nil if attachments.blank?
    return default_attachment.thumbnail unless default_attachment.thumbnail.blank?
    attachments.each { |a| return a.thumbnail unless a.thumbnail.blank? }
    nil
  end

  def retrieve_link
    "#{default_attachment.retrieve_url}"
  end

  def cache_link
    AttachmentCacheService.new(default_attachment).link
  def url_reference
    get_metadata_value_of 'dc.object.url'
  end

Giovanne Marcelo's avatar
Giovanne Marcelo committed
  ## checks if learning object link to an url.
  # returns boolean
  def has_url_reference?
    !url_reference.blank?
  end
  ## score methods
  def normalized_collected
    max = CollectionItem.where(collectionable_type: 'LearningObject').group(:collectionable_id).order('count_all DESC').count
    value = CollectionItem.where(collectionable: self).count

    return 0.0 if max.blank?

    average_by_max(value, max.first[1])
  end

  def user_category
    publisher.try('user_category')
  end