From 51fe553ff9731449199dfecfdbe98ff24a8a03ba Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Wed, 28 Aug 2019 15:29:41 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=95=E5=8D=B7=E9=A2=98=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exercise_bank_questions_controller.rb | 2 + app/controllers/exercise_banks_controller.rb | 1 + app/helpers/exercise_bank_questions_helper.rb | 2 + app/models/exercise_bank_standard_answer.rb | 1 + .../_exercise_bank_questions.json.jbuilder | 69 +++++++++++++++++++ app/views/exercise_banks/show.json.jbuilder | 5 +- ...dd_is_ordered_to_exercise_bank_question.rb | 5 ++ 7 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 app/controllers/exercise_bank_questions_controller.rb create mode 100644 app/helpers/exercise_bank_questions_helper.rb create mode 100644 app/views/exercise_bank_questions/_exercise_bank_questions.json.jbuilder create mode 100644 db/migrate/20190828055710_add_is_ordered_to_exercise_bank_question.rb diff --git a/app/controllers/exercise_bank_questions_controller.rb b/app/controllers/exercise_bank_questions_controller.rb new file mode 100644 index 000000000..c7babb949 --- /dev/null +++ b/app/controllers/exercise_bank_questions_controller.rb @@ -0,0 +1,2 @@ +class ExerciseBankQuestionsController < ApplicationController +end diff --git a/app/controllers/exercise_banks_controller.rb b/app/controllers/exercise_banks_controller.rb index c51688713..e3d9718b3 100644 --- a/app/controllers/exercise_banks_controller.rb +++ b/app/controllers/exercise_banks_controller.rb @@ -1,3 +1,4 @@ +#encoding: UTF-8 class ExerciseBanksController < ApplicationController before_action :require_login before_action :find_bank diff --git a/app/helpers/exercise_bank_questions_helper.rb b/app/helpers/exercise_bank_questions_helper.rb new file mode 100644 index 000000000..10df0f5b3 --- /dev/null +++ b/app/helpers/exercise_bank_questions_helper.rb @@ -0,0 +1,2 @@ +module ExerciseBankQuestionsHelper +end diff --git a/app/models/exercise_bank_standard_answer.rb b/app/models/exercise_bank_standard_answer.rb index 9cd82afe6..2535473b0 100644 --- a/app/models/exercise_bank_standard_answer.rb +++ b/app/models/exercise_bank_standard_answer.rb @@ -2,4 +2,5 @@ class ExerciseBankStandardAnswer < ApplicationRecord belongs_to :exercise_bank_question belongs_to :exercise_bank_choice #attr_accessible :answer_text + scope :standard_by_ids, lambda { |s| where(exercise_bank_choice_id: s) } end \ No newline at end of file diff --git a/app/views/exercise_bank_questions/_exercise_bank_questions.json.jbuilder b/app/views/exercise_bank_questions/_exercise_bank_questions.json.jbuilder new file mode 100644 index 000000000..a77b8a975 --- /dev/null +++ b/app/views/exercise_bank_questions/_exercise_bank_questions.json.jbuilder @@ -0,0 +1,69 @@ +json.question_id question.id +q_positon = question.question_number +if ques_position.present? + q_positon = ques_position +end +json.q_position q_positon +json.question_title question.question_title +json.question_type question.question_type +json.question_score question.question_score.round(1).to_s +if question.question_type <= 2 #当为选择题或判断题时,只显示选项的位置 + standard_answers_array = question.get_standard_answer_ids + exercise_choices = choices.order("choice_position ASC") + json.question_choices exercise_choices.each do |a| + #TODO: 旧版本来一个题只有一个标准答案的,新版又做成了一个题有多个标准答案(exercise_choice_id存放的是标准答案的位置..) + standard_answer_b = standard_answers_array.join("").include?(a.choice_position.to_s) + choice_text = a.choice_text + if question.question_type == 2 + if choice_text == "对" + choice_text = "正确" + end + if choice_text == "错" + choice_text = "错误" + end + end + json.choice_id a.id + # json.choice_text (edit_type.present? || question.question_type == 2) ? a.choice_text : "#{(index+65).chr}.#{a.choice_text}" + + json.choice_text choice_text + json.choice_position a.choice_position + json.standard_boolean standard_answer_b + end + + json.standard_answer standard_answers_array + if question.question_type == 2 #返回答案的文字 + standard_text = standard_answers_array.first.to_i == 1 ? "正确" : "错误" + else + array_text_answer = [] + standard_answers_array.each{|a| array_text_answer.push((a+64).chr)} + standard_text = array_text_answer.join("") + end + json.standard_answer_show standard_text + +elsif question.question_type == 3 #当为填空题时 + standard_answers_count = question.exercise_bank_standard_answers + standard_answer_ex = standard_answers_count.pluck(:exercise_bank_choice_id).uniq + json.is_ordered question.is_ordered + json.multi_count standard_answer_ex.size #有几个填空 + json.standard_answer do + json.array! standard_answer_ex.each do |a| + s_answer_text = standard_answers_count.standard_by_ids(a).pluck(:answer_text) + json.choice_id a + json.answer_text s_answer_text + end + end +elsif question.question_type == 4 #简答题时 + json.standard_answer question.exercise_bank_standard_answers.pluck(:answer_text) +elsif question.question_type == 5 + json.shixun_id question.shixun_id + json.shixun_name question.shixun_name.present? ? question.shixun_name : "" + json.shixun_identifier question.shixun.identifier + json.shixun do + json.array! shixun_challenges do |s| + json.challenge_id s.challenge_id + json.challenge_position s.position + json.challenge_name s.challenge.subject + json.challenge_score s.question_score.round(1).to_s + end + 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 index 773d7bf9f..1b2a931fa 100644 --- a/app/views/exercise_banks/show.json.jbuilder +++ b/app/views/exercise_banks/show.json.jbuilder @@ -6,13 +6,10 @@ json.partial! "exercises/exercise_scores" json.exercise_questions do json.array! @exercise_questions do |q| - json.partial! "exercise_questions/exercise_questions", + json.partial! "exercise_bank_questions/exercise_bank_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 diff --git a/db/migrate/20190828055710_add_is_ordered_to_exercise_bank_question.rb b/db/migrate/20190828055710_add_is_ordered_to_exercise_bank_question.rb new file mode 100644 index 000000000..967d89e5f --- /dev/null +++ b/db/migrate/20190828055710_add_is_ordered_to_exercise_bank_question.rb @@ -0,0 +1,5 @@ +class AddIsOrderedToExerciseBankQuestion < ActiveRecord::Migration[5.2] + def change + add_column :exercise_bank_questions, :is_ordered, :boolean, :default => true + end +end