Exercise里exercise_lists的优化

dev_forum
SylorHuang 5 years ago
parent e7da0d9be8
commit 53e244a0a5

@ -1164,69 +1164,65 @@ class ExercisesController < ApplicationController
@course_all_members = @course.students
@c_group_counts = @course.course_groups_count
question_types = @exercise.exercise_questions.pluck(:question_type).uniq
@exercise_publish_count = get_user_permission_course(exercise_ids,2).count #判断是否有已发布的分班
@exercise_unpublish_count = get_user_permission_course(exercise_ids,1).count #判断是否有未发布的分班
@exercise_publish_count = get_user_permission_course(exercise_ids,Exercise::PUBLISHED).count #判断是否有已发布的分班
@exercise_unpublish_count = get_user_permission_course(exercise_ids,Exercise::UNPUBLISHED).count #判断是否有未发布的分班
if (question_types.size > 1) && question_types.include?(4) #是否包含主观题,或者是否大于1
if (question_types.size > 1) && question_types.include?(Exercise::SUBJECTIVE) #是否包含主观题,或者是否大于1
@subjective_type = 1
else
@subjective_type = 0
end
#初始化值
@exercise_users_list = [] #答题用户列表
@exercise_course_groups = [] #当前用户有权限的班级
@exercise_unanswers = 0 # 未答用户数
@exercise_answers = 0 #已答用户数
@exercise_users_count = 0 #全部用户数
@teacher_review_count = 0 #已评数
@teacher_unreview_count = 0 #未评数
#试卷的答题列表页的显示用户
if @user_course_identity < Course::STUDENT #当前为老师,而且老师只能查看自己班级的/课堂的试卷
@exercise_current_user_status = 0
if @exercise_status == 1
@exercise_users_list = []
@exercise_course_groups = []
@exercise_unanswers = 0
@exercise_answers = 0
else
unless @exercise_status == 1
ex_common_ids = @exercise.common_published_ids(current_user.id)
@exercise_course_groups = @course.get_ex_published_course(ex_common_ids)
@exercise_users_list = @exercise.all_exercise_users(current_user.id) #当前老师所在班级的全部学生
get_exercise_answers(@exercise_users_list, @exercise_status)
end
elsif @user_course_identity > Course::ASSISTANT_PROFESSOR #当前为学生或者有过答题的(提交/未提交)
else #当前为学生或者有过答题的
@ex_user_end_time = @exercise.get_exercise_end_time(current_user.id) #当前用户所看到的剩余时间
@exercise_all_users = @exercise.get_stu_exercise_users
get_exercise_answers(@exercise_all_users, @exercise_status) # 未答和已答的
exercise_current_user = @exercise_all_users.exercise_commit_users(current_user.id) #当前用户是否开始做试卷(提交/未提交/没做)
if exercise_current_user.present?
@exercise_current_user_status = 1 #当前用户的状态,为学生
if @exercise.score_open && @exercise_status == 3 && exercise_current_user.present? #勾选了成绩公开且试卷已截止的
exercise_current_user = @exercise_all_users.exercise_commit_users(current_user.id)
if exercise_current_user.exists? #表示为课堂学生或已回答的
@exercise_current_user_status = 1
if @exercise.score_open && @exercise_status == 3 #勾选了成绩公开且试卷已截止的
all_user_ids = @exercise_all_users.pluck(:user_id)
all_user_ids.delete(current_user.id) #删除了当前用户的ID
@exercise_users_list = @exercise_all_users.exercise_commit_users(all_user_ids).distinct
@current_user_ex_answers = exercise_current_user #当前用户的回答
else
@exercise_users_list = exercise_current_user.present? ? exercise_current_user.distinct : []
@exercise_users_list = exercise_current_user
end
else
@exercise_all_users = @exercise.get_stu_exercise_users
get_exercise_answers(@exercise_all_users, @exercise_status) # 未答和已答的
else #表示为未回答的,或未非课堂成员的
@exercise_current_user_status = 2 #当前用户非课堂成员
@exercise_users_list = []
end
end
if @exercise_users_list.present? && @exercise_users_list.count > 0
@exercise_users_count = @exercise_users_list.count #当前显示的全部成员数量
else
@exercise_users_count = 0
end
if @exercise_unanswers < 0
@exercise_unanswers = 0
end
@teacher_review_count = 0
@teacher_unreview_count = 0
#筛选/分类,排序
order = params[:order]
if @exercise_users_list.present? && @exercise_users_list.size > 0
if @exercise_users_list.exists? && @exercise_users_list.size > 0
@exercise_users_count = @exercise_users_list.size #当前显示的全部成员数量
teacher_reviews = @exercise_users_list.exercise_review
teacher_unreviews = @exercise_users_list.exercise_unreview
@teacher_review_count = teacher_reviews.count #已评阅
@teacher_unreview_count = teacher_unreviews.count #未评阅
@teacher_review_count = teacher_reviews.size #已评阅
@teacher_unreview_count = teacher_unreviews.size #未评阅
#是否评阅
if params[:review].present?
@ -1284,7 +1280,7 @@ class ExercisesController < ApplicationController
if params[:format] == "xlsx"
if @user_course_identity > Course::ASSISTANT_PROFESSOR
tip_exception(403,"无权限操作")
elsif @exercise_status == 1
elsif @exercise_status == Exercise::UNPUBLISHED
normal_status(-1,"试卷未发布")
elsif (@exercise_users_size == 0) || ( @export_ex_users&.exercise_user_committed.size == 0)
normal_status(-1,"暂无用户提交")
@ -1530,30 +1526,22 @@ class ExercisesController < ApplicationController
def get_user_permission_course(exercise_ids,status)
exercise_status = status.to_i #传入的试卷发布状态
unpublish_group = []
# g_course_ids = @course.teacher_course_groups.get_user_groups(current_user.id).pluck(:course_group_id).reject(&:blank?).uniq #当前用户有权限的分班
# #用户的班级,理论上用户的班级要大于等于试卷设置的班级
# if g_course_ids.blank? || g_course_ids.include?(0)
# user_groups_id = @course.course_groups.pluck(:id)
# else
# user_groups_id = g_course_ids
# end
course_groups = []
user_groups_id = @course.charge_group_ids(current_user)
exercises_all = Exercise.where(id:exercise_ids)
exercises_all = Exercise.includes(:exercise_group_settings).where(id:exercise_ids)
exercises_all.each do |exercise|
if exercise.present?
if exercise.unified_setting #统一设置只有两种情况,全部发布,全部截止
exercise_user_status = exercise.get_exercise_status(current_user) #当前用户的能看到的试卷
if (exercise_user_status == exercise_status) || exercise_status == 3 #未发布的情况
if (exercise_user_status == exercise_status) || exercise_status == Exercise::ENDED #未发布的情况
unpublish_group = unpublish_group + user_groups_id
else
unpublish_group = []
end
else
ex_all_group_settings = exercise.exercise_group_settings
ex_group_settings = ex_all_group_settings.exercise_group_published.pluck(:course_group_id).uniq #问卷设置的班级
if exercise_status == 1
if exercise_status == Exercise::UNPUBLISHED
unpublish_group = user_groups_id - ex_group_settings
elsif exercise_status == 3
elsif exercise_status == Exercise::ENDED
ex_ended_groups = ex_all_group_settings.exercise_group_ended.pluck(:course_group_id).uniq
ex_and_user = user_groups_id & ex_group_settings #用户已设置的分班
unpublish_group = unpublish_group + ex_and_user - ex_ended_groups #已发布的全部班级减去截止的全部班级
@ -1567,8 +1555,6 @@ class ExercisesController < ApplicationController
unpublish_group = unpublish_group.uniq
if unpublish_group.count > 0
course_groups = CourseGroup.by_group_ids(unpublish_group)
else
course_groups = []
end
course_groups
end

@ -259,10 +259,9 @@ module ExercisesHelper
#获取试卷的已答/未答人数
def get_exercise_answers(ex_users, status)
if status == 1
@exercise_answers = 0
@exercise_unanswers = 0
else
@exercise_answers = 0
@exercise_unanswers = 0
unless status == Exercise::UNPUBLISHED
@exercise_answers = ex_users.commit_exercise_by_status(1).size #表示已经提交了的用户
course_all_members_count = ex_users.size
@exercise_unanswers = (course_all_members_count - @exercise_answers)

@ -23,6 +23,21 @@ class Exercise < ApplicationRecord
after_create :create_exercise_list
# 试卷的问题类型
SINGLE = 0 #单选题
MULTIPLE = 1 #多选题
JUDGMENT = 2 #判断题
COMPLETION = 3 # 填空题
SUBJECTIVE = 4 # 主观题
PRACTICAL = 5 #实训题
# 试卷的状态
UNPUBLISHED = 1 #未发布
PUBLISHED = 2 #已发布
DEADLINE = 3 #已截止
ENDED = 4 #课堂已结束
def create_exercise_list
str = ""
# TODO: 一次性为所有学生创建数据是否存在问题?
@ -93,8 +108,6 @@ class Exercise < ApplicationRecord
def common_published_ids(user_id)
current_user_groups = course.teacher_course_ids(user_id)
if unified_setting
# un_group_ids = (course.none_group_count > 0) ? [0] : []
# published_group_ids = (current_user_groups + un_group_ids).uniq #统一设置时为当前用户的分班id及未分班
published_group_ids = current_user_groups
else
ex_group_setting = exercise_group_settings.select(:course_group_id).pluck("course_group_id").uniq
@ -118,13 +131,12 @@ class Exercise < ApplicationRecord
ex_time = get_exercise_times(user_id,false)
pb_time = ex_time[:publish_time]
ed_time = ex_time[:end_time]
if pb_time.present? && ed_time.present? && pb_time <= Time.now && ed_time > Time.now
status = 2
status = Exercise::PUBLISHED
elsif ed_time.present? && ed_time <= Time.now
status = 3
status = Exercise::ENDED
else
status = 1
status = Exercise::UNPUBLISHED
end
else
status = exercise_status #当为老师的时候,则为试卷的总状态

Loading…
Cancel
Save