试卷题库的接口

dev_aliyun_beta
cxt 6 years ago
parent 23f58bfd54
commit f0ec97c033

@ -124,6 +124,7 @@ class ExerciseBankQuestionsController < ApplicationController
@exercise_question.update_attributes(:question_score => question_score, :shixun_name=> shixun_name)
end
end
normal_status("创建成功")
rescue Exception => e
uid_logger_error(e.message)
tip_exception("试卷问题创建失败!")
@ -185,7 +186,6 @@ class ExerciseBankQuestionsController < ApplicationController
if @exercise_question.question_type <= Exercise::JUDGMENT #选择题/判断题,标准答案为一个或多个
exercise_standard_choices = @exercise_answers_array.pluck(:exercise_bank_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 # 传入的标准答案差集共同的,剩余的表示需要新建
@ -216,7 +216,6 @@ class ExerciseBankQuestionsController < ApplicationController
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_bank_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
@ -300,7 +299,7 @@ class ExerciseBankQuestionsController < ApplicationController
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|
@exercise_question.exercise_bank_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)
@ -309,32 +308,63 @@ class ExerciseBankQuestionsController < ApplicationController
@exercise_question.shixun_name = shixun_name
@exercise_question.save
end
normal_status(0,"试卷更新成功")
rescue Exception => e
uid_logger_error(e.message)
tip_exception("页面调用失败!")
raise ActiveRecord::Rollback
end
end
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是否重新计算学生答题的成绩?")
def up_down
ActiveRecord::Base.transaction do
begin
opr = params[:opr]
current_q_p = @exercise_question.question_number.to_i #问题的当前位置
if opr.present?
if opr.to_s == "up"
last_q_p = @exercise.exercise_bank_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) # 重新获取当前问题的位置
normal_status(0, "问题上移成功!")
else
normal_status(-1, "移动失败,已经是第一个问题了!")
end
elsif opr.to_s == "down"
next_q_p = @exercise.exercise_bank_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)
normal_status(0, "问题下移成功!")
else
normal_status(-1, "移动失败,已经是最后一个问题了!")
end
end
else
normal_status(0,"试卷更新成功!")
normal_status(-1, "移动失败,请输入参数")
end
rescue Exception => e
uid_logger_error(e.message)
tip_exception("问题移动失败!")
end
end
end
#试卷问题的删除
def destroy
ActiveRecord::Base.transaction do
begin
question_d_id = @exercise_question.question_number.to_i #问题的当前位置
exercise_questions = @exercise.exercise_bank_questions
left_questions = exercise_questions.where("question_number > ?", question_d_id)
left_questions.update_all("question_number = question_number - 1") if left_questions
@exercise_question.destroy
normal_status(0, "删除成功")
rescue Exception => e
uid_logger_error(e.message)
tip_exception("页面调用失败!")
raise ActiveRecord::Rollback
tip_exception("删除失败")
end
end
end

@ -1,8 +1,9 @@
#encoding: UTF-8
class ExerciseBanksController < ApplicationController
before_action :require_login
before_action :find_bank
before_action :find_bank, except: [:choose_shixun]
before_action :bank_admin, only: [:update]
before_action :commit_shixun_present, only: [:commit_shixun]
def show
@exercise_questions = @bank.exercise_bank_questions&.includes(:exercise_bank_choices, :exercise_bank_shixun_challenges,
@ -47,6 +48,38 @@ class ExerciseBanksController < ApplicationController
normal_status(0,"试卷更新成功!")
end
def choose_shixun
search = params[:search]
if !current_user.admin? #当不为管理员的时候
user_school_id = current_user.school_id #当前用户的学校id
if user_school_id.present?
none_shixun_ids = ShixunSchool.where("school_id != #{user_school_id}").pluck(:shixun_id)
@publish_shixuns = Shixun.where.not(id: none_shixun_ids).unhidden
end
else
@publish_shixuns = Shixun.unhidden
end
if search.present?
@publish_shixuns = @publish_shixuns.search_by_name(search)
end
@shixuns = @publish_shixuns.joins(:challenges).where("challenges.st != 0").distinct
# 全部页面,需返回
@shixuns_count = @shixuns.count
# 分页
@page = params[:page] || 1
@limit = params[:limit] || 8
@shixuns = @shixuns.page(@page).per(@limit)
end
#确认实训的选择
def commit_shixun
@shixun_challenges = @shixun.challenges
@shixun_challenges_count = @shixun_challenges.size
end
private
def find_bank
@ -57,4 +90,17 @@ class ExerciseBanksController < ApplicationController
def bank_admin
tip_exception(403, "无权限") unless (current_user.certification_teacher? && @bank.user_id == current_user.id) || current_user.admin?
end
#判断实训是否已选择
def commit_shixun_present
question_shixun_ids = @exercise.exercise_bank_questions.pluck(:shixun_id).reject(&:blank?)
shixun_id = params[:shixun_id]
@shixun = Shixun.find_by(id: shixun_id)
if shixun_id.present? && question_shixun_ids.include?(shixun_id)
normal_status(-1,"该实训已选择!")
elsif @shixun.blank?
normal_status(-1,"该实训不存在!")
end
end
end

@ -8,7 +8,7 @@ class Users::QuestionBanksController < Users::BaseController
@count = question_banks.count
@course_lists = service.course_lists
@question_banks = paginate(question_banks.includes(:user, :course_list), special: true)
@question_banks = paginate(question_banks.includes(:user, :course_list))
load_question_banks_solve_count # for solve n + 1
end

@ -0,0 +1,10 @@
json.shixun_counts @shixuns_count
json.shixuns do
json.array! @shixuns do |s|
json.shixun_id s.id
json.shixun_name s.name
json.shixun_user s.user.real_name
json.shixun_user_count s.myshixuns_count
end
end

@ -0,0 +1,10 @@
json.shixun_id @shixun.id
json.shixun_name @shixun.name
json.challenges do
json.array! @shixun_challenges do |s|
json.challenge_name s.subject
end
end
json.challenge_counts @shixun_challenges_count

@ -643,7 +643,23 @@ Rails.application.routes.draw do
resources :gtopic_banks
resources :task_banks
resources :exercise_banks
resources :exercise_banks do
collection do
get :choose_shixun
end
member do
get :commit_shixun
end
end
resources :exercise_bank_questions do
member do
post :up_down
get :choose_shixun
end
end
resources :attachments

@ -0,0 +1,5 @@
class MigrateGtopicBankIsPublic < ActiveRecord::Migration[5.2]
def change
change_column :gtopic_banks, :is_public, :boolean, default: 0
end
end
Loading…
Cancel
Save