94 lines
4.1 KiB
Ruby
94 lines
4.1 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
|
|
# 获取竞赛的管理人员
|
|
def contest_managers contest
|
|
contest.contest_members.select{|cm| cm.roles.to_s.include?("ContestManager")}
|
|
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(",") + ")"
|
|
homework_detail_manuals = WorkDetailManual.where("evaluation_start <= '#{Date.today}' and work_id in #{work_ids}")
|
|
homework_detail_manuals.each do |homework_detail_manual|
|
|
work = homework_detail_manual.work
|
|
contestant_works = work.contestant_works.has_committed
|
|
judges = contest_judges work.contest
|
|
|
|
# 作品数为0||评委数为0 启动失败给管理员发消息
|
|
if contestant_works.count == 0 || judges.count == 0
|
|
contest = work.contest
|
|
contest_managers(contest).each do |m|
|
|
work.contest_messages << ContestMessage.new(:user_id => m.user_id, :contest_id => contest.id, :viewed => false, :status => 4)
|
|
end
|
|
else
|
|
#限定评审人数
|
|
if homework_detail_manual.evaluation_num != -1
|
|
# 分配作品 启动成功给评委发消息
|
|
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
|
|
work.update_column('work_status', 3)
|
|
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
|
|
end
|
|
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
|