#coding=utf-8

namespace :homework_publishtime do
  desc "start publish homework and end homework"
  task :publish => :environment do
    homework_commons = HomeworkCommon.where("publish_time = '#{Date.today}'")
    homework_commons.each do |homework|
      homework_detail_manual = homework.homework_detail_manual
      if homework_detail_manual.comment_status == 0
        homework_detail_manual.update_column('comment_status', 1)
        course = homework.course
        course.members.each do |m|
          homework.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => course.id, :viewed => false, :status => nil)
        end
        if homework.course_acts.size == 0
          homework.course_acts << CourseActivity.new(:user_id => homework.user_id,:course_id => homework.course_id)
        end
        # 邮件通知
        Mailer.run.homework_added(homework)
      end
    end
  end

  task :end => :environment do
    homework_commons = HomeworkCommon.where("end_time = '#{Date.today}'")
    homework_commons.each do |homework|
      if homework.anonymous_comment == 1
        homework_detail_manual = homework.homework_detail_manual
        if homework_detail_manual.comment_status == 1
          homework_detail_manual.update_column('comment_status', 3)
        end
      end
    end
  end
end