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 644 additions and 30 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/>.
class EmailMailer < ApplicationMailer
default from: 'integrada.contato@mec.gov.br'
def new_email(email, to_address)
@email = email
mail(subject: @email.subject, to:to_address)
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 SuggestionsMailer < ApplicationMailer
def new_suggestion_received(suggestion)
@suggestion = suggestion
@subject = "Sugestão de " + @suggestion.name
mail(subject: @subject)
end
def suggestion_updated(suggestion)
@suggestion = suggestion
@subject = "A sugestão de " + @suggestion.name + " foi atualizada"
mail(subject: @subject)
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 TeacherMailer < ApplicationMailer
default to: 'integrada.contato@mec.gov.br'
def new_teacher_received(teacher)
@teacher = teacher
mail(subject: "Novo professor cadastrado")
end
def teacher_update_received(teacher)
@teacher = teacher
mail(subject: "Professor cadastrado atualizado")
end
def new_teacher_approved(user)
@teacher = user
mail(from: "integrada.contato@mec.gov.br", to: @teacher.email, subject: "Cadastro como professor aprovado")
end
def new_teacher_rejected(user)
@teacher = user
mail(from: "integrada.contato@mec.gov.br", to: @teacher.email, subject: "Cadastro como professor rejeitado")
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: answers
# t.integer "question_id"
# t.integer "submission_id"
# t.boolean "accepted"
# t.datetime "created_at", null: false
# t.datetime "updated_at", null: false
class Answer < ApplicationRecord
belongs_to :question
belongs_to :submission
validates :submission, :question, presence: true
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 Application < ApplicationRecord
belongs_to :user
......
# 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 ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
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: bookmarks
......@@ -20,4 +39,8 @@ class Bookmark < ApplicationRecord
validates_presence_of :user, :bookmarkable
validates :user_id, uniqueness: { scope: [:bookmarkable_id, :bookmarkable_type] }
def recipient
bookmarkable
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/>.
##Represents a software issue
class Bug
include ActiveModel::Model
......
# 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: carousels
......@@ -15,8 +34,8 @@
class Carousel < ApplicationRecord
has_attached_file :image, styles: {
larger: "600x600>",
thumbnail: "100x100>"
}, default_url: "/images/:style/missing.png"
larger: '600x600>',
thumbnail: '100x100>'
}
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
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: collections
......@@ -25,47 +44,83 @@
class Collection < ApplicationRecord
include Reviewable
include Sociable
include Downloadable
include Followable
include Scoreable
include Thumbnailable
include Taggable
include Subjectable
include Stageable
include Trackable
include Highlights
include Complainable
acts_as_paranoid
has_many :collection_items, as: :collectionable, dependent: :destroy
has_many :collection_items, -> { order("position ASC") }, as: :collectionable, dependent: :destroy
has_many :collection_items
has_many :collections, through: :collection_items, source: :collectionable, source_type: 'Collection'
has_many :learning_objects, through: :collection_items, source: :collectionable, source_type: 'LearningObject'
has_many :collection_items
belongs_to :owner, polymorphic: true
belongs_to :owner, polymorphic: true, counter_cache: true
validates :name, :owner, presence: true
validates_inclusion_of :privacy, in: %w(public private), message: "Privacy must be public or private"
validates_inclusion_of :privacy, in: %w(public private), message: 'Privacy must be public or private'
before_destroy :delete_index
scope :from_user, ->(user) { where(owner: user) }
searchkick language: 'brazilian', match: :word_start, searchable: [:name, :description, :author], callbacks: :async
searchkick language: 'brazilian', match: :word_start, searchable: [:name, :description, :author], callbacks: :async, index_prefix: Socket.gethostname.downcase, merge_mappings: true, mappings: {
collection: {
properties: {
created_at: {
type: "date"
}
}
}
}
acts_as_paranoid
has_paper_trail
def search_data
owner_name = (owner.nil? || owner.name.blank?) ? "" : owner.name
{
name: name,
description: description,
privacy: privacy,
owner: owner.name,
owner: owner_name,
tags: tags.map(&:name),
subjects: subjects.pluck(:id),
educational_stages: educational_stages.pluck(:id),
score: score,
created_at: created_at
created_at: created_at,
likes: likes_count,
downloads: downloads_count,
review_average: review_ratings_average,
empty: collection_items.empty?
}
end
def name=(name)
write_attribute(:name, name.strip) if !name.blank?
end
def should_index?
deleted_at.nil?
end
def delete_index
Collection.searchkick_index.remove(self)
end
def owner?(candidate)
owner == candidate
end
def user_own?(user)
return true if user.is_a? Institution and owner.users.include?(user)
return true if user.is_a?(Institution) && owner.users.include?(user)
return false unless user.is_a? User
user.is_admin? || owner?(user)
end
......@@ -74,8 +129,48 @@ class Collection < ApplicationRecord
privacy == 'private'
end
def add_items(items)
errors = []
items.each do |item|
col_item = CollectionItem.where(collection: self, collectionable_id: item[:id], collectionable_type: item[:type])
if col_item.blank?
i = item[:type].constantize.where(id: item[:id]).first
unless i.blank?
col_item = CollectionItem.create(collection: self, collectionable: i)
col_item.set_list_position(item[:position]) unless item[:position].nil?
else
errors << {error_type:"inexistent", id: item[:id], type: item[:type]}
end
else
errors << {error_type:"repeated", id: item[:id], type: item[:type]}
end
end
return errors
end
def delete_items(items)
items.each do |item|
CollectionItem.find(item[:id]).destroy
end
end
def really_delete_items(items)
items.each do |item|
CollectionItem.find(item[:id]).really_destroy!
end
end
## score methods
def user_category
owner.try('user_category')
end
def download_link
'/'+PackageService.link(self)
end
def ignore_changes
super + %w(score views_count downloads_count likes_count shares_count follows_count)
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: collection_items
......@@ -19,5 +38,24 @@ class CollectionItem < ApplicationRecord
belongs_to :collectionable, polymorphic: true
validates :collection, :collectionable, presence: true
validates :order, uniqueness: true, allow_blank: true
acts_as_list scope: :collection
acts_as_paranoid
def recipient
collection
end
def public?
collectionable.privacy == 'public'
end
def owner?(candidate)
collectionable.owner == candidate
end
def thumbnail
collectionable_type == 'LearningObject' ? LearningObject.find(collectionable_id).default_thumbnail : Collection.find(collectionable_id).thumbnail
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: complaints
......@@ -5,8 +24,8 @@
# id :integer not null, primary key
# description :text
# user_id :integer
# complaintable_id :integer
# complaintable_type :string
# complainable_id :integer
# complainable_type :string
# complaint_reason_id :integer
# created_at :datetime not null
# updated_at :datetime not null
......@@ -18,15 +37,21 @@ class Complaint < ApplicationRecord
belongs_to :complaint_reason
belongs_to :user
belongs_to :complaintable, polymorphic: true
belongs_to :complainable, polymorphic: true
validates_presence_of :user, :complaintable, :description, :complaint_reason
validates :user_id, uniqueness: { scope: [:complaintable_id, :complaintable_type] }
validates_presence_of :user, :complainable, :description, :complaint_reason
validates :user_id, uniqueness: { scope: [:complainable_id, :complainable_type] }
acts_as_paranoid
has_paper_trail
enum state: { complained: 0, rejected: 1, accepted: 2}
def reason
complaint_reason.reason
end
def recipient
complainable
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: complaint_reasons
......@@ -13,6 +32,8 @@ class ComplaintReason < ApplicationRecord
# Only admins can create complaint reasons
include Trackable
enum status:[:inactive, :active]
has_many :complaints
validates_presence_of :reason
......
# 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 Complainable
extend ActiveSupport::Concern
included do
has_many :complaints, as: :complainable
end
def complained?(user)
!complaints.where(user: user).blank?
end
def treat_complaintment
raise NotImplementedError
end
def complaint_accept(params)
raise NotImplementedError
end
def complaint_reject(params)
raise NotImplementedError
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 Downloadable
extend ActiveSupport::Concern
included do
has_many :downloads, as: :downloadable, dependent: :destroy
has_many :package_items, as: :packageable, dependent: :destroy
has_many :packages, through: :package_items, dependent: :destroy
end
def download(user, ip)
Download.where(user: user, ip: ip, downloadable: self).first_or_create
end
def downloaded?(user, ip)
!Download.where(user: user, ip: ip, downloadable: self).blank?
end
def download_link
raise NotImplementedError
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 Followable
extend ActiveSupport::Concern
......@@ -10,11 +29,14 @@ module Followable
Follow.where(followable: self)
end
def followed?(user)
!follows.where(user: user).blank?
end
## get all activities from user
# return an array of PublicActivity::Activity
def activities
condition = '(owner_type = :type AND owner_id = :id) OR (recipient_type = :type AND recipient_id = :id)'
PublicActivity::Activity.order('created_at DESC').where(condition, {type: self.class.to_s, id: self.id}).all
PublicActivity::Activity.order('created_at DESC').where(condition, type: self.class.to_s, id: id).all
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 Highlights
extend ActiveSupport::Concern
included do
scope :this_week, -> (limit = 1000) { where('created_at >= ?', 1.week.ago).order(score: :desc).limit(limit) }
scope :this_month, -> (limit = 1000) { where('created_at >= ?', 1.month.ago).order(score: :desc).limit(limit) }
scope :this_week, ->(limit = 1000) { where('created_at >= ?', 1.week.ago).order(score: :desc).limit(limit) }
scope :this_month, ->(limit = 1000) { where('created_at >= ?', 1.month.ago).order(score: :desc).limit(limit) }
end
end
\ No newline at end of file
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/>.
## Metadatable concern for models which have metadata
# This concern will include some methods to handle @metadata attr of application models
module Metadatable
......@@ -12,6 +31,7 @@ module Metadatable
end
def get_metadata_values_of(key)
return [] if metadata.blank?
metadata[key].blank? ? [] : metadata[key]
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 Publisher
extend ActiveSupport::Concern
included do
has_many :learning_objects, as: :publisher
has_many :collections, as: :owner
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 Reputationable
extend ActiveSupport::Concern
......@@ -10,18 +29,20 @@ module Reputationable
end
def reviews_score_average
scores = learning_objects.map(&:review_ratings_average)
scores = []
learning_objects.find_each(batch_size: 1000) do |object|
scores << object.review_ratings_average
end
return 0.0 if scores.empty?
scores.inject(0.0) { |a, e| a + e }.to_f / scores.size.to_f
scores.inject(0.0) { |acc, elem| acc + elem }.to_f / scores.size.to_f
end
def learning_objects_in_best_collections
return 0.0 if collections.nil?
average = Collection.average(:score)
collections.where('score >= ?', average).count
collections.where('score >= ?', collections_avg_score).count
end
def learning_objects_best_score
......@@ -36,7 +57,7 @@ module Reputationable
return 0.0 if reviews.nil? || reviews.size < 5
array = reviews.map(&:rates_percentage)
array.inject(0.0) { |a, e| a + e } / array.size
array.inject(0.0) { |acc, elem| acc + elem } / array.size
end
def learning_objects_recently_submitted
......@@ -46,8 +67,16 @@ module Reputationable
end
def collections_score_average
return 0.0 if collections.nil? || collections.where(privacy: 'public').count == 0
return 0.0 if collections.nil? || collections.where(privacy: 'public').count.zero?
collections.where(privacy: 'public').average(:score)
end
private
def collections_avg_score
Rails.cache.fetch("Collection/avg_score", expires_in: 23.hours) do
Collection.average(:score)
end
end
end