220 lines
9.0 KiB
Ruby
220 lines
9.0 KiB
Ruby
class SelectedCourse
|
|
include Mongoid::Document
|
|
include Mongoid::Timestamps
|
|
include OrbitModel::Status
|
|
include MemberHelper
|
|
include Slug
|
|
|
|
field :course_id
|
|
field :course_title, as: :slug_title, type: String, localize: true
|
|
field :course_objective, localize: true
|
|
field :year, type: Integer
|
|
field :other_course_url, type: String, default: ""
|
|
has_many :selected_course_syllabus_files, :dependent => :destroy, :autosave => true
|
|
has_many :selected_course_progress_files, :dependent => :destroy, :autosave => true
|
|
has_many :selected_course_activity_files, :dependent => :destroy, :autosave => true
|
|
has_many :selected_course_multimedia_files, :dependent => :destroy, :autosave => true
|
|
has_many :selected_course_material_files, :dependent => :destroy, :autosave => true
|
|
has_many :selected_course_supplement_files, :dependent => :destroy, :autosave => true
|
|
has_many :selected_course_evaluation_files, :dependent => :destroy, :autosave => true
|
|
|
|
belongs_to :selected_course_semester
|
|
belongs_to :selected_course_category
|
|
|
|
accepts_nested_attributes_for :selected_course_syllabus_files, :allow_destroy => true
|
|
accepts_nested_attributes_for :selected_course_progress_files, :allow_destroy => true
|
|
accepts_nested_attributes_for :selected_course_activity_files, :allow_destroy => true
|
|
accepts_nested_attributes_for :selected_course_multimedia_files, :allow_destroy => true
|
|
accepts_nested_attributes_for :selected_course_material_files, :allow_destroy => true
|
|
accepts_nested_attributes_for :selected_course_supplement_files, :allow_destroy => true
|
|
accepts_nested_attributes_for :selected_course_evaluation_files, :allow_destroy => true
|
|
|
|
belongs_to :member_profile
|
|
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|
|
|
course = record.course
|
|
if record.new_record?
|
|
if course
|
|
record.course_title = course.title
|
|
record.year = course.year
|
|
if record.class.where(:course_id=>course.id,:member_profile_id=>record.member_profile_id,:id.ne=>record.id).count != 0
|
|
false
|
|
else
|
|
if course && !course.student_ids.include?(record.member_profile_id.to_s)
|
|
Course.where(:id=>course_id).update_all(:student_ids=>course.student_ids.push(record.member_profile_id.to_s))
|
|
end
|
|
true
|
|
end
|
|
end
|
|
elsif( course.student_ids.include?(record.member_profile_id.to_s) rescue false)
|
|
true
|
|
elsif course
|
|
record.course_title = course.title
|
|
record.year = course.year
|
|
if self.member_profile_id_changed?
|
|
new_student_ids = course.student_ids
|
|
if self.member_profile_id_was.nil?
|
|
new_student_ids.push(record.member_profile_id.to_s)
|
|
else
|
|
new_student_ids.delete(self.member_profile_id_was.to_s)
|
|
new_student_ids.push(record.member_profile_id.to_s)
|
|
end
|
|
Course.where(:id=>course_id).update_all(:student_ids=>new_student_ids)
|
|
self.member_profile = MemberProfile.find(self.member_profile_id) rescue nil
|
|
end
|
|
end
|
|
end
|
|
|
|
before_destroy do |record|
|
|
course = record.course
|
|
if course
|
|
new_student_ids = course.student_ids
|
|
if new_student_ids.include? record.member_profile_id.to_s
|
|
new_student_ids.delete(record.member_profile_id.to_s)
|
|
Course.where(:id=>course_id).update_all(:student_ids=>new_student_ids)
|
|
end
|
|
end
|
|
end
|
|
def course_url
|
|
course_object = Course.find(course_id) rescue nil
|
|
page = Page.where(:module=>"personal_course").first
|
|
return ("/#{I18n.locale}"+page.url+'/'+course_object.to_param).gsub('//','/') rescue "#"
|
|
end
|
|
def course
|
|
course = Course rescue nil
|
|
if course
|
|
Course.find(course_id) rescue nil
|
|
else
|
|
return nil
|
|
end
|
|
end
|
|
def self.get_plugin_datas_to_member(datas)
|
|
|
|
fields_to_show = [
|
|
"selected_course_category",
|
|
"course_title",
|
|
"course_url",
|
|
"year",
|
|
]
|
|
|
|
fields_to_remove = []
|
|
fields_not_remove = ["course_url"]
|
|
course_fields = ["selected_course_category","selected_course_semester","selected_course_syllabus_file", "selected_course_progress_file", "selected_course_activity_file", "selected_course_multimedia_file", "selected_course_material_file", "selected_course_supplement_file", "selected_course_evaluation_file"]
|
|
pd_title = []
|
|
|
|
fields_to_show.each do |t|
|
|
if course_fields.include? t
|
|
if (self.course.fields[t].type.to_s == "String" || self.course.fields[t].type.to_s == "Object" rescue false)
|
|
fields_to_remove << t if (datas.course.where(t.to_sym.ne => nil, t.to_sym.ne => "").count == 0 rescue false)
|
|
else
|
|
fields_to_remove << t if (datas.course.where(t.to_sym.ne => nil).count == 0 rescue false)
|
|
end
|
|
else
|
|
if !fields_not_remove.include?(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
|
|
end
|
|
end
|
|
pd_title << {
|
|
"plugin_data_title" => I18n.t("personal_selected_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 == "course_title"
|
|
link = OrbitHelper.url_to_plugin_show(p.to_param,'personal_selected_course')
|
|
url_to_plugin_show_blank = OrbitHelper.instance_variable_get(:@url_to_plugin_show_blank)
|
|
tmp_title = p.course_title
|
|
pd_data << { "data_title" => (url_to_plugin_show_blank ? tmp_title : "<a title=\"#{tmp_title}\" href=\"#{link}\" target=\"_blank\">#{tmp_title}") }
|
|
elsif t == "selected_course_category"
|
|
pd_data << {"data_title" => (p.course.course_category.title rescue "")}
|
|
elsif t == "course_url"
|
|
request = OrbitHelper.request
|
|
protocol = ("#{URI(request.referer).scheme}://" rescue request.protocol)
|
|
host_url = protocol+(request.host_with_port)
|
|
course_url = host_url + p.course_url
|
|
pd_data << {"data_title" => "<a title=\"#{p.course_title}\" href=\"#{course_url}\">#{course_url}<a>"}
|
|
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 "selected_course_category"
|
|
value = self.course.course_category.title rescue ""
|
|
when "selected_course_semester"
|
|
value = self.course.course_semester.title rescue ""
|
|
when "course_url"
|
|
request = OrbitHelper.request
|
|
protocol = ("#{URI(request.referer).scheme}://" rescue request.protocol)
|
|
host_url = protocol+(request.host_with_port)
|
|
if self.course
|
|
course_url = host_url + self.course_url
|
|
else
|
|
course_url = self.other_course_url
|
|
end
|
|
value = "<a title=\"#{self.course_title}\" href=\"#{course_url}\">#{course_url}<a>" rescue ""
|
|
when "selected_course_syllabus_file", "selected_course_progress_file", "selected_course_activity_file", "selected_course_multimedia_file", "selected_course_material_file", "selected_course_supplement_file", "selected_course_evaluation_file"
|
|
files = []
|
|
self.course.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.course.member_profile.to_param, 'member') rescue '#'
|
|
value = "<a href='#{path}'>#{self.course.member_profile.name}</a>"
|
|
when "course_student"
|
|
value = self.member_profile.name rescue ""
|
|
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_selected_course.'+field),
|
|
"value"=>value
|
|
}
|
|
end
|
|
|
|
end
|