From 218f5f02621559506ca294d4441fefa11e8af3ab Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Thu, 2 Jan 2020 18:55:48 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=95=E5=8D=B7=E7=9A=84=E6=9F=A5=E7=9C=8B?= =?UTF-8?q?=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../examination_banks_controller.rb | 10 +++- app/models/examination_item.rb | 17 +++++++ app/models/examination_item_analysis.rb | 3 ++ .../examination_banks/show.json.jbuilder | 51 +++++++++++++++++++ ...103810_create_examination_item_analyses.rb | 10 ++++ spec/models/examination_item_analysis_spec.rb | 5 ++ 6 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 app/models/examination_item_analysis.rb create mode 100644 db/migrate/20200102103810_create_examination_item_analyses.rb create mode 100644 spec/models/examination_item_analysis_spec.rb diff --git a/app/controllers/examination_banks_controller.rb b/app/controllers/examination_banks_controller.rb index 15e567d62..e5e9ca4ca 100644 --- a/app/controllers/examination_banks_controller.rb +++ b/app/controllers/examination_banks_controller.rb @@ -11,7 +11,11 @@ class ExaminationBanksController < ApplicationController end def show - + @items = @exam.examination_items + @single_questions = @items.where(item_type: "SINGLE") + @multiple_questions = @items.where(item_type: "MULTIPLE") + @judgement_questions = @items.where(item_type: "JUDGMENT") + @program_questions = @items.where(item_type: "PROGRAM") end def create @@ -28,10 +32,12 @@ class ExaminationBanksController < ApplicationController difficulty: item.difficulty, score: basket.score, position: basket.position) new_item.save! item.increment!(:quotes) + if item.item_type == "PROGRAM" new_hack = item.container.fork new_item.update_attributes!(container: new_hack) - elsif item.item_choices.present? + else + new_item.examination_item_analysis.create!(analysis: item.analysis) item.item_choices.each do |choice| new_item.examination_item_choices << ExaminationItemChoice.new(choice_text: choice.choice_text, is_answer: choice.is_answer) end diff --git a/app/models/examination_item.rb b/app/models/examination_item.rb index f26f15b57..0cc515cae 100644 --- a/app/models/examination_item.rb +++ b/app/models/examination_item.rb @@ -5,6 +5,23 @@ class ExaminationItem < ApplicationRecord belongs_to :item_bank, optional: true has_many :examination_item_choices, dependent: :destroy + has_one :examination_item_analysis, dependent: :destroy belongs_to :container, polymorphic: true, optional: true + def analysis + examination_item_analysis&.analysis + end + + def item_choices + examination_item_choices + end + + def public + 0 + end + + def quotes + 0 + end + end diff --git a/app/models/examination_item_analysis.rb b/app/models/examination_item_analysis.rb new file mode 100644 index 000000000..e96ab769f --- /dev/null +++ b/app/models/examination_item_analysis.rb @@ -0,0 +1,3 @@ +class ExaminationItemAnalysis < ApplicationRecord + belongs_to :examination_item +end diff --git a/app/views/examination_banks/show.json.jbuilder b/app/views/examination_banks/show.json.jbuilder index e69de29bb..2a2da9af9 100644 --- a/app/views/examination_banks/show.json.jbuilder +++ b/app/views/examination_banks/show.json.jbuilder @@ -0,0 +1,51 @@ +json.single_questions do + json.questions @single_questions.each do |question| + json.(question, :id, :position, :score, :item_type) + json.partial! "item_banks/item", locals: {item: question} + end + json.questions_score @single_questions.map(&:score).sum + json.questions_count @single_questions.size +end + +json.multiple_questions do + json.questions @multiple_questions.each do |question| + json.(question, :id, :position, :score, :item_type) + json.partial! "item_banks/item", locals: {item: question} + end + json.questions_score @multiple_questions.map(&:score).sum + json.questions_count @multiple_questions.size +end + +json.judgement_questions do + json.questions @judgement_questions.each do |question| + json.(question, :id, :position, :score, :item_type) + json.partial! "item_banks/item", locals: {item: question} + end + json.questions_score @judgement_questions.map(&:score).sum + json.questions_count @judgement_questions.size +end + +json.program_questions do + json.questions @program_questions.each do |question| + json.(question, :id, :position, :score, :item_type) + json.partial! "item_banks/item", locals: {item: question} + end + json.questions_score @program_questions.map(&:score).sum + json.questions_count @program_questions.size +end + +json.exam do + json.(@exam, :id, :name, :difficulty, :sub_discipline_id, :duration) + json.all_score @items.map(&:score).sum + json.all_questions_count @items.size + + json.discipline do + json.(@exam.sub_discipline&.discipline, :id, :name) + end + json.sub_discipline do + json.(@exam.sub_discipline, :id, :name) + end + json.tag_disciplines @exam.tag_disciplines do |tag| + json.(tag, :id, :name) + end +end \ No newline at end of file diff --git a/db/migrate/20200102103810_create_examination_item_analyses.rb b/db/migrate/20200102103810_create_examination_item_analyses.rb new file mode 100644 index 000000000..eb33e8efc --- /dev/null +++ b/db/migrate/20200102103810_create_examination_item_analyses.rb @@ -0,0 +1,10 @@ +class CreateExaminationItemAnalyses < ActiveRecord::Migration[5.2] + def change + create_table :examination_item_analyses do |t| + t.references :examination_item, index: true, unique: true + t.text :analysis + + t.timestamps + end + end +end diff --git a/spec/models/examination_item_analysis_spec.rb b/spec/models/examination_item_analysis_spec.rb new file mode 100644 index 000000000..2291c099c --- /dev/null +++ b/spec/models/examination_item_analysis_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe ExaminationItemAnalysis, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end