# encoding: UTF-8
class CourseMessages < ActiveRecord::Migration
  def up
    # Course.all.each do |course|
    #   transaction do
    #     put course.id
    #     course.course_messages << CourseMessage.new(:user_id => course.tea_id,:course_id => course.id)
    #     # 作业
    #     course.homework_commons.each do |homework_common|
    #       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
    #     end
    #
    #     # 通知
    #     course.news.each do |new|
    #       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
    #     end
    #
    #     # 通知的回复
    #     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
    #     end
    #
    #     # 讨论区
    #     if course.boards.first
    #       course.boards.first.messages.each do |message|
    #         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
    #       end
    #     end
    #
    #     #  问卷
    #     Poll.where("polls_type = 'Course' and polls_group_id = #{course.id}").each do |poll|
    #       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

        # 作品评阅
        # 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
        # 作品讨论

    #   end
    # end
  end

  def down
  end
end
