Skip to content
Snippets Groups Projects
learning_object.rb 5.97 KiB
Newer Older
bfs15's avatar
bfs15 committed

# Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana
#
# This file is part of portalmec.
#
# portalmec is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# portalmec is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with portalmec.  If not, see <http://www.gnu.org/licenses/>.

 # == 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
#

Mateus Rambo Strey's avatar
Mateus Rambo Strey committed
class LearningObject < ApplicationRecord
  include Metadatable
  include Reviewable
  include Sociable
  include Stateful
  include Scoreable
  include Thumbnailable
Giovanne Marcelo's avatar
Giovanne Marcelo committed
  include Subjectable
  include Stageable
  include Complainable

  # *current_user* create learning object
  # *current_user* update learning object
  # *current_user* destroy learning object
  include Trackable
  has_paper_trail

  has_many :collection_items, as: :collectionable, dependent: :destroy
  has_many :collections, through: :collection_items
  has_many :attachments, class_name: 'LearningObject::Attachment', autosave: true # autosave to allow import
  belongs_to :publisher, polymorphic: true, counter_cache: true
  belongs_to :language
  belongs_to :license
  belongs_to :attachment, class_name: 'LearningObject::Attachment'
  validates :name, :publisher, :object_type, :language, :author, unless: :draft?, presence: true
  validates :id_dspace, presence: true, uniqueness: true, unless: :published?
  default_scope { includes(:object_type, :attachment, :attachments) }
  scope :missing_thumbnail, ->() { where(thumbnail_file_name: nil) }
  searchkick language: 'brazilian', match: :word_start, searchable: [:name, :description, :author, :object_type, :tags, :subjects, :educational_stages], callbacks: :async
  scope :search_import, -> { includes(:object_type, :tags, :subjects, :educational_stages, :publisher) }
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(:id)
      name: name,
      description: description,
      author: author,
      object_type: type,
      score: score,
      published_at: published_at,
      tags: tags.map(&:name),
      subjects: subjects.pluck(:id),
      educational_stages: educational_stages.pluck(:id),
      source: source,
      state: LearningObject.states[state],
      likes: likes_count,
      downloads: downloads_count,
      review_average: review_ratings_average
  def categories
    get_metadata_value_of 'dc.subject.category'
  end
  def default_attachment
    unless attachments.blank?
      if object_type.try(:name) == 'Vídeo'
        at = attachments.where("name LIKE '%.mp4'").first
        return at unless at.nil?
      end
      at = attachments.find_by(bundle_name: 'ORIGINAL')
      at = attachments.first if at.nil?
      update(attachment: at)
  # If a LO has more than one object, this gives the location of the main one
  def default_attachment_location
    object_type.try(:name) == ('Vídeo' || 'Áudio') ? default_attachment.try(:retrieve_cache_link) : default_attachment.try(:retrieve_url)
  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
    default_attachment.retrieve_cache_link
  # Download link with all relevant objects (currently only one)
  def download_link
    default_attachment_location
  def ignore_changes
    super + %w(score views_count downloads_count likes_count shares_count)
  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
bfs15's avatar
bfs15 committed

  def ignore_changes
    super + %w(score views_count downloads_count likes_count shares_count attachment_id)
  end

  def privacy
    return "public" if self.published?
    return "private"
  end