Skip to content
Snippets Groups Projects
learning_object.rb 7.1 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
#  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
  has_and_belongs_to_many :language

  belongs_to :publisher, polymorphic: true, counter_cache: true
Ayumaru's avatar
Ayumaru committed
  belongs_to :license, optional: true
Ayumaru's avatar
Ayumaru committed
  belongs_to :attachment, class_name: 'LearningObject::Attachment', optional: true
  has_one :submission, dependent: :destroy
  validates :name, :publisher, :object_type, :author, unless: :draft?, presence: true
  validates :language, unless: :draft?, :length => { :minimum => 1 }
  validates :id_dspace, presence: true, uniqueness: true, unless: :published?
  validates :terms_of_service, acceptance: true, unless: :draft?

  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, :languages], callbacks: :async, index_prefix: Socket.gethostname.downcase, merge_mappings: true, mappings: {
    learning_object: {
      properties: {
        published_at: {
          type: "date"
        }
      }
    }
  }
  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.name : ""
      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),
      languages: language.pluck(:id),
      source: source,
      state: LearningObject.states[state],
      likes: likes_count,
      downloads: downloads_count,
      review_average: review_ratings_average
  def name=(name)
    write_attribute(:name, name.strip) if !name.blank?
  end

  def should_index?
    deleted_at.nil?
  end

  def delete_index
    LearningObject.searchkick_index.remove(self)
  def categories
    get_metadata_value_of 'dc.subject.category'
  end
  def default_attachment
    return attachment unless attachment.nil?
    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.where("name NOT LIKE '%.torrent' AND (bundle_name = 'ORIGINAL' OR bundle_name = 'ORE' OR bundle_name = 'TEMP')")
      # at = attachments.first if at.nil?
      unless at.blank?
  # If a LO has more than one object, this gives the location of the main one
  def default_attachment_location
    default_attachment.try(:retrieve_url)
  def default_thumbnail
    return thumbnail unless thumbnail.blank?
    return nil if attachments.blank?
		unless default_attachment.blank?
			return default_attachment.thumbnail unless default_attachment.thumbnail.blank?
		end
    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

  def treat_complaintment
    #Uncomment the line below if an arbitrary number of complaints are necessary to suspend the LO.
    # if (Complaint.where(complainable_id: @complaint.complainable_id).count < 5)
    self.suspended!
  end

  def activity_owner
    proc { |controller, model| model.try(:publisher) || controller.try(:current_user) }
  end