Merge remote-tracking branch 'origin/dev_aliyun' into dev_aliyun

dev_video
杨树明 5 years ago
commit 9e72cd1e59

@ -4,7 +4,7 @@ module ControllerRescueHandler
included do
rescue_from Exception do |e|
Util.logger_error e
render json: {status: -1, message: e.message}
render json: {status: -1, message: "接口异常"}
end
# rescue_from ActionView::MissingTemplate, with: :object_not_found
# rescue_from ActiveRecord::RecordNotFound, with: :object_not_found

File diff suppressed because it is too large Load Diff

@ -1168,7 +1168,7 @@ class ExercisesController < ApplicationController
#班级的选择
if params[:exercise_group_id].present?
group_id = params[:exercise_group_id]
exercise_students = @course_all_members.course_find_by_ids("course_group_id", group_id) #试卷所分班的全部人数
exercise_students = @course_all_members.course_find("course_group_id", group_id) #试卷所分班的全部人数
user_ids = exercise_students.pluck(:user_id).reject(&:blank?)
@exercise_users_list = @exercise_users_list.exercise_commit_users(user_ids)
end

@ -1,13 +1,13 @@
class PollQuestionsController < ApplicationController
before_action :require_login, :check_auth
before_action :get_poll,only:[:new,:create]
before_action :get_poll_question,except: [:new,:create]
before_action :get_poll, only: [:new, :create]
before_action :get_poll_question, except: [:new, :create]
before_action :is_course_teacher
before_action :get_poll_questions_count,only:[:create]
before_action :get_poll_question_answers,only:[:edit,:update]
before_action :check_poll_status,only: [:new,:create,:delete_answer,:destroy]
before_action :validates_params,only:[:create,:update]
before_action :validates_update_params,only: [:update]
before_action :get_poll_questions_count, only: [:create]
before_action :get_poll_question_answers, only: [:edit, :update]
before_action :check_poll_status, only: [:new, :create, :delete_answer, :destroy]
before_action :validates_params, only: [:create, :update]
before_action :validates_update_params, only: [:update]
def new
@ -25,54 +25,48 @@ class PollQuestionsController < ApplicationController
# 创建题目和选择的答案
def create
ActiveRecord::Base.transaction do
begin
poll_options = {
:question_title => params[:question_title],
:question_type => params[:question_type],
:is_necessary => params[:is_necessary].to_i,
:question_number => @poll_ques_count + 1,
:max_choices => params[:max_choices] || nil,
:min_choices => params[:min_choices] || nil
}
@poll_question = @poll.poll_questions.new(poll_options)
poll_options = {
:question_title => params[:question_title],
:question_type => params[:question_type],
:is_necessary => params[:is_necessary].to_i,
:question_number => @poll_ques_count + 1,
:max_choices => params[:max_choices] || nil,
:min_choices => params[:min_choices] || nil
}
@poll_question = @poll.poll_questions.new(poll_options)
if params[:insert_id].present? #插入问题时那么从插入的这个id以后的question_num都将要+1
insert_poll = @poll.poll_questions.find_by(id: params[:insert_id])
if insert_poll.present? #如果该问题存在的话,意思是如果是第一题,那么就不存在插入
ques_num = insert_poll.question_number.to_i
@poll_question.question_number = ques_num + 1 #更新了问题的位置
@poll.poll_questions.insert_question(ques_num).update_all("question_number = question_number + 1")
end
if params[:insert_id].present? #插入问题时那么从插入的这个id以后的question_num都将要+1
insert_poll = @poll.poll_questions.find_by(id: params[:insert_id])
if insert_poll.present? #如果该问题存在的话,意思是如果是第一题,那么就不存在插入
ques_num = insert_poll.question_number.to_i
@poll_question.question_number = ques_num + 1 #更新了问题的位置
@poll.poll_questions.insert_question(ques_num).update_all("question_number = question_number + 1")
end
if @poll_question.save!
if params[:question_type] != 3
p_answer = params[:question_answers]
p_other_answer = params[:question_other_answer]
# 新增选择题答案选择的选项
(1..p_answer.count).each do |i|
answer = p_answer[i-1] # 传入的答案的内容
question_option = {
:answer_position => i,
:answer_text => answer
}
poll_answers = @poll_question.poll_answers.new question_option
poll_answers.save!
end
# 新增答案的其他选项
if p_other_answer
question_option = {
:answer_position => p_answer.count + 1,
:answer_text => ''
}
poll_answers = @poll_question.poll_answers.new question_option
poll_answers.save!
end
end
if @poll_question.save!
if params[:question_type] != 3
p_answer = params[:question_answers]
p_other_answer = params[:question_other_answer]
# 新增选择题答案选择的选项
(1..p_answer.count).each do |i|
answer = p_answer[i - 1] # 传入的答案的内容
question_option = {
:answer_position => i,
:answer_text => answer
}
poll_answers = @poll_question.poll_answers.new question_option
poll_answers.save!
end
# 新增答案的其他选项
if p_other_answer
question_option = {
:answer_position => p_answer.count + 1,
:answer_text => ''
}
poll_answers = @poll_question.poll_answers.new question_option
poll_answers.save!
end
end
rescue Exception => e
uid_logger_error(e.message)
tip_exception("问卷的问题创建失败!")
raise ActiveRecord::Rollback
end
end
end
@ -103,61 +97,55 @@ class PollQuestionsController < ApplicationController
def update
ActiveRecord::Base.transaction do
begin
if @poll_question.question_type < 3 #当为单选题或多选题时
p_answer = params[:question_answers]
p_other_answer = params[:question_other_answer]
p_answer_count = p_answer.count
@poll_question.poll_answers.each do |an|
if (p_answer_count < @poll_current_answers) && (p_answer_count..@poll_current_answers).to_a.include?(an.answer_position)
an.destroy
end
if @poll_question.question_type < 3 #当为单选题或多选题时
p_answer = params[:question_answers]
p_other_answer = params[:question_other_answer]
p_answer_count = p_answer.count
@poll_question.poll_answers.each do |an|
if (p_answer_count < @poll_current_answers) && (p_answer_count..@poll_current_answers).to_a.include?(an.answer_position)
an.destroy
end
(1..p_answer_count).each do |i|
answer = @poll_question.poll_answers.find_answer_by_custom("answer_position",i).first
if answer # 判断该位置的answer是否存在存在则更新.不存在则跳到下一步
answer.answer_text = p_answer[i-1]
answer.answer_position = i
answer.save!
else
answer_options = {
:answer_position => i,
:answer_text => p_answer[i-1]
}
@poll_question.poll_answers.new answer_options
end
end
(1..p_answer_count).each do |i|
answer = @poll_question.poll_answers.find_answer_by_custom("answer_position", i).first
if answer # 判断该位置的answer是否存在存在则更新.不存在则跳到下一步
answer.answer_text = p_answer[i - 1]
answer.answer_position = i
answer.save!
else
answer_options = {
:answer_position => i,
:answer_text => p_answer[i - 1]
}
@poll_question.poll_answers.new answer_options
end
if p_other_answer #判断答案的其他选项是否存在
other_answer = @poll_question.poll_answers.find_answer_by_custom("answer_text","").first
if other_answer.blank?
question_option = {
:answer_position => p_answer_count + 1,
:answer_text => ''
}
@poll_question.poll_answers.new question_option
else
other_answer.answer_position = p_answer_count + 1
other_answer.save!
end
end
if p_other_answer #判断答案的其他选项是否存在
other_answer = @poll_question.poll_answers.find_answer_by_custom("answer_text", "").first
if other_answer.blank?
question_option = {
:answer_position => p_answer_count + 1,
:answer_text => ''
}
@poll_question.poll_answers.new question_option
else
other_answer.answer_position = p_answer_count + 1
other_answer.save!
end
end
@poll_question.update!(poll_questions_params)
rescue Exception => e
uid_logger_error(e.message)
tip_exception("更新失败")
raise ActiveRecord::Rollback
end
@poll_question.update!(poll_questions_params)
end
end
def delete_answer
ActiveRecord::Base.transaction do
begin
answer_d_id = params[:answer_no].to_i # 答案的当前位置
answer_d_id = params[:answer_no].to_i # 答案的当前位置
poll_answers = @poll_question.poll_answers
delete_answer = poll_answers.find_by(answer_position: answer_d_id)
left_answers = poll_answers.where("answer_position > ?",answer_d_id)
left_answers = poll_answers.where("answer_position > ?", answer_d_id)
left_answers.update_all("answer_position = answer_position - 1") if left_answers
if delete_answer.destroy
@ -175,7 +163,7 @@ class PollQuestionsController < ApplicationController
def destroy
ActiveRecord::Base.transaction do
begin
question_d_id = @poll_question.question_number.to_i #问题的当前位置
question_d_id = @poll_question.question_number.to_i #问题的当前位置
poll_questions = @poll.poll_questions
left_questions = poll_questions.insert_question(question_d_id)
left_questions.update_all("question_number = question_number - 1") if left_questions
@ -192,11 +180,11 @@ class PollQuestionsController < ApplicationController
ActiveRecord::Base.transaction do
begin
opr = params[:opr]
current_q_p = @poll_question.question_number.to_i #问题的当前位置
current_q_p = @poll_question.question_number.to_i #问题的当前位置
if @poll.polls_status.to_i == 1
if opr.present?
if opr.to_s == "up"
last_q_p = @poll.poll_questions.find_by(question_number: (current_q_p-1)) #当前问题的前一个问题
last_q_p = @poll.poll_questions.find_by(question_number: (current_q_p - 1)) #当前问题的前一个问题
if last_q_p.present?
@poll_question.update!(question_number: (current_q_p - 1))
last_q_p.update!(question_number: current_q_p) # 重新获取当前问题的位置
@ -205,7 +193,7 @@ class PollQuestionsController < ApplicationController
normal_status(-1, "移动失败,已经是第一个问题了!")
end
elsif opr.to_s == "down"
next_q_p = @poll.poll_questions.find_by(question_number: (current_q_p+1)) #当前问题的后一个问题
next_q_p = @poll.poll_questions.find_by(question_number: (current_q_p + 1)) #当前问题的后一个问题
if next_q_p.present?
@poll_question.update!(question_number: (current_q_p + 1))
next_q_p.update!(question_number: current_q_p)
@ -219,7 +207,7 @@ class PollQuestionsController < ApplicationController
normal_status(-1, "移动失败,请输入参数")
end
else
normal_status(-1,"已发布的不能移动问题")
normal_status(-1, "已发布的不能移动问题")
end
rescue Exception => e
uid_logger_error(e.message)
@ -231,7 +219,7 @@ class PollQuestionsController < ApplicationController
private
def poll_questions_params
params.require(:poll_question).permit(:question_title,:question_type,:is_necessary,:question_number,:max_choices,:min_choices)
params.require(:poll_question).permit(:question_title, :question_type, :is_necessary, :question_number, :max_choices, :min_choices)
end
def validates_params
@ -242,12 +230,12 @@ class PollQuestionsController < ApplicationController
normal_status(-1, "最小可选不能大于最大可选!")
elsif params[:question_answers].present? && (params[:max_choices].to_i > params[:question_answers].count)
normal_status(-1, "选择题的最大可选项不能大于答案数!")
elsif [1,3].include?(params[:question_type]) && (params[:max_choices].to_i > 0 || params[:min_choices].to_i > 0)
elsif [1, 3].include?(params[:question_type]) && (params[:max_choices].to_i > 0 || params[:min_choices].to_i > 0)
normal_status(-1, "单选题或主观题不能有最大或最小选择数!")
elsif params[:question_type] == 3 && (params[:question_answers] || params[:question_other_answer])
normal_status(-1, "主观问题不需要可选答案!")
elsif params[:question_type] != 3
if params[:question_answers].present? && params[:question_answers].select{|answer| answer.blank?}.count > 0
if params[:question_answers].present? && params[:question_answers].select {|answer| answer.blank?}.count > 0
normal_status(-1, "选项不能有空值!")
elsif params[:question_other_answer].present? && !params[:question_other_answer].blank?
normal_status(-1, "其他选项不能有值!")
@ -262,11 +250,11 @@ class PollQuestionsController < ApplicationController
def validates_update_params
question_a_count = params[:question_answers].present? ? params[:question_answers].count : 0
question_o_count = params[:question_other_answer].present? ? params[:question_other_answer].count : 0
normal_status(-1, "已发布的问卷不允许增删答案!") if (((question_a_count+question_o_count) != @poll_current_answers) && (@poll.polls_status.to_i != 1))
normal_status(-1, "已发布的问卷不允许增删答案!") if (((question_a_count + question_o_count) != @poll_current_answers) && (@poll.polls_status.to_i != 1))
end
def get_poll
@poll = Poll.find_by(id:params[:poll_id])
@poll = Poll.find_by(id: params[:poll_id])
if @poll.blank?
tip_exception(404)
end
@ -283,7 +271,7 @@ class PollQuestionsController < ApplicationController
def get_poll_question
@poll_question = PollQuestion.find_by(id: params[:id])
if @poll_question.present?
@poll = Poll.find_by(id:@poll_question.poll_id)
@poll = Poll.find_by(id: @poll_question.poll_id)
else
tip_exception(404)
end
@ -305,7 +293,7 @@ class PollQuestionsController < ApplicationController
tip_exception(404)
else
@identity = current_user.course_identity(@course)
normal_status(-1, "权限不够") unless(@course.present? && @identity < Course::STUDENT) #课堂存在,且当前用户为教师/管理员
normal_status(-1, "权限不够") unless (@course.present? && @identity < Course::STUDENT) #课堂存在,且当前用户为教师/管理员
end
end
end

@ -5,120 +5,114 @@ class PollVotesController < ApplicationController
before_action :check_answer_in_question
def create #每一次答案的点击,请求一次
begin
question_votes = @poll_question.poll_votes
question_type = @poll_question.question_type
question_answer_id = params[:poll_answer_id] ? params[:poll_answer_id] : -1 #该答案的id
question_answer_text = params[:vote_text].present? ? params[:vote_text] : nil #其他选项的内容
user_votes = question_votes.find_current_vote("user_id",current_user.id) #当前用户的答案,可能有多个
# 当前用户的当前答案,如果已存在,当再次点击的时候,取消答案,即删除该答案
current_vote_text = nil
def create #每一次答案的点击,请求一次
question_votes = @poll_question.poll_votes
question_type = @poll_question.question_type
question_answer_id = params[:poll_answer_id] ? params[:poll_answer_id] : -1 #该答案的id
question_answer_text = params[:vote_text].present? ? params[:vote_text] : nil #其他选项的内容
user_votes = question_votes.find_current_vote("user_id", current_user.id) #当前用户的答案,可能有多个
# 当前用户的当前答案,如果已存在,当再次点击的时候,取消答案,即删除该答案
current_vote_text = nil
# if user_votes.find_vote_text.present?
# current_vote_text = user_votes.find_vote_text.first
# end
# if user_votes.find_vote_text.present?
# current_vote_text = user_votes.find_vote_text.first
# end
vote_answer_params = {
:user_id => current_user.id,
:poll_question_id => @poll_question.id,
:poll_answer_id => question_answer_id,
:vote_text => question_answer_text
}
#begin
if question_type == 1
if user_votes.present? #用户曾经回答过的,答案选择不一样,否则新建
current_user_answer = user_votes.first
if current_user_answer&.poll_answer_id != question_answer_id #如果说更换了答案,则以前的答案删除,并新建记录
current_user_answer.destroy
PollVote.create!(vote_answer_params)
else
vote_answer_params = {
:user_id => current_user.id,
:poll_question_id => @poll_question.id,
:poll_answer_id => question_answer_id,
:vote_text => question_answer_text
}
#begin
if question_type == 1
if user_votes.present? #用户曾经回答过的,答案选择不一样,否则新建
current_user_answer = user_votes.first
if current_user_answer&.poll_answer_id != question_answer_id #如果说更换了答案,则以前的答案删除,并新建记录
current_user_answer.destroy
PollVote.create!(vote_answer_params)
else
if question_answer_text.present?
current_user_answer.update!(vote_text: question_answer_text)
end
if question_answer_text.present?
current_user_answer.update!(vote_text: question_answer_text)
end
else
PollVote.create!(vote_answer_params)
end
elsif question_type == 2 #多选题的话答案应该是1个以上
question_answer_ids = params[:poll_answer_id] ? params[:poll_answer_id] : [] #该答案的id
if question_answer_ids.present?
if question_answer_text.present? #有文字输入,但是不存在其他选项的
ques_vote_id = question_answer_ids.map(&:to_i).max
ques_vote = user_votes.find_by(poll_answer_id: ques_vote_id)
if ques_vote.present?
ques_vote.update!(vote_text: question_answer_text)
else
answer_option = {
:user_id => current_user.id,
:poll_question_id => @poll_question.id,
:poll_answer_id => ques_vote_id,
:vote_text => question_answer_text
}
PollVote.create!(answer_option)
# 重新取一次poll_votes
user_votes = @poll_question.poll_votes.find_current_vote("user_id",current_user.id)
end
# if current_vote_text.present? #已有其他输入文字的选项
# current_vote_text.update_attribute("vote_text", question_answer_text)
# else
#
# end
else
PollVote.create!(vote_answer_params)
end
elsif question_type == 2 #多选题的话答案应该是1个以上
question_answer_ids = params[:poll_answer_id] ? params[:poll_answer_id] : [] #该答案的id
if question_answer_ids.present?
if question_answer_text.present? #有文字输入,但是不存在其他选项的
ques_vote_id = question_answer_ids.map(&:to_i).max
ques_vote = user_votes.find_by(poll_answer_id: ques_vote_id)
if ques_vote.present?
ques_vote.update!(vote_text: question_answer_text)
else
answer_option = {
:user_id => current_user.id,
:poll_question_id => @poll_question.id,
:poll_answer_id => ques_vote_id,
:vote_text => question_answer_text
}
PollVote.create!(answer_option)
# 重新取一次poll_votes
user_votes = @poll_question.poll_votes.find_current_vote("user_id", current_user.id)
end
# if current_vote_text.present? #已有其他输入文字的选项
# current_vote_text.update_attribute("vote_text", question_answer_text)
# else
#
# end
end
ea_ids = user_votes.pluck(:poll_answer_id)
common_answer_ids = question_answer_ids & ea_ids #已经存在的试卷选项id
new_ids = question_answer_ids - common_answer_ids # 新增的id
old_ids = ea_ids - common_answer_ids #没有选择的,则删掉
if new_ids.size > 0
new_ids.each do |e|
answer_option = {
:user_id => current_user.id,
:poll_question_id => @poll_question.id,
:poll_answer_id => e,
:vote_text => nil
}
ex_a = PollVote.new(answer_option)
ex_a.save!
end
ea_ids = user_votes.pluck(:poll_answer_id)
common_answer_ids = question_answer_ids & ea_ids #已经存在的试卷选项id
new_ids = question_answer_ids - common_answer_ids # 新增的id
old_ids = ea_ids - common_answer_ids #没有选择的,则删掉
if new_ids.size > 0
new_ids.each do |e|
answer_option = {
:user_id => current_user.id,
:poll_question_id => @poll_question.id,
:poll_answer_id => e,
:vote_text => nil
}
ex_a = PollVote.new(answer_option)
ex_a.save!
end
if old_ids.size > 0
ea_answer = user_votes.find_current_vote("poll_answer_id",old_ids)
ea_answer.destroy_all
end
else
user_votes.destroy_all
end
else #主观题的输入
# current_vote_text = user_votes.find_vote_text
if user_votes.present?
user_votes.first.update!(vote_text: question_answer_text)
# if question_answer_text.present?
# user_votes.first.update_attribute("vote_text", question_answer_text)
# else
# user_votes.destroy_all
# end
else
PollVote.create!(vote_answer_params)
if old_ids.size > 0
ea_answer = user_votes.find_current_vote("poll_answer_id", old_ids)
ea_answer.destroy_all
end
else
user_votes.destroy_all
end
@current_question_number = @poll_question.question_number
@current_question_necessary = @poll_question.is_necessary
#问答记录存在,且有值,才会有返回值。
@current_question_status = 0
new_user_votes = question_votes.where(user_id: current_user.id)
if new_user_votes.present?
vote_answer_id = new_user_votes.pluck(:poll_answer_id).reject(&:blank?).size
vote_text_count = new_user_votes.pluck(:vote_text).reject(&:blank?).size
if vote_text_count > 0 || vote_answer_id > 0
@current_question_status = 1
end
else #主观题的输入
# current_vote_text = user_votes.find_vote_text
if user_votes.present?
user_votes.first.update!(vote_text: question_answer_text)
# if question_answer_text.present?
# user_votes.first.update_attribute("vote_text", question_answer_text)
# else
# user_votes.destroy_all
# end
else
PollVote.create!(vote_answer_params)
end
end
@current_question_number = @poll_question.question_number
@current_question_necessary = @poll_question.is_necessary
#问答记录存在,且有值,才会有返回值。
@current_question_status = 0
new_user_votes = question_votes.where(user_id: current_user.id)
if new_user_votes.present?
vote_answer_id = new_user_votes.pluck(:poll_answer_id).reject(&:blank?).size
vote_text_count = new_user_votes.pluck(:vote_text).reject(&:blank?).size
if vote_text_count > 0 || vote_answer_id > 0
@current_question_status = 1
end
rescue Exception => e
uid_logger_error(e.message)
tip_exception("页面调用失败!")
raise ActiveRecord::Rollback
end
end
@ -128,14 +122,14 @@ class PollVotesController < ApplicationController
def get_poll_question
@poll_question = PollQuestion.find_by_id(params[:poll_question_id])
if @poll_question.blank?
normal_status(-1,"问卷试题不存在!")
normal_status(-1, "问卷试题不存在!")
else
@poll = @poll_question.poll
@course = @poll.course
if @poll.blank?
normal_status(-1,"问卷不存在!")
normal_status(-1, "问卷不存在!")
elsif @course.blank?
normal_status(-1,"课堂不存在!")
normal_status(-1, "课堂不存在!")
end
end
end
@ -146,7 +140,7 @@ class PollVotesController < ApplicationController
question_type = @poll_question&.question_type
if (question_type == 1) && params[:poll_answer_id].blank?
normal_status(-1,"答案ID错误!")
normal_status(-1, "答案ID错误!")
elsif question_type == 2
user_vote_count = params[:poll_answer_id].size
if @poll_question.max_choices.present?
@ -155,10 +149,10 @@ class PollVotesController < ApplicationController
question_max_choices = 0
end
if question_max_choices > 0 && (user_vote_count > question_max_choices)
normal_status(-1,"多选题答案超过最大限制!")
normal_status(-1, "多选题答案超过最大限制!")
end
elsif (poll_user.present? && poll_user.commit_status == 1) || poll_user_status == 3
normal_status(-1,"已提交/已结束的问卷不允许修改!")
normal_status(-1, "已提交/已结束的问卷不允许修改!")
end
end

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save