personal-course/app/models/course.rb

130 lines
4.6 KiB
Ruby

class Course
include Mongoid::Document
include Mongoid::Timestamps
include OrbitModel::Status
include MemberHelper
include Slug
field :title, as: :slug_title, type: String, localize: true
field :objective, localize: true
field :year, type: Integer
has_many :course_syllabus_files, :dependent => :destroy, :autosave => true
has_many :course_progress_files, :dependent => :destroy, :autosave => true
has_many :course_activity_files, :dependent => :destroy, :autosave => true
has_many :course_multimedia_files, :dependent => :destroy, :autosave => true
has_many :course_material_files, :dependent => :destroy, :autosave => true
has_many :course_supplement_files, :dependent => :destroy, :autosave => true
has_many :course_evaluation_files, :dependent => :destroy, :autosave => true
belongs_to :course_semester
belongs_to :course_category
belongs_to :member_profile
accepts_nested_attributes_for :course_syllabus_files, :allow_destroy => true
accepts_nested_attributes_for :course_progress_files, :allow_destroy => true
accepts_nested_attributes_for :course_activity_files, :allow_destroy => true
accepts_nested_attributes_for :course_multimedia_files, :allow_destroy => true
accepts_nested_attributes_for :course_material_files, :allow_destroy => true
accepts_nested_attributes_for :course_supplement_files, :allow_destroy => true
accepts_nested_attributes_for :course_evaluation_files, :allow_destroy => true
scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:year=>'desc') }
def self.get_plugin_datas_to_member(datas)
fields_to_show = [
"course_category",
"title",
"year",
]
fields_to_remove = []
pd_title = []
fields_to_show.each do |t|
if (self.fields[t].type.to_s == "String" || self.fields[t].type.to_s == "Object" rescue false)
fields_to_remove << t if (datas.where(t.to_sym.ne => nil, t.to_sym.ne => "").count == 0 rescue false)
else
fields_to_remove << t if (datas.where(t.to_sym.ne => nil).count == 0 rescue false)
end
pd_title << {
"plugin_data_title" => I18n.t("personal_course.#{t}")
} if !fields_to_remove.include?(t)
end
fields_to_show = fields_to_show - fields_to_remove
plugin_datas = datas.sort_for_frontend.collect.with_index do |p,index|
pd_data = []
fields_to_show.collect do |t|
if t == "title"
pd_data << { "data_title" => "<a href='#{OrbitHelper.url_to_plugin_show(p.to_param,'personal_course')}' target='_blank'>#{p.send(t)}" }
elsif t == "course_category"
pd_data << {"data_title" => (p.course_category.title rescue "")}
else
pd_data << { "data_title" => p.send(t) }
end
end
{
"pd_datas" => pd_data,
"type-sort" => (p.course_category.sort_position.to_i rescue 1000),
"sort-index" => index
}
end
plugin_datas = plugin_datas.sort_by{|pd| [pd["type-sort"], pd["sort-index"]]}
return [pd_title,plugin_datas]
end
def get_plugin_data(fields_to_show)
plugin_datas = []
fields_to_show.each do |field|
plugin_data = self.get_plugin_field_data(field) rescue nil
next if plugin_data.blank? or plugin_data['value'].blank?
plugin_datas << plugin_data
end
plugin_datas
end
def get_plugin_field_data(field)
case field
when "course_category"
value = self.course_category.title rescue ""
when "course_semester"
value = self.course_semester.title rescue ""
when "course_syllabus_file", "course_progress_file", "course_activity_file", "course_multimedia_file", "course_material_file", "course_supplement_file", "course_evaluation_file"
files = []
self.send(field.pluralize).each do |file|
if !file.new_record?
url = file.file.url
title = (file.title.blank? ? File.basename(file.file.path) : file.title)
files << "<li><a href='#{url}'' target='_blank'>#{title}</li>"
end
end
value = files.join("")
when "authors"
path = OrbitHelper.url_to_plugin_show(self.member_profile.to_param, 'member') rescue '#'
value = "<a href='#{path}'>#{self.member_profile.name}</a>"
else
value = self.send(field) rescue ""
end
value = (value =~ /\A#{URI::regexp(['http', 'https'])}\z/) ? "<a href='#{value}' target='blank'>#{value}</a>" : value
{
"key"=>field,
"title_class"=>"course-#{field.gsub('_','-')}-field",
"value_class"=>"course-#{field.gsub('_','-')}-value",
"title"=>I18n.t('personal_course.'+field),
"value"=>value
}
end
end