diff --git a/app/controllers/exercises_controller.rb b/app/controllers/exercises_controller.rb index e1e8e226b..2103a8614 100644 --- a/app/controllers/exercises_controller.rb +++ b/app/controllers/exercises_controller.rb @@ -729,7 +729,6 @@ class ExercisesController < ApplicationController if g_course.map(&:to_i).sort == user_course_groups.sort # 如果是设置为全部班级,则试卷不用分组,且试卷设定为统一设置,否则则分组设置 exercise.exercise_group_settings.destroy_all ex_unified = true - notify_receiver_ids = @course.students.pluck(:user_id) else ex_unified = false g_course.each do |i| @@ -749,12 +748,11 @@ class ExercisesController < ApplicationController end end - notify_receiver_ids = @course.students.where(course_group_id: params[:group_ids]).pluck(:user_id) + group_ids = params[:group_ids] end else exercise.exercise_group_settings.destroy_all ex_unified = true - notify_receiver_ids = @course.students.pluck(:user_id) end if exercise.end_time.blank? e_time = ex_end_time @@ -775,7 +773,7 @@ class ExercisesController < ApplicationController if exercise.course_acts.size == 0 exercise.course_acts << CourseActivity.new(:user_id => exercise.user_id,:course_id => exercise.course_id) end - ExercisePublishNotifyJob.perform_later(exercise.id, notify_receiver_ids) + ExercisePublishNotifyJob.perform_later(exercise.id, group_ids) end end end diff --git a/app/jobs/exercise_publish_notify_job.rb b/app/jobs/exercise_publish_notify_job.rb index c66fa5660..563f6ceb0 100644 --- a/app/jobs/exercise_publish_notify_job.rb +++ b/app/jobs/exercise_publish_notify_job.rb @@ -2,11 +2,20 @@ class ExercisePublishNotifyJob < ApplicationJob queue_as :notify - def perform(exercise_id, receiver_ids) + def perform(exercise_id, group_ids) exercise = Exercise.find_by(id: exercise_id) return if exercise.blank? 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[ 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 @@ -19,14 +28,14 @@ class ExercisePublishNotifyJob < ApplicationJob viewed: 0, tiding_type: 'Exercise' } 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) - exercise.course.teachers.find_each do |teacher| - worker.add same_attrs.merge(user_id: teacher.user_id) + teacher_ids.find_each do |user_id| + worker.add same_attrs.merge(user_id: user_id) 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) end end diff --git a/app/jobs/poll_publish_notify_job.rb b/app/jobs/poll_publish_notify_job.rb index d7ed1ae02..89fb45ea5 100644 --- a/app/jobs/poll_publish_notify_job.rb +++ b/app/jobs/poll_publish_notify_job.rb @@ -31,8 +31,8 @@ class PollPublishNotifyJob < ApplicationJob Tiding.bulk_insert(*attrs) do |worker| teacher_ids = teachers.pluck(:user_id) unless poll.tidings.exists?(parent_container_type: 'PollPublish', user_id: teacher_ids) - poll.course.teachers.find_each do |teacher| - worker.add same_attrs.merge(user_id: teacher.user_id) + teacher_ids.find_each do |user_id| + worker.add same_attrs.merge(user_id: user_id) end end diff --git a/app/tasks/exercise_publish_task.rb b/app/tasks/exercise_publish_task.rb index 3df5f84ae..220512664 100644 --- a/app/tasks/exercise_publish_task.rb +++ b/app/tasks/exercise_publish_task.rb @@ -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&.each do |exercise| exercise.update_column('exercise_status', 2) - course = exercise.course - tid_str = "" - course.teachers.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 exercise.unified_setting - course.students.find_each do |student| + ExercisePublishNotifyJob.perform_later(exercise.id, nil) + else + course = exercise.course + teachers = course.teachers.where.not(id: course.teacher_course_groups.select(:course_member_id)) + tid_str = "" + teachers.find_each do |member| 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')}')" + 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 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 - - 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 - - if str != "" - sql = "insert into exercise_users (user_id, exercise_id, commit_status, created_at, updated_at) values" + str - ActiveRecord::Base.connection.execute sql + 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 - if exercise.course_acts.size == 0 - exercise.course_acts << CourseActivity.new(:user_id => exercise.user_id,:course_id => exercise.course_id) - end + exercise.course_acts << CourseActivity.new(user_id: exercise.user_id, course_id: exercise.course_id) if !exercise.course_acts.exists? end # 分组设置发布时间的测验 @@ -48,18 +33,8 @@ class ExercisePublishTask exercise_group_settings&.each do |exercise_group| exercise = exercise_group.exercise if exercise.present? - course = exercise.course exercise.update_attributes(:exercise_status => 2) if exercise.exercise_status == 1 - tid_str = "" - 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 + ExercisePublishNotifyJob.perform_later(exercise.id, [exercise_group.course_group_id]) end end Rails.logger.info("log--------------------------------exercise_publish end")