personal-course/app/models/course.rb

169 lines
6.5 KiB
Ruby
Raw Normal View History

2015-12-29 11:35:44 +00:00
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: []
2021-02-24 03:38:05 +00:00
field :course_code, type: String, default: ""
2021-03-08 06:32:33 +00:00
field :rss2_id
2015-12-29 11:35:44 +00:00
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
2020-09-20 09:20:43 +00:00
has_many :course_assignments, :dependent => :destroy, :autosave => true
2015-12-29 11:35:44 +00:00
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
2022-07-26 11:11:11 +00:00
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|
2020-09-20 09:20:43 +00:00
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?
2020-09-21 15:29:30 +00:00
SelectedCourse.create(:member_profile => MemberProfile.find(student_id) , :course_id=>record.id , :year=>record.year,:course_title_translations=>record.title_translations)
2020-09-20 09:20:43 +00:00
else
2020-09-21 15:29:30 +00:00
selected_course.update(:year=>record.year,:course_title_translations=>record.title_translations)
end
end
2020-09-20 09:20:43 +00:00
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
2015-12-29 11:35:44 +00:00
def self.get_plugin_datas_to_member(datas)
fields_to_show = [
"course_category",
2021-02-24 03:38:05 +00:00
"course_code",
2015-12-29 11:35:44 +00:00
"title",
"year",
]
2016-01-12 12:33:53 +00:00
fields_to_remove = []
pd_title = []
fields_to_show.each do |t|
2016-01-13 10:47:16 +00:00
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
2016-01-12 12:33:53 +00:00
pd_title << {
"plugin_data_title" => I18n.t("personal_course.#{t}")
} if !fields_to_remove.include?(t)
2015-12-29 11:35:44 +00:00
end
2016-01-12 12:33:53 +00:00
fields_to_show = fields_to_show - fields_to_remove
2022-07-26 11:11:11 +00:00
plugin_datas = datas.sort_for_frontend.collect.with_index do |p,idx|
2015-12-29 11:35:44 +00:00
pd_data = []
fields_to_show.collect do |t|
if t == "title"
2022-12-15 09:33:19 +00:00
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}") }
2015-12-29 11:35:44 +00:00
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),
2022-07-26 11:11:11 +00:00
"sort-index" => idx
2015-12-29 11:35:44 +00:00
}
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
2015-12-29 11:35:44 +00:00
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