学生加入课堂和退出课堂时的数据处理

dev_course
cxt 5 years ago
parent ae5c1c05b1
commit 8d2a90a947

@ -548,6 +548,7 @@ class CoursesController < ApplicationController
course_teacher = CourseMember.find_by(user_id: current_user.id, role: %i[CREATOR PROFESSOR ASSISTANT_PROFESSOR], course_id: @course.id) course_teacher = CourseMember.find_by(user_id: current_user.id, role: %i[CREATOR PROFESSOR ASSISTANT_PROFESSOR], course_id: @course.id)
course_student.destroy! course_student.destroy!
course_teacher.update_attributes(is_active: 1) if course_teacher.present? && !course_teacher.is_active course_teacher.update_attributes(is_active: 1) if course_teacher.present? && !course_teacher.is_active
CourseDeleteStudentDeleteWorksJob.perform_later(@course.id, [current_user.id])
normal_status(0, "退出成功") normal_status(0, "退出成功")
end end
@ -684,11 +685,16 @@ class CoursesController < ApplicationController
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
begin begin
students = params[:students] students = params[:students]
student_ids = []
students.each do |student| students.each do |student|
course_member = CourseMember.find_by!(id: student[:course_member_id].to_i, course_id: @course.id) course_member = CourseMember.find_by(id: student[:course_member_id].to_i, course_id: @course.id)
course_member.destroy! if course_member.present?
student_ids << course_member.user_id
course_member.destroy!
end
end end
CourseDeleteStudentDeleteWorksJob.perform_later(@course.id, student_ids) if student_ids.present?
normal_status(0, "操作成功") normal_status(0, "操作成功")
rescue => e rescue => e
uid_logger(e.message) uid_logger(e.message)
@ -720,6 +726,7 @@ class CoursesController < ApplicationController
else else
new_student.is_active = 0 if existing_course_member.is_active new_student.is_active = 0 if existing_course_member.is_active
new_student.save! new_student.save!
student_ids << user_id
end end
else else
new_student.save! new_student.save!
@ -727,6 +734,7 @@ class CoursesController < ApplicationController
end end
end end
CourseAddStudentCreateWorksJob.perform_later(@course.id, student_ids) if student_ids.present?
TeacherInviteJoinCourseNotifyJob.perform_later(current_user.id, @course.id, 10, student_ids) if student_ids.present? TeacherInviteJoinCourseNotifyJob.perform_later(current_user.id, @course.id, 10, student_ids) if student_ids.present?
normal_status(0, "添加成功") normal_status(0, "添加成功")
rescue => e rescue => e
@ -868,6 +876,7 @@ class CoursesController < ApplicationController
new_student.course_group_id = course_group.id if course_group.present? new_student.course_group_id = course_group.id if course_group.present?
new_student.save! new_student.save!
CourseAddStudentCreateWorksJob.perform_later(course.id, [current_user.id])
StudentJoinCourseNotifyJob.perform_later(current_user.id, course.id) StudentJoinCourseNotifyJob.perform_later(current_user.id, course.id)
end end
end end

@ -22,8 +22,45 @@ class CourseAddStudentCreateWorksJob < ApplicationJob
attrs = %i[homework_common_id user_id created_at updated_at] attrs = %i[homework_common_id user_id created_at updated_at]
StudentWork.bulk_insert(*attrs) do |worker| StudentWork.bulk_insert(*attrs) do |worker|
student_ids.each do |teacher| student_ids.each do |user_id|
worker.add same_attrs.merge(user_id: teacher.user_id) same_attrs = {user_id: user_id}
course.homework_commons.where(homework_type: %i[normal group practice]).each do |homework|
next if homework.student_works.where(user_id: user_id).any?
worker.add same_attrs.merge(homework_common_id: homework.id)
end
end
end
attrs = %i[exercise_id user_id created_at updated_at]
ExerciseUser.bulk_insert(*attrs) do |worker|
student_ids.each do |user_id|
same_attrs = {user_id: user_id}
course.exercises.each do |exercise|
next if exercise.exercise_users.where(user_id: user_id).any?
worker.add same_attrs.merge(exercise_id: exercise.id)
end
end
end
attrs = %i[poll_id user_id created_at updated_at]
PollUser.bulk_insert(*attrs) do |worker|
student_ids.each do |user_id|
same_attrs = {user_id: user_id}
course.polls.each do |poll|
next if poll.poll_users.where(user_id: user_id).any?
worker.add same_attrs.merge(poll_id: poll.id)
end
end
end
attrs = %i[graduation_task_id user_id course_id created_at updated_at]
GraduationWork.bulk_insert(*attrs) do |worker|
student_ids.each do |user_id|
same_attrs = {user_id: user_id, course_id: course.id}
course.graduation_tasks.each do |task|
next if task.graduation_works.where(user_id: user_id).any?
worker.add same_attrs.merge(graduation_task_id: task.id)
end
end end
end end
end end

@ -1,7 +1,19 @@
class CourseDeleteStudentDeleteWorksJob < ApplicationJob class CourseDeleteStudentDeleteWorksJob < ApplicationJob
queue_as :course_member queue_as :course_member
def perform(*args) def perform(course_id, student_ids)
# Do something later course = Course.find_by(id: course_id)
return if course.blank?
course = self.course
student_works = StudentWork.joins(:homework_common).where(user_id: student_ids, homework_commons: {course_id: course.id})
student_works.update_all(is_delete: 1)
exercise_users = ExerciseUser.joins(:exercise).where(user_id: student_ids, exercises: {course_id: course.id})
exercise_users.update_all(is_delete: 1)
poll_users = PollUser.joins(:poll).where(user_id: student_ids, polls: {course_id: course.id})
poll_users.update_all(is_delete: 1)
course.graduation_works.where(user_id: student_ids).update_all(is_delete: 1)
end end
end end

@ -20,8 +20,8 @@ class CourseMember < ApplicationRecord
# 未分班 # 未分班
scope :ungroup_students, -> { where(course_group_id: 0, role: 4) } scope :ungroup_students, -> { where(course_group_id: 0, role: 4) }
after_destroy :delete_works # after_destroy :delete_works
after_create :work_operation # after_create :work_operation
def delete_works def delete_works
if self.role == "STUDENT" if self.role == "STUDENT"
course = self.course course = self.course

Loading…
Cancel
Save