Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • portalmec/portalmec
  • rfhferreira/cleanning-portalmec
2 results
Show changes
Showing
with 772 additions and 85 deletions
# 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/>.
module Reviewable
extend ActiveSupport::Concern
......@@ -5,10 +24,13 @@ module Reviewable
has_many :reviews, as: :reviewable, dependent: :destroy
end
def reviewed?(user)
!reviews.where(user: user).blank?
end
def review_ratings_average
array = reviews.map(&:rating_average)
return 0.0 if array.size == 0
array.inject(0.0) { |a, e| a + e }.to_f / array.size.to_f
return 0.0 if array.size.zero?
array.inject(0.0) { |acc, elem| acc + elem }.to_f / array.size.to_f
end
end
# 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/>.
module Scoreable
extend ActiveSupport::Concern
......@@ -14,29 +33,28 @@ module Scoreable
end
def normalized_views
average_by_max(views.count, self.class.maximum(:views_count))
average_by_max(views.count, max_views)
end
def normalized_likes
average_by_max(likes.count, self.class.maximum(:likes_count))
average_by_max(likes.count, max_likes)
end
def normalized_downloads
average_by_max(downloads.count, self.class.maximum(:downloads_count))
average_by_max(downloads.count, max_downloads)
end
def normalized_shares
average_by_max(shares.count, self.class.maximum(:shares_count))
end
# def normalized_shares
# average_by_max(shares.count, self.class.maximum(:shares_count))
# end
def normalized_bookmarked
max = Bookmark.where(bookmarkable_type: self.class.name).group(:bookmarkable_id).order('count_all DESC').count
value = Bookmark.where(bookmarkable_id: id, bookmarkable_type: self.class.name).count
# def normalized_bookmarked
# max = Bookmark.where(bookmarkable_type: self.class.name).group(:bookmarkable_id).order('count_all DESC').count
# return 0.0 if max.blank?
return 0.0 if max.blank?
average_by_max(value, max.first[1])
end
# value = Bookmark.where(bookmarkable_id: id, bookmarkable_type: self.class.name).count
# average_by_max(value, max.first[1])
# end
private
......@@ -45,4 +63,21 @@ module Scoreable
value.to_f / max.to_f
end
def max_views
Rails.cache.fetch("#{self.class.name}/max_views", expires_in: 23.hours) do
self.class.maximum(:views_count)
end
end
def max_likes
Rails.cache.fetch("#{self.class.name}/max_likes", expires_in: 23.hours) do
self.class.maximum(:likes_count)
end
end
def max_downloads
Rails.cache.fetch("#{self.class.name}/max_downloads", expires_in: 23.hours) do
self.class.maximum(:downloads_count)
end
end
end
# 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/>.
module Sociable
extend ActiveSupport::Concern
included do
has_many :views, as: :viewable, dependent: :destroy
has_many :downloads, as: :downloadable, dependent: :destroy
has_many :likes, as: :likeable, dependent: :destroy
has_many :shares, as: :shareable, dependent: :destroy
end
......@@ -20,14 +38,6 @@ module Sociable
Like.where(user: user, likeable: self).destroy_all
end
def download(user)
Download.create(user: user, downloadable: self)
end
def downloaded?(user)
!Download.where(user: user, downloadable: self).blank?
end
def share(user)
Share.create(user: user, shareable: self)
end
......@@ -36,12 +46,11 @@ module Sociable
!Share.where(user: user, shareable: self).blank?
end
def view(user)
View.create(user: user, viewable: self)
def view(user, ip)
View.create(user: user, ip: ip, viewable: self)
end
def viewed?(user)
!View.where(user: user, viewable: self).blank?
end
end
# 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/>.
module Stageable
extend ActiveSupport::Concern
included do
has_many :stage_relations, as: :stageable, dependent: :destroy
has_many :educational_stages, through: :stage_relations, dependent: :destroy
end
# A stageable can add educational stages
# Examples:
# stageable.add_educational_stages(ids: [1, 2, 3])
# stageable.add_educational_stages(ids: [1])
def add_educational_stages(ids: [])
ids.each do |educational_stage_id|
educational_stage = EducationalStage.find(educational_stage_id)
self.educational_stages << educational_stage
end
end
# A stageable can remove educational stages
# Examples:
# stageable.remove_educational_stages(ids: [1, 2, 3])
def remove_educational_stages(ids: [])
ids.each do |educational_stage_id|
educational_stage = EducationalStage.find(educational_stage_id)
StageRelation.where(educational_stage: educational_stage, stageable: self).destroy_all
self.educational_stages.delete(educational_stage)
end
end
def update_educational_stages(ids: [])
stageable = self
old_ids = stageable.educational_stages.map {|es| es.id }
add_educational_stages(ids: ids - old_ids)
remove_educational_stages(ids: old_ids - ids)
end
end
# 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/>.
module Stateful
extend ActiveSupport::Concern
included do
enum state: { draft: 0, published: 1, suspended: 2 }
enum state: { draft: 0, published: 1, suspended: 2, submitted: 3 }
end
end
# 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/>.
module Subjectable
extend ActiveSupport::Concern
included do
has_many :subject_relations, as: :subjectable, dependent: :destroy
has_many :subjects, through: :subject_relations, dependent: :destroy
end
# A subjectable can add subjects
# Examples:
# subjectable.add_subjects(ids: [1, 2, 3])
# subjectable.add_subjects(ids: [1])
def add_subjects(ids: [])
ids.each do |subject_id|
subject = Subject.find(subject_id)
SubjectRelation.where(subject: subject, subjectable: self).first_or_create
self.subjects << subject
end
end
# A subjectable can remove subjects
# Examples:
# subjectable.remove_subjects(ids: [1, 2, 3])
def remove_subjects(ids: [])
ids.each do |subject_id|
subject = Subject.find(subject_id)
SubjectRelation.where(subject: subject, subjectable: self).destroy_all
self.subjects.delete(subject)
end
end
def update_subjects(ids: [])
subjectable = self
old_ids = subjectable.subjects.map { |su| su.id }
add_subjects(ids: ids - old_ids)
remove_subjects(ids: old_ids - ids)
end
end
# 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/>.
module Taggable
extend ActiveSupport::Concern
......
# 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/>.
module Tagger
extend ActiveSupport::Concern
......@@ -5,7 +24,6 @@ module Tagger
has_many :taggings, as: :tagger, dependent: :destroy
end
# ~~~~ taggable actions ~~~~ #
# An tagger can tagging learning object and collection
# Examples:
# tagger.tag(LearningObject.find(1), with: "Tag")
......@@ -13,7 +31,7 @@ module Tagger
def tag(taggable, with: [])
with.each do |tag_name|
tag = Tag.where(name: tag_name).first_or_create
Tagging.where(tag: tag, tagger_id: self.id, tagger_type: self.class.name, taggable_id: taggable, taggable_type: taggable.class.name).first_or_create
Tagging.where(tag: tag, tagger_id: id, tagger_type: self.class.name, taggable_id: taggable, taggable_type: taggable.class.name).first_or_create
end
end
......@@ -21,12 +39,16 @@ module Tagger
# Examples:
# tagger.untag(LearningObject.find(1), "Tag")
# tagger.untag(Collection.find(1), "Tag")
def untag(taggable, tag_name)
tag = Tag.find_by_name(tag_name)
Tagging.where(tagger: self, tag: tag, taggable: taggable).destroy_all
def untag(taggable, with: [])
with.each do |tag_name|
tag = Tag.find_by(name: tag_name)
Tagging.where(tagger: self, tag: tag, taggable: taggable).destroy_all
end
end
# ~~~~ end taggable actions ~~~~ #
def update_tags(taggable, with: [])
old_tags = taggable.tags.map { |t| t.name }
tag(taggable, with: with - old_tags)
untag(taggable, with: old_tags - with)
end
end
# 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/>.
module Thumbnailable
extend ActiveSupport::Concern
......@@ -5,5 +24,4 @@ module Thumbnailable
has_attached_file :thumbnail, styles: { medium: '530x300', small: '250x140' }, default_url: ''
validates_attachment_content_type :thumbnail, content_type: ['image/jpg', 'image/jpeg', 'image/png', 'image/gif']
end
end
# 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/>.
module Trackable
extend ActiveSupport::Concern
include PublicActivity::Model
include PublicActivity::Common
include ActivitiesFilterService
included do
tracked owner: proc { |controller, model|
model.try(:user) || model.try(:owner) || controller.try(:current_user) }
after_create :new_create_activity
before_destroy :new_destroy_activity
after_update :new_update_activity
end
def new_update_activity
return nil if previous_changes.blank?
return new_activity(:update) if ignore_changes == %w(updated_at)
filtered = previous_changes.reject { |x| ignore_changes.include?(x) }
new_activity(:update) unless filtered.empty?
end
def new_create_activity
new_activity(:create)
end
def new_destroy_activity
params = { name: self.try(:name)}
new_activity(:destroy,params)
end
def new_activity(action,params=nil)
create_activity(
action,
owner: activity_owner,
recipient: activity_recipient,
privacy: activity_privacy,
parameters: params
)
end
def ignore_changes
%w(updated_at)
end
def activity_owner
proc { |controller, model| model.try(:user) || model.try(:owner) || controller.try(:current_user) }
end
def activity_recipient
proc { |_controller, model| model.try(:recipient) || model }
end
def activity_privacy
proc { |_controller, model| model.try(:privacy) || model.try(:recipient).try(:privacy) || 'public' }
end
end
# 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: contacts
#
# id :integer not null, primary key
# name :string
# email :string
# message :text
#
class Contact < ApplicationRecord
end
# 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: curator_assignments
# t.integer "submission_id"
# t.integer "user_id"
# t.integer "status", default: 0
# t.datetime "created_at", null: false
# t.datetime "updated_at", null: false
class CuratorAssignment < ApplicationRecord
include Trackable
enum status: [:assigned, :answered, :ignored]
belongs_to :submission
belongs_to :user
validates :submission, :user, presence: true
after_update :update_activities
def update_activities
if status_changed? && self.ignored?
PublicActivity::Activity.where(trackable: self).update(parameters: { status: status })
end
end
def activity_privacy
'private'
end
def activity_recipient
submission.learning_object
end
end
# 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: downloads
......@@ -8,14 +27,24 @@
# user_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
# ip :string not null
class Download < ApplicationRecord
# *current_user* download *downloadable*
include Trackable
belongs_to :downloadable, polymorphic: true, counter_cache: true
belongs_to :user
belongs_to :user, optional: true
validates :ip, :downloadable, presence: true
def recipient
downloadable
end
private
validates_presence_of :user, :downloadable
def new_create_activity
new_activity(:create) unless user.nil?
end
end
# 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/>.
class EducationalStage < ApplicationRecord
has_many :stage_relations
has_and_belongs_to_many :searches
validates_presence_of :name
end
# 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/>.
class Email < ApplicationRecord
# attr_accessor :subject, :body, :roles, :emails, :all_users
has_and_belongs_to_many :roles
belongs_to :user
validates :emails, presence: true, if: :validate_emails?
after_save :set_emails, unless: :validate_emails?
def validate_emails?
!all_users && roles.blank?
end
def set_emails
merged_emails = self.emails
if all_users
# concat users emails to saved emails
merged_emails = merged_emails | User.pluck(:email)
else
roles.each do |role|
role_emails = role.users.pluck(:email)
merged_emails = merged_emails | role_emails
end
end
self.update_columns(emails: merged_emails)
end
end
# 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: follows
......@@ -19,4 +38,8 @@ class Follow < ApplicationRecord
validates_presence_of :user, :followable
validates :user_id, uniqueness: { scope: [:followable_id, :followable_type] }
def recipient
followable
end
end
# 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: institutions
......@@ -14,17 +33,16 @@
class Institution < ApplicationRecord
include Tagger
# *current_user* add member for instituion *name*
# *current_user* create instituion
# *current_user* destroy instituion
# *current_user* update instituion
include Trackable
include Publisher
has_and_belongs_to_many :users
has_many :learning_objects, as: :publisher
has_many :collections, as: :owner
has_attached_file :avatar, styles: { medium: '300x300>', thumb: '60x60>' }, default_url: ''
validates_attachment_content_type :avatar, content_type: ['image/jpg', 'image/jpeg', 'image/png', 'image/gif']
validates_presence_of :name
acts_as_paranoid
......
# 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: languages
......@@ -10,7 +29,7 @@
#
class Language < ApplicationRecord
has_many :learning_objects
has_and_belongs_to_many :learning_objects
validates_uniqueness_of :code
validates_presence_of :name, :code
......
# 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
......@@ -14,7 +33,6 @@
# 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
......@@ -34,55 +52,94 @@ class LearningObject < ApplicationRecord
include Metadatable
include Reviewable
include Sociable
include Downloadable
include Stateful
include Scoreable
include Thumbnailable
include Taggable
include Subjectable
include Stageable
include Highlights
include Complainable
# *current_user* create learning object
# *current_user* update learning object
# *current_user* destroy learning object
include Trackable
has_many :collection_items, as: :collectionable
has_paper_trail
has_many :collection_items, as: :collectionable, dependent: :destroy
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 :license
belongs_to :object_type
belongs_to :attachment, class_name: 'LearningObject::Attachment'
has_and_belongs_to_many :language
belongs_to :publisher, polymorphic: true, counter_cache: true
belongs_to :license, optional: true
belongs_to :object_type, optional: true
belongs_to :attachment, class_name: 'LearningObject::Attachment', optional: true
has_one :submission
validates_presence_of :name, :publisher, :object_type, :language, :author
validates :id_dspace, presence: true, uniqueness: true, unless: :draft?
validates :name, :publisher, :object_type, :author, presence: true, unless: :draft?
validates :language, unless: :draft?, :length => { :minimum => 1 }
validates :id_dspace, presence: true, uniqueness: true, unless: :published?
default_scope { includes(:object_type, :attachment, :attachments).order(score: :desc) }
before_destroy :delete_index
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], callbacks: :async
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"
}
}
}
}
acts_as_paranoid
scope :search_import, -> { includes(:tags) }
scope :search_import, -> { includes(:object_type, :tags, :subjects, :educational_stages, :publisher) }
def search_data
source = (!publisher.nil? && publisher.is_a?(Institution)) ? publisher.name : nil
type = object_type.try(:name)
source = !publisher.nil? ? publisher.name : ""
type = object_type.try(:id)
{
name: name,
description: description,
author: author,
object_type: type,
score: score,
published_at: published_at,
tags: tags.map(&:name),
source: source,
state: LearningObject.states[state]
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
}
end
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)
end
def categories
get_metadata_value_of 'dc.subject.category'
end
......@@ -91,18 +148,31 @@ class LearningObject < ApplicationRecord
return attachment unless attachment.nil?
unless attachments.blank?
at = attachments.find_by(bundle_name: 'ORIGINAL')
at = attachments.first if at.nil?
update(attachment: at)
return attachment
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?
update(attachment: at.first)
return attachment
end
end
nil
end
# 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)
end
def default_thumbnail
return thumbnail unless thumbnail.blank?
return nil if attachments.blank?
return default_attachment.thumbnail unless default_attachment.thumbnail.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
......@@ -115,14 +185,14 @@ class LearningObject < ApplicationRecord
default_attachment.retrieve_cache_link
end
def url_reference
get_metadata_value_of 'dc.object.url'
# Download link with all relevant objects (currently only one)
def download_link
return link unless link.blank?
default_attachment_location
end
## checks if learning object link to an url.
# returns boolean
def has_url_reference?
!url_reference.blank?
def ignore_changes
super + %w(score views_count downloads_count likes_count shares_count)
end
## score methods
......@@ -139,4 +209,30 @@ class LearningObject < ApplicationRecord
publisher.try('user_category')
end
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 complaint_accept(params)
self.destroy
end
def complaint_reject(params)
self.published!
end
def activity_owner
proc { |controller, model| model.try(:publisher) || controller.try(:current_user) }
end
end
# 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_object_attachments
......@@ -26,8 +45,14 @@ class LearningObject::Attachment < ApplicationRecord
include ::Thumbnailable
belongs_to :learning_object
scope :unknown_mime_type, ->() { where(format: 'Unknown') }
has_one :learning_object_attachment, as: :learning_object_attachment_id_son
belongs_to :learning_object_attachment, optional: true
def retrieve_url
return retrieve_link if bundle_name == "ORE" || bundle_name == "LINK"
return retrieve_link.sub("public", "") if bundle_name == "TEMP"
return "#{DspaceService.link}#{retrieve_link}" if retrieve_link.include? "rest"
"#{DspaceService.link}/rest#{retrieve_link}"
end
......