#coding=utf-8 namespace :homework_evaluation do desc "start and end evaluation" def get_assigned_homeworks(student_works, n, index) student_works += student_works student_works[index + 1 .. index + n] end #自动开启匿评的任务 task :start_evaluation => :environment do Rails.logger.info("log--------------------------------start_evaluation start") puts "--------------------------------start_evaluation start" homework_detail_manuals = HomeworkDetailManual.where("evaluation_start = '#{Date.today}'") homework_detail_manuals.each do |homework_detail_manual| homework_common = homework_detail_manual.homework_common if homework_common.anonymous_comment == 0 && homework_detail_manual.comment_status == 1 #新建状态才可开启匿评 student_works = homework_common.student_works.has_committed if student_works && student_works.size >= 2 if homework_common.homework_type == 3 student_work_projects = homework_common.student_work_projects.where("student_work_id is not null") student_work_projects.each_with_index do |pro_work, pro_index| n = homework_detail_manual.evaluation_num n = (n < student_works.size && n != -1) ? n : student_works.size - 1 work_index = -1 student_works.each_with_index do |stu_work, stu_index| if stu_work.id.to_i == pro_work.student_work_id.to_i work_index = stu_index end end assigned_homeworks = get_assigned_homeworks(student_works, n, work_index) assigned_homeworks.each do |h| student_works_evaluation_distributions = StudentWorksEvaluationDistribution.new(user_id: pro_work.user_id, student_work_id: h.id) student_works_evaluation_distributions.save end end else student_works.each_with_index do |work, index| user = work.user n = homework_detail_manual.evaluation_num n = (n < student_works.size && n != -1) ? n : student_works.size - 1 assigned_homeworks = get_assigned_homeworks(student_works, n, index) assigned_homeworks.each do |h| student_works_evaluation_distributions = StudentWorksEvaluationDistribution.new(user_id: user.id, student_work_id: h.id) student_works_evaluation_distributions.save end end end homework_detail_manual.update_column('comment_status', 2) # 匿评开启消息邮件通知,# 所有人 #send_message_anonymous_comment(homework_common, 2) course = homework_common.course course.members.each do |m| homework_common.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => course.id, :viewed => false, :status => 2) end # 邮件通知 Mailer.send_mail_anonymous_comment_open(homework_common).deliver else #作业数小于2,启动失败, 只给老师发 # status==4 发送失败 # 匿评开启消息邮件通知,# 所有人 course = homework_common.course course.members.each do |m| if m.user.allowed_to?(:as_teacher,course) homework_common.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => course.id, :viewed => false, :status => 4) end end # 邮件通知 Mailer.send_mail_anonymous_comment_fail(homework_common).deliver end end end Rails.logger.info("log--------------------------------start_evaluation end") puts "--------------------------------start_evaluation end" end #自动关闭匿评的任务 task :end_evaluation => :environment do homework_detail_manuals = HomeworkDetailManual.where("evaluation_end = '#{Date.today}'") homework_detail_manuals.each do |homework_detail_manual| homework_common = homework_detail_manual.homework_common if homework_common.anonymous_comment == 0 && homework_detail_manual.comment_status == 2 #开启匿评状态才可关闭匿评 #计算缺评扣分 work_ids = "(" + homework_common.student_works.has_committed.map(&:id).join(",") + ")" if homework_common.homework_type != 3 homework_common.student_works.has_committed.each do |student_work| absence_penalty_count = student_work.user.student_works_evaluation_distributions.where("student_work_id IN #{work_ids}").count - student_work.user.student_works_scores.where("student_work_id IN #{work_ids}").count student_work.absence_penalty = absence_penalty_count > 0 ? absence_penalty_count * homework_detail_manual.absence_penalty : 0 student_work.save end else homework_common.student_works.has_committed.each do |student_work| absence_penalty_count = student_work.user.student_works_evaluation_distributions.where("student_work_id IN #{work_ids}").count - student_work.user.student_works_scores.where("student_work_id IN #{work_ids}").count student_work.absence_penalty = absence_penalty_count > 0 ? absence_penalty_count * homework_detail_manual.absence_penalty : 0 student_work.save if student_work.absence_penalty != 0 pros = student_work.student_work_projects.where("is_leader = 0") user_ids = pros.empty? ? "(-1)" : "(" + pros.map{|stu|stu.user_id}.join(",") + ")" student_works = homework_common.student_works.where("user_id in #{user_ids}") student_works.each do |st_work| absence_penalty_count = st_work.user.student_works_evaluation_distributions.where("student_work_id IN #{work_ids}").count - st_work.user.student_works_scores.where("student_work_id IN #{work_ids}").count st_work.absence_penalty = absence_penalty_count > 0 ? absence_penalty_count * @homework_detail_manual.absence_penalty : 0 st_work.save end end end end homework_detail_manual.update_column('comment_status', 3) # 匿评关闭消息通知 给所有人发 course = homework_common.course course.members.each do |m| homework_common.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => course.id, :viewed => false, :status => 3) end # 邮件通知 Mailer.send_mail_anonymous_comment_close(homework_common).deliver end end end end