socialforge/lib/tasks/contest_work_evaluation.rake

75 lines
3.3 KiB
Ruby

#coding=utf-8
namespace :contest_work_evaluation do
desc "start and end evaluation"
def get_assigned_contestant_works(judges, n, index, num)
judges = judges + judges
first_index = (index * n) % num
last_index = (index * n + n - 1) % num < first_index ? ((index * n + n - 1) % num) + num : (index * n + n - 1) % num
judges[first_index .. last_index]
end
# 获取竞赛的评委人员
def contest_judges contest
contest.contest_members.select{|cm| cm.roles.to_s.include?("Judge")}
end
#自动开启匿评的任务
task :start_evaluation => :environment do
Rails.logger.info("log--------------------------------contest_work_start_evaluation start")
puts "--------------------------------contest_work_start_evaluation start"
works = Work.where(:online_evaluation => true, :work_status => [1, 2])
work_ids = works.blank? ? "(-1)" : "(" + works.map{|work| work.id}.join(",") + ")"
puts "1111 #{work_ids}"
homework_detail_manuals = WorkDetailManual.where("evaluation_start <= '#{Date.today}' and work_id in #{work_ids}")
homework_detail_manuals.each do |homework_detail_manual|
puts "222 #{homework_detail_manual.evaluation_start}"
work = homework_detail_manual.work
contestant_works = work.contestant_works.has_committed
if homework_detail_manual.evaluation_num != -1
judges = contest_judges work.contest
n = homework_detail_manual.evaluation_num
n = (n < judges.size && n != -1) ? n : judges.size
contestant_works.each_with_index do |work, index|
user = work.user
assigned_judges = get_assigned_contestant_works(judges, n, index, judges.size)
assigned_judges.each do |h|
contestant_works_evaluation_distributions = ContestantWorkEvaluationDistribution.new(user_id: h.user_id, contestant_work_id: work.id)
contestant_works_evaluation_distributions.save
end
end
work.update_column('work_status', 3)
# 匿评开启消息邮件通知,# 所有人
#send_message_anonymous_comment(homework_common, 2)
contest = work.contest
contest.contest_members.each do |m|
work.contest_messages << ContestMessage.new(:user_id => m.user_id, :contest_id => contest.id, :viewed => false, :status => 2)
end
else
end
end
Rails.logger.info("log--------------------------------contest_work_start_evaluation end")
puts "--------------------------------contest_work_start_evaluation end"
end
#自动关闭匿评的任务
task :end_evaluation => :environment do
works = Work.where(:online_evaluation => true, :work_status => 3)
homework_detail_manuals = WorkDetailManual.where(:evaluation_end => Date.today, :work_id => works.map{|work| work.id})
homework_detail_manuals.each do |homework_detail_manual|
work = homework_detail_manual.work
work.contestant_works.has_committed.each do |student_work|
student_work.save
end
work.update_column('work_status', 4)
# 匿评关闭消息通知 给所有人发
contest = work.contest
contest.contest_members.each do |m|
work.contest_messages << ContestMessage.new(:user_id => m.user_id, :contest_id => contest.id, :viewed => false, :status => 3)
end
end
end
end