试卷的定时任务调整

dev_forum
cxt 5 years ago
parent 3f933be7dd
commit a04bdef1eb

@ -729,7 +729,6 @@ class ExercisesController < ApplicationController
if g_course.map(&:to_i).sort == user_course_groups.sort # 如果是设置为全部班级,则试卷不用分组,且试卷设定为统一设置,否则则分组设置 if g_course.map(&:to_i).sort == user_course_groups.sort # 如果是设置为全部班级,则试卷不用分组,且试卷设定为统一设置,否则则分组设置
exercise.exercise_group_settings.destroy_all exercise.exercise_group_settings.destroy_all
ex_unified = true ex_unified = true
notify_receiver_ids = @course.students.pluck(:user_id)
else else
ex_unified = false ex_unified = false
g_course.each do |i| g_course.each do |i|
@ -749,12 +748,11 @@ class ExercisesController < ApplicationController
end end
end end
notify_receiver_ids = @course.students.where(course_group_id: params[:group_ids]).pluck(:user_id) group_ids = params[:group_ids]
end end
else else
exercise.exercise_group_settings.destroy_all exercise.exercise_group_settings.destroy_all
ex_unified = true ex_unified = true
notify_receiver_ids = @course.students.pluck(:user_id)
end end
if exercise.end_time.blank? if exercise.end_time.blank?
e_time = ex_end_time e_time = ex_end_time
@ -775,7 +773,7 @@ class ExercisesController < ApplicationController
if exercise.course_acts.size == 0 if exercise.course_acts.size == 0
exercise.course_acts << CourseActivity.new(:user_id => exercise.user_id,:course_id => exercise.course_id) exercise.course_acts << CourseActivity.new(:user_id => exercise.user_id,:course_id => exercise.course_id)
end end
ExercisePublishNotifyJob.perform_later(exercise.id, notify_receiver_ids) ExercisePublishNotifyJob.perform_later(exercise.id, group_ids)
end end
end end
end end

@ -2,11 +2,20 @@
class ExercisePublishNotifyJob < ApplicationJob class ExercisePublishNotifyJob < ApplicationJob
queue_as :notify queue_as :notify
def perform(exercise_id, receiver_ids) def perform(exercise_id, group_ids)
exercise = Exercise.find_by(id: exercise_id) exercise = Exercise.find_by(id: exercise_id)
return if exercise.blank? return if exercise.blank?
user = exercise.user user = exercise.user
if group_ids.present?
students = course.students.where(course_group_id: group_ids)
subquery = course.teacher_course_groups.where(course_group_id: group_ids).select(:course_member_id)
teachers = course.teachers.where(id: subquery)
else
students = course.students
teachers = course.teachers
end
attrs = %i[ attrs = %i[
user_id trigger_user_id container_id container_type parent_container_id parent_container_type 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 belong_container_id belong_container_type viewed tiding_type created_at updated_at
@ -19,14 +28,14 @@ class ExercisePublishNotifyJob < ApplicationJob
viewed: 0, tiding_type: 'Exercise' viewed: 0, tiding_type: 'Exercise'
} }
Tiding.bulk_insert(*attrs) do |worker| Tiding.bulk_insert(*attrs) do |worker|
teacher_ids = exercise.course.teachers.pluck(:user_id) teacher_ids = teachers.pluck(:user_id)
unless exercise.tidings.exists?(parent_container_type: 'ExercisePublish', user_id: teacher_ids) unless exercise.tidings.exists?(parent_container_type: 'ExercisePublish', user_id: teacher_ids)
exercise.course.teachers.find_each do |teacher| teacher_ids.find_each do |user_id|
worker.add same_attrs.merge(user_id: teacher.user_id) worker.add same_attrs.merge(user_id: user_id)
end end
end end
receiver_ids.each do |user_id| students.pluck(:user_id).each do |user_id|
worker.add same_attrs.merge(user_id: user_id) worker.add same_attrs.merge(user_id: user_id)
end end
end end

@ -31,8 +31,8 @@ class PollPublishNotifyJob < ApplicationJob
Tiding.bulk_insert(*attrs) do |worker| Tiding.bulk_insert(*attrs) do |worker|
teacher_ids = teachers.pluck(:user_id) teacher_ids = teachers.pluck(:user_id)
unless poll.tidings.exists?(parent_container_type: 'PollPublish', user_id: teacher_ids) unless poll.tidings.exists?(parent_container_type: 'PollPublish', user_id: teacher_ids)
poll.course.teachers.find_each do |teacher| teacher_ids.find_each do |user_id|
worker.add same_attrs.merge(user_id: teacher.user_id) worker.add same_attrs.merge(user_id: user_id)
end end
end end

@ -8,39 +8,24 @@ class ExercisePublishTask
exercises = Exercise.includes(:exercise_users).where("publish_time is not null and exercise_status = 1 and publish_time <=?",Time.now) exercises = Exercise.includes(:exercise_users).where("publish_time is not null and exercise_status = 1 and publish_time <=?",Time.now)
exercises&.each do |exercise| exercises&.each do |exercise|
exercise.update_column('exercise_status', 2) exercise.update_column('exercise_status', 2)
if exercise.unified_setting
ExercisePublishNotifyJob.perform_later(exercise.id, nil)
else
course = exercise.course course = exercise.course
teachers = course.teachers.where.not(id: course.teacher_course_groups.select(:course_member_id))
tid_str = "" tid_str = ""
course.teachers.find_each do |member| teachers.find_each do |member|
tid_str += "," if tid_str != "" tid_str += "," if tid_str != ""
tid_str += "(#{member.user_id}, #{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" tid_str += "(#{member.user_id}, #{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
end end
if exercise.unified_setting
course.students.find_each do |student|
tid_str += "," if tid_str != ""
tid_str += "(#{student.user_id}, #{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
end
end
if tid_str != "" if tid_str != ""
tid_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" + tid_str tid_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" + tid_str
ActiveRecord::Base.connection.execute tid_sql ActiveRecord::Base.connection.execute tid_sql
end end
if exercise.exercise_users.size == 0
str = ""
course.students.find_each do |student|
str += "," if str != ""
str += "(#{student.user_id}, #{exercise.id}, 0, '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
end end
if str != "" exercise.course_acts << CourseActivity.new(user_id: exercise.user_id, course_id: exercise.course_id) if !exercise.course_acts.exists?
sql = "insert into exercise_users (user_id, exercise_id, commit_status, created_at, updated_at) values" + str
ActiveRecord::Base.connection.execute sql
end
end
if exercise.course_acts.size == 0
exercise.course_acts << CourseActivity.new(:user_id => exercise.user_id,:course_id => exercise.course_id)
end
end end
# 分组设置发布时间的测验 # 分组设置发布时间的测验
@ -48,18 +33,8 @@ class ExercisePublishTask
exercise_group_settings&.each do |exercise_group| exercise_group_settings&.each do |exercise_group|
exercise = exercise_group.exercise exercise = exercise_group.exercise
if exercise.present? if exercise.present?
course = exercise.course
exercise.update_attributes(:exercise_status => 2) if exercise.exercise_status == 1 exercise.update_attributes(:exercise_status => 2) if exercise.exercise_status == 1
tid_str = "" ExercisePublishNotifyJob.perform_later(exercise.id, [exercise_group.course_group_id])
members = course.students.where(:course_group_id => exercise_group.course_group_id)
members.find_each do |member|
tid_str += "," if tid_str != ""
tid_str += "(#{member.user_id},#{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
end
if tid_str != ""
tid_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" + tid_str
ActiveRecord::Base.connection.execute tid_sql
end
end end
end end
Rails.logger.info("log--------------------------------exercise_publish end") Rails.logger.info("log--------------------------------exercise_publish end")

Loading…
Cancel
Save