作品列表的性能优化

dev_course
cxt 6 years ago
parent 06db1820d6
commit 870952ecda

@ -89,7 +89,7 @@ class HomeworkCommonsController < ApplicationController
@homework_commons = @homework_commons.page(page).per(15) @homework_commons = @homework_commons.page(page).per(15)
if @homework_type == 4 if @homework_type == 4
@homework_commons = @homework_commons.includes(:homework_detail_manual, :published_settings, :shixuns) @homework_commons = @homework_commons.includes(:homework_detail_manual, :published_settings, :shixuns, :student_works)
elsif @homework_type == 3 elsif @homework_type == 3
@homework_commons = @homework_commons.includes(:homework_detail_manual, :published_settings, :homework_detail_group) @homework_commons = @homework_commons.includes(:homework_detail_manual, :published_settings, :homework_detail_group)
else else
@ -134,7 +134,7 @@ class HomeworkCommonsController < ApplicationController
@student_works = [] @student_works = []
end end
elsif @user_course_identity < Course::STUDENT elsif @user_course_identity < Course::STUDENT
@student_works = @homework.teacher_works(@current_user.id) @student_works = @homework.teacher_works(@member)
@all_member_count = @student_works.count @all_member_count = @student_works.count
elsif @user_course_identity > Course::STUDENT && @homework.work_public elsif @user_course_identity > Course::STUDENT && @homework.work_public
@student_works = student_works @student_works = student_works

@ -174,8 +174,8 @@ module HomeworkCommonsHelper
end end
# 作品数统计type 1 已提交 0 未提交 # 作品数统计type 1 已提交 0 未提交
def studentwork_count homework_common, type, user_id def studentwork_count homework_common, type, member
student_works = homework_common.teacher_works(user_id) student_works = homework_common.teacher_works(member)
type == 1 ? student_works.select{|work| work.work_status != 0}.size : student_works.select{|work| work.work_status = 0}.size type == 1 ? student_works.select{|work| work.work_status != 0}.size : student_works.select{|work| work.work_status = 0}.size
end end
@ -223,10 +223,10 @@ module HomeworkCommonsHelper
end end
# 作品状态 # 作品状态
def homework_status homework, user_id def homework_status homework, member
[{id: 0, name: "未提交", count: homework.unfinished_count(user_id)}, [{id: 0, name: "未提交", count: homework.unfinished_count(member)},
{id: 1, name: "按时提交", count: homework.finished_count(user_id)}, {id: 1, name: "按时提交", count: homework.finished_count(member)},
{id: 2, name: "延时提交", count: homework.delay_finished_count(user_id)}] {id: 2, name: "延时提交", count: homework.delay_finished_count(member)}]
end end
# 作品分数的显示 # 作品分数的显示

@ -78,20 +78,20 @@ class HomeworkCommon < ApplicationRecord
# 根据是否统一发布获取作业的作品列表 # 根据是否统一发布获取作业的作品列表
def all_works def all_works
student_works = self.unified_setting ? self.student_works : student_works = self.unified_setting ? self.student_works :
self.student_works.where(user_id: self.course.students.where( self.student_works.joins("join course_members cm on student_works.user_id=cm.user_id").
course_group_id: self.published_settings.pluck(:course_group_id)). where(course_members: {course_group_id: self.published_settings.pluck(:course_group_id)})
pluck(:user_id))
end end
# 分班权限的老师可见的作品列表 # 分班权限的老师可见的作品列表
def teacher_works user_id def teacher_works member
member = course.course_member(user_id) # member = course.course_member(user_id)
teacher_course_groups = member.try(:teacher_course_groups) teacher_course_groups = member.try(:teacher_course_groups)
all_student_works = self.all_works all_student_works = self.all_works
# 有分班权限的统计管理的分班且已发布的学生情况 # 有分班权限的统计管理的分班且已发布的学生情况
if member.present? && teacher_course_groups.size > 0 if member.present? && teacher_course_groups.size > 0
group_ids = teacher_course_groups.pluck(:course_group_id) group_ids = teacher_course_groups.pluck(:course_group_id)
all_student_works = all_student_works.where(user_id: course.students.where(course_group_id: group_ids).pluck(:user_id)) all_student_works = all_student_works.joins("join course_members cm on student_works.user_id=cm.user_id").
where(course_members: {course_group_id: group_ids})
end end
all_student_works all_student_works
end end
@ -216,17 +216,17 @@ class HomeworkCommon < ApplicationRecord
end end
# 作品未提交数 # 作品未提交数
def unfinished_count user_id def unfinished_count member
self.teacher_works(user_id).unfinished.count self.teacher_works(member).unfinished.count
end end
# 任务按时提交数 # 任务按时提交数
def finished_count user_id def finished_count member
self.teacher_works(user_id).finished.count self.teacher_works(member).finished.count
end end
def delay_finished_count user_id def delay_finished_count member
self.teacher_works(user_id).delay_finished.count self.teacher_works(member).delay_finished.count
end end
# 分组作业的最大分组id # 分组作业的最大分组id

@ -23,8 +23,8 @@ json.homeworks @homework_commons.each do |homework|
json.allow_late homework.allow_late json.allow_late homework.allow_late
unless curr_status[:status].include?("未发布") unless curr_status[:status].include?("未发布")
json.commit_count studentwork_count homework, 1, @user.id json.commit_count studentwork_count homework, 1, @member
json.uncommit_count studentwork_count homework, 0, @user.id json.uncommit_count studentwork_count homework, 0, @member
end end
if @user_course_identity < Course::STUDENT if @user_course_identity < Course::STUDENT

@ -20,12 +20,12 @@ if @user_course_identity < Course::STUDENT
if @homework.homework_type != "practice" if @homework.homework_type != "practice"
json.teacher_comment teacher_comment @homework, @current_user.id json.teacher_comment teacher_comment @homework, @current_user.id
end end
json.task_status homework_status @homework, @current_user.id json.task_status homework_status @homework, @member
json.course_group_info course_group_info @course, @current_user.id json.course_group_info course_group_info @course, @current_user.id
elsif @user_course_identity == Course::STUDENT elsif @user_course_identity == Course::STUDENT
json.commit_count studentwork_count @homework, 1, @current_user.id json.commit_count studentwork_count @homework, 1, @member
json.uncommit_count studentwork_count @homework, 0, @current_user.id json.uncommit_count studentwork_count @homework, 0, @member
json.left_time left_time @homework, @current_user.id json.left_time left_time @homework, @current_user.id
if @homework.homework_type == "practice" if @homework.homework_type == "practice"

Loading…
Cancel
Save