#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.includes(homework_common: :course).where("evaluation_start <= '#{Time.now}' and (homework_detail_manuals.comment_status < 3)") homework_detail_manuals.each do |homework_detail_manual| homework_common = homework_detail_manual.homework_common if homework_common.anonymous_comment if homework_common.homework_group_settings.where("end_time is null or end_time > '#{Time.now}'").count == 0 if homework_common.homework_type == "group" student_works = homework_common.student_works.where("work_status != 0").group(:group_id) else student_works = homework_common.student_works.has_committed end if student_works.present? && student_works.length >= 2 if homework_common.homework_type == "group" student_work_projects = homework_common.student_works.where("work_status != 0").shuffle 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.group_id.to_i == pro_work.group_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 #更新CourseHomeworkStatistics中该学生的待匿评数 # course_statistics = CourseHomeworkStatistics.find_by_course_id_and_user_id(homework_common.course_id, pro_work.user_id) # course_statistics.update_attribute('un_evaluation_work_num', course_statistics.un_evaluation_work_num + n) if course_statistics end else student_works = student_works.shuffle 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 #更新CourseHomeworkStatistics中该学生的待匿评数 # course_statistics = CourseHomeworkStatistics.find_by_course_id_and_user_id(homework_common.course_id, user.id) # course_statistics.update_attribute('un_evaluation_work_num', course_statistics.un_evaluation_work_num + n) if course_statistics end end homework_detail_manual.update_column('comment_status', 3) # 匿评开启消息邮件通知,# 所有人 str = "" homework_common.course.course_members.pluck(:user_id).uniq.each do |user_id| str += "," if str != "" str += "('#{user_id}', '#{homework_common.user_id}', '#{homework_common.id}','HomeworkCommon','#{homework_common.id}', 'AnonymousComment',#{homework_common.course_id},'Course',0,'HomeworkCommon', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" end if str != "" sql = "insert into tidings (user_id,trigger_user_id,container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + str ActiveRecord::Base.connection.execute sql end else #作业数小于2,启动失败, 只给老师和助教发 extra = "作品数量低于两个,无法开启匿评" end else extra = "存在尚未截止的分班,无法开启匿评" end if extra.present? str = '' homework_common.course.teachers.each do |mem| str += "," if str != "" str += "('#{mem.user.id}', '#{homework_common.user_id}', '#{homework_common.id}','HomeworkCommon','#{homework_common.id}', 'AnonymousCommentFail',#{homework_common.course_id},'Course',0,'System','#{extra}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" end if str != "" sql = "insert into tidings (user_id,trigger_user_id,container_id, container_type,parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, extra, created_at, updated_at) values" + str ActiveRecord::Base.connection.execute sql end homework_detail_manual.update_attributes(:evaluation_start => nil, :evaluation_end => nil, :absence_penalty => 0, :evaluation_num => 0, :appeal_time => nil, :appeal_penalty => 0) homework_common.update_attributes(:anonymous_comment => 0, :anonymous_appeal => 0) end end end Rails.logger.info("log--------------------------------start_evaluation end") puts "--------------------------------start_evaluation end" end #自动关闭匿评的任务 task :end_evaluation => :environment do homework_detail_manuals = HomeworkDetailManual.includes(:homework_common).where("evaluation_end <= '#{Time.now}' and homework_detail_manuals.comment_status = 3") homework_detail_manuals.each do |homework_detail_manual| homework_common = homework_detail_manual.homework_common if homework_common.anonymous_comment #开启匿评状态才可关闭匿评 #计算缺评扣分 参与匿评 work_ids = "(" + homework_common.student_works.has_committed.map(&:id).join(",") + ")" homework_common.student_works.where("work_status != 0").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} and reviewer_role = 3").count student_work.absence_penalty = absence_penalty_count > 0 ? absence_penalty_count * homework_detail_manual.absence_penalty : 0 student_work.save #更新CourseHomeworkStatistics中该学生的待匿评数和缺评数 # absence_penalty_count = absence_penalty_count > 0 ? absence_penalty_count : 0 # course_statistics = CourseHomeworkStatistics.find_by_course_id_and_user_id(homework_common.course_id, student_work.user_id) # course_statistics.update_attribute('un_evaluation_work_num', (course_statistics.un_evaluation_work_num - absence_penalty_count) < 0 ? 0 : # (course_statistics.un_evaluation_work_num - absence_penalty_count)) if course_statistics # course_statistics.update_attribute('absence_evaluation_work_num', course_statistics.absence_evaluation_work_num + absence_penalty_count) if course_statistics end # 未参与匿评 if homework_common.homework_detail_manual.no_anon_penalty == 0 all_dis_eva = StudentWorksEvaluationDistribution.where("student_work_id IN #{work_ids}") has_sw_count = all_dis_eva.select("distinct user_id").count anon_count = all_dis_eva.count / has_sw_count homework_common.student_works.where("work_status != 0").each do |student_work| if student_work.user.student_works_evaluation_distributions.where("student_work_id IN #{work_ids}").count == 0 student_work.absence_penalty = homework_detail_manual.absence_penalty * anon_count student_work.save end end end if homework_common.anonymous_appeal homework_detail_manual.update_column('comment_status', 4) # 申诉开启 eva_distribution = StudentWorksEvaluationDistribution.where(:student_work_id => homework_common.student_works.pluck(:id)) str = "" eva_distribution.pluck(:user_id).uniq.each do |user_id| str += "," if str != "" str += "(#{user_id}, #{homework_common.user_id}, #{homework_common.id}, 'HomeworkCommon', #{homework_common.id}, 'AnonymousAppeal', #{homework_common.course_id}, 'Course', 0, 'HomeworkCommon', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" end if str != "" sql = "insert into tidings (user_id,trigger_user_id,container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + str ActiveRecord::Base.connection.execute sql end else homework_detail_manual.update_column('comment_status', 5) end # 匿评关闭消息通知 给所有人发 # 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 #自动关闭申诉的任务 task :end_appeal => :environment do homework_detail_manuals = HomeworkDetailManual.where("appeal_time <= '#{Time.now}' and homework_detail_manuals.comment_status = 4") homework_detail_manuals.update_all(:comment_status => 5) end end