dev_course
SylorHuang 6 years ago
parent 41f92346db
commit 6f7598bde1

@ -1,7 +1,6 @@
class ExerciseAnswersController < ApplicationController
before_action :require_login
before_action :get_exercise_question
before_action :commit_exercise_time
include ExercisesHelper
def create #每一次答案的点击,请求一次,实训题不在这里回答
@ -95,9 +94,11 @@ class ExerciseAnswersController < ApplicationController
def get_exercise_question
@exercise_question = ExerciseQuestion.find_by_id(params[:exercise_question_id])
@exercise = @exercise_question.exercise
@exercise = @exercise_question.exercise.includes(:exercise_users)
@course = @exercise.course
@exercise_user = @exercise.exercise_users.exercise_commit_users(current_user.id).first #当前用户
@exercise_user = @exercise.exercise_users.find_by(user_id: current_user.id) #当前用户
@exercise_user_status = @exercise.get_exercise_status(current_user.id)
if @exercise_question.blank?
normal_status(-1,"试卷问题不存在!")
@ -105,34 +106,15 @@ class ExerciseAnswersController < ApplicationController
normal_status(-1,"试卷不存在!")
elsif @course.blank?
normal_status(-1,"该课堂不存在!")
elsif (@exercise_user.present? && @exercise_user.commit_status == 1) || (@exercise.end_time.present? && @exercise.end_time < Time.now) #已提交答案的/时间已结束的试卷不允许再修改
elsif (@exercise_user.present? && @exercise_user&.commit_status == 1) || @exercise_user_status == 3 #已提交答案的/时间已结束的试卷不允许再修改
normal_status(-1,"已提交/已结束的试卷不允许修改!")
elsif @exercise.time > 0
user_start_at = @exercise.exercise_users.exercise_commit_users(current_user.id).first.start_at
exercise_time = @exercise.time.to_i
user_start_at = @exercise_user&.start_at
exercise_time = @exercise.time.to_i + 1 #
if (user_start_at + exercise_time.minutes) < Time.now
normal_status(-1,"限时试卷已结束!")
end
end
end
def commit_exercise_time
@exercise_user_current = @exercise.exercise_users.exercise_commit_users(current_user.id).first #查找当前用户是否有过答题
if @exercise.exercise_status == 3 || (@exercise.time > 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(-1,"考试时间已到,已交卷成功!")
end
end

@ -2,8 +2,7 @@ class PollVotesController < ApplicationController
#在开始回答和提交问卷的时候,已经做了判断用户的身份权限
before_action :require_login
before_action :get_poll_question
before_action :check_answer_in_question,only: [:create]
before_action :check_multi_answers
before_action :check_answer_in_question
def create #每一次答案的点击,请求一次
@ -124,7 +123,7 @@ class PollVotesController < ApplicationController
if @poll_question.blank?
normal_status(-1,"问卷试题不存在!")
else
@poll = @poll_question.poll
@poll = @poll_question.poll.includes(:poll_users)
@course = @poll.course
if @poll.blank?
normal_status(-1,"问卷不存在!")
@ -132,20 +131,22 @@ class PollVotesController < ApplicationController
normal_status(-1,"课堂不存在!")
end
end
end
def check_answer_in_question
poll_answer_ids = @poll_question.poll_answers.pluck(:id)
if @poll_question.question_type == 1 #单选题/多选题
unless (params[:poll_answer_id].present? && poll_answer_ids.include?(params[:poll_answer_id].to_i)) || (params[:poll_answer_id].blank? && params[:vote_text].present?)
normal_status(-1, "答案ID错误")
end
end
end
# poll_answer_ids = @poll_question.poll_answers.pluck(:id)
# if @poll_question.question_type == 1 #单选题/多选题
# unless (params[:poll_answer_id].present? && poll_answer_ids.include?(params[:poll_answer_id].to_i)) || (params[:poll_answer_id].blank? && params[:vote_text].present?)
# normal_status(-1, "答案ID错误")
# end
# end
poll_user_status = @poll.get_poll_status(current_user.id)
poll_user = @poll.poll_users.find_by(user_id: current_user.id) #当前用户
def check_multi_answers
if @poll_question.question_type == 2
question_type = @poll_question&.question_type
if [1,2].include?(question_type) && params[:poll_answer_id].blank?
normal_status(-1,"答案ID错误!")
elsif question_type == 2
user_vote_count = params[:poll_answer_id].size
if @poll_question.max_choices.present?
question_max_choices = @poll_question.max_choices
@ -155,6 +156,9 @@ class PollVotesController < ApplicationController
if question_max_choices > 0 && user_vote_count > question_max_choices
normal_status(-1,"多选题答案超过最大限制!")
end
elsif (poll_user.present? && poll_user&.commit_status) || poll_user_status == 3
normal_status(-1,"已提交/已结束的问卷不允许修改!")
end
end
end

Loading…
Cancel
Save