diff --git a/app/controllers/exercise_banks_controller.rb b/app/controllers/exercise_banks_controller.rb new file mode 100644 index 000000000..c51688713 --- /dev/null +++ b/app/controllers/exercise_banks_controller.rb @@ -0,0 +1,71 @@ +class ExerciseBanksController < ApplicationController + before_action :require_login + before_action :find_bank + before_action :bank_admin, only: [:update] + + def show + @exercise_questions = @bank.exercise_bank_questions&.includes(:exercise_bank_choices, :exercise_bank_shixun_challenges, + :exercise_bank_standard_answers).order("question_number ASC") + @exercise_ques_count = @exercise_questions.size # 全部的题目数 + @exercise_ques_scores = @exercise_questions.pluck(:question_score).sum + + #单选题的数量及分数 + exercise_single_ques = @exercise_questions.find_by_custom("question_type", Exercise::SINGLE) + @exercise_single_ques_count = exercise_single_ques.size + @exercise_single_ques_scores = exercise_single_ques.pluck(:question_score).sum + + #多选题的数量及分数 + exercise_double_ques = @exercise_questions.find_by_custom("question_type", Exercise::MULTIPLE) + @exercise_double_ques_count = exercise_double_ques.size + @exercise_double_ques_scores = exercise_double_ques.pluck(:question_score).sum + + # 判断题数量及分数 + exercise_ques_judge = @exercise_questions.find_by_custom("question_type", Exercise::JUDGMENT) + @exercise_ques_judge_count = exercise_ques_judge.size + @exercise_ques_judge_scores = exercise_ques_judge.pluck(:question_score).sum + + #填空题数量及分数 + exercise_ques_null = @exercise_questions.find_by_custom("question_type", Exercise::COMPLETION) + @exercise_ques_null_count = exercise_ques_null.size + @exercise_ques_null_scores = exercise_ques_null.pluck(:question_score).sum + + #简答题数量及分数 + exercise_ques_main = @exercise_questions.find_by_custom("question_type", Exercise::SUBJECTIVE) + @exercise_ques_main_count = exercise_ques_main.size + @exercise_ques_main_scores = exercise_ques_main.pluck(:question_score).sum + + #实训题数量及分数 + exercise_ques_shixun = @exercise_questions.find_by_custom("question_type", Exercise::PRACTICAL) + @exercise_ques_shixun_count = exercise_ques_shixun.size + @exercise_ques_shixun_scores = exercise_ques_shixun.pluck(:question_score).sum + end + + def update + + end + + private + + def find_bank + @bank = ExerciseBank.find_by!(id: params[:id]) + tip_exception(403, "无权限") unless (current_user.certification_teacher? && (@bank.is_public || @bank.user_id == current_user.id)) || current_user.admin? + end + + def bank_admin + tip_exception(403, "无权限") unless (current_user.certification_teacher? && @bank.user_id == current_user.id) || current_user.admin? + end + + def gtask_bank_params + tip_exception("name参数不能为空") if params[:gtask_bank][:name].blank? + tip_exception("description参数不能为空") if params[:gtask_bank][:description].blank? + if @bank.homework_type == 3 + tip_exception("base_on_project参数不能为空") if params[:gtask_bank][:base_on_project].nil? + tip_exception("min_num参数不能为空") if params[:gtask_bank][:min_num].blank? + tip_exception("max_num参数不能为空") if params[:gtask_bank][:max_num].blank? + tip_exception("最小人数不能小于1") if params[:gtask_bank][:min_num].to_i < 1 + tip_exception("最大人数不能小于最小人数") if params[:gtask_bank][:max_num].to_i < params[:gtask_bank][:min_num].to_i + end + params.require(:gtask_bank).permit(:name, :description) if @bank.task_type == 1 + params.require(:gtask_bank).permit(:name, :description, :min_num, :max_num, :base_on_project) if @bank.task_type == 2 + end +end diff --git a/app/models/exercise_bank_question.rb b/app/models/exercise_bank_question.rb index 5a39fd5d2..af533836b 100644 --- a/app/models/exercise_bank_question.rb +++ b/app/models/exercise_bank_question.rb @@ -5,13 +5,16 @@ class ExerciseBankQuestion < ApplicationRecord has_many :exercise_bank_choices, :dependent => :destroy has_many :exercise_bank_standard_answers, :dependent => :destroy #attr_accessible :question_number, :question_score, :question_title, :question_type + scope :find_by_custom, lambda {|k,v| where("#{k} = ?",v)} #根据传入的参数查找问题 def question_type_name case self.question_type - when 1 + when 0 "单选题" - when 2 + when 1 "多选题" + when 2 + "判断题" when 3 "填空题" when 4 @@ -20,4 +23,14 @@ class ExerciseBankQuestion < ApplicationRecord "实训题" end end + + + #获取问题的全部标准答案 + def get_standard_answer_ids + exercise_bank_standard_answers.pluck(:exercise_bank_choice_id) + end + + def get_standard_answer_text + exercise_bank_standard_answers.pluck(:answer_text) + end end \ No newline at end of file diff --git a/app/views/exercise_banks/show.json.jbuilder b/app/views/exercise_banks/show.json.jbuilder new file mode 100644 index 000000000..773d7bf9f --- /dev/null +++ b/app/views/exercise_banks/show.json.jbuilder @@ -0,0 +1,19 @@ +json.exercise do + json.extract! @bank, :id, :name, :description, :is_public +end + +json.partial! "exercises/exercise_scores" + +json.exercise_questions do + json.array! @exercise_questions do |q| + json.partial! "exercise_questions/exercise_questions", + question: q, + choices:q.exercise_bank_choices, + shixun_challenges: q.exercise_bank_shixun_challenges, + exercise_type:1, + user_answer:[], + shixun_type:0, + ques_position:nil, + edit_type:nil + end +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 3bed4265e..dcce1976b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -643,6 +643,7 @@ Rails.application.routes.draw do resources :gtopic_banks resources :task_banks + resources :exercise_banks resources :attachments