socialforge/lib/tasks/homework_publishtime.rake

66 lines
3.1 KiB
Ruby

#coding=utf-8
namespace :homework_publishtime do
desc "start publish homework and end homework"
task :publish => :environment do
puts "--------------------------------homework_publish start"
Rails.logger.info("log--------------------------------homework_publish start")
homework_commons = HomeworkCommon.includes(:homework_detail_manual).where("publish_time < '#{Time.now}' and homework_detail_manuals.comment_status = 0")
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)
if homework.homework_type != 3
students = homework.course.student
if !homework.course.nil? && !students.empty?
name = homework.name
name_str = name + "的作品提交"
str = ""
students.each do |student|
if str != ""
str += ","
end
str += "('#{name_str}',#{homework.id},#{student.student_id}, '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
end
sql = "insert into student_works (name, homework_common_id,user_id, created_at, updated_at) values" + str
ActiveRecord::Base.connection.execute sql
end
end
#更新CourseHomeworkStatistics中每个学生的未交作品数
homework.course.student.each do |student|
course_statistics = CourseHomeworkStatistics.find_by_course_id_and_user_id(homework.course_id, student.student_id)
course_statistics.update_attribute('un_commit_work_num', course_statistics.un_commit_work_num + 1) if course_statistics
end
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
Rails.logger.info("log--------------------------------homework_publish end")
puts "--------------------------------homework_publish end"
end
# task :end => :environment do
# puts "--------------------------------homework_publish_end start"
# Rails.logger.info("log--------------------------------homework_publish_end start")
# 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
# Rails.logger.info("log--------------------------------homework_publish_end end")
# puts "--------------------------------homework_publish_end end"
# end
end