diff --git a/app/controllers/concerns/controller_rescue_handler.rb b/app/controllers/concerns/controller_rescue_handler.rb index 101b72f5c..b8b0d4616 100644 --- a/app/controllers/concerns/controller_rescue_handler.rb +++ b/app/controllers/concerns/controller_rescue_handler.rb @@ -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 diff --git a/app/controllers/exercise_questions_controller.rb b/app/controllers/exercise_questions_controller.rb index ff45b34f0..e237f4605 100644 --- a/app/controllers/exercise_questions_controller.rb +++ b/app/controllers/exercise_questions_controller.rb @@ -1,18 +1,18 @@ class ExerciseQuestionsController < ApplicationController - before_action :require_login, :check_auth #用户需登陆 - before_action :get_exercise,only:[:new,:create] #获取试卷 - before_action :get_exercise_question,except: [:new,:create] #获取试卷的问题及试卷 - before_action :is_course_teacher #是否为老师 - before_action :validate_params,only: [:create,:update] #传入参数的验证 - before_action :check_exercise_status,only: [:new,:create,:delete_answer,:destroy] #除未发布状态之外,其余状态不能进行增删操作 - before_action :cannot_change_column,only: [:update] #更新时不能更改的内容 - before_action :check_adjust_score,only: [:adjust_score] + before_action :require_login, :check_auth #用户需登陆 + before_action :get_exercise, only: [:new, :create] #获取试卷 + before_action :get_exercise_question, except: [:new, :create] #获取试卷的问题及试卷 + before_action :is_course_teacher #是否为老师 + before_action :validate_params, only: [:create, :update] #传入参数的验证 + before_action :check_exercise_status, only: [:new, :create, :delete_answer, :destroy] #除未发布状态之外,其余状态不能进行增删操作 + before_action :cannot_change_column, only: [:update] #更新时不能更改的内容 + before_action :check_adjust_score, only: [:adjust_score] include ExercisesHelper def new ActiveRecord::Base.transaction do begin - @exercise_question = @exercise.exercise_questions.new + @exercise_question = @exercise.exercise_questions.new rescue Exception => e uid_logger_error(e.message) tip_exception("页面访问失败失败!") @@ -24,127 +24,121 @@ class ExerciseQuestionsController < ApplicationController #question_type 0为单选题,1为多选题,2为判断题,3为填空题(单空和多空),4为简答题,5为实训题 def create ActiveRecord::Base.transaction do - begin - question_options = { - :question_title => params[:question_title], - :question_type => params[:question_type].present? ? params[:question_type].to_i : 0, #默认为单选题 - :question_number => @exercise.exercise_questions.count + 1, - :question_score => params[:question_score].present? ? params[:question_score].to_f.round(1) : 5.0, - :shixun_id => params[:shixun_id].blank? ? "" : params[:shixun_id], - :is_ordered => params[:is_ordered] # 填空题的答案是否为一一对应关系,默认为true即为一一对应 - } - @exercise_question = @exercise.exercise_questions.new(question_options) - #插入问题时,那么从插入的这个id以后的question_num都将要+1 - if params[:insert_id].present? - insert_exercise = @exercise.exercise_questions.find_by(id: params[:insert_id]) - if insert_exercise.present? #如果该问题存在的话,意思是如果是第一题,那么就不存在插入 - ques_num = insert_exercise.question_number.to_i - @exercise_question.question_number = ques_num + 1 #更新了问题的位置 - @exercise.exercise_questions.insert_question_ex(ques_num).update_all("question_number = question_number + 1") - end + question_options = { + :question_title => params[:question_title], + :question_type => params[:question_type].present? ? params[:question_type].to_i : 0, #默认为单选题 + :question_number => @exercise.exercise_questions.count + 1, + :question_score => params[:question_score].present? ? params[:question_score].to_f.round(1) : 5.0, + :shixun_id => params[:shixun_id].blank? ? "" : params[:shixun_id], + :is_ordered => params[:is_ordered] # 填空题的答案是否为一一对应关系,默认为true即为一一对应 + } + @exercise_question = @exercise.exercise_questions.new(question_options) + #插入问题时,那么从插入的这个id以后的question_num都将要+1 + if params[:insert_id].present? + insert_exercise = @exercise.exercise_questions.find_by(id: params[:insert_id]) + if insert_exercise.present? #如果该问题存在的话,意思是如果是第一题,那么就不存在插入 + ques_num = insert_exercise.question_number.to_i + @exercise_question.question_number = ques_num + 1 #更新了问题的位置 + @exercise.exercise_questions.insert_question_ex(ques_num).update_all("question_number = question_number + 1") end + end - if @exercise_question.save! - #为选择题(包括单选和多选)的时候,创建问题选项 - ques_type = @exercise_question.question_type - if ques_type <= Exercise::MULTIPLE - choices_array = params[:question_choices] - choices_count= choices_array.count - standard_answer = params[:standard_answers] #为数组格式,因为可能会有单选和多选,标准答案,已提前判断不能为空, - standard_answer = standard_answer.uniq.reject(&:blank?) - (1..choices_count).each do |c| - choice = choices_array[c-1] #每一个选项的内容 - choice_option = { - :choice_position => c, - :choice_text => choice.strip - } - question_choices = @exercise_question.exercise_choices.new(choice_option) - question_choices.save! + if @exercise_question.save! + #为选择题(包括单选和多选)的时候,创建问题选项 + ques_type = @exercise_question.question_type + if ques_type <= Exercise::MULTIPLE + choices_array = params[:question_choices] + choices_count = choices_array.count + standard_answer = params[:standard_answers] #为数组格式,因为可能会有单选和多选,标准答案,已提前判断不能为空, + standard_answer = standard_answer.uniq.reject(&:blank?) + (1..choices_count).each do |c| + choice = choices_array[c - 1] #每一个选项的内容 + choice_option = { + :choice_position => c, + :choice_text => choice.strip + } + question_choices = @exercise_question.exercise_choices.new(choice_option) + question_choices.save! + end + #标准答案的存储,如:["1","2","3"..]等,1对应A,2对应B,3对应C。。。 + standard_answer.each do |a| + choice_id = a.to_i + standard_option = { + :exercise_question_id => @exercise_question.id, + :exercise_choice_id => choice_id #即为选择的位置参数 + } + question_standard_answer = ExerciseStandardAnswer.new(standard_option) + question_standard_answer.save! + if standard_answer.count > 1 && ques_type == Exercise::SINGLE #当标准答案数大于1,且不为多选时,修改为多选 + @exercise_question.update_attribute("question_type", Exercise::MULTIPLE) + elsif standard_answer.count == 1 && ques_type == Exercise::MULTIPLE + @exercise_question.update_attribute("question_type", Exercise::SINGLE) end - #标准答案的存储,如:["1","2","3"..]等,1对应A,2对应B,3对应C。。。 - standard_answer.each do |a| - choice_id = a.to_i + end + elsif ques_type == Exercise::JUDGMENT #这个为判断题 + choices_array = params[:question_choices] #判断的选项,对/错等等 + choices_count = choices_array.count + (1..choices_count).each do |c| + choice = choices_array[c - 1] #每一个选项的内容 + choice_option = { + :choice_position => c, + :choice_text => choice.strip + } + question_choices = @exercise_question.exercise_choices.create!(choice_option) + question_choices.save! + end + standard_answer = params[:standard_answers] #对应选项的id + standard_option = { + :exercise_question_id => @exercise_question.id, + :exercise_choice_id => standard_answer.first.to_i + } + question_standard_answer = ExerciseStandardAnswer.new(standard_option) + question_standard_answer.save! + elsif ques_type == Exercise::COMPLETION #填空题,每空的参考答案有多个,那么以位置对应 + standard_answer = params[:standard_answers] + standard_answer.each do |a| + null_choice_id = a[:choice_id] + null_choice_text = a[:answer_text] + null_choice_text.each do |n| standard_option = { - :exercise_question_id => @exercise_question.id, - :exercise_choice_id => choice_id #即为选择的位置参数 + :exercise_question_id => @exercise_question.id, + :exercise_choice_id => null_choice_id, + :answer_text => n } question_standard_answer = ExerciseStandardAnswer.new(standard_option) question_standard_answer.save! - if standard_answer.count > 1 && ques_type == Exercise::SINGLE #当标准答案数大于1,且不为多选时,修改为多选 - @exercise_question.update_attribute("question_type",Exercise::MULTIPLE) - elsif standard_answer.count == 1 && ques_type == Exercise::MULTIPLE - @exercise_question.update_attribute("question_type",Exercise::SINGLE) - end - end - elsif ques_type == Exercise::JUDGMENT #这个为判断题 - choices_array = params[:question_choices] #判断的选项,对/错等等 - choices_count= choices_array.count - (1..choices_count).each do |c| - choice = choices_array[c-1] #每一个选项的内容 - choice_option = { - :choice_position => c, - :choice_text => choice.strip - } - question_choices = @exercise_question.exercise_choices.create!(choice_option) - question_choices.save! end - standard_answer = params[:standard_answers] #对应选项的id - standard_option = { - :exercise_question_id => @exercise_question.id, - :exercise_choice_id => standard_answer.first.to_i - } - question_standard_answer = ExerciseStandardAnswer.new(standard_option) - question_standard_answer.save! - elsif ques_type == Exercise::COMPLETION #填空题,每空的参考答案有多个,那么以位置对应 + end + elsif ques_type == Exercise::SUBJECTIVE #简答题 + if params[:standard_answers].present? && params[:standard_answers].reject(&:blank?).count > 0 standard_answer = params[:standard_answers] standard_answer.each do |a| - null_choice_id = a[:choice_id] - null_choice_text = a[:answer_text] - null_choice_text.each do |n| - standard_option = { - :exercise_question_id => @exercise_question.id, - :exercise_choice_id => null_choice_id, - :answer_text => n - } - question_standard_answer = ExerciseStandardAnswer.new(standard_option) - question_standard_answer.save! - end - end - elsif ques_type == Exercise::SUBJECTIVE #简答题 - if params[:standard_answers].present? && params[:standard_answers].reject(&:blank?).count > 0 - standard_answer = params[:standard_answers] - standard_answer.each do |a| - standard_option = { - :exercise_question_id => @exercise_question.id, - :answer_text => a, - } - question_standard_answer = ExerciseStandardAnswer.new(standard_option) - question_standard_answer.save! - end - end - elsif ques_type == Exercise::PRACTICAL #实训题 - shixun = Shixun.find_by(id: params[:shixun_id]) - shixun_scores = params[:question_scores] #试卷有多个的分值有多个分数表,所以为分数的数组 - shixun_name = params[:shixun_name] || shixun.name - question_score = 0 - shixun.challenges.try(:each_with_index) do |challenge,index| - shixun_option = { - :challenge_id => challenge.id, - :shixun_id => shixun.id, - :exercise_question_id => @exercise_question.id, - :position => (index + 1), - :question_score => shixun_scores[index].present? ? shixun_scores[index].to_f.round(1) : 5 + standard_option = { + :exercise_question_id => @exercise_question.id, + :answer_text => a, } - ex_shixun_challenge = ExerciseShixunChallenge.create!(shixun_option) - question_score += ex_shixun_challenge.question_score # 问题的分数,为各个关卡分数的总和 + question_standard_answer = ExerciseStandardAnswer.new(standard_option) + question_standard_answer.save! end - @exercise_question.update!(:question_score => question_score,:shixun_name=> shixun_name) end + elsif ques_type == Exercise::PRACTICAL #实训题 + shixun = Shixun.find_by(id: params[:shixun_id]) + shixun_scores = params[:question_scores] #试卷有多个的分值有多个分数表,所以为分数的数组 + shixun_name = params[:shixun_name] || shixun.name + question_score = 0 + shixun.challenges.try(:each_with_index) do |challenge, index| + shixun_option = { + :challenge_id => challenge.id, + :shixun_id => shixun.id, + :exercise_question_id => @exercise_question.id, + :position => (index + 1), + :question_score => shixun_scores[index].present? ? shixun_scores[index].to_f.round(1) : 5 + } + ex_shixun_challenge = ExerciseShixunChallenge.create!(shixun_option) + question_score += ex_shixun_challenge.question_score # 问题的分数,为各个关卡分数的总和 + end + @exercise_question.update!(:question_score => question_score, :shixun_name => shixun_name) end - rescue Exception => e - uid_logger_error(e.message) - tip_exception("试卷问题创建失败!") - raise ActiveRecord::Rollback end end end @@ -164,222 +158,209 @@ class ExerciseQuestionsController < ApplicationController def edit ActiveRecord::Base.transaction do - begin - @exercise_choices = @exercise_question.exercise_choices - @exercise_question_shixun = @exercise_question.exercise_shixun_challenges - rescue Exception => e - uid_logger_error(e.message) - tip_exception("页面调用失败!") - raise ActiveRecord::Rollback - end + @exercise_choices = @exercise_question.exercise_choices + @exercise_question_shixun = @exercise_question.exercise_shixun_challenges end end def update ActiveRecord::Base.transaction do - begin - standard_answer_change = false - # 更新试卷题目的内容 - question_options = { - :question_title => params[:question_title], - :is_ordered => params[:is_ordered], # 填空题的答案是否为一一对应关系,默认为true即为一一对应 - :question_score => params[:question_score].present? ? params[:question_score].to_f.round(1) : 5.0 #不可修改分数 - } - choices_array = params[:question_choices] - stan_answer_params = params[:standard_answers] - standard_answer = stan_answer_params.present? ? stan_answer_params.uniq.reject(&:blank?) : [] - @exercise_question.update!(question_options) - #当选项存在时,可修改选项内容,但是不能更改选项的位置(即不能增删选项) - if choices_array.present? - ex_choices = @exercise_question.exercise_choices - ex_choice_count = ex_choices.count - choice_array_count = choices_array.count - ex_choice_count_array = (1..ex_choice_count).to_a - choice_array_count_array = (1..choice_array_count).to_a - if ex_choice_count > choice_array_count #如果选项有减少的,那么只更新传入的,删除以前的 - choice_array_count_array.each do |c| - choice = choices_array[c-1] #每一个选项的内容 - exercise_choice = @exercise_question.exercise_choices.find_choice_custom("choice_position",(c)) - exercise_choice.update(choice_text:choice) + standard_answer_change = false + # 更新试卷题目的内容 + question_options = { + :question_title => params[:question_title], + :is_ordered => params[:is_ordered], # 填空题的答案是否为一一对应关系,默认为true即为一一对应 + :question_score => params[:question_score].present? ? params[:question_score].to_f.round(1) : 5.0 #不可修改分数 + } + choices_array = params[:question_choices] + stan_answer_params = params[:standard_answers] + standard_answer = stan_answer_params.present? ? stan_answer_params.uniq.reject(&:blank?) : [] + @exercise_question.update!(question_options) + #当选项存在时,可修改选项内容,但是不能更改选项的位置(即不能增删选项) + if choices_array.present? + ex_choices = @exercise_question.exercise_choices + ex_choice_count = ex_choices.count + choice_array_count = choices_array.count + ex_choice_count_array = (1..ex_choice_count).to_a + choice_array_count_array = (1..choice_array_count).to_a + if ex_choice_count > choice_array_count #如果选项有减少的,那么只更新传入的,删除以前的 + choice_array_count_array.each do |c| + choice = choices_array[c - 1] #每一个选项的内容 + exercise_choice = @exercise_question.exercise_choices.find_choice_custom("choice_position", (c)) + exercise_choice.update(choice_text: choice) + end + drop_ex_choice = @exercise_question.exercise_choices.left_choice_choose("choice_position", (choice_array_count)) + drop_ex_choice.destroy_all + else + ex_choice_count_array.each do |c| + choice = choices_array[c - 1] #每一个选项的内容 + exercise_choice = @exercise_question.exercise_choices.find_choice_custom("choice_position", (c)) + exercise_choice.update(choice_text: choice) + end + new_add_choice = choice_array_count_array - ex_choice_count_array #新传入的需新增 + if new_add_choice.count > 0 + new_add_choice.each do |i| + choice_option = { + :choice_position => i, + :choice_text => choices_array[i - 1].strip + } + question_choices = @exercise_question.exercise_choices.new(choice_option) + question_choices.save! end - drop_ex_choice = @exercise_question.exercise_choices.left_choice_choose("choice_position",(choice_array_count)) - drop_ex_choice.destroy_all - else - ex_choice_count_array.each do |c| - choice = choices_array[c-1] #每一个选项的内容 - exercise_choice = @exercise_question.exercise_choices.find_choice_custom("choice_position",(c)) - exercise_choice.update(choice_text:choice) + end + end + end + #试卷未发布时,当标准答案存在时,可修改标准答案内容,可增删标准答案,否则只能修改标准答案,不能增删标准答案 + @exercise_answers_array = @exercise_question.exercise_standard_answers #问卷的全部标准答案 + if standard_answer.present? + if @exercise_question.question_type <= Exercise::JUDGMENT #选择题/判断题,标准答案为一个或多个 + exercise_standard_choices = @exercise_answers_array.pluck(:exercise_choice_id) #问题以前的全部标准答案选项位置 + if exercise_standard_choices.sort != standard_answer.sort #表示答案有更改的 + standard_answer_change = true + common_standard_choices = standard_answer & exercise_standard_choices # 传入的标准答案的选项位置和以前的并集,即表示不用做更改的 + old_left_standard_choices = exercise_standard_choices - common_standard_choices # 以前的差集共同的,剩余的表示需要删掉 + new_left_standard_choices = standard_answer - common_standard_choices # 传入的标准答案差集共同的,剩余的表示需要新建 + if old_left_standard_choices.count > 0 + @exercise_answers_array.standard_by_ids(old_left_standard_choices).destroy_all end - new_add_choice = choice_array_count_array - ex_choice_count_array #新传入的需新增 - if new_add_choice.count > 0 - new_add_choice.each do |i| - choice_option = { - :choice_position => i, - :choice_text => choices_array[i-1].strip + if new_left_standard_choices.count > 0 #新建标准答案 + new_left_standard_choices.each do |s| + standard_option = { + :exercise_question_id => @exercise_question.id, + :exercise_choice_id => s.to_i #即为选择的位置参数 } - question_choices = @exercise_question.exercise_choices.new(choice_option) - question_choices.save! + question_standard_answer = ExerciseStandardAnswer.new(standard_option) + question_standard_answer.save! end + + end + if standard_answer.count > 1 && @exercise_question.question_type == Exercise::SINGLE #当标准答案数大于1,且不为多选时,修改为多选 + @exercise_question.update_attribute("question_type", Exercise::MULTIPLE) + elsif standard_answer.count == 1 && @exercise_question.question_type == Exercise::MULTIPLE + @exercise_question.update_attribute("question_type", Exercise::SINGLE) end end - end - #试卷未发布时,当标准答案存在时,可修改标准答案内容,可增删标准答案,否则只能修改标准答案,不能增删标准答案 - @exercise_answers_array = @exercise_question.exercise_standard_answers #问卷的全部标准答案 - if standard_answer.present? - if @exercise_question.question_type <= Exercise::JUDGMENT #选择题/判断题,标准答案为一个或多个 - exercise_standard_choices = @exercise_answers_array.pluck(:exercise_choice_id) #问题以前的全部标准答案选项位置 - if exercise_standard_choices.sort != standard_answer.sort #表示答案有更改的 - standard_answer_change = true - common_standard_choices = standard_answer & exercise_standard_choices # 传入的标准答案的选项位置和以前的并集,即表示不用做更改的 - old_left_standard_choices = exercise_standard_choices - common_standard_choices # 以前的差集共同的,剩余的表示需要删掉 - new_left_standard_choices = standard_answer - common_standard_choices # 传入的标准答案差集共同的,剩余的表示需要新建 - if old_left_standard_choices.count > 0 - @exercise_answers_array.standard_by_ids(old_left_standard_choices).destroy_all - end - if new_left_standard_choices.count > 0 #新建标准答案 - new_left_standard_choices.each do |s| - standard_option = { - :exercise_question_id => @exercise_question.id, - :exercise_choice_id => s.to_i #即为选择的位置参数 - } - question_standard_answer = ExerciseStandardAnswer.new(standard_option) - question_standard_answer.save! - end - - end - if standard_answer.count > 1 && @exercise_question.question_type == Exercise::SINGLE #当标准答案数大于1,且不为多选时,修改为多选 - @exercise_question.update_attribute("question_type",Exercise::MULTIPLE) - elsif standard_answer.count == 1 && @exercise_question.question_type == Exercise::MULTIPLE - @exercise_question.update_attribute("question_type",Exercise::SINGLE) - end + elsif @exercise_question.question_type == Exercise::COMPLETION #填空题 + old_ex_answer = @exercise_question.exercise_standard_answers #当前问题的全部标准答案 + old_ex_answer_choice_texts = old_ex_answer.pluck(:answer_text).sort + new_ex_answer_choice_texts = standard_answer.pluck(:answer_text).sum.sort + if old_ex_answer_choice_texts != new_ex_answer_choice_texts #填空题标准答案有更改时,才会更新标准答案 + new_ex_answer_choice_ids = standard_answer.map {|a| a[:choice_id]}.uniq #新传入的答案数组序号 + old_ex_answer_choice_ids = old_ex_answer.pluck(:exercise_choice_id).uniq #全部的答案数组序号 + standard_answer_change = true + #删除多余的选项 + if old_ex_answer_choice_ids.count > new_ex_answer_choice_ids.count #有减少的填空 + delete_ex_answer_choice_ids = old_ex_answer_choice_ids - new_ex_answer_choice_ids + old_ex_answer.standard_by_ids(delete_ex_answer_choice_ids).destroy_all end - elsif @exercise_question.question_type == Exercise::COMPLETION #填空题 - old_ex_answer = @exercise_question.exercise_standard_answers #当前问题的全部标准答案 - old_ex_answer_choice_texts = old_ex_answer.pluck(:answer_text).sort - new_ex_answer_choice_texts = standard_answer.pluck(:answer_text).sum.sort - if old_ex_answer_choice_texts != new_ex_answer_choice_texts #填空题标准答案有更改时,才会更新标准答案 - new_ex_answer_choice_ids = standard_answer.map {|a| a[:choice_id]}.uniq #新传入的答案数组序号 - old_ex_answer_choice_ids = old_ex_answer.pluck(:exercise_choice_id).uniq #全部的答案数组序号 - standard_answer_change = true - #删除多余的选项 - if old_ex_answer_choice_ids.count > new_ex_answer_choice_ids.count #有减少的填空 - delete_ex_answer_choice_ids = old_ex_answer_choice_ids - new_ex_answer_choice_ids - old_ex_answer.standard_by_ids(delete_ex_answer_choice_ids).destroy_all - end - standard_answer.each do |aa| - null_choice_id = aa[:choice_id] - null_choice_text = aa[:answer_text] - null_choice_text_count = null_choice_text.count #当前传入的答案数量 - null_choice_text_count_array = (1..null_choice_text_count).to_a - - ex_answer_pre = old_ex_answer.standard_by_ids(null_choice_id) #当前问题的全部答案 - ex_answer_pre_count = ex_answer_pre.count - ex_answer_pre_count_array = (1..ex_answer_pre_count).to_a - - if old_ex_answer_choice_ids.include?(null_choice_id) #以前的填空题答案包含有现在的填空序号 - if null_choice_text_count >= ex_answer_pre_count - new_add_choice = null_choice_text_count_array - ex_answer_pre_count_array - ex_answer_pre_count_array.each do |n| - @hash_symbol_null_ = { + standard_answer.each do |aa| + null_choice_id = aa[:choice_id] + null_choice_text = aa[:answer_text] + null_choice_text_count = null_choice_text.count #当前传入的答案数量 + null_choice_text_count_array = (1..null_choice_text_count).to_a + + ex_answer_pre = old_ex_answer.standard_by_ids(null_choice_id) #当前问题的全部答案 + ex_answer_pre_count = ex_answer_pre.count + ex_answer_pre_count_array = (1..ex_answer_pre_count).to_a + + if old_ex_answer_choice_ids.include?(null_choice_id) #以前的填空题答案包含有现在的填空序号 + if null_choice_text_count >= ex_answer_pre_count + new_add_choice = null_choice_text_count_array - ex_answer_pre_count_array + ex_answer_pre_count_array.each do |n| + @hash_symbol_null_ = { + :exercise_question_id => @exercise_question.id, + :exercise_choice_id => null_choice_id, + :answer_text => null_choice_text[n - 1] + } + standard_option = @hash_symbol_null_ + ex_answer_pre[n - 1].update(standard_option) + end + if new_add_choice.count > 0 #表示有新增的 + new_add_choice.each do |i| + standard_option = { :exercise_question_id => @exercise_question.id, :exercise_choice_id => null_choice_id, - :answer_text => null_choice_text[n - 1] + :answer_text => null_choice_text[i - 1] } - standard_option = @hash_symbol_null_ - ex_answer_pre[n-1].update(standard_option) - end - if new_add_choice.count > 0 #表示有新增的 - new_add_choice.each do |i| - standard_option = { - :exercise_question_id => @exercise_question.id, - :exercise_choice_id => null_choice_id, - :answer_text => null_choice_text[i-1] - } - question_standard_answer = ExerciseStandardAnswer.new(standard_option) - question_standard_answer.save! - end - end - else - new_delete_choice = ex_answer_pre_count_array - null_choice_text_count_array - null_choice_text.each_with_index do |n,index| - standard_option = { - :exercise_question_id => @exercise_question.id, - :exercise_choice_id => null_choice_id, - :answer_text => n - } - ex_answer_pre[index].update(standard_option) - end - if new_delete_choice.count > 0 #表示填空题的答案有删减的 - new_delete_choice.each do |d| - ex_answer_pre[d-1].destroy - end + question_standard_answer = ExerciseStandardAnswer.new(standard_option) + question_standard_answer.save! end end else - null_choice_text.each do |n| + new_delete_choice = ex_answer_pre_count_array - null_choice_text_count_array + null_choice_text.each_with_index do |n, index| standard_option = { - :exercise_question_id => @exercise_question.id, - :exercise_choice_id => null_choice_id, - :answer_text => n + :exercise_question_id => @exercise_question.id, + :exercise_choice_id => null_choice_id, + :answer_text => n } - question_standard_answer = ExerciseStandardAnswer.new(standard_option) - question_standard_answer.save! + ex_answer_pre[index].update(standard_option) end + if new_delete_choice.count > 0 #表示填空题的答案有删减的 + new_delete_choice.each do |d| + ex_answer_pre[d - 1].destroy + end + end + end + else + null_choice_text.each do |n| + standard_option = { + :exercise_question_id => @exercise_question.id, + :exercise_choice_id => null_choice_id, + :answer_text => n + } + question_standard_answer = ExerciseStandardAnswer.new(standard_option) + question_standard_answer.save! end end end end end - if @exercise_question.question_type == Exercise::SUBJECTIVE #主观题 - main_standard_answer = standard_answer.present? ? standard_answer.first : nil - if @exercise_answers_array.present? - @exercise_answers_array.first.update_attribute("answer_text",main_standard_answer) - else - standard_option = { - :exercise_question_id => @exercise_question.id, - :answer_text => main_standard_answer, - } - question_standard_answer = ExerciseStandardAnswer.new(standard_option) - question_standard_answer.save! - end - elsif @exercise_question.question_type == Exercise::PRACTICAL - question_score = 0 - shixun_name = params[:shixun_name] || @exercise_question.shixun_name - @exercise_question.exercise_shixun_challenges.each_with_index do |challenge, index| - challenge.question_score = params[:question_scores][index].to_f.round(1) - challenge.save! - question_score += params[:question_scores][index].to_f.round(1) - end - @exercise_question.question_score = question_score - @exercise_question.shixun_name = shixun_name - @exercise_question.save! - end - - #当试卷已发布时(试卷的总状态),当标准答案修改时,如有已提交的学生,需重新计算分数. - - if standard_answer_change && @exercise.exercise_status >= Exercise::PUBLISHED - # ex_users_committed = @exercise.exercise_users.exercise_user_committed - # if ex_users_committed.size > 0 - # ex_users_committed.each do |ex_user| - # update_objective_score = update_single_score(@exercise_question,ex_user.user_id,standard_answer) - # if update_objective_score != 0 - # objective_score = ex_user.objective_score - # new_objective_score = objective_score + update_objective_score - # total_score = ex_user.score + update_objective_score - # total_score = total_score < 0.0 ? 0.0 : total_score - # ex_user.update_attributes(objective_score:new_objective_score,score:total_score) - # end - # end - # end - normal_status(3,"修改了标准答案\n是否重新计算学生答题的成绩?") + end + if @exercise_question.question_type == Exercise::SUBJECTIVE #主观题 + main_standard_answer = standard_answer.present? ? standard_answer.first : nil + if @exercise_answers_array.present? + @exercise_answers_array.first.update_attribute("answer_text", main_standard_answer) else - normal_status(0,"试卷更新成功!") + standard_option = { + :exercise_question_id => @exercise_question.id, + :answer_text => main_standard_answer, + } + question_standard_answer = ExerciseStandardAnswer.new(standard_option) + question_standard_answer.save! + end + elsif @exercise_question.question_type == Exercise::PRACTICAL + question_score = 0 + shixun_name = params[:shixun_name] || @exercise_question.shixun_name + @exercise_question.exercise_shixun_challenges.each_with_index do |challenge, index| + challenge.question_score = params[:question_scores][index].to_f.round(1) + challenge.save! + question_score += params[:question_scores][index].to_f.round(1) end + @exercise_question.question_score = question_score + @exercise_question.shixun_name = shixun_name + @exercise_question.save! + end - rescue Exception => e - uid_logger_error(e.message) - tip_exception("页面调用失败!") - raise ActiveRecord::Rollback + #当试卷已发布时(试卷的总状态),当标准答案修改时,如有已提交的学生,需重新计算分数. + + if standard_answer_change && @exercise.exercise_status >= Exercise::PUBLISHED + # ex_users_committed = @exercise.exercise_users.exercise_user_committed + # if ex_users_committed.size > 0 + # ex_users_committed.each do |ex_user| + # update_objective_score = update_single_score(@exercise_question,ex_user.user_id,standard_answer) + # if update_objective_score != 0 + # objective_score = ex_user.objective_score + # new_objective_score = objective_score + update_objective_score + # total_score = ex_user.score + update_objective_score + # total_score = total_score < 0.0 ? 0.0 : total_score + # ex_user.update_attributes(objective_score:new_objective_score,score:total_score) + # end + # end + # end + normal_status(3, "修改了标准答案\n是否重新计算学生答题的成绩?") + else + normal_status(0, "试卷更新成功!") end end end @@ -391,17 +372,17 @@ class ExerciseQuestionsController < ApplicationController ex_users_committed = @exercise.exercise_users.exercise_user_committed if ex_users_committed.size > 0 ex_users_committed.each do |ex_user| - update_objective_score = update_single_score(@exercise_question,ex_user.user_id,standard_answer) + update_objective_score = update_single_score(@exercise_question, ex_user.user_id, standard_answer) if update_objective_score != 0 objective_score = ex_user.objective_score new_objective_score = objective_score + update_objective_score total_score = ex_user.score + update_objective_score total_score = total_score < 0.0 ? 0.0 : total_score - ex_user.update!(objective_score:new_objective_score,score:total_score) + ex_user.update!(objective_score: new_objective_score, score: total_score) end end end - normal_status(0,"学生成绩更新成功") + normal_status(0, "学生成绩更新成功") rescue Exception => e uid_logger_error(e.message) tip_exception("答案删除失败!") @@ -414,11 +395,11 @@ class ExerciseQuestionsController < ApplicationController ActiveRecord::Base.transaction do begin opr = params[:opr] - current_q_p = @exercise_question.question_number.to_i #问题的当前位置 + current_q_p = @exercise_question.question_number.to_i #问题的当前位置 if @exercise.exercise_status.to_i == Exercise::UNPUBLISHED if opr.present? if opr.to_s == "up" - last_q_p = @exercise.exercise_questions.find_by(question_number: (current_q_p - 1)) # 当前问题的前一个问题 + last_q_p = @exercise.exercise_questions.find_by(question_number: (current_q_p - 1)) # 当前问题的前一个问题 if last_q_p.present? @exercise_question.update_attribute('question_number', (current_q_p - 1)) last_q_p.update_attribute('question_number', current_q_p) # 重新获取当前问题的位置 @@ -427,7 +408,7 @@ class ExerciseQuestionsController < ApplicationController normal_status(-1, "移动失败,已经是第一个问题了!") end elsif opr.to_s == "down" - next_q_p = @exercise.exercise_questions.find_by(question_number: (current_q_p + 1)) # 当前问题的前一个问题 + next_q_p = @exercise.exercise_questions.find_by(question_number: (current_q_p + 1)) # 当前问题的前一个问题 if next_q_p.present? @exercise_question.update_attribute('question_number', (current_q_p + 1)) next_q_p.update_attribute('question_number', current_q_p) @@ -453,10 +434,10 @@ class ExerciseQuestionsController < ApplicationController def delete_answer ActiveRecord::Base.transaction do begin - choice_d_id = params[:choice_no].to_i # 选项的当前位置 + choice_d_id = params[:choice_no].to_i # 选项的当前位置 question_choices = @exercise_question.exercise_choices delete_answer = question_choices.find_by(choice_position: choice_d_id) - left_choices = question_choices.where("choice_position > ? ",choice_d_id) + left_choices = question_choices.where("choice_position > ? ", choice_d_id) left_choices.update_all("choice_position = choice_position - 1") if left_choices if delete_answer.destroy normal_status(0, "答案删除成功!") @@ -474,7 +455,7 @@ class ExerciseQuestionsController < ApplicationController def destroy ActiveRecord::Base.transaction do begin - question_d_id = @exercise_question.question_number.to_i #问题的当前位置 + question_d_id = @exercise_question.question_number.to_i #问题的当前位置 exercise_questions = @exercise.exercise_questions left_questions = exercise_questions.where("question_number > ?", question_d_id) left_questions.update_all("question_number = question_number - 1") if left_questions @@ -493,185 +474,179 @@ class ExerciseQuestionsController < ApplicationController #老师调分窗口 def adjust_score ActiveRecord::Base.transaction do - begin - ex_all_scores = @exercise.exercise_questions.pluck(:question_score).sum - ex_obj_score = @exercise_current_user.objective_score #全部客观题得分 - ex_subj_score = @exercise_current_user.subjective_score < 0.0 ? 0.0 : @exercise_current_user.subjective_score #全部主观题得分 - ex_answers = @exercise_question.exercise_answers.search_answer_users("user_id",@user_id) #当前用户答案的得分 - if @exercise_question.question_type == Exercise::MULTIPLE - if ex_answers.present? #学生有回答时 取学生的答题得分,否则0分 - answer_choice_array = [] - ex_answers.each do |a| - if a.try(:exercise_choice).try(:choice_position).present? - answer_choice_array.push(a&.exercise_choice&.choice_position) #学生答案的位置 - end - end - user_answer_content = answer_choice_array.reject(&:blank?).sort - standard_answer = @exercise_question.exercise_standard_answers.pluck(:exercise_choice_id).sort - if standard_answer.size == 1 # 老数据需要判断学生答题是否正确, 正确取原题得分,否则是0分 - standard_answer = standard_answer.first.to_s.split("").map(&:to_i).sort - if user_answer_content == standard_answer - ex_answer_old = @exercise_question.question_score - else - ex_answer_old = 0 - end - else # 新多选题只需取第一条答题记录的得分 - ex_answer_old = ex_answers.first.score > 0 ? ex_answers.first.score : 0 + ex_all_scores = @exercise.exercise_questions.pluck(:question_score).sum + ex_obj_score = @exercise_current_user.objective_score #全部客观题得分 + ex_subj_score = @exercise_current_user.subjective_score < 0.0 ? 0.0 : @exercise_current_user.subjective_score #全部主观题得分 + ex_answers = @exercise_question.exercise_answers.search_answer_users("user_id", @user_id) #当前用户答案的得分 + if @exercise_question.question_type == Exercise::MULTIPLE + if ex_answers.present? #学生有回答时 取学生的答题得分,否则0分 + answer_choice_array = [] + ex_answers.each do |a| + if a.try(:exercise_choice).try(:choice_position).present? + answer_choice_array.push(a&.exercise_choice&.choice_position) #学生答案的位置 end - ex_answers.update_all(:score => @c_score) #所有的正确选项需重新更新 - else - answer_option = { - :user_id => @user_id, - :exercise_question_id => @exercise_question.id, - :score => @c_score, - :answer_text => "" - } - ExerciseAnswer.create!(answer_option) - ex_answer_old = 0 - end - if ex_obj_score <= 0.0 - new_obj_score = @c_score - else - new_obj_score = ex_obj_score - ex_answer_old + @c_score end - - total_scores = new_obj_score + ex_subj_score - if total_scores < 0.0 - total_scores = 0.0 - elsif total_scores > ex_all_scores - total_scores = ex_all_scores + user_answer_content = answer_choice_array.reject(&:blank?).sort + standard_answer = @exercise_question.exercise_standard_answers.pluck(:exercise_choice_id).sort + if standard_answer.size == 1 # 老数据需要判断学生答题是否正确, 正确取原题得分,否则是0分 + standard_answer = standard_answer.first.to_s.split("").map(&:to_i).sort + if user_answer_content == standard_answer + ex_answer_old = @exercise_question.question_score + else + ex_answer_old = 0 + end + else # 新多选题只需取第一条答题记录的得分 + ex_answer_old = ex_answers.first.score > 0 ? ex_answers.first.score : 0 end - ex_scores = { - :objective_score => new_obj_score, - :score => total_scores + ex_answers.update_all(:score => @c_score) #所有的正确选项需重新更新 + else + answer_option = { + :user_id => @user_id, + :exercise_question_id => @exercise_question.id, + :score => @c_score, + :answer_text => "" } - @exercise_current_user.update!(ex_scores) - - elsif @exercise_question.question_type == Exercise::COMPLETION #当为填空题,更新问题的总分, - - if ex_answers.exists? - ex_answer_old = ex_answers.score_reviewed.pluck(:score).sum #每一关的得分总和 - each_right_score = (@c_score / ex_answers.count.to_f) #调分后,平均每关的分数 - new_obj_score = ex_obj_score - ex_answer_old + @c_score - ex_answers.update_all(:score => each_right_score) #所有的正确选项需重新更新 - else #如果学生未答,则创建新的答题记录 - answer_option = { - :user_id => @user_id, - :exercise_question_id => @exercise_question.id, - :score => @c_score, - :answer_text => "" - } - ExerciseAnswer.create!(answer_option) - new_obj_score = ex_obj_score + @c_score - end + ExerciseAnswer.create!(answer_option) + ex_answer_old = 0 + end + if ex_obj_score <= 0.0 + new_obj_score = @c_score + else + new_obj_score = ex_obj_score - ex_answer_old + @c_score + end - total_scores = new_obj_score + ex_subj_score - if total_scores < 0.0 - total_scores = 0.0 - elsif total_scores > ex_all_scores - total_scores = ex_all_scores - end - ex_scores = { - :objective_score => new_obj_score, - :score => total_scores - } - @exercise_current_user.update!(ex_scores) - elsif @exercise_question.question_type == Exercise::SUBJECTIVE #当为主观题时 - if ex_answers.exists? - ex_answers_old_score = ex_answers.first.score > 0.0 ? ex_answers.first.score : 0.0 #原分数小于0,取0 - new_sub_score = ex_subj_score - ex_answers_old_score + @c_score #原全部主观题总分减去原该主观题得分再加调分后的分数,即为当前全部主观题得分 - ex_answers.first.update_attribute("score",@c_score) - else #如果学生未答,则创建新的答题记录 - answer_option = { - :user_id => @user_id, - :exercise_question_id => @exercise_question.id, - :score => @c_score, - :answer_text => "" - } - ExerciseAnswer.create!(answer_option) - new_sub_score = ex_subj_score + @c_score - end - total_scores = ex_obj_score + new_sub_score - if total_scores < 0.0 - total_scores = 0.0 - elsif total_scores > ex_all_scores - total_scores = ex_all_scores - end - ex_scores = { - :subjective_score => new_sub_score, - :score => total_scores + total_scores = new_obj_score + ex_subj_score + if total_scores < 0.0 + total_scores = 0.0 + elsif total_scores > ex_all_scores + total_scores = ex_all_scores + end + ex_scores = { + :objective_score => new_obj_score, + :score => total_scores + } + @exercise_current_user.update!(ex_scores) + + elsif @exercise_question.question_type == Exercise::COMPLETION #当为填空题,更新问题的总分, + + if ex_answers.exists? + ex_answer_old = ex_answers.score_reviewed.pluck(:score).sum #每一关的得分总和 + each_right_score = (@c_score / ex_answers.count.to_f) #调分后,平均每关的分数 + new_obj_score = ex_obj_score - ex_answer_old + @c_score + ex_answers.update_all(:score => each_right_score) #所有的正确选项需重新更新 + else #如果学生未答,则创建新的答题记录 + answer_option = { + :user_id => @user_id, + :exercise_question_id => @exercise_question.id, + :score => @c_score, + :answer_text => "" } - @exercise_current_user.update!(ex_scores) - - elsif @exercise_question.question_type == Exercise::PRACTICAL - ex_answers = @exercise_question.exercise_shixun_answers.where(user_id:@user_id,exercise_shixun_challenge_id:@shixun_a_id) + ExerciseAnswer.create!(answer_option) + new_obj_score = ex_obj_score + @c_score + end - if ex_answers.present? #当为实训题时 - ex_shixun_old_score = ex_answers.first.score > 0.0 ? ex_answers.first.score : 0.0 - new_obj_score = ex_obj_score - ex_shixun_old_score + @c_score - ex_answers.first.update_attribute("score",@c_score) - else - ex_shixun_option ={ - :exercise_question_id => @exercise_question.id, - :user_id => @user_id, - :exercise_shixun_challenge_id => @shixun_a_id, - :score => @c_score, - :status => 0 - } - ExerciseShixunAnswer.create!(ex_shixun_option) - new_obj_score = ex_obj_score + @c_score - end - total_scores = new_obj_score + ex_subj_score - if total_scores < 0.0 - total_scores = 0.0 - elsif total_scores > ex_all_scores - total_scores = ex_all_scores - end - ex_scores = { - :objective_score => new_obj_score, - :score => total_scores - } - @exercise_current_user.update!(ex_scores) + total_scores = new_obj_score + ex_subj_score + if total_scores < 0.0 + total_scores = 0.0 + elsif total_scores > ex_all_scores + total_scores = ex_all_scores end - comments = params[:comment] - question_comment = @exercise_question.exercise_answer_comments&.first - - if question_comment.present? - comment_option = { - :comment => comments, - :score => @c_score, - :exercise_answer_id => ex_answers.present? ? ex_answers.first.id : nil, - :user_id => current_user.id + ex_scores = { + :objective_score => new_obj_score, + :score => total_scores + } + @exercise_current_user.update!(ex_scores) + elsif @exercise_question.question_type == Exercise::SUBJECTIVE #当为主观题时 + if ex_answers.exists? + ex_answers_old_score = ex_answers.first.score > 0.0 ? ex_answers.first.score : 0.0 #原分数小于0,取0 + new_sub_score = ex_subj_score - ex_answers_old_score + @c_score #原全部主观题总分减去原该主观题得分再加调分后的分数,即为当前全部主观题得分 + ex_answers.first.update_attribute("score", @c_score) + else #如果学生未答,则创建新的答题记录 + answer_option = { + :user_id => @user_id, + :exercise_question_id => @exercise_question.id, + :score => @c_score, + :answer_text => "" } - question_comment.update!(comment_option) - @exercise_comments = question_comment + ExerciseAnswer.create!(answer_option) + new_sub_score = ex_subj_score + @c_score + end + total_scores = ex_obj_score + new_sub_score + if total_scores < 0.0 + total_scores = 0.0 + elsif total_scores > ex_all_scores + total_scores = ex_all_scores + end + ex_scores = { + :subjective_score => new_sub_score, + :score => total_scores + } + @exercise_current_user.update!(ex_scores) + + elsif @exercise_question.question_type == Exercise::PRACTICAL + ex_answers = @exercise_question.exercise_shixun_answers.where(user_id: @user_id, exercise_shixun_challenge_id: @shixun_a_id) + + if ex_answers.present? #当为实训题时 + ex_shixun_old_score = ex_answers.first.score > 0.0 ? ex_answers.first.score : 0.0 + new_obj_score = ex_obj_score - ex_shixun_old_score + @c_score + ex_answers.first.update_attribute("score", @c_score) else - ex_answer_comment_id = @exercise_question.exercise_answers.find_by(user_id: @user_id).try(:id) - comment_option = { - :user_id => current_user.id, - :comment => comments, - :score => @c_score, - :exercise_question_id => @exercise_question.id, - :exercise_shixun_answer_id => @shixun_a_id.present? ? @shixun_a_id : nil, - :exercise_answer_id => ex_answer_comment_id + ex_shixun_option = { + :exercise_question_id => @exercise_question.id, + :user_id => @user_id, + :exercise_shixun_challenge_id => @shixun_a_id, + :score => @c_score, + :status => 0 } - @exercise_comments = ExerciseAnswerComment.new(comment_option) - @exercise_comments.save! - - # 给被评阅人发送消息,同一个教师评阅无需重复发消息 + ExerciseShixunAnswer.create!(ex_shixun_option) + new_obj_score = ex_obj_score + @c_score + end + total_scores = new_obj_score + ex_subj_score + if total_scores < 0.0 + total_scores = 0.0 + elsif total_scores > ex_all_scores + total_scores = ex_all_scores + end + ex_scores = { + :objective_score => new_obj_score, + :score => total_scores + } + @exercise_current_user.update!(ex_scores) + end + comments = params[:comment] + question_comment = @exercise_question.exercise_answer_comments&.first + + if question_comment.present? + comment_option = { + :comment => comments, + :score => @c_score, + :exercise_answer_id => ex_answers.present? ? ex_answers.first.id : nil, + :user_id => current_user.id + } + question_comment.update!(comment_option) + @exercise_comments = question_comment + else + ex_answer_comment_id = @exercise_question.exercise_answers.find_by(user_id: @user_id).try(:id) + comment_option = { + :user_id => current_user.id, + :comment => comments, + :score => @c_score, + :exercise_question_id => @exercise_question.id, + :exercise_shixun_answer_id => @shixun_a_id.present? ? @shixun_a_id : nil, + :exercise_answer_id => ex_answer_comment_id + } + @exercise_comments = ExerciseAnswerComment.new(comment_option) + @exercise_comments.save! - unless Tiding.where(user_id: @user_id, trigger_user_id: current_user.id, parent_container_id: @exercise.id, parent_container_type: "ExerciseScore").exists? - Tiding.create!(user_id: @user_id, trigger_user_id: current_user.id, container_id: @exercise.id, - container_type: "Exercise", parent_container_id: @exercise.id, - parent_container_type: "ExerciseScore", belong_container_id: @course.id, - belong_container_type: 'Course', tiding_type: "Exercise") - end + # 给被评阅人发送消息,同一个教师评阅无需重复发消息 + unless Tiding.where(user_id: @user_id, trigger_user_id: current_user.id, parent_container_id: @exercise.id, parent_container_type: "ExerciseScore").exists? + Tiding.create!(user_id: @user_id, trigger_user_id: current_user.id, container_id: @exercise.id, + container_type: "Exercise", parent_container_id: @exercise.id, + parent_container_type: "ExerciseScore", belong_container_id: @course.id, + belong_container_type: 'Course', tiding_type: "Exercise") end - rescue Exception => e - uid_logger_error(e.message) - tip_exception(e.message) - raise ActiveRecord::Rollback + end end end @@ -679,19 +654,19 @@ class ExerciseQuestionsController < ApplicationController private def questions_params - params.require(:exercise_question).permit(:question_title,:question_type, - :question_number,:exercise_id, - :question_score,:shixun_id,:is_ordered) + params.require(:exercise_question).permit(:question_title, :question_type, + :question_number, :exercise_id, + :question_score, :shixun_id, :is_ordered) end def get_exercise - @exercise = Exercise.find_by(id:params[:exercise_id]) + @exercise = Exercise.find_by(id: params[:exercise_id]) if @exercise.blank? - tip_exception(404,"试卷不存在") + tip_exception(404, "试卷不存在") else @course = @exercise.course if @course.blank? - tip_exception(404,"课堂不存在") + tip_exception(404, "课堂不存在") end end end @@ -708,78 +683,78 @@ class ExerciseQuestionsController < ApplicationController if @exercise_question.present? @exercise = @exercise_question.exercise if @exercise.blank? - tip_exception(404,"试卷不存在") + tip_exception(404, "试卷不存在") else @course = @exercise.course if @course.blank? - tip_exception(404,"课堂不存在") + tip_exception(404, "课堂不存在") end end else - tip_exception(404,"试卷问题不存在") + tip_exception(404, "试卷问题不存在") end end def validate_params - normal_status(-1,"题目不允许为空!") if (params[:question_title].blank? && params[:question_type].to_i != Exercise::PRACTICAL ) #除了实训题,其余题目必需有题干 - normal_status(-1,"问题类型不允许为空!" ) if params[:question_type].blank? - normal_status(-1,"分值不允许为空!" ) if params[:question_score].blank? && params[:question_scores].blank? #分值的数组或参数必需存在一个 - if params[:question_score].present? && params[:question_score].to_f <= 0.0 #问题类型存在,则分值不能为空,且必需大于0 - normal_status(-1,"分值必需大于0!") - elsif (params[:question_score].present? && params[:question_score].to_f.round(1) > 100.0) || (params[:question_scores].present? && (params[:question_scores].map{|a| a.to_f.round(1)}.max > 100.0)) - normal_status(-1,"分值不能超过100分!") + normal_status(-1, "题目不允许为空!") if (params[:question_title].blank? && params[:question_type].to_i != Exercise::PRACTICAL) #除了实训题,其余题目必需有题干 + normal_status(-1, "问题类型不允许为空!") if params[:question_type].blank? + normal_status(-1, "分值不允许为空!") if params[:question_score].blank? && params[:question_scores].blank? #分值的数组或参数必需存在一个 + if params[:question_score].present? && params[:question_score].to_f <= 0.0 #问题类型存在,则分值不能为空,且必需大于0 + normal_status(-1, "分值必需大于0!") + elsif (params[:question_score].present? && params[:question_score].to_f.round(1) > 100.0) || (params[:question_scores].present? && (params[:question_scores].map {|a| a.to_f.round(1)}.max > 100.0)) + normal_status(-1, "分值不能超过100分!") elsif params[:question_scores].present? && params[:question_scores].include?(0.0) #如果有负数,则自动取绝对值,#多个分数值,针对实训题的 - normal_status(-1,"分值必需大于0!") + normal_status(-1, "分值必需大于0!") elsif params[:standard_answers].present? && params[:question_choices].present? && (params[:standard_answers].count > params[:question_choices].count) - normal_status(-1,"标准答案数不能大于选项数!") - elsif [0,1,2,3].include?(params[:question_type].to_i) && (params[:standard_answers].blank? || params[:standard_answers].include?("")) #选择题/判断题/填空题 问题选项/标准答案不能为空,也不能包含空的内容 - normal_status(-1,"标准答案不能为空!") + normal_status(-1, "标准答案数不能大于选项数!") + elsif [0, 1, 2, 3].include?(params[:question_type].to_i) && (params[:standard_answers].blank? || params[:standard_answers].include?("")) #选择题/判断题/填空题 问题选项/标准答案不能为空,也不能包含空的内容 + normal_status(-1, "标准答案不能为空!") elsif params[:question_type].to_i == 2 && (params[:standard_answers].count > 1 || params[:question_choices].blank? || params[:question_choices].include?("")) #判断题的标准答案不能大于1个,选项不能为空 - normal_status(-1,"判断题选项不能为空/标准答案不能大于1个!") + normal_status(-1, "判断题选项不能为空/标准答案不能大于1个!") elsif params[:question_type].to_i <= 1 && (params[:question_choices].blank? || params[:question_choices].include?("") || params[:question_choices].count < 2) #选择题选项不能为空,且不能小于2 - normal_status(-1,"选择题选项内容不能为空,且不能少于2个!") - elsif params[:question_type].to_i == 3 && (params[:standard_answers].blank? || params[:standard_answers].count > 5 ) #填空题选项最多为5个,且如果为1个的话,不允许修改is_ordered - normal_status(-1,"填空题标准答案不能为空/不能超过5个!") + normal_status(-1, "选择题选项内容不能为空,且不能少于2个!") + elsif params[:question_type].to_i == 3 && (params[:standard_answers].blank? || params[:standard_answers].count > 5) #填空题选项最多为5个,且如果为1个的话,不允许修改is_ordered + normal_status(-1, "填空题标准答案不能为空/不能超过5个!") elsif params[:question_type].to_i == 4 && params[:standard_answers].count > 2 #简单题参考答案最多为1个 - normal_status(-1,"简答题的参考答案不能大于2个!") + normal_status(-1, "简答题的参考答案不能大于2个!") elsif params[:question_type].to_i == 5 - if params[:shixun_id].blank? #实训题的id不能为空 - normal_status(-1,"实训题id不能为空!") + if params[:shixun_id].blank? #实训题的id不能为空 + normal_status(-1, "实训题id不能为空!") elsif params[:shixun_name].blank? - normal_status(-1,"实训题名称不能为空!") + normal_status(-1, "实训题名称不能为空!") end end end def check_exercise_status - normal_status(-1,"不能更改试卷问题!") if @exercise.exercise_status != Exercise::UNPUBLISHED + normal_status(-1, "不能更改试卷问题!") if @exercise.exercise_status != Exercise::UNPUBLISHED end #更新时不能修改的内容 def cannot_change_column #已发布的/已截止的/评阅中的状态时,不能修改分数,不能增删问题和答案,不能修改标准答案,可以修改选项内容/题目内容,这里仅指单个问题 if @exercise.exercise_status != Exercise::UNPUBLISHED - question_score = @exercise_question.question_score #原来的分数 - update_question_score = params[:question_score].to_f.round(1) #传入的分数 - choices_count = @exercise_question.exercise_choices.size #原来的选项个数 + question_score = @exercise_question.question_score #原来的分数 + update_question_score = params[:question_score].to_f.round(1) #传入的分数 + choices_count = @exercise_question.exercise_choices.size #原来的选项个数 exercise_choice_ids = @exercise_question.exercise_standard_answers.pluck(:exercise_choice_id).uniq standard_answers_text = @exercise_question.exercise_standard_answers.pluck(:answer_text).uniq update_choices_count = params[:question_choices].present? ? params[:question_choices].count : choices_count #传入的选项个数 - standard_answer = params[:standard_answers] #传参数是怎么传的?能不能传空值? + standard_answer = params[:standard_answers] #传参数是怎么传的?能不能传空值? if update_question_score.present? && question_score != update_question_score #分数有更改 - normal_status(-1,"已发布/已截止,分数不允许修改!") + normal_status(-1, "已发布/已截止,分数不允许修改!") elsif update_choices_count != choices_count #选项个数有修改 - normal_status(-1,"已发布/已截止,不允许增删答案!") + normal_status(-1, "已发布/已截止,不允许增删答案!") elsif standard_answer.present? if @exercise_question.question_type == Exercise::COMPLETION # exercise_answers_text = standard_answer.map{|a| a[:answer_text]}.sum.uniq # unless (standard_answer.count == exercise_choice_ids.count) && (standard_answers_text.count == exercise_answers_text.count) unless standard_answer.count == exercise_choice_ids.count - normal_status(-1,"已发布/已截止,不允许增删标准答案!") + normal_status(-1, "已发布/已截止,不允许增删标准答案!") end elsif @exercise_question.question_type == Exercise::SUBJECTIVE unless standard_answers_text.count == standard_answer.count - normal_status(-1,"已发布/已截止,不允许增删标准答案!") + normal_status(-1, "已发布/已截止,不允许增删标准答案!") end end end @@ -787,17 +762,17 @@ class ExerciseQuestionsController < ApplicationController end def check_adjust_score - @c_score = params[:score].to_f #调分后的分数 + @c_score = params[:score].to_f #调分后的分数 @user_id = params[:user_id] @exercise_current_user = @exercise.exercise_users.exercise_commit_users(@user_id).first #当前试卷用户的答案内容 if @exercise_current_user.blank? - normal_status(-1,"用户不存在!") + normal_status(-1, "用户不存在!") elsif @c_score.blank? - normal_status(-1,"分数不能为空!") + normal_status(-1, "分数不能为空!") elsif @exercise_question.question_type == Exercise::SINGLE || @exercise_question.question_type == Exercise::JUDGMENT - normal_status(-1,"单选题/判断题不能调分!") + normal_status(-1, "单选题/判断题不能调分!") elsif params[:comment].present? && params[:comment].length > 100 - normal_status(-1,"评语不能超过100个字符!") + normal_status(-1, "评语不能超过100个字符!") else @shixun_a_id = params[:shixun_challenge_id] if @exercise_question.question_type == Exercise::PRACTICAL #当为实训题时,为关卡的分数 @@ -806,13 +781,13 @@ class ExerciseQuestionsController < ApplicationController @old_ques_score = @shixun_challenge.first.question_score else @old_ques_score = nil - normal_status(-1,"实训答案id不能为空!") + normal_status(-1, "实训答案id不能为空!") end else @old_ques_score = @exercise_question.question_score #当不为实训题时,为各问题的分数 end if @old_ques_score.present? && (@c_score > @old_ques_score) - normal_status(-1,"分数不能大于题目分数!") + normal_status(-1, "分数不能大于题目分数!") end end end diff --git a/app/controllers/exercises_controller.rb b/app/controllers/exercises_controller.rb index 193ef857b..e3f7cbd97 100644 --- a/app/controllers/exercises_controller.rb +++ b/app/controllers/exercises_controller.rb @@ -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 diff --git a/app/controllers/poll_questions_controller.rb b/app/controllers/poll_questions_controller.rb index c60eaff38..e8ce0ac5a 100644 --- a/app/controllers/poll_questions_controller.rb +++ b/app/controllers/poll_questions_controller.rb @@ -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 diff --git a/app/controllers/poll_votes_controller.rb b/app/controllers/poll_votes_controller.rb index 42dbbe3df..ad7fef502 100644 --- a/app/controllers/poll_votes_controller.rb +++ b/app/controllers/poll_votes_controller.rb @@ -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 diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb index d320379be..21a919efd 100644 --- a/app/controllers/polls_controller.rb +++ b/app/controllers/polls_controller.rb @@ -1,25 +1,25 @@ class PollsController < ApplicationController # before_action :check_poll_status 问卷的发消息和定时任务没有做 - before_action :require_login, :check_auth,except: [:index] - before_action :find_course, except: [:show,:poll_setting,:commit_setting,:edit,:update,:start_answer,:commit_poll, - :commit_result,:poll_lists,:cancel_publish,:cancel_publish_modal,:common_header,:publish_groups] - before_action :get_poll_and_course, only: [:show,:poll_setting,:commit_setting,:edit,:update,:start_answer, - :commit_poll,:commit_result,:poll_lists,:cancel_publish, - :cancel_publish_modal,:common_header, :publish_groups] + before_action :require_login, :check_auth, except: [:index] + before_action :find_course, except: [:show, :poll_setting, :commit_setting, :edit, :update, :start_answer, :commit_poll, + :commit_result, :poll_lists, :cancel_publish, :cancel_publish_modal, :common_header, :publish_groups] + before_action :get_poll_and_course, only: [:show, :poll_setting, :commit_setting, :edit, :update, :start_answer, + :commit_poll, :commit_result, :poll_lists, :cancel_publish, + :cancel_publish_modal, :common_header, :publish_groups] before_action :user_course_identity - before_action :is_course_teacher, except: [:index,:start_answer,:poll_setting,:commit_poll,:commit_result,:poll_lists,:common_header] #判断是否为课堂老师 + before_action :is_course_teacher, except: [:index, :start_answer, :poll_setting, :commit_poll, :commit_result, :poll_lists, :common_header] #判断是否为课堂老师 before_action :check_user_status before_action :is_course_public, only: [:set_public] - before_action :check_user_on_answer, only: [:show,:start_answer,:commit_poll,:poll_lists] #判断当前用户在问卷的权限/老师是否属于分班的权限 - before_action :validate_params, only: [:create,:update] - before_action :validates_multi_ids, only: [:publish,:end_poll,:destroys,:set_public,:join_poll_banks] - before_action :check_poll_setting_status,only: [:commit_setting] - before_action :get_questions_count ,only: [:start_answer,:show,:commit_result,:edit] - before_action :check_user_id_start_answer,only: [:start_answer] - before_action :check_poll_question_complete,only: [:commit_poll] #问卷提交前来判断问题是否完成 - before_action :check_poll_commit_result,only: [:commit_result] + before_action :check_user_on_answer, only: [:show, :start_answer, :commit_poll, :poll_lists] #判断当前用户在问卷的权限/老师是否属于分班的权限 + before_action :validate_params, only: [:create, :update] + before_action :validates_multi_ids, only: [:publish, :end_poll, :destroys, :set_public, :join_poll_banks] + before_action :check_poll_setting_status, only: [:commit_setting] + before_action :get_questions_count, only: [:start_answer, :show, :commit_result, :edit] + before_action :check_user_id_start_answer, only: [:start_answer] + before_action :check_poll_question_complete, only: [:commit_poll] #问卷提交前来判断问题是否完成 + before_action :check_poll_commit_result, only: [:commit_result] # before_action :get_all_polls_commit, only: [:commit_result] #该问卷全部的用户 - before_action :get_left_banner_id, only:[:common_header,:start_answer,:new,:edit,:index] + before_action :get_left_banner_id, only: [:common_header, :start_answer, :new, :edit, :index] include PollsHelper def index @@ -29,27 +29,27 @@ class PollsController < ApplicationController @polls_all = @course.polls member_show_polls = @polls_all.publish_or_not # 已发布的或已截止的问卷 @current_user_ = current_user - @course_status = @course.is_end ? 0 : 1 # 课堂是否结束 + @course_status = @course.is_end ? 0 : 1 # 课堂是否结束 @course_is_public = @course.is_public # 课堂的学生人数 @course_all_members = @course.students #当前课堂的全部学生 - @current_student = @course_all_members.find_by(user_id: current_user.id) #当前用户是否为课堂的学生 + @current_student = @course_all_members.find_by(user_id: current_user.id) #当前用户是否为课堂的学生 # polls的不同用户群体的显示 - if @user_course_identity < Course::STUDENT # @is_teacher_or 1为老师/管理员/助教 + if @user_course_identity < Course::STUDENT # @is_teacher_or 1为老师/管理员/助教 @is_teacher_or = 1 - @polls = @polls_all #老师能看到全部的问卷,不管是已发布的/未发布的/已截止的/统一设置的/私有设置的(看到内容不同) - elsif @user_course_identity == Course::STUDENT # 2为课堂成员,能看到统一设置的和自己班级的 + @polls = @polls_all #老师能看到全部的问卷,不管是已发布的/未发布的/已截止的/统一设置的/私有设置的(看到内容不同) + elsif @user_course_identity == Course::STUDENT # 2为课堂成员,能看到统一设置的和自己班级的 @is_teacher_or = 2 - @member_group_id = @current_student.try(:course_group_id).to_i # 成员的分班id,默认为0 - if @member_group_id == 0 #表示是课堂的未分班成员,只能查看统一设置的试卷(已发布的/已截止的) + @member_group_id = @current_student.try(:course_group_id).to_i # 成员的分班id,默认为0 + if @member_group_id == 0 #表示是课堂的未分班成员,只能查看统一设置的试卷(已发布的/已截止的) @polls = member_show_polls.size > 0 ? member_show_polls.public_or_unset : [] - else #已分班级的成员,可以查看统一设置和单独设置(试卷是发布在该班级)试卷 + else #已分班级的成员,可以查看统一设置和单独设置(试卷是发布在该班级)试卷 # 已发布 当前用户班级分组的 试卷id publish_poll_ids = @course.poll_group_settings.poll_group_published.where("course_group_id = #{@member_group_id}").pluck(:poll_id) @polls = member_show_polls.unified_setting.or(member_show_polls.where(id: publish_poll_ids)) end - else #用户未登陆或不是该课堂成员,仅显示统一设置的(已发布的/已截止的),如有公开,则不显示锁,不公开,则显示锁 + else #用户未登陆或不是该课堂成员,仅显示统一设置的(已发布的/已截止的),如有公开,则不显示锁,不公开,则显示锁 @is_teacher_or = 0 @polls = member_show_polls.public_or_unset end @@ -66,7 +66,7 @@ class PollsController < ApplicationController poll_setting_ids = @course.poll_group_settings.where("course_group_id = #{@member_group_id}").poll_group_not_published.pluck(:poll_id) when 2 poll_setting_ids = @course.poll_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(:poll_id) + .where("publish_time is not null and publish_time <= ? and end_time > ?", Time.now, Time.now).pluck(:poll_id) when 3 poll_setting_ids = @course.poll_group_settings.where("course_group_id = #{@member_group_id}").poll_group_ended.pluck(:poll_id) end @@ -83,8 +83,8 @@ class PollsController < ApplicationController # 分页 @polls_select_count = @polls.size - @polls = @polls.order( "IF(ISNULL(publish_time),0,1), publish_time DESC,created_at DESC") - @page = params[:page] || 1 + @polls = @polls.order("IF(ISNULL(publish_time),0,1), publish_time DESC,created_at DESC") + @page = params[:page] || 1 @limit = params[:limit] || 15 @polls = @polls.page(@page).per(@limit) @@ -93,10 +93,10 @@ class PollsController < ApplicationController @polls = [] end - @polls_count = @polls_all.size # 全部页面,需返回 + @polls_count = @polls_all.size # 全部页面,需返回 @polls_unpublish_counts = @polls_all.poll_by_status(1).size #未发布的问卷数 - @polls_published_counts = @polls_count - @polls_unpublish_counts # 已发布的问卷数 - @course_all_members_count = @course_all_members.size #当前课堂的学生数 + @polls_published_counts = @polls_count - @polls_unpublish_counts # 已发布的问卷数 + @course_all_members_count = @course_all_members.size #当前课堂的学生数 rescue Exception => e uid_logger_error(e.message) @@ -126,23 +126,17 @@ class PollsController < ApplicationController # un_anonymous 是否实名,默认为false,即不公开 def create ActiveRecord::Base.transaction do - begin - poll_name = params[:polls_name] - poll_desc = params[:polls_description] - poll_options = { - :polls_name => poll_name, - :polls_description => poll_desc, - :user_id => current_user.id, - :course_id => @course.id, - :polls_status => 1, - :polls_type => "Course", - } - @poll = Poll.create!(poll_options) - rescue Exception => e - uid_logger_error(e.message) - tip_exception("问卷创建失败!") - raise ActiveRecord::Rollback - end + poll_name = params[:polls_name] + poll_desc = params[:polls_description] + poll_options = { + :polls_name => poll_name, + :polls_description => poll_desc, + :user_id => current_user.id, + :course_id => @course.id, + :polls_status => 1, + :polls_type => "Course", + } + @poll = Poll.create!(poll_options) end end @@ -160,20 +154,14 @@ class PollsController < ApplicationController def update ActiveRecord::Base.transaction do - begin - poll_name = params[:polls_name] - poll_des = params[:polls_description] - poll_params = { - :polls_name => poll_name, - :polls_description => poll_des - } - @poll.update!(poll_params) - normal_status(0,"问卷更新成功!") - rescue Exception => e - uid_logger_error(e.message) - tip_exception("没有权限") - raise ActiveRecord::Rollback - end + poll_name = params[:polls_name] + poll_des = params[:polls_description] + poll_params = { + :polls_name => poll_name, + :polls_description => poll_des + } + @poll.update!(poll_params) + normal_status(0, "问卷更新成功!") end end @@ -204,17 +192,17 @@ class PollsController < ApplicationController @user_poll_answer = @poll.check_user_votes_status(current_user, @poll_status) else @is_teacher_or = 1 - @user_poll_answer = 3 #教师页面 + @user_poll_answer = 3 #教师页面 end poll_id_array = [@poll.id] - @poll_publish_count = get_user_permission_course(poll_id_array,2).count #是否存在已发布的 - @poll_unpublish_count = get_user_permission_course(poll_id_array,1).count #是否存在未发布的 + @poll_publish_count = get_user_permission_course(poll_id_array, 2).count #是否存在已发布的 + @poll_unpublish_count = get_user_permission_course(poll_id_array, 1).count #是否存在未发布的 - if (@poll_publish_count == 0) && (@poll_unpublish_count == 0) #即表示没有分班 + if (@poll_publish_count == 0) && (@poll_unpublish_count == 0) #即表示没有分班 if @poll_status == 1 - @poll_unpublish_count = 1 #试卷未发布,且课堂没有分班的时候 + @poll_unpublish_count = 1 #试卷未发布,且课堂没有分班的时候 elsif @poll_status == 2 - @poll_publish_count = 1 #试卷未发布,且课堂没有分班的时候 + @poll_publish_count = 1 #试卷未发布,且课堂没有分班的时候 end end rescue Exception => e @@ -231,7 +219,7 @@ class PollsController < ApplicationController begin poll_ids = params[:check_ids] if poll_ids.count > 0 - @course_groups = get_user_permission_course(poll_ids,1) + @course_groups = get_user_permission_course(poll_ids, 1) else @course_groups = [] end @@ -258,84 +246,82 @@ class PollsController < ApplicationController if params[:detail].blank? tip_exception("缺少截止时间参数") if params[:end_time].blank? tip_exception("截止时间不能早于当前时间") if params[:end_time] <= strf_time(Time.now) - tip_exception("截止时间不能晚于课堂结束时间(#{@course.end_date.end_of_day.strftime("%Y-%m-%d %H:%M")})") if - @course.end_date.present? && params[:end_time] > strf_time(@course.end_date.end_of_day) + tip_exception("截止时间不能晚于课堂结束时间(#{@course.end_date.end_of_day.strftime("%Y-%m-%d %H:%M")})") if @course.end_date.present? && params[:end_time] > strf_time(@course.end_date.end_of_day) else - group_end_times = params[:group_end_times].reject(&:blank?).map{|time| time.to_time} + group_end_times = params[:group_end_times].reject(&:blank?).map {|time| time.to_time} tip_exception("缺少截止时间参数") if group_end_times.blank? tip_exception("截止时间和分班参数的个数不一致") if group_end_times.length != group_ids.length group_end_times.each do |time| tip_exception("分班截止时间不能早于当前时间") if time <= Time.now - tip_exception("分班截止时间不能晚于课堂结束时间(#{@course.end_date.end_of_day.strftime("%Y-%m-%d %H:%M")})") if - @course.end_date.present? && time > @course.end_date.end_of_day + tip_exception("分班截止时间不能晚于课堂结束时间(#{@course.end_date.end_of_day.strftime("%Y-%m-%d %H:%M")})") if @course.end_date.present? && time > @course.end_date.end_of_day end end ActiveRecord::Base.transaction do begin check_ids = Poll.where(id: params[:check_ids]) - ex_end_time = params[:end_time].blank? ? Time.at(((1.month.since.to_i)/3600.0).ceil * 3600) : params[:end_time].to_time + ex_end_time = params[:end_time].blank? ? Time.at(((1.month.since.to_i) / 3600.0).ceil * 3600) : params[:end_time].to_time check_ids.each do |poll| if poll.unified_setting - pl_status = poll.polls_status #则为试卷的状态 + pl_status = poll.polls_status #则为试卷的状态 else pl_status = @course.course_groups.where(id: group_ids).size != - poll.poll_group_settings.where(course_group_id: group_ids).poll_group_published.size ? 1 : 0 #立即发布针对分组设置的全部未发布的班级才生效 + poll.poll_group_settings.where(course_group_id: group_ids).poll_group_published.size ? 1 : 0 #立即发布针对分组设置的全部未发布的班级才生效 end - if pl_status == 1 #如果问卷存在已发布的,或者是已截止的,那么则直接跳过 - g_course = group_ids #表示是否传入分班参数,如果传入分班的参数,那么poll的统一设置需修改 + if pl_status == 1 #如果问卷存在已发布的,或者是已截止的,那么则直接跳过 + g_course = group_ids #表示是否传入分班参数,如果传入分班的参数,那么poll的统一设置需修改 if g_course user_course_groups = @course.course_groups.pluck(:id) - if g_course.map(&:to_i).sort == user_course_groups.sort && - ((params[:detail] && group_end_times.min == group_end_times.max) || params[:detail].blank?) # 如果是设置为全部班级,则问卷不用分组,且问卷设定为统一设置,否则则分组设置 - poll.poll_group_settings.destroy_all - poll_unified = true - e_time = params[:detail] ? group_end_times.max : ex_end_time - else - poll_unified = false - g_course.each_with_index do |i, index| - poll_group_setting = poll.poll_group_settings.find_in_poll_group("course_group_id",i).first #根据课堂分班的id,寻找问卷所在的班级 - group_end_time = params[:detail] ? group_end_times[index] : ex_end_time - if poll_group_setting.present? #如果该问卷分组存在,则更新,否则新建 - poll_group_setting.update!(publish_time: Time.now, end_time: group_end_time) - else - p_course_group = { - :poll_id => poll.id, - :course_group_id => i, - :course_id => poll.course.id, - :publish_time => Time.now, - :end_time => group_end_time, - } - new_poll_group = poll.poll_group_settings.new p_course_group - new_poll_group.save! - end - end - e_time = poll.poll_group_settings.end_time_present.map(&:end_time).max - # group_ids = params[:group_ids] - end + if g_course.map(&:to_i).sort == user_course_groups.sort && + ((params[:detail] && group_end_times.min == group_end_times.max) || params[:detail].blank?) # 如果是设置为全部班级,则问卷不用分组,且问卷设定为统一设置,否则则分组设置 + poll.poll_group_settings.destroy_all + poll_unified = true + e_time = params[:detail] ? group_end_times.max : ex_end_time + else + poll_unified = false + g_course.each_with_index do |i, index| + poll_group_setting = poll.poll_group_settings.find_in_poll_group("course_group_id", i).first #根据课堂分班的id,寻找问卷所在的班级 + group_end_time = params[:detail] ? group_end_times[index] : ex_end_time + if poll_group_setting.present? #如果该问卷分组存在,则更新,否则新建 + poll_group_setting.update!(publish_time: Time.now, end_time: group_end_time) + else + p_course_group = { + :poll_id => poll.id, + :course_group_id => i, + :course_id => poll.course.id, + :publish_time => Time.now, + :end_time => group_end_time, + } + new_poll_group = poll.poll_group_settings.new p_course_group + new_poll_group.save! + end + end + e_time = poll.poll_group_settings.end_time_present.map(&:end_time).max + # group_ids = params[:group_ids] + end else poll.poll_group_settings.destroy_all poll_unified = true e_time = ex_end_time end - poll_status = set_poll_status(Time.now,e_time) + poll_status = set_poll_status(Time.now, e_time) poll_params = { - :publish_time => Time.now, - :end_time => e_time, - :polls_status => poll_status, - :unified_setting => poll_unified + :publish_time => Time.now, + :end_time => e_time, + :polls_status => poll_status, + :unified_setting => poll_unified } poll.update!(poll_params) if poll.course_acts.size == 0 - poll.course_acts << CourseActivity.new(:user_id => poll.user_id,:course_id => poll.course_id) + poll.course_acts << CourseActivity.new(:user_id => poll.user_id, :course_id => poll.course_id) end PollPublishNotifyJob.perform_later(poll.id, g_course) end end normal_status(0, "问卷发布成功!") - ## 需添加发送消息的接口,稍后添加 + ## 需添加发送消息的接口,稍后添加 rescue Exception => e uid_logger_error(e.message) tip_exception("问卷发布失败") @@ -350,7 +336,7 @@ class PollsController < ApplicationController begin poll_ids = params[:check_ids] if poll_ids.count > 0 - @course_groups = get_user_permission_course(poll_ids,3) + @course_groups = get_user_permission_course(poll_ids, 3) else @course_groups = [] end @@ -369,8 +355,8 @@ class PollsController < ApplicationController check_ids = Poll.where(id: params[:check_ids]) check_ids.each do |poll| poll_status = poll.get_poll_status(current_user) - if poll_status == 2 #跳过已截止的或未发布的 - g_course = params[:group_ids] #表示是否传入分班参数,如果传入分班的参数,那么poll的统一设置需修改,poll_group_settings + if poll_status == 2 #跳过已截止的或未发布的 + g_course = params[:group_ids] #表示是否传入分班参数,如果传入分班的参数,那么poll的统一设置需修改,poll_group_settings if g_course # 如果问卷之前是统一设置且分班参数与课堂的分班数不一致,则变为非统一设置,同时创建poll_group_settings if poll.unified_setting && g_course.length != @course.course_groups_count @@ -380,17 +366,17 @@ class PollsController < ApplicationController publish_time: poll.publish_time, end_time: poll.end_time) end else - poll_unified = poll.unified_setting #这个作为初始化的值是否要好点呢?-hs + poll_unified = poll.unified_setting #这个作为初始化的值是否要好点呢?-hs end poll_users = poll_unified ? poll.poll_users : - poll.poll_users.joins("join course_members on poll_users.user_id = course_members.user_id").where(course_members: {course_group_id: g_course, role: 4}) + poll.poll_users.joins("join course_members on poll_users.user_id = course_members.user_id").where(course_members: {course_group_id: g_course, role: 4}) poll_group_setting = poll.poll_group_settings - old_poll_groups = poll_group_setting.find_in_poll_group("course_group_id",g_course) #问卷分组的截止时间更改 + old_poll_groups = poll_group_setting.find_in_poll_group("course_group_id", g_course) #问卷分组的截止时间更改 old_poll_groups.update_all(:end_time => Time.now) - new_end_time = poll_group_setting.end_time_present.map(&:end_time) # 问卷结束时间不为空的 + new_end_time = poll_group_setting.end_time_present.map(&:end_time) # 问卷结束时间不为空的 new_end_time_s = new_end_time.size > 0 ? new_end_time.max : Time.now - new_poll_status = set_poll_status(poll.publish_time,new_end_time_s) - poll.update!(:end_time => new_end_time_s,:polls_status => new_poll_status,:unified_setting => poll_unified) + new_poll_status = set_poll_status(poll.publish_time, new_end_time_s) + poll.update!(:end_time => new_end_time_s, :polls_status => new_poll_status, :unified_setting => poll_unified) elsif poll.unified_setting poll_users = poll.poll_users poll.update!(:polls_status => 3, :end_time => Time.now) @@ -414,7 +400,7 @@ class PollsController < ApplicationController begin poll_ids = [@poll.id] if poll_ids.count > 0 - @course_groups = get_user_permission_course(poll_ids,2) + @course_groups = get_user_permission_course(poll_ids, 2) else @course_groups = [] end @@ -431,32 +417,32 @@ class PollsController < ApplicationController ActiveRecord::Base.transaction do begin g_course = params[:group_ids] - if !@poll.unified_setting #如果问卷为分班设置,且传入的班级id存在 + if !@poll.unified_setting #如果问卷为分班设置,且传入的班级id存在 if g_course.present? - course_group_ids = @course.course_members.course_find_by_ids("course_group_id",g_course).pluck(:user_id).uniq #传入班级的全部学生人数 - all_poll_settings = @poll.poll_group_settings.find_in_poll_group("course_group_id",g_course) #根据传入的班级id来获取问卷的分组设置 - all_poll_settings.destroy_all #问卷的设置文件全部删除 + course_group_ids = @course.course_members.course_find_by_ids("course_group_id", g_course).pluck(:user_id).uniq #传入班级的全部学生人数 + all_poll_settings = @poll.poll_group_settings.find_in_poll_group("course_group_id", g_course) #根据传入的班级id来获取问卷的分组设置 + all_poll_settings.destroy_all #问卷的设置文件全部删除 # @poll.poll_users.find_by_group_ids(course_group_ids).destroy_all #问卷的提交信息 - poll_question_ids = @poll.poll_questions.pluck(:id) #问卷的全部问卷 - PollVote.find_current_vote("user_id",course_group_ids).find_current_vote("poll_question_id",poll_question_ids).destroy_all + poll_question_ids = @poll.poll_questions.pluck(:id) #问卷的全部问卷 + PollVote.find_current_vote("user_id", course_group_ids).find_current_vote("poll_question_id", poll_question_ids).destroy_all poll_user_options = { - :commit_status => 0, - :start_at => nil, - :end_at => nil + :commit_status => 0, + :start_at => nil, + :end_at => nil } - @poll.poll_users.find_by_group_ids(course_group_ids).update_all(poll_user_options) #试卷的用户全部更新为未答题状态 + @poll.poll_users.find_by_group_ids(course_group_ids).update_all(poll_user_options) #试卷的用户全部更新为未答题状态 poll_groups = @poll.poll_group_settings - if poll_groups.present? #是否还有问卷的发布分组,如果有的话,则是根据发布规则取最大和最小值 + if poll_groups.present? #是否还有问卷的发布分组,如果有的话,则是根据发布规则取最大和最小值 unified_setting = false e_time_present = poll_groups.end_time_present.map(&:end_time) p_time_present = poll_groups.publish_time_present.map(&:publish_time) e_time = e_time_present.size > 0 ? e_time_present.max : nil p_time = p_time_present.size > 0 ? p_time_present.min : nil poll_status = 1 - if p_time.nil? #发布时间为空,则表示问卷未发布 + if p_time.nil? #发布时间为空,则表示问卷未发布 poll_status = 1 elsif p_time.present? && e_time.present? - poll_status = set_poll_status(p_time,e_time) + poll_status = set_poll_status(p_time, e_time) end else unified_setting = true @@ -465,34 +451,34 @@ class PollsController < ApplicationController poll_status = 1 end poll_options = { - :unified_setting => unified_setting, - :polls_status => poll_status, - :publish_time => p_time, - :end_time => e_time + :unified_setting => unified_setting, + :polls_status => poll_status, + :publish_time => p_time, + :end_time => e_time } @poll.update!(poll_options) - normal_status(0,"分班问卷撤销发布成功!") + normal_status(0, "分班问卷撤销发布成功!") else - normal_status(-1,"请选择撤销发布班级!") + normal_status(-1, "请选择撤销发布班级!") end else poll_user_options = { - :commit_status => 0, - :start_at => nil, - :end_at => nil + :commit_status => 0, + :start_at => nil, + :end_at => nil } @poll.poll_users.update_all(poll_user_options) poll_question_ids = @poll.poll_questions.pluck(:id) - PollVote.find_current_vote("poll_question_id",poll_question_ids).destroy_all + PollVote.find_current_vote("poll_question_id", poll_question_ids).destroy_all poll_new_params = { - :polls_status => 1, - :publish_time => nil, - :end_time => nil, - :unified_setting => true + :polls_status => 1, + :publish_time => nil, + :end_time => nil, + :unified_setting => true } @poll.update!(poll_new_params) @poll.poll_group_settings.destroy_all - normal_status(0,"问卷撤销发布成功!") + normal_status(0, "问卷撤销发布成功!") ## @poll.tidings.destroy_all 用户的发送消息全部删除,因接口未定,所以先注释 @@ -510,7 +496,7 @@ class PollsController < ApplicationController def destroys ActiveRecord::Base.transaction do begin - check_ids = Poll.where(id:params[:check_ids]) + check_ids = Poll.where(id: params[:check_ids]) check_ids.each do |poll| if poll.present? poll.destroy @@ -529,7 +515,7 @@ class PollsController < ApplicationController def set_public ActiveRecord::Base.transaction do begin - check_ids = Poll.where(id:params[:check_ids]) + check_ids = Poll.where(id: params[:check_ids]) check_ids.each do |poll| poll.update!(is_public: true) end @@ -548,42 +534,42 @@ class PollsController < ApplicationController begin check_ids = Poll.where(id: params[:check_ids]) check_ids.each do |poll| - current_ex_bank = current_user.exercise_banks.find_by_container(poll.id,"Poll").first - if current_ex_bank.present? #当前用户的选择问卷是否已加入习题库,存在则更新习题库和问题库,否则新建习题库和问题库 + current_ex_bank = current_user.exercise_banks.find_by_container(poll.id, "Poll").first + if current_ex_bank.present? #当前用户的选择问卷是否已加入习题库,存在则更新习题库和问题库,否则新建习题库和问题库 ex_params = { - :name => poll.polls_name, - :description => poll.polls_description, - :course_list_id => poll.course.try(:course_list_id) + :name => poll.polls_name, + :description => poll.polls_description, + :course_list_id => poll.course.try(:course_list_id) } current_ex_bank.update!(ex_params) - question_bank = QuestionBank.ques_by_container(current_ex_bank.id,current_ex_bank.container_type) #该习题库是否存在于问题库里 + question_bank = QuestionBank.ques_by_container(current_ex_bank.id, current_ex_bank.container_type) #该习题库是否存在于问题库里 ques_params = { - :name => current_ex_bank.name, - :course_list_id => current_ex_bank.course_list_id + :name => current_ex_bank.name, + :course_list_id => current_ex_bank.course_list_id } question_bank.first.update!(ques_params) if question_bank.present? - current_ex_bank.exercise_bank_questions.destroy_all # 更新后,习题库的问题全部删除,后续重新再建 + current_ex_bank.exercise_bank_questions.destroy_all # 更新后,习题库的问题全部删除,后续重新再建 else ex_params = { - :name => poll.polls_name, - :description => poll.polls_description, - :user_id => current_user.id, - :is_public => 0, - :course_list_id => poll.course.try(:course_list_id), - :container_id => poll.id, - :container_type => "Poll", - :quotes => 1 + :name => poll.polls_name, + :description => poll.polls_description, + :user_id => current_user.id, + :is_public => 0, + :course_list_id => poll.course.try(:course_list_id), + :container_id => poll.id, + :container_type => "Poll", + :quotes => 1 } - current_ex_bank= ExerciseBank.new ex_params - if current_ex_bank.save! #如果习题库保存成功,则会创建问题库question_bank + current_ex_bank = ExerciseBank.new ex_params + if current_ex_bank.save! #如果习题库保存成功,则会创建问题库question_bank ques_params = { - :name => current_ex_bank.name, - :container_id => current_ex_bank.id, - :container_type => current_ex_bank.container_type, - :quotes => current_ex_bank.quotes, - :user_id => current_ex_bank.user_id, - :is_public => current_ex_bank.is_public, - :course_list_id => current_ex_bank.course_list_id + :name => current_ex_bank.name, + :container_id => current_ex_bank.id, + :container_type => current_ex_bank.container_type, + :quotes => current_ex_bank.quotes, + :user_id => current_ex_bank.user_id, + :is_public => current_ex_bank.is_public, + :course_list_id => current_ex_bank.course_list_id } question_bank = QuestionBank.new ques_params question_bank.save! @@ -593,20 +579,20 @@ class PollsController < ApplicationController # 问卷的问题的输入 poll.poll_questions.each do |f| option = { - :question_title => f.question_title, - :question_type => f.question_type, - :is_necessary => f.is_necessary, - :question_number => f.question_number, - :max_choices => f.max_choices, - :min_choices => f.min_choices + :question_title => f.question_title, + :question_type => f.question_type, + :is_necessary => f.is_necessary, + :question_number => f.question_number, + :max_choices => f.max_choices, + :min_choices => f.min_choices } exercise_bank_question = current_ex_bank.exercise_bank_questions.new option exercise_bank_question.save! ## 问卷答案的输入 f.poll_answers.each do |a| choice_option = { - :choice_position => a.answer_position, - :choice_text => a.answer_text + :choice_position => a.answer_position, + :choice_text => a.answer_text } exercise_bank_c = exercise_bank_question.exercise_bank_choices.new choice_option exercise_bank_c.save! @@ -634,7 +620,7 @@ class PollsController < ApplicationController search_type = params[:search].to_s.strip @current_user_exercises = @current_user_exercises.exercise_bank_search(search_type) end - page = params[:page] || 1 + page = params[:page] || 1 limit = params[:limit] || 15 @my_exercises_count = @current_user_exercises.size @current_user_exercises = @current_user_exercises.page(page).per(limit) @@ -654,7 +640,7 @@ class PollsController < ApplicationController ActiveRecord::Base.transaction do begin if current_user.is_certification_teacher - @user_certification = 1 #用户已通过认证 + @user_certification = 1 #用户已通过认证 @public_exercises = ExerciseBank.find_by_c_type("Poll").public_exercises if @public_exercises.present? @@ -663,7 +649,7 @@ class PollsController < ApplicationController @public_exercises = @public_exercises.exercise_bank_search(search_type) end - page = params[:page] || 1 + page = params[:page] || 1 limit = params[:limit] || 15 @public_exercises_count = @public_exercises.size @@ -673,7 +659,7 @@ class PollsController < ApplicationController @public_exercises = [] end else - @user_certification = 0 #用户未通过认证 + @user_certification = 0 #用户未通过认证 @public_exercises_count = 0 @public_exercises = [] end @@ -690,11 +676,11 @@ class PollsController < ApplicationController ActiveRecord::Base.transaction do begin @user_permission = 2 - @user_course_groups = @course.teacher_group(current_user.id) #当前老师的分班 - @being_setting_course_ids = @poll.poll_published_ids(current_user.id) #当前用户已发布的班级的id - @user_published_setting = @poll.poll_group_settings.find_in_poll_group("course_group_id",@being_setting_course_ids) + @user_course_groups = @course.teacher_group(current_user.id) #当前老师的分班 + @being_setting_course_ids = @poll.poll_published_ids(current_user.id) #当前用户已发布的班级的id + @user_published_setting = @poll.poll_group_settings.find_in_poll_group("course_group_id", @being_setting_course_ids) poll_ids = [@poll.id] - @poll_publish_count = get_user_permission_course(poll_ids,2).count + @poll_publish_count = get_user_permission_course(poll_ids, 2).count rescue Exception => e uid_logger_error(e.message) @@ -708,9 +694,9 @@ class PollsController < ApplicationController def commit_setting ActiveRecord::Base.transaction do begin - error_count = 0 # 判断循环里是否有已发布/已截止的,且时间更改了的分班。 + error_count = 0 # 判断循环里是否有已发布/已截止的,且时间更改了的分班。 - course_group_ids = @course.charge_group_ids(current_user) #当前老师的班级id数组 + course_group_ids = @course.charge_group_ids(current_user) #当前老师的班级id数组 poll_status = @poll.get_poll_status(current_user) if poll_status == 1 && course_group_ids.size > 0 # 问卷未发布,且老师的分班大于1 ,才可以修改统一设置,否则按poll默认的来处理 @@ -725,50 +711,48 @@ class PollsController < ApplicationController tip_exception("发布时间不能为空") if params[:publish_time].blank? tip_exception("截止时间不能为空") if params[:end_time].blank? tip_exception("截止时间必须晚于发布时间") if params[:publish_time].to_time >= params[:end_time].to_time - tip_exception("截止时间不能晚于课堂结束时间(#{@course.end_date.end_of_day.strftime("%Y-%m-%d %H:%M")})") if - @course.end_date.present? && params[:end_time].to_time > @course.end_date.end_of_day + tip_exception("截止时间不能晚于课堂结束时间(#{@course.end_date.end_of_day.strftime("%Y-%m-%d %H:%M")})") if @course.end_date.present? && params[:end_time].to_time > @course.end_date.end_of_day params_publish_time = params[:publish_time].to_time params_end_time = params[:end_time].to_time if poll_status != 1 && @poll.publish_time != params_publish_time - normal_status(-1,"不允许修改发布时间") + normal_status(-1, "不允许修改发布时间") elsif params_publish_time.present? && params_end_time.present? && params_end_time < params_publish_time - normal_status(-1,"截止时间不能小于发布时间") + normal_status(-1, "截止时间不能小于发布时间") else #发布时间小于当前时间,则poll显示为未发布,当截止时间大于当前时间,则显示为已截止 - poll_status_n = set_poll_status(params_publish_time,params_end_time) + poll_status_n = set_poll_status(params_publish_time, params_end_time) poll_params = { - :unified_setting => unified_setting, - :show_result => show_result, - :un_anonymous => un_anonymous, - :polls_status => poll_status_n, - :publish_time => params_publish_time, - :end_time => params_end_time + :unified_setting => unified_setting, + :show_result => show_result, + :un_anonymous => un_anonymous, + :polls_status => poll_status_n, + :publish_time => params_publish_time, + :end_time => params_end_time } @poll.update!(poll_params) @poll.poll_group_settings.destroy_all normal_status(0, "问卷设置成功!") end else - params_times = params[:publish_time_groups] #分班返回的json数组{"publish_time_groups":[{"course_group_id":["1","2"],"publish_time":"xx","end_time":"xxx"}]} - poll_groups = @poll.poll_group_settings.find_in_poll_group("course_id",@course.id) #当前课堂问卷的班级全部分班信息 - poll_groups_ids = poll_groups.pluck(:course_group_id) #问卷的全部分班id - total_common = params_times.map{|k| k[:course_group_id]}.sum #传入的所有分组的分班id - total_common_group = poll_groups_ids & total_common #传入的分班与问卷已存在的分班的交集 + params_times = params[:publish_time_groups] #分班返回的json数组{"publish_time_groups":[{"course_group_id":["1","2"],"publish_time":"xx","end_time":"xxx"}]} + poll_groups = @poll.poll_group_settings.find_in_poll_group("course_id", @course.id) #当前课堂问卷的班级全部分班信息 + poll_groups_ids = poll_groups.pluck(:course_group_id) #问卷的全部分班id + total_common = params_times.map {|k| k[:course_group_id]}.sum #传入的所有分组的分班id + total_common_group = poll_groups_ids & total_common #传入的分班与问卷已存在的分班的交集 old_poll_groups = poll_groups_ids - total_common_group #后来传入的分班里,没有了的班级,即需要删除 params_times.each do |t| tip_exception("发布时间不能为空") if t[:publish_time].blank? tip_exception("截止时间不能为空") if t[:end_time].blank? tip_exception("截止时间不能早于发布时间") if t[:publish_time].to_time > t[:end_time].to_time - tip_exception("截止时间不能晚于课堂结束时间(#{@course.end_date.end_of_day.strftime("%Y-%m-%d %H:%M")})") if - @course.end_date.present? && t[:end_time].to_time > @course.end_date.end_of_day + tip_exception("截止时间不能晚于课堂结束时间(#{@course.end_date.end_of_day.strftime("%Y-%m-%d %H:%M")})") if @course.end_date.present? && t[:end_time].to_time > @course.end_date.end_of_day course_id = t[:course_group_id] poll_publish_time = t[:publish_time].to_time poll_end_time = t[:end_time].to_time - poll_group = poll_groups.find_in_poll_group("course_group_id",course_id) #判断该分班是否存在 + poll_group = poll_groups.find_in_poll_group("course_group_id", course_id) #判断该分班是否存在 if poll_group.present? && (poll_group.first.publish_time < Time.now) && (poll_publish_time != poll_group.first.publish_time) error_count += 1 @@ -779,16 +763,16 @@ class PollsController < ApplicationController if error_count == 0 common_group = poll_groups_ids & course_id #传入的班级与问卷已存在的班级的交集,即表示已有分班的 - new_group_ids = course_id - common_group #新传入的班级id - if common_group.size > 0 #传入的参数存在已发布的分班 - poll_group = poll_groups.find_in_poll_group("course_group_id",common_group) + new_group_ids = course_id - common_group #新传入的班级id + if common_group.size > 0 #传入的参数存在已发布的分班 + poll_group = poll_groups.find_in_poll_group("course_group_id", common_group) poll_group.each do |the_group_setting| poll_group_params = { :publish_time => poll_publish_time, :end_time => poll_end_time } - the_group_setting_status = set_poll_status(the_group_setting.publish_time,the_group_setting.end_time) + the_group_setting_status = set_poll_status(the_group_setting.publish_time, the_group_setting.end_time) if the_group_setting_status == 2 poll_group_params = { :publish_time => the_group_setting.publish_time, @@ -806,11 +790,11 @@ class PollsController < ApplicationController if new_group_ids.size > 0 new_group_ids.each do |c| poll_group_params = { - :poll_id => @poll.id, - :course_group_id => c, - :course_id => @course.id, - :publish_time => poll_publish_time, - :end_time => poll_end_time + :poll_id => @poll.id, + :course_group_id => c, + :course_id => @course.id, + :publish_time => poll_publish_time, + :end_time => poll_end_time } new_poll_group = PollGroupSetting.new(poll_group_params) new_poll_group.save! @@ -821,11 +805,11 @@ class PollsController < ApplicationController if error_count > 0 error_count == 0 - normal_status(-1,"已发布/已截止的问卷不允许修改时间") + normal_status(-1, "已发布/已截止的问卷不允许修改时间") else # 未发布的分班设置才能删除 if old_poll_groups.size > 0 - old_all_poll_groups = poll_groups.find_in_poll_group("course_group_id",old_poll_groups).poll_group_not_published + old_all_poll_groups = poll_groups.find_in_poll_group("course_group_id", old_poll_groups).poll_group_not_published old_all_poll_groups.destroy_all end #问卷更新为poll_group_setting的发布时间最小,截止时间最大 @@ -834,24 +818,24 @@ class PollsController < ApplicationController e_time = e_time_present.size > 0 ? e_time_present.max : nil p_time = p_time_present.size > 0 ? p_time_present.min : nil poll_status = 1 - if p_time.nil? #发布时间为空,则表示问卷未发布 + if p_time.nil? #发布时间为空,则表示问卷未发布 poll_status = 1 elsif p_time.present? && e_time.present? - poll_status = set_poll_status(p_time,e_time) + poll_status = set_poll_status(p_time, e_time) end poll_params = { - :unified_setting => unified_setting, - :show_result => show_result, - :un_anonymous => un_anonymous, - :polls_status => poll_status, - :publish_time => p_time, - :end_time => e_time + :unified_setting => unified_setting, + :show_result => show_result, + :un_anonymous => un_anonymous, + :polls_status => poll_status, + :publish_time => p_time, + :end_time => e_time } @poll.update!(poll_params) if @poll.polls_status == 2 if @poll.course_acts.size == 0 - @poll.course_acts << CourseActivity.new(:user_id => @poll.user_id,:course_id => @poll.course_id) + @poll.course_acts << CourseActivity.new(:user_id => @poll.user_id, :course_id => @poll.course_id) end end normal_status(0, "问卷设置成功!") @@ -870,15 +854,15 @@ class PollsController < ApplicationController def start_answer ActiveRecord::Base.transaction do begin - poll_user_current = PollUser.where(user_id:@poll_current_user_id,poll_id:@poll.id)&.first #查找当前用户是否有过答题 + poll_user_current = PollUser.where(user_id: @poll_current_user_id, poll_id: @poll.id)&.first #查找当前用户是否有过答题 @poll_status = @poll.get_poll_status(current_user) if poll_user_current.blank? if @user_course_identity > Course::ASSISTANT_PROFESSOR #当为老师的时候,不创建poll_user表,理论上老师是不能进入答题的 poll_user_params = { - :user_id => @poll_current_user_id, - :poll_id => @poll.id, - :start_at => Time.now, - :commit_status => 0 + :user_id => @poll_current_user_id, + :poll_id => @poll.id, + :start_at => Time.now, + :commit_status => 0 } PollUser.create!(poll_user_params) end @@ -887,9 +871,9 @@ class PollsController < ApplicationController end if @user_course_identity < Course::STUDENT || (@poll_status == 3) || (poll_user_current.present? && poll_user_current.commit_status == 1) - @user_poll_status = 1 #当前用户为老师/问卷已截止/问卷已提交不可编辑 + @user_poll_status = 1 #当前用户为老师/问卷已截止/问卷已提交不可编辑 else - @user_poll_status = 0 #可编辑 + @user_poll_status = 0 #可编辑 end @answer_status = [] @@ -897,10 +881,10 @@ class PollsController < ApplicationController # 判断是否已经回答还是新建的回答 @poll_questions.each do |q| - ques_vote = q.poll_votes.find_current_vote("user_id",@poll_current_user_id) + ques_vote = q.poll_votes.find_current_vote("user_id", @poll_current_user_id) ques_type = q.question_type - if ques_type != 3 #非简答题时 + if ques_type != 3 #非简答题时 if ques_vote.exists? ques_status = 1 question_answered += 1 @@ -923,9 +907,9 @@ class PollsController < ApplicationController # ques_status = 0 # end answer_status = { - :ques_id => q.id, - :ques_number => q.question_number, - :ques_status => ques_status + :ques_id => q.id, + :ques_number => q.question_number, + :ques_status => ques_status } @answer_status = @answer_status.push(answer_status) end @@ -941,13 +925,13 @@ class PollsController < ApplicationController def commit_poll ActiveRecord::Base.transaction do begin - @poll_multi_questions = @poll.poll_questions.where(question_type:2).select(:id,:max_choices,:min_choices,:question_number) + @poll_multi_questions = @poll.poll_questions.where(question_type: 2).select(:id, :max_choices, :min_choices, :question_number) error_question = [] @poll_multi_questions.each do |q| - poll_user_votes = current_user.poll_votes.where(poll_question_id:q.id)&.size + poll_user_votes = current_user.poll_votes.where(poll_question_id: q.id)&.size if q.max_choices.present? && (q.max_choices > 0) && (poll_user_votes > q.max_choices) error_messages = "第#{q.question_number}题:超过最大选项限制" - elsif q.min_choices.present? && (q.min_choices > 0)&& (poll_user_votes < q.min_choices) + elsif q.min_choices.present? && (q.min_choices > 0) && (poll_user_votes < q.min_choices) error_messages = "第#{q.question_number}题:不得少于最小选项限制" else error_messages = nil @@ -960,8 +944,8 @@ class PollsController < ApplicationController else poll_user_current = @poll.poll_users.find_by_group_ids(current_user.id).first poll_user_params = { - :commit_status => 1, - :end_at => Time.now + :commit_status => 1, + :end_at => Time.now } poll_user_current.update!(poll_user_params) CommitPollNotifyJobJob.perform_later(@poll.id, current_user.id) @@ -980,26 +964,26 @@ class PollsController < ApplicationController def commit_result ActiveRecord::Base.transaction do begin - @poll_users = @poll.all_poll_users(current_user.id).where(commit_status:1) # 问卷已提交的用户 + @poll_users = @poll.all_poll_users(current_user.id).where(commit_status: 1) # 问卷已提交的用户 @poll_commit_ids = @poll_users.commit_by_status(1).pluck(:user_id) #问卷提交用户的id - @page = params[:page] || 1 + @page = params[:page] || 1 @limit = params[:limit] || 10 @poll_export_questions = @poll_questions @poll_questions = @poll_questions.page(@page).per(@limit) if params[:format] == "xlsx" if @user_course_identity > Course::ASSISTANT_PROFESSOR - tip_exception(403,"无权限操作") + tip_exception(403, "无权限操作") elsif (@poll.polls_status == 1) || (@poll_export_questions.size == 0) || (@poll_commit_ids.size == 0) - normal_status(-1,"暂无用户提交") + normal_status(-1, "暂无用户提交") elsif params[:export].present? && params[:export] - normal_status(0,"正在下载中") + normal_status(0, "正在下载中") else respond_to do |format| - format.xlsx{ + format.xlsx { set_export_cookies polls_export_name_ = "#{current_user.real_name}_#{@course.name}_#{@poll.polls_name}_#{Time.now.strftime('%Y%m%d_%H%M%S')}" - polls_user_commit = poll_commit_result(@poll,@poll_export_questions,@poll_users,@poll_commit_ids) - render xlsx: "#{polls_export_name_.strip}",template: "polls/commit_result.xlsx.axlsx",locals: {polls_user_commit:polls_user_commit} + polls_user_commit = poll_commit_result(@poll, @poll_export_questions, @poll_users, @poll_commit_ids) + render xlsx: "#{polls_export_name_.strip}", template: "polls/commit_result.xlsx.axlsx", locals: {polls_user_commit: polls_user_commit} } end end @@ -1018,17 +1002,17 @@ class PollsController < ApplicationController begin poll_ids = [@poll.id] @poll_list_status = @poll.get_poll_status(current_user) - @poll_publish_count = get_user_permission_course(poll_ids,2).count - @poll_unpublish_count = get_user_permission_course(poll_ids,1).count + @poll_publish_count = get_user_permission_course(poll_ids, 2).count + @poll_unpublish_count = get_user_permission_course(poll_ids, 1).count @course_all_members = @course.students @poll_group_counts = @course.course_groups_count - if @user_course_identity < Course::STUDENT #当前为老师,而且老师只能查看自己班级的/课堂的问卷 + if @user_course_identity < Course::STUDENT #当前为老师,而且老师只能查看自己班级的/课堂的问卷 @poll_current_user_status = 0 - @poll_users_list = @poll.all_poll_users(current_user.id) #该老师分班的全部学生 + @poll_users_list = @poll.all_poll_users(current_user.id) #该老师分班的全部学生 get_poll_answers(@poll_users_list, @poll_list_status) if @poll_list_status == 1 - @poll_course_groups =[] + @poll_course_groups = [] else poll_common_ids = @poll.poll_published_ids(current_user.id) @poll_course_groups = @course.get_ex_published_course(poll_common_ids) @@ -1038,10 +1022,10 @@ class PollsController < ApplicationController end elsif @user_course_identity > Course::ASSISTANT_PROFESSOR @poll_all_users = @poll.get_poll_exercise_users - get_poll_answers(@poll_all_users, @poll_list_status) # 未答和已答的 - @poll_course_groups = [] #当为学生的时候,不显示分班情况 - @poll_current_user_status = 1 #当前用户的状态,为学生 - poll_current_user = @poll_all_users.find_by_group_ids(current_user.id) #当前用户是否开始做问卷(提交/未提交/没做) + get_poll_answers(@poll_all_users, @poll_list_status) # 未答和已答的 + @poll_course_groups = [] #当为学生的时候,不显示分班情况 + @poll_current_user_status = 1 #当前用户的状态,为学生 + poll_current_user = @poll_all_users.find_by_group_ids(current_user.id) #当前用户是否开始做问卷(提交/未提交/没做) if poll_current_user.present? && @poll_list_status != 1 #当前为学生或者有过答题的(提交/未提交),且问卷已发布的 @poll_users_list = poll_current_user.distinct else @@ -1049,13 +1033,13 @@ class PollsController < ApplicationController end else @poll_all_users = @poll.get_poll_exercise_users - get_poll_answers(@poll_all_users, @poll_list_status) # 未答和已答的 - @poll_current_user_status = 2 #当前用户非课堂成员 + get_poll_answers(@poll_all_users, @poll_list_status) # 未答和已答的 + @poll_current_user_status = 2 #当前用户非课堂成员 @poll_users_list = [] end if @poll_users_list.present? && @poll_users_list.count > 0 - @poll_users_count = @poll_users_list.count #当前显示的全部成员数量 + @poll_users_count = @poll_users_list.count #当前显示的全部成员数量 else @poll_users_count = 0 end @@ -1080,7 +1064,7 @@ class PollsController < ApplicationController #班级的选择 if group_id.present? - poll_students = @course_all_members.course_find_by_ids("course_group_id",group_id) # 问卷所分班的全部人数 + poll_students = @course_all_members.course_find_by_ids("course_group_id", group_id) # 问卷所分班的全部人数 user_ids = poll_students.pluck(:user_id).reject(&:blank?) @poll_users_list = @poll_users_list.find_by_group_ids(user_ids) end @@ -1090,7 +1074,7 @@ class PollsController < ApplicationController @poll_users_list = @poll_users_list.joins(user: :user_extension).where("CONCAT(lastname, firstname) like ? OR student_id like ?", "%#{search_content}%", "%#{search_content}%") end - poll_users_joins = @poll_users_list.joins(user: :user_extension) + poll_users_joins = @poll_users_list.joins(user: :user_extension) if order == "student_id" @poll_users_list = poll_users_joins.order("user_extensions.student_id #{order_type}") @@ -1101,7 +1085,7 @@ class PollsController < ApplicationController @poll_users_size = @poll_users_list.count # 分页 - page = params[:page] || 1 + page = params[:page] || 1 limit = params[:limit] || 20 @poll_users_list = @poll_users_list.page(page).per(limit) else @@ -1134,8 +1118,8 @@ class PollsController < ApplicationController end def poll_params - params.require(:poll).permit(:polls_name,:polls_status,:publish_time,:end_time,:polls_description, - :show_result,:exercise_bank_id,:is_public,:unified_setting,:un_anonymous) + params.require(:poll).permit(:polls_name, :polls_status, :publish_time, :end_time, :polls_description, + :show_result, :exercise_bank_id, :is_public, :unified_setting, :un_anonymous) end # 获得问卷及课堂 @@ -1158,7 +1142,7 @@ class PollsController < ApplicationController # 在设置问卷公开前,需判断课堂是否公开 def is_course_public - unless @course.is_public == 1 # 0为私有,1为公开 + unless @course.is_public == 1 # 0为私有,1为公开 tip_exception(403) end end @@ -1166,17 +1150,17 @@ class PollsController < ApplicationController ## 判断开始答题页面的用户权限 def check_user_on_answer poll_status = @poll.get_poll_status(current_user) - if @user_course_identity == Course::STUDENT && poll_status == 1 #问卷未发布,且当前用户不为老师/管理员 + if @user_course_identity == Course::STUDENT && poll_status == 1 #问卷未发布,且当前用户不为老师/管理员 normal_status(-1, "未发布问卷!") - elsif @user_course_identity > Course::STUDENT && (!@poll.is_public || (@poll.is_public && !@poll.unified_setting)) ##不为课堂成员,且问卷不为公开的,或问卷公开,但是不是统一设置的 + elsif @user_course_identity > Course::STUDENT && (!@poll.is_public || (@poll.is_public && !@poll.unified_setting)) ##不为课堂成员,且问卷不为公开的,或问卷公开,但是不是统一设置的 normal_status(-1, "问卷暂未公开!") end end - def check_unified_setting?(poll) #问卷是否统一设置,如为分班设置,当前用户是否在班级内 - if !poll.unified_setting #如果为分班设置,则需判断用户是否在班级内 + def check_unified_setting?(poll) #问卷是否统一设置,如为分班设置,当前用户是否在班级内 + if !poll.unified_setting #如果为分班设置,则需判断用户是否在班级内 poll_group_settings = poll.poll_group_settings.pluck(:course_group_id) - member = @course.course_members.course_find_by_ids("user_id",current_user.id) + member = @course.course_members.course_find_by_ids("user_id", current_user.id) member_group_id = member.pluck(:course_group_id).uniq if (member_group_id & poll_group_settings).size > 0 || @user_course_identity < Course::STUDENT true @@ -1196,7 +1180,7 @@ class PollsController < ApplicationController end end - def set_poll_status(publish_time,end_time) + def set_poll_status(publish_time, end_time) time_now_i = Time.now if publish_time.present? && end_time.present? && publish_time <= time_now_i && end_time > time_now_i 2 @@ -1204,8 +1188,8 @@ class PollsController < ApplicationController 1 elsif end_time.present? && end_time <= time_now_i 3 - # elsif end_time.present? && publish_time.present? && end_time < publish_time - # normal_status(-1,"时间设置错误!") + # elsif end_time.present? && publish_time.present? && end_time < publish_time + # normal_status(-1,"时间设置错误!") else 1 end @@ -1217,20 +1201,20 @@ class PollsController < ApplicationController @poll_question_singles = @poll_questions.ques_count(1).size # 单选题 @poll_question_doubles = @poll_questions.ques_count(2).size # 多选题 @poll_question_mains = @poll_questions.ques_count(3).size #主观题 - @poll_questions = @poll_questions&.includes(:poll_answers,:poll_votes).distinct + @poll_questions = @poll_questions&.includes(:poll_answers, :poll_votes).distinct end - def check_poll_question_complete #commit_poll 的权限 + def check_poll_question_complete #commit_poll 的权限 poll_user_current = @poll.poll_users.find_by_group_ids(current_user.id).first poll_status = @poll.get_poll_status(current_user) if @user_course_identity < Course::STUDENT || (poll_status == 3) || (poll_user_current.present? && poll_user_current.commit_status == 1) - normal_status(-1,"用户没有权限!") #老师/管理员在提交时没有权限 + normal_status(-1, "用户没有权限!") #老师/管理员在提交时没有权限 else necessary_answer = 0 poll_questions = @poll.poll_questions - necessary_question = poll_questions.ques_necessary # 问卷必答的问题 + necessary_question = poll_questions.ques_necessary # 问卷必答的问题 necessary_question.each do |q| - user_vote = q.poll_votes.find_current_vote("user_id",current_user.id) + user_vote = q.poll_votes.find_current_vote("user_id", current_user.id) vote_answer_id = user_vote.pluck(:poll_answer_id).reject(&:blank?).size vote_text_count = user_vote.pluck(:vote_text).reject(&:blank?).size if vote_answer_id == 0 && vote_text_count == 0 @@ -1238,7 +1222,7 @@ class PollsController < ApplicationController end end if necessary_answer > 0 - normal_status(-1,"问卷提交失败,有#{necessary_answer}个未答的必答题!") + normal_status(-1, "问卷提交失败,有#{necessary_answer}个未答的必答题!") end end end @@ -1246,20 +1230,20 @@ class PollsController < ApplicationController def check_poll_commit_result poll_status = @poll.get_poll_status(current_user) commit_poll_user = @poll.poll_users.find_by_group_ids(current_user.id).commit_by_status(1) #当前用户已提交问卷的 - unless (@user_course_identity < Course::STUDENT) || ((@poll.show_result == 1) && (poll_status == 3) && commit_poll_user.present?) - normal_status(-1,"没有权限!") #当前为老师/问卷公开统计,且问卷已截止,且用户有过回答的 + unless (@user_course_identity < Course::STUDENT) || ((@poll.show_result == 1) && (poll_status == 3) && commit_poll_user.present?) + normal_status(-1, "没有权限!") #当前为老师/问卷公开统计,且问卷已截止,且用户有过回答的 end end def check_user_id_start_answer #判断用户在开始答题时,是否有用户id传入,如果为老师,则id必需,否则为当前用户的id user_login = params[:login] # @poll_current_user_id = params[:user_id] - if user_login.blank? && @user_course_identity < Course::STUDENT #id不存在,且当前为老师/管理员等 - normal_status(-1,"请输入学生登陆名!") + if user_login.blank? && @user_course_identity < Course::STUDENT #id不存在,且当前为老师/管理员等 + normal_status(-1, "请输入学生登陆名!") else - @answer_user = User.find_by(login:user_login) + @answer_user = User.find_by(login: user_login) if @answer_user.blank? - normal_status(404,"答题用户不存在") + normal_status(404, "答题用户不存在") else @poll_current_user_id = @answer_user.id || current_user.id end @@ -1273,15 +1257,15 @@ class PollsController < ApplicationController @poll_questions_count = @poll_questions.size end - def get_user_permission_course(poll_ids,status) #获取用户权限范围内的已发布/未发布 + def get_user_permission_course(poll_ids, status) #获取用户权限范围内的已发布/未发布 poll_status = status.to_i unpublish_group = [] user_groups_id = @course.charge_group_ids(current_user) - all_polls = Poll.where(id:poll_ids) + all_polls = Poll.where(id: poll_ids) all_polls.each do |poll| if poll.present? if poll.unified_setting - poll_user_status = poll.get_poll_status(current_user) #当前用户的能看到的试卷 + poll_user_status = poll.get_poll_status(current_user) #当前用户的能看到的试卷 if poll_user_status == poll_status || poll_status == 3 #未发布的情况 unpublish_group = unpublish_group + user_groups_id else @@ -1290,15 +1274,15 @@ class PollsController < ApplicationController # unpublish_group = unpublish_group + user_groups_id else poll_all_group_settings = poll.poll_group_settings - poll_group_settings = poll_all_group_settings.poll_group_published.pluck(:course_group_id).uniq #问卷设置已发布的班级 + poll_group_settings = poll_all_group_settings.poll_group_published.pluck(:course_group_id).uniq #问卷设置已发布的班级 if poll_status == 1 - unpublish_group = user_groups_id - poll_group_settings + unpublish_group = user_groups_id - poll_group_settings elsif poll_status == 3 poll_ended_groups = poll_all_group_settings.poll_group_ended.pluck(:course_group_id).uniq - poll_and_user = user_groups_id & poll_group_settings #用户已设置的分班 - unpublish_group = unpublish_group + poll_and_user - poll_ended_groups #已发布的全部班级减去截止的全部班级 + poll_and_user = user_groups_id & poll_group_settings #用户已设置的分班 + unpublish_group = unpublish_group + poll_and_user - poll_ended_groups #已发布的全部班级减去截止的全部班级 else - poll_and_user = user_groups_id & poll_group_settings #用户已设置的分班 + poll_and_user = user_groups_id & poll_group_settings #用户已设置的分班 unpublish_group = unpublish_group + poll_and_user end end @@ -1317,28 +1301,28 @@ class PollsController < ApplicationController publish_course = params[:publish_time_groups] unified_setting = params[:unified_setting] if @course.is_end - normal_status(-1,"课堂已结束不能再修改!") + normal_status(-1, "课堂已结束不能再修改!") elsif unified_setting poll_group_settings = @poll.poll_group_settings if poll_group_settings.present? p_time_present = poll_group_settings.publish_time_present.map(&:publish_time).min if p_time_present < Time.now - normal_status(-1,"设置失败,存在已发布的分班!") + normal_status(-1, "设置失败,存在已发布的分班!") end elsif params[:publish_time].blank? - normal_status(-1,"发布时间不允许为空") + normal_status(-1, "发布时间不允许为空") end - elsif unified_setting.present? && !unified_setting #非统一设置,分班不能为空 + elsif unified_setting.present? && !unified_setting #非统一设置,分班不能为空 if publish_course.present? - course_ids = publish_course.map{|a| a[:course_group_id]}.sum - publish_t = publish_course.map{|a| a[:publish_time]} + course_ids = publish_course.map {|a| a[:course_group_id]}.sum + publish_t = publish_course.map {|a| a[:publish_time]} if course_ids.include?(nil) || course_ids.count == 0 - normal_status(-1,"请选择分班!") + normal_status(-1, "请选择分班!") elsif publish_t.include?(nil) || publish_t.count == 0 - normal_status(-1,"发布时间不允许为空") + normal_status(-1, "发布时间不允许为空") end else - normal_status(-1,"请选择分班!") + normal_status(-1, "请选择分班!") end end end @@ -1354,64 +1338,64 @@ class PollsController < ApplicationController end #问卷的统计结果的导出 - def poll_commit_result(poll,poll_questions,poll_users,poll_commit_ids) + def poll_commit_result(poll, poll_questions, poll_users, poll_commit_ids) sub_commit = [] user_commit = [] poll_users_info = %w(序号) poll_ques_titles = poll_questions.pluck(:question_title).map {|k| ActionController::Base.helpers.strip_tags(k) if k.present?} poll_un_anony = poll.un_anonymous - if poll_un_anony #是否匿名,默认为false + if poll_un_anony #是否匿名,默认为false user_info = %w(登陆名 真实姓名 分班 邮箱 学号 学员单位) else user_info = [] end poll_users_info = poll_users_info + user_info + poll_ques_titles poll_questions.each do |q| - if q.question_type != 3 #问题不为主观题 - question_vote_user = q.poll_votes.find_current_vote("user_id",poll_commit_ids)&.size #该问题的有效填写量 - sheet_row = ["第#{q.question_number}题"] #选择题答案选项的数组 - sheet_answer_row = ["小计"] #选择题回答的答案人数,数组 + if q.question_type != 3 #问题不为主观题 + question_vote_user = q.poll_votes.find_current_vote("user_id", poll_commit_ids)&.size #该问题的有效填写量 + sheet_row = ["第#{q.question_number}题"] #选择题答案选项的数组 + sheet_answer_row = ["小计"] #选择题回答的答案人数,数组 sheet_answer_percent = ["比例"] - sheet_answer_useful = ["本题有效填写人次",question_vote_user] - q.poll_answers.each do |a| #问卷的答案选项 - answer_users_count = a.poll_votes.find_current_vote("user_id",poll_commit_ids)&.size - answer_percent = (question_vote_user == 0) ? "0.0%" : "#{((answer_users_count / question_vote_user.to_f)*100).round(1).to_s}%" + sheet_answer_useful = ["本题有效填写人次", question_vote_user] + q.poll_answers.each do |a| #问卷的答案选项 + answer_users_count = a.poll_votes.find_current_vote("user_id", poll_commit_ids)&.size + answer_percent = (question_vote_user == 0) ? "0.0%" : "#{((answer_users_count / question_vote_user.to_f) * 100).round(1).to_s}%" sheet_row.push(a.answer_text) sheet_answer_row.push(answer_users_count) sheet_answer_percent.push(answer_percent) end sheet_sub_commit = { - sub_title: sheet_row, - sub_user_votes:[sheet_answer_row,sheet_answer_percent,sheet_answer_useful] + sub_title: sheet_row, + sub_user_votes: [sheet_answer_row, sheet_answer_percent, sheet_answer_useful] } sub_commit.push(sheet_sub_commit) - else #主观题答案 - user_votes= [] - main_show_row = ["第#{q.question_number}题", q.question_title ] - q.poll_votes.each_with_index do |v,index| #主观题的答案 - q_poll_vote = [(index+1), v.vote_text.present? ? v.vote_text : "--"] + else #主观题答案 + user_votes = [] + main_show_row = ["第#{q.question_number}题", q.question_title] + q.poll_votes.each_with_index do |v, index| #主观题的答案 + q_poll_vote = [(index + 1), v.vote_text.present? ? v.vote_text : "--"] user_votes.push(q_poll_vote) end sheet_sub_commit = { - sub_title: main_show_row, - sub_user_votes:user_votes + sub_title: main_show_row, + sub_user_votes: user_votes } sub_commit.push(sheet_sub_commit) end - end #each_with_index + end #each_with_index - poll_users.includes(user: [:user_extension,:poll_votes]).each_with_index do |u,index| + poll_users.includes(user: [:user_extension, :poll_votes]).each_with_index do |u, index| u_user = u.user user_answer_array = [] poll_questions.each do |q| - user_poll_votes = u_user.poll_votes.find_current_vote("poll_question_id",q.id) + user_poll_votes = u_user.poll_votes.find_current_vote("poll_question_id", q.id) if user_poll_votes.present? if q.question_type < 3 user_poll_answer_ids = user_poll_votes.pluck(:poll_answer_id).reject(&:blank?) if user_poll_answer_ids.count > 0 - answer_content = q.poll_answers.find_answer_by_custom("id",user_poll_answer_ids) - if user_poll_answer_ids.count >1 + answer_content = q.poll_answers.find_answer_by_custom("id", user_poll_answer_ids) + if user_poll_answer_ids.count > 1 u_answer = answer_content.pluck(:answer_text).join(";") else u_answer = answer_content.first&.answer_text @@ -1436,23 +1420,23 @@ class PollsController < ApplicationController end user_answer_array.push(u_answer) end - user_cell = [index+1] + user_cell = [index + 1] if poll_un_anony user_login = u_user.login user_name = u_user.real_name.present? ? u_user.real_name : "--" user_class = poll.course.user_group_name(u_user.id) user_student_id = u_user.student_id.present? ? u_user.student_id : "--" user_school_name = u_user.school_name.present? ? u_user.school_name : "--" - user_cell += [user_login,user_name, user_class, u_user.mail, user_student_id, user_school_name] + user_cell += [user_login, user_name, user_class, u_user.mail, user_student_id, user_school_name] end all_user_cell = user_cell + user_answer_array user_commit.push(all_user_cell) end { - poll_users_info:poll_users_info, - sub_commit:sub_commit, - user_commit:user_commit + poll_users_info: poll_users_info, + sub_commit: sub_commit, + user_commit: user_commit } end