personal-course/app/models/course.rb

169 lines
6.5 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
field :student_ids, type: Array, default: []
field :course_code, type: String, default: ""
field :rss2_id
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
has_many :course_assignments, :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
index({year: -1, _id: -1}, { unique: false, background: false })
scope :sort_year, ->{ order_by(:year => "desc", :id=>"desc") }
scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:year=>'desc', :id=>"desc") }
before_save do |record|
selected_course = SelectedCourse rescue nil
if selected_course
student_ids_remove = record.student_ids_was.to_a - record.student_ids
record.student_ids.each do |student_id|
selected_course = SelectedCourse.where(:member_profile_id => student_id,:course_id=>record.id).first
if selected_course.nil?
SelectedCourse.create(:member_profile => MemberProfile.find(student_id) , :course_id=>record.id , :year=>record.year,:course_title_translations=>record.title_translations)
else
selected_course.update(:year=>record.year,:course_title_translations=>record.title_translations)
end
end
student_ids_remove.each do |student_id|
SelectedCourse.where(:member_profile_id => student_id,:course_id=>record.id).destroy
end
end
end
before_destroy do |record|
selected_course = SelectedCourse rescue nil
if selected_course
SelectedCourse.where(:course_id=>record.id).destroy
end
end
def students
return MemberProfile.where(:id.in=>self.student_ids)
end
def display_students
return MemberProfile.where(:id.in=>self.student_ids).map{|m| m.name}.join(", ")
end
def self.get_plugin_datas_to_member(datas)
fields_to_show = [
"course_category",
"course_code",
"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,idx|
pd_data = []
fields_to_show.collect do |t|
if t == "title"
link = OrbitHelper.url_to_plugin_show(p.to_param,'personal_course')
url_to_plugin_show_blank = OrbitHelper.instance_variable_get(:@url_to_plugin_show_blank)
tmp_title = p.title
pd_data << { "data_title" => (url_to_plugin_show_blank ? tmp_title : "<a title=\"#{tmp_title}\" href=\"#{link}\" target=\"_blank\">#{tmp_title}</a>") }
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" => idx
}
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>"
when "students"
value = self.display_students
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