class CourseMember < ActiveRecord::Base # attr_accessible :title, :body belongs_to :course, counter_cache: true belongs_to :user belongs_to :course_group, counter_cache: true after_create :create_student_works, :create_exercise_users, :create_poll_users, :create_graduation_works #加入班级时创建作业的作品 def create_student_works if role == 4 course = self.course str = "" homeworks = course.homework_commons.includes(:homework_detail_manual).where("homework_type in (1, 3, 4)") if homeworks.count != 0 homeworks.each do |hw| next if hw.student_works.where(user_id: user_id).any? str += "," if str != "" str += "('#{hw.name}的作品提交',#{hw.id},#{user_id}, '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" end if str != "" sql = "insert into student_works (name, homework_common_id, user_id, created_at, updated_at) values" + str ActiveRecord::Base.connection.execute sql end end end end # 加入班级时创建已发布试卷的作品 def create_exercise_users if role == 4 course = self.course str = "" exercises = course.exercises if exercises.count != 0 exercises.each do |ex| next if ex.exercise_users.where(user_id: user_id).any? str += "," if str != "" str += "(#{ex.id},#{user_id}, 0, '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" end if str != "" sql = "insert into exercise_users (exercise_id, user_id, commit_status, created_at, updated_at) values" + str ActiveRecord::Base.connection.execute sql end end end end # 加入班级时创建已发布问卷的作品 def create_poll_users if role == 4 course = self.course str = "" polls = course.polls if polls.count != 0 polls.each do |poll| next if poll.poll_users.where(user_id: user_id).any? str += "," if str != "" str += "(#{poll.id},#{user_id}, 0, '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" end if str != "" sql = "insert into poll_users (poll_id, user_id, commit_status, created_at, updated_at) values" + str ActiveRecord::Base.connection.execute sql end end end end # 创建毕设任务作品 def create_graduation_works if role == 4 course = self.course str = "" tasks = course.graduation_tasks if tasks.count != 0 tasks.each do |task| next if task.graduation_works.where(user_id: user_id).any? str += "," if str != "" str += "(#{task.id}, #{user_id}, #{course.id}, '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" end if str != "" sql = "insert into graduation_works (graduation_task_id, user_id, course_id, created_at, updated_at) values" + str ActiveRecord::Base.connection.execute sql end end end end end