socialforge/db/migrate/20150829024549_course_messa...

117 lines
5.3 KiB
Ruby
Raw Normal View History

2015-08-29 16:08:09 +08:00
# encoding: UTF-8
2015-08-29 11:50:08 +08:00
class CourseMessages < ActiveRecord::Migration
def up
Course.all.each do |course|
transaction do
put course.id
2015-08-29 16:08:09 +08:00
course.course_messages << CourseMessage.new(:user_id => course.tea_id,:course_id => course.id)
# 作业
2015-08-29 11:50:08 +08:00
course.homework_commons.each do |homework_common|
2015-08-29 16:08:09 +08:00
homework_common.course.members.each do |m|
if m.user_id != homework_common.user_id
if m.created_on < homework_common.created_at
homework_common.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => course.id, :viewed => true)
end
end
end
2015-08-29 11:50:08 +08:00
end
2015-08-29 16:08:09 +08:00
# 通知
2015-08-29 11:50:08 +08:00
course.news.each do |new|
2015-08-29 16:08:09 +08:00
new.course.members.each do |m|
if m.user_id != new.author_id
if m.created_on < new.created_on # 在成员加入课程之后
new.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => course.id, :viewed => true)
end
end
end
2015-08-29 11:50:08 +08:00
end
2015-08-29 16:08:09 +08:00
# 通知的回复
course.news.each do |new|
if new.comments
new.comments.each do |comment|
if comment.author_id != comment.commented.author_id
comment.course_messages << CourseMessage.new(:user_id => comment.commented.author_id, :course_id => course.id, :viewed => true)
end
end
end
2015-08-29 11:50:08 +08:00
end
2015-08-29 16:08:09 +08:00
# 讨论区
2015-08-29 11:50:08 +08:00
if course.boards.first
course.boards.first.messages.each do |message|
2015-08-29 16:08:09 +08:00
if message.parent_id.nil? # 主贴
message.course.members.each do |m|
if message.author.allowed_to?(:as_teacher, message.course) && m.user_id != message.author_id # 老师 自己的帖子不给自己发送消息
if m.created_on < message.created_on
message.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => course.id, :viewed => true)
end
end
end
else # 回帖
message.course.members.each do |m|
if m.user_id == Message.find(message.parent_id).author_id && m.user_id != message.author_id # 只针对主贴回复,回复自己的帖子不发消息
if m.created_on < message.created_on
message.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => course.id, :viewed => true)
end
end
end
end
2015-08-29 11:50:08 +08:00
end
end
2015-08-29 16:08:09 +08:00
# 问卷
2015-08-29 11:50:08 +08:00
Poll.where("polls_type = 'Course' and polls_group_id = #{course.id}").each do |poll|
2015-08-29 16:08:09 +08:00
if poll.polls_status == 2 #问卷是发布状态
Course.find(poll.polls_group_id).members.each do |m|
if m.user_id != poll.user_id
if m.created_on < poll.created_at
poll.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => course.id, :viewed => true)
end
end
end
elsif poll.polls_status == 1 #问卷是新建状态
poll.course_messages.destroy_all
end
end
# 作品评阅
2015-08-29 19:26:23 +08:00
# course.homework_commons.each do |homework_common|
# if homework_common.student_works
# homework_common.student_works.each do |student_work|
# if student_work.student_works_scores
# student_work.student_works_scores.each do |student_works_score|
# receiver = student_works_score.student_work.user
# if student_works_score.created_at == student_works_score.updated_at
# if student_works_score.comment.nil?
# student_works_score.course_messages << CourseMessage.new(:user_id => receiver.id, :course_id => course.id,
# :viewed => true, :content => "作业评分:#{student_works_score.score}", :status=> true)
# else
# student_works_score.course_messages << CourseMessage.new(:user_id => receiver.id, :course_id => course.id,
# :viewed => true, :content => "作业评分:#{student_works_score.score}&nbsp;&nbsp;&nbsp; 评语:#{student_works_score.comment}", :status=> true)
# end
# else # 更新
# if student_works_score.comment.nil?
# student_works_score.course_messages << CourseMessage.new(:user_id => receiver.id, :course_id => course.id,
# :viewed => true, :content => "作业评分:#{student_works_score.score}", :status=> true)
# else
# student_works_score.course_messages << CourseMessage.new(:user_id => receiver.id, :course_id => course.id,
# :viewed => true, :content => "作业评分:#{student_works_score.score}&nbsp;&nbsp;&nbsp; 评语:#{student_works_score.comment}", :status=> true)
# end
# end
# end
# end
# end
# end
# end
2015-08-29 16:08:09 +08:00
# 作品讨论
2015-08-29 11:50:08 +08:00
end
end
end
def down
end
end