@ -73,8 +73,14 @@ class ExerciseController < ApplicationController
@exercise_user = ExerciseUser . where ( " user_id=? and exercise_id=? " , User . current . id , @exercise . id ) . first
if @exercise_user . nil?
@exercise_user = ExerciseUser . create ( :user_id = > User . current . id , :exercise_id = > @exercise . id , :start_at = > Time . now , :status = > false , :commit_status = > 0 )
if @exercise . exercise_level_settings . where ( " num != exercise_questions_count " ) . count > 0
@exercise . create_user_question_list ( @exercise_user )
end
# @exercise_user = ExerciseUser.where("user_id=? and exercise_id=?", User.current.id, @exercise.id).first
elsif @exercise_user . start_at . nil?
if @exercise . exercise_level_settings . where ( " num != exercise_questions_count " ) . count > 0
@exercise . create_user_question_list ( @exercise_user )
end
@exercise_user . update_attributes ( :start_at = > Time . now )
end
@ -103,7 +109,9 @@ class ExerciseController < ApplicationController
@exercise_user = ExerciseUser . where ( " user_id=? and exercise_id=? " , params [ :user_id ] , @exercise . id ) . first
@can_edit_excercise = false
end
Rails . logger . info ( " # # # # # # # # # # # can_edit_excercise: #{ @can_edit_excercise } " )
@exercise_questions = user_question_list @exercise_user . id
# @percent = get_percent(@exercise,User.current)
#@exercise_questions = @exercise.exercise_questions
respond_to do | format |
@ -139,11 +147,13 @@ class ExerciseController < ApplicationController
else
none_shixun_ids = ShixunSchool . where ( " school_id != #{ User . current . user_extensions . try ( :school_id ) } " ) . pluck ( :shixun_id )
end
@shixuns = Shixun . where ( " status = 2 and id not in ( #{ none_shixun_ids . join ( " , " ) } ) " ) . where ( " name like ? " , " % #{ search } % " ) . select { | shixun | shixun . challenges . where ( :st = > 1 ) . count == 0 }
@shixuns = Shixun . where ( " status = 2 and challenges_count = 1 and id not in ( #{ none_shixun_ids . join ( " , " ) } ) " ) .
where ( " name like ? " , " % #{ search } % " ) . select { | shixun | shixun . challenges . where ( :st = > 1 ) . count == 0 }
@limit = 8
@shixuns = paginateHelper @shixuns , @limit
respond_to do | format |
format . js
render json : exercise_shixun_json_data ( @shixuns )
end
end
@ -977,118 +987,122 @@ class ExerciseController < ApplicationController
# 已提交过的且是限时的则不允许答题
exercise_user = ExerciseUser . where ( :exercise_id = > @exercise . id , :user_id = > User . current . id ) . first
if ( exercise_user && exercise_user . commit_status == 1 ) || @exercise . end_time < Time . now
render :json = > { :text = > " Over " }
return
end
if eq . question_type == 1
# 单选题
ea = ExerciseAnswer . find_by_exercise_question_id_and_user_id ( params [ :exercise_question_id ] , User . current . id )
if ea . nil?
# 尚未答该题,添加答案
ea = ExerciseAnswer . new
ea . user_id = User . current . id
ea . exercise_question_id = params [ :exercise_question_id ]
end
#修改该题对应答案
ea . exercise_choice_id = params [ :exercise_choice_id ]
if ea . save
# 保存成功返回成功信息及当前以答题百分比
uncomplete_question = get_uncomplete_question ( @exercise , User . current )
if uncomplete_question . count < 1
complete = 1 ;
else
complete = 0 ;
end
@percent = get_percent ( @exercise , User . current )
render :json = > { :text = > " ok " , :complete = > complete , :percent = > format ( " %.2f " , @percent ) }
else
#返回失败信息
render :json = > { :text = > " failure " }
if exercise_user
if ( exercise_user && exercise_user . commit_status == 1 ) || @exercise . end_time < Time . now
render :json = > { :text = > " Over " }
return
end
elsif eq . question_type == 2
#多选题
ea = ExerciseAnswer . find_by_exercise_choice_id_and_user_id ( params [ :exercise_choice_id ] , User . current . id )
if ea . nil?
#尚未答该题,添加答案
ea = ExerciseAnswer . new
ea . user_id = User . current . id
ea . exercise_question_id = params [ :exercise_question_id ]
if eq . question_type == 1
# 单选题
ea = ExerciseAnswer . find_by_exercise_question_id_and_user_id ( params [ :exercise_question_id ] , User . current . id )
if ea . nil?
# 尚未答该题,添加答案
ea = ExerciseAnswer . new
ea . user_id = User . current . id
ea . exercise_question_id = params [ :exercise_question_id ]
end
#修改该题对应答案
ea . exercise_choice_id = params [ :exercise_choice_id ]
if ea . save
uncomplete_question = get_uncomplete_question ( @exercise , User . current )
# 保存成功返回成功信息及当前以答题百分比
uncomplete_question = get_uncomplete_question ( @exercise , exercise_user )
if uncomplete_question . count < 1
complete = 1 ;
else
complete = 0 ;
end
is_answer = User . current . exercise_answer . where ( :exercise_question_id = > params [ :exercise_question_id ] . to_i ) . count > 0
@percent = get_percent ( @exercise , User . current )
render :json = > { :text = > " ok " , :complete = > complete , :percent = > format ( " %.2f " , @percent ) , :is_answer = > is_answer }
else
render :json = > { :text = > " failure " }
end
else
#pv不为空, 则当前选项之前已被选择, 再次点击则是不再选择该项, 故删除该答案
if ea . delete
is_answer = User . current . exercise_answer . where ( :exercise_question_id = > params [ :exercise_question_id ] . to_i ) . count > 0
@percent = get_percent ( @exercise , User . current )
render :json = > { :text = > " false " , :percent = > format ( " %.2f " , @percent ) , :is_answer = > is_answer }
@percent = get_percent ( @exercise , exercise_user )
render :json = > { :text = > " ok " , :complete = > complete , :percent = > format ( " %.2f " , @percent ) }
else
#返回失败信息
render :json = > { :text = > " failure " }
end
end
elsif eq . question_type == 3 || eq . question_type == 4
#单行文本,多行文本题
ea = ExerciseAnswer . find_by_exercise_question_id_and_user_id ( params [ :exercise_question_id ] , User . current . id )
if ea . nil?
# ea为空之前尚未答题, 添加答案
if params [ :answer_text ] . nil? || params [ :answer_text ] . blank?
#用户提交空答案,视作不作答
@percent = get_percent ( @exercise , User . current )
render :json = > { :text = > " " , :percent = > format ( " %.2f " , @percent ) }
else
#添加答案
elsif eq . question_type == 2
#多选题
ea = ExerciseAnswer . find_by_exercise_choice_id_and_user_id ( params [ :exercise_choice_id ] , User . current . id )
if ea . nil?
#尚未答该题,添加答案
ea = ExerciseAnswer . new
ea . user_id = User . current . id
ea . exercise_question_id = params [ :exercise_question_id ]
ea . answer_text = params [ :answer_text ]
ea . exercise_choice_id = params [ :exercise_choice_id ]
if ea . save
uncomplete_question = get_uncomplete_question ( @exercise , User . current )
uncomplete_question = get_uncomplete_question ( @exercise , exercise_user )
if uncomplete_question . count < 1
complete = 1 ;
else
complete = 0 ;
end
@percent = get_percent ( @exercise , User . current )
render :json = > { :text = > ea . answer_text , :complete = > complete , :percent = > format ( " %.2f " , @percent ) }
is_answer = User . current . exercise_answer . where ( :exercise_question_id = > params [ :exercise_question_id ] . to_i ) . count > 0
@percent = get_percent ( @exercise , exercise_user )
render :json = > { :text = > " ok " , :complete = > complete , :percent = > format ( " %.2f " , @percent ) , :is_answer = > is_answer }
else
render :json = > { :text = > " failure " }
end
end
else
# ea不为空说明用户之前已作答
if params [ :answer_text ] . nil? || params [ :answer_text ] . blank?
# 用户提交空答案,视为删除答案
else
#pv不为空, 则当前选项之前已被选择, 再次点击则是不再选择该项, 故删除该答案
if ea . delete
@percent = get_percent ( @exercise , User . current )
render :json = > { :text = > " " , :percent = > format ( " %.2f " , @percent ) }
is_answer = User . current . exercise_answer . where ( :exercise_question_id = > params [ :exercise_question_id ] . to_i ) . count > 0
@percent = get_percent ( @exercise , exercise_user )
render :json = > { :text = > " false " , :percent = > format ( " %.2f " , @percent ) , :is_answer = > is_answer }
else
render :json = > { :text = > " failure " }
end
end
elsif eq . question_type == 3 || eq . question_type == 4
#单行文本,多行文本题
ea = ExerciseAnswer . find_by_exercise_question_id_and_user_id ( params [ :exercise_question_id ] , User . current . id )
if ea . nil?
# ea为空之前尚未答题, 添加答案
if params [ :answer_text ] . nil? || params [ :answer_text ] . blank?
#用户提交空答案,视作不作答
@percent = get_percent ( @exercise , exercise_user )
render :json = > { :text = > " " , :percent = > format ( " %.2f " , @percent ) }
else
#添加答案
ea = ExerciseAnswer . new
ea . user_id = User . current . id
ea . exercise_question_id = params [ :exercise_question_id ]
ea . answer_text = params [ :answer_text ]
if ea . save
uncomplete_question = get_uncomplete_question ( @exercise , exercise_user )
if uncomplete_question . count < 1
complete = 1 ;
else
complete = 0 ;
end
@percent = get_percent ( @exercise , exercise_user )
render :json = > { :text = > ea . answer_text , :complete = > complete , :percent = > format ( " %.2f " , @percent ) }
else
render :json = > { :text = > " failure " }
end
end
else
#用户修改答案
ea . answer_text = params [ :answer_text ]
if ea . save
@percent = get_percent ( @exercise , User . current )
render :json = > { :text = > ea . answer_text , :percent = > format ( " %.2f " , @percent ) }
# ea不为空说明用户之前已作答
if params [ :answer_text ] . nil? || params [ :answer_text ] . blank?
# 用户提交空答案,视为删除答案
if ea . delete
@percent = get_percent ( @exercise , exercise_user )
render :json = > { :text = > " " , :percent = > format ( " %.2f " , @percent ) }
else
render :json = > { :text = > " failure " }
end
else
render :json = > { :text = > " failure " }
#用户修改答案
ea . answer_text = params [ :answer_text ]
if ea . save
@percent = get_percent ( @exercise , exercise_user )
render :json = > { :text = > ea . answer_text , :percent = > format ( " %.2f " , @percent ) }
else
render :json = > { :text = > " failure " }
end
end
end
end
else
render :json = > { :text = > " failure " }
end
else
render :json = > { :text = > " failure " }
end
@ -1165,9 +1179,10 @@ class ExerciseController < ApplicationController
# 获取学生未答情况
def get_student_uncomplete_question
exercise_user = @exercise . exercise_users . where ( :user_id = > User . current ) . first
time_limit = @exercise . time == - 1 || @exercise . time . nil?
uncomplete_count = get_uncomplete_question ( @exercise , User . current ) . count
uncomplete_shixun = get_uncomplete_shixun ( @exercise , User . current ) . count
uncomplete_count = get_uncomplete_question ( @exercise , exercise_user ) . count
uncomplete_shixun = get_uncomplete_shixun ( @exercise , exercise_user ) . count
render :json = > { :time_limit = > time_limit , :uncomplete_count = > uncomplete_count , :uncomplete_shixun = > uncomplete_shixun }
end
@ -1241,8 +1256,8 @@ class ExerciseController < ApplicationController
@is_teacher = params [ :pdf ] ? true : User . current . allowed_to? ( :as_teacher , @course ) || User . current . admin?
if @is_teacher || ( User . current . member_of_course? ( @exercise . course ) && @exercise . exercise_status > 2 )
@user = User . find params [ :user_id ]
@exercise_questions = @exercise . exercise_questions
@exercise_user = ExerciseUser . where ( " user_id =? and exercise_id=? " , @user . id , @exercise . id ) . first
@exercise_questions = @exercise . user_question_list @exercise_user . id
respond_to do | format |
if params [ :pdf ]
format . html { render :layout = > " pdf " }
@ -1305,7 +1320,8 @@ class ExerciseController < ApplicationController
score2 = 0
score3 = 0
score4 = 0
exercise_qustions = exercise . exercise_questions
exercise_user = exercise . exercise_users . where ( user_id : user . id ) . first
exercise_qustions = exercise . user_question_list ( exercise_user . try ( :id ) )
exercise_qustions . each do | question |
if question . question_type != 5
answer = get_user_answer ( question , user )
@ -1608,11 +1624,11 @@ class ExerciseController < ApplicationController
standard_answer
end
# 是否完成了答题
def get_complete_question ( exercise , user)
questions = exercise . exercise_questions . includes ( :exercise_answers )
def get_complete_question ( exercise , exercise_ user)
questions = exercise . user_question_list( exercise_user . id ) . includes ( :exercise_answers )
complete_question = [ ]
questions . each do | question |
answers = question . exercise_answers . select { | e | e . user_id == user. id}
answers = question . exercise_answers . select { | e | e . user_id == exercise_ user. user_ id}
if ! ( answers . nil? || answers . count < 1 )
complete_question << question
end
@ -1621,12 +1637,13 @@ class ExerciseController < ApplicationController
end
# 获取答题百分比
def get_percent exercise , user
complete_count = get_complete_question ( exercise , user ) . count
if exercise . exercise_questions . count == 0
def get_percent exercise , exercise_user
complete_count = get_complete_question ( exercise , exercise_user ) . count
questions = exercise . user_question_list ( exercise_user . id )
if questions . count == 0
return 0
else
return ( complete_count . to_f / exercise. exercise_ questions. count . to_f ) * 100
return ( complete_count . to_f / questions. count . to_f ) * 100
end
end