@ -23,7 +23,6 @@ class ExercisesController < ApplicationController
before_action :commit_shixun_present , only : [ :commit_shixun ]
include ExportHelper
include ExercisesHelper
# require 'pdfkit'
def index
ActiveRecord :: Base . transaction do
@ -32,60 +31,54 @@ class ExercisesController < ApplicationController
@exercises_all = @course . exercises
member_show_exercises = @exercises_all . is_exercise_published #已发布的或已截止的试卷
@current_user_ = current_user
@exercises_count = @exercises_all . size # 全部页面,需返回
@exercises_unpublish_counts = @exercises_all . exercise_by_status ( 1 ) . size #未发布的试卷数
@exercises_published_counts = @exercises_all . exercise_by_status ( [ 2 , 3 ] ) . size # 已发布的试卷数,包含已截止的
@exercises_ended_counts = @exercises_all . exercise_by_status ( 3 ) . size #已截止的试卷数
# 课堂的学生人数
@course_all_members = @course . students #当前课堂的全部学生
@course_all_members_count = @course_all_members . size #当前课堂的学生数
@current_student = @course_all_members . course_find_by_ids ( " user_id " , current_user . id ) #当前用户是否为课堂的学生
# exercises的不同用户群体的显示
if @user_course_identity < Course :: STUDENT # @is_teacher_or 1为老师/管理员/助教
@is_teacher_or = 1
# @teacher_groups_ids = @course.teacher_course_groups.get_user_groups(current_user.id).pluck(:course_group_id).reject(&:blank?)
@exercises = @exercises_all #老师能看到全部的试卷,不管是已发布的/未发布的/已截止的/统一设置的/私有设置的(看到内容不同)
@exercises = @exercises_all #老师能看到全部的试卷,不管是已发布的/未发布的/已截止的/统一设置的/私有设置的(看到内容不同)
elsif @user_course_identity == Course :: STUDENT # 2为课堂成员, 能看到统一设置的和自己班级的
@is_teacher_or = 2
# get_exercise_left_time(@exercise,current_user)
member_group_id = @current_student . first . try ( :course_group_id ) . to_i # 成员的分班id, 默认为0
if member_group_id == 0 #表示是课堂的未分班成员,只能查看统一设置的试卷(已发布的/已截止的)
@exercises = member_show_exercises . present? ? member_show_exercises . unified_setting : [ ]
@member_group_id = @current_student . first . try ( :course_group_id ) . to_i # 成员的分班id, 默认为0
if @member_group_id == 0 #表示是课堂的未分班成员,只能查看统一设置的试卷(已发布的/已截止的)
@exercises = member_show_exercises . exists? ? member_show_exercises . unified_setting : [ ]
else #已分班级的成员,可以查看统一设置和单独设置(试卷是发布在该班级)试卷
# 已发布 当前用户班级分组的 试卷id
exercise_settings_ids = @course . exercise_group_settings . exercise_group_published
. where ( course_group_id : member_group_id ) . pluck ( :exercise_id ) . uniq
@exercises = member_show_exercises . present? ?
member_show_exercises . unified_setting . or ( member_show_exercises . where ( id : exercise_settings_ids ) )
: [ ]
not_exercise_ids = @course . exercise_group_settings . exercise_group_not_published . where ( " course_group_id = #{ @member_group_id } " ) . pluck ( :exercise_id )
@exercises = member_show_exercises . where . not ( id : not_exercise_ids )
# exercise_settings_ids = @course.exercise_group_settings.exercise_group_published
# .where(course_group_id: member_group_id).pluck(:exercise_id).uniq
# @exercises = member_show_exercises.exists? ?
# member_show_exercises.unified_setting.or(member_show_exercises.where(id: exercise_settings_ids))
# : []
end
else #用户未登陆或不是该课堂成员,仅显示统一设置的(已发布的/已截止的),如有公开,则不显示锁,不公开,则显示锁
@is_teacher_or = 0
@exercises = member_show_exercises . present? ? member_show_exercises . unified_setting : [ ]
@exercises = member_show_exercises . unified_setting
end
if @exercises . size > 0
if params [ :type ] . present?
choose_type = params [ :type ] . to_i
member_group_id = @current_student . first . try ( :course_group_id ) . to_i
if @is_teacher_or == 2 && member_group_id > 0
exercise_groups_sets = @course . exercise_group_settings . where ( course_group_id : member_group_id )
. exercise_group_published
exercise_settings_ids = exercise_groups_sets . pluck ( :exercise_id )
exercise_ended_ids = exercise_groups_sets . exercise_group_ended . pluck ( :exercise_id ) . uniq
if choose_type == 2
@exercises = @exercises_all . present? ?
@exercises_all . exercise_by_status ( 2 ) . unified_setting
. or ( @exercises_all . where ( id : ( exercise_settings_ids - exercise_ended_ids ) . uniq ) )
: [ ]
elsif choose_type == 3
@exercises = @exercises_all . present? ?
@exercises_all . exercise_by_status ( 3 ) . unified_setting
. or ( @exercises_all . where ( id : exercise_ended_ids ) )
: [ ]
end
ex_setting_ids = [ ]
if @is_teacher_or != 2
@exercises = @exercises . where ( " exercise_status = #{ choose_type } " )
else
@exercises = @exercises . exercise_by_status ( choose_type )
case choose_type
when 1
ex_setting_ids = @course . exercise_group_settings . where ( " course_group_id = #{ @member_group_id } " ) . exercise_group_not_published . pluck ( :exercise_id )
when 2
ex_setting_ids = @course . exercise_group_settings . where ( " course_group_id = #{ @member_group_id } " )
. where ( " publish_time is not null and publish_time <= ? and end_time > ? " , Time . now , Time . now ) . pluck ( :exercise_id )
when 3
ex_setting_ids = @course . exercise_group_settings . where ( " course_group_id = #{ @member_group_id } " ) . exercise_group_ended . pluck ( :exercise_id )
end
unified_setting_ids = @exercises . unified_setting . where ( " exercise_status = #{ choose_type } " ) . pluck ( :id )
ex_ids = ( ex_setting_ids + unified_setting_ids ) . uniq
@exercises = @exercises . where ( id : ex_ids )
end
end
@ -106,6 +99,11 @@ class ExercisesController < ApplicationController
@exercises = [ ]
end
@course_all_members_count = @course_all_members . size #当前课堂的学生数
@exercises_count = @exercises_all . size # 全部页面,需返回
@exercises_unpublish_counts = @exercises_all . exercise_by_status ( 1 ) . size #未发布的试卷数
@exercises_published_counts = @exercises_count - @exercises_unpublish_counts # 已发布的试卷数,包含已截止的
rescue Exception = > e
uid_logger_error ( e . message )
tip_exception ( e . message )
@ -477,10 +475,8 @@ class ExercisesController < ApplicationController
params_end_time = params [ :end_time ] . to_time
end
if exercise_status == 2 && @exercise . publish_time != params_publish_time
normal_status ( - 1 , " 已发布,不允许修改发布时间 " )
elsif exercise_status == 3 && ( @exercise . end_time != params_end_time || @exercise . publish_time != params_publish_time )
normal_status ( - 1 , " 已截止,不允许修改时间 " )
if ( exercise_status != 1 ) && ( @exercise . publish_time != params_publish_time )
normal_status ( - 1 , " 已发布/已截止,不允许修改发布时间 " )
elsif params_publish_time . present? && params_end_time . present? && params_end_time < params_publish_time
normal_status ( - 1 , " 截止时间不能小于发布时间 " )
else
@ -523,12 +519,7 @@ class ExercisesController < ApplicationController
end
# exercise_end_time = t[:end_time].present? ? t[:end_time].to_time : nil
exercise_group = exercise_groups . find_in_exercise_group ( " course_group_id " , course_id ) #判断该分班是否存在
if exercise_group . present? && exercise_group . first . end_time < = Time . now &&
( exercise_end_time != exercise_group . first . end_time || exercise_publish_time != exercise_group . first . publish_time ) #已截止且时间改变的,则提示错误
error_count += 1
end
if exercise_group . present? && exercise_group . first . publish_time <
Time . now && exercise_publish_time != exercise_group . first . publish_time
if exercise_group . present? && ( exercise_group . first . publish_time < Time . now ) && ( exercise_publish_time != exercise_group . first . publish_time )
error_count += 1
end
if error_count == 0
@ -1300,21 +1291,23 @@ class ExercisesController < ApplicationController
if params [ :format ] == " xlsx "
if @user_course_identity > Course :: ASSISTANT_PROFESSOR
tip_exception ( 403 , " 无权限操作 " )
elsif @exercise_status == 1
normal_status ( - 1 , " 试卷未发布 " )
elsif ( @exercise_users_size == 0 ) || ( @export_ex_users & . exercise_user_committed . size == 0 )
normal_status ( - 1 , " 暂无用户提交 " )
else
respond_to do | format |
format . xlsx {
get_export_users ( @exercise , @course , @export_ex_users )
exercise_export_name_ =
" #{ current_user . real_name } _ #{ @course . name } _ #{ @exercise . exercise_name } "
exercise_export_name = Base64 . urlsafe_encode64 ( exercise_export_name_ . strip . first ( 30 ) )
render xlsx : " #{ exercise_export_name } " , template : " exercises/exercise_lists.xlsx.axlsx " , locals : { table_columns : @table_columns , exercise_users : @user_columns }
}
end
end
end
respond_to do | format |
format . json
format . xlsx {
get_export_users ( @exercise , @course , @export_ex_users )
exercise_export_name =
" #{ current_user . real_name } _ #{ @course . name } _ #{ @exercise . exercise_name } _ #{ Time . now . strftime ( '%Y%m%d_%H%M%S' ) } "
render xlsx : " #{ exercise_export_name . strip . first ( 30 ) } " , template : " exercises/exercise_lists.xlsx.axlsx " , locals : { table_columns : @table_columns , exercise_users : @user_columns }
}
end
rescue Exception = > e
uid_logger_error ( e . message )
tip_exception ( " 页面调用失败! " )
@ -1327,7 +1320,8 @@ class ExercisesController < ApplicationController
def export_exercise
@request_url = request . base_url
@exercise_questions = @exercise . exercise_questions . includes ( :exercise_choices ) . order ( " question_number ASC " )
filename = " #{ @exercise . user . real_name } _ #{ @exercise . exercise_name } _ #{ Time . current . strftime ( '%Y%m%d_%H%M' ) } .pdf "
filename_ = " #{ @exercise . user . real_name } _ #{ @exercise . exercise_name } "
filename = Base64 . urlsafe_encode64 ( filename_ . strip . first ( 30 ) )
stylesheets = " #{ Rails . root } /app/templates/exercise_export/exercise_export.css "
render pdf : 'exercise_export/blank_exercise' , filename : filename , stylesheets : stylesheets
end
@ -1639,36 +1633,9 @@ class ExercisesController < ApplicationController
@answer_committed_user = @exercise . exercise_users . exercise_commit_users ( current_user . id ) & . first
if @answer_committed_user . blank?
normal_status ( 404 , " 答题用户不存在 " )
# elsif @exercise.get_exercise_status(current_user.id) == 2 && @answer_committed_user.commit_status == 1 #当试卷截止时,会自动提交
# normal_status(-1,"提交错误,试卷用户已提交!")
end
end
# def commit_user_exercise
# @exercise_user_current = @exercise.exercise_users.exercise_commit_users(@exercise_current_user_id)&.first #查找当前用户是否有过答题
# if @user_course_identity == Course::STUDENT
# if @exercise_user_current.present?
# if @exercise.time > 0 && @exercise_user_current.start_at.present? && (@exercise_user_current.commit_status == 0) &&
# ((@exercise_user_current.start_at + (@exercise.time.to_i + 1).minutes) < Time.now)
# #当前用户存在,且已回答,且试卷时间已过,且未提交,则自动提交。最好是前端控制
# objective_score = calculate_student_score(@exercise,current_user)[:total_score]
# subjective_score = @exercise_user_current.subjective_score < 0.0 ? 0.0 : @exercise_user_current.subjective_score
# total_score = objective_score + subjective_score
# commit_option = {
# :status => 1,
# :commit_status => 1,
# :end_at => Time.now,
# :objective_score => objective_score,
# :score => total_score,
# :subjective_score => subjective_score
# }
# @exercise_user_current.update_attributes(commit_option)
# normal_status(0,"已交卷成功!")
# end
# end
# end
# end
#打回重做时的初步判断
def check_exercise_status
@exercise_users = @exercise . all_exercise_users ( current_user . id ) . commit_exercise_by_status ( 1 ) #当前教师所在分班的全部已提交的学生数