2013-09-12 10:41:15 +08:00
|
|
|
class StudentsForCourse < ActiveRecord::Base
|
|
|
|
attr_accessible :course_id, :student_id
|
|
|
|
|
2014-06-16 17:14:05 +08:00
|
|
|
belongs_to :course, :class_name => 'Course', :foreign_key => :course_id
|
|
|
|
|
2013-09-12 10:41:15 +08:00
|
|
|
belongs_to :student, :class_name => 'User', :foreign_key => :student_id
|
|
|
|
|
|
|
|
validates_presence_of :course_id, :student_id
|
|
|
|
validates_uniqueness_of :student_id, :scope => :course_id
|
2016-11-11 13:58:07 +08:00
|
|
|
|
|
|
|
after_destroy :delete_student_works
|
2016-11-17 10:23:13 +08:00
|
|
|
after_create :recovery_student_works
|
2016-11-11 13:58:07 +08:00
|
|
|
|
2016-11-17 10:23:13 +08:00
|
|
|
#退出班级或删除学生时隐藏学生的作品
|
2016-11-11 13:58:07 +08:00
|
|
|
def delete_student_works
|
|
|
|
course = self.course
|
|
|
|
homework_ids = course.homework_commons.blank? ? "(-1)" : "(" + course.homework_commons.map{|hw| hw.id}.join(",") + ")"
|
|
|
|
student_works = StudentWork.where("user_id = #{self.student_id} && homework_common_id in #{homework_ids}")
|
2016-11-17 10:23:13 +08:00
|
|
|
student_works.update_all(:is_delete => 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
#加入班级时还原作品
|
|
|
|
def recovery_student_works
|
|
|
|
course = self.course
|
|
|
|
homework_ids = course.homework_commons.blank? ? "(-1)" : "(" + course.homework_commons.map{|hw| hw.id}.join(",") + ")"
|
|
|
|
student_works = StudentWork.where("user_id = #{self.student_id} && homework_common_id in #{homework_ids}")
|
|
|
|
student_works.update_all(:is_delete => 0)
|
2016-11-11 13:58:07 +08:00
|
|
|
end
|
2013-09-12 10:41:15 +08:00
|
|
|
end
|