diff --git a/app/controllers/phases_controller.rb b/app/controllers/phases_controller.rb index aae87b7..1fce879 100644 --- a/app/controllers/phases_controller.rb +++ b/app/controllers/phases_controller.rb @@ -16,8 +16,7 @@ end # authorization done on plan so found in plan_policy authorize @plan - - phase_id = params[:id].to_i + @answers = @plan.answers.reduce({}){ |m, a| m[a.question_id] = a; m } @readonly = !@plan.editable_by?(current_user.id) # Now we need to get all the themed guidance for the plan. diff --git a/app/models/plan.rb b/app/models/plan.rb index d095217..a71bcff 100644 --- a/app/models/plan.rb +++ b/app/models/plan.rb @@ -953,13 +953,12 @@ # Returns the number of answered questions from the entire plan def num_answered_questions - n = 0 - self.template.phases.each do |p| - p.sections.each do |s| - n+= s.num_answered_questions(self.id) + return Answer.where(id: answers.map(&:id)).includes({question: :question_format}, :question_options).reduce(0) do |m, a| + if a.is_valid? + m+=1 end + m end - return n end # Returns a section given its id or nil if does not exist for the current plan @@ -971,8 +970,8 @@ # for when sections and their questions are eager loaded so that avoids SQL queries. def num_questions n = 0 - self.sections.each do |s| - n+= s.questions.size() + sections.includes(:questions).joins(:questions).each do |s| + n+= s.questions.length end return n end @@ -993,20 +992,9 @@ end def self.load_for_phase(id, phase_id) - plan = Plan.includes(template: {phases: {sections: {questions: :answers}}}).joins(template: {phases: {sections: {questions: :answers}}}).where("phases.id = #{phase_id}").distinct.merge( Plan.where("phases.id=#{phase_id}").joins(:phases).includes({answers: :notes})).find_by(id: id) - phase = nil - # return nil for both if plan does not exist - if plan.blank? - return nil, nil - end - plan.template.phases.each do |p| - next unless p.id = phase_id - phase = p - break - end + plan = Plan.joins(template: {phases: {sections: :questions}}).where("plans.id = #{id} AND phases.id = #{phase_id} ").includes(template: {phases: {sections: :questions}}).merge(Plan.includes(answers: :notes))[0] + phase = plan.template.phases.first return plan, phase - - end diff --git a/app/models/section.rb b/app/models/section.rb index 4d4562b..18a3420 100644 --- a/app/models/section.rb +++ b/app/models/section.rb @@ -24,14 +24,15 @@ end # Returns the number of answered questions for a given plan id - def num_answered_questions(plan_id) - n = 0 - self.questions.each do |question| - n += question.plan_answers(plan_id).select{|answer| answer.is_valid?}.count + def num_answered_questions(plan) + questions_hash = questions.reduce({}){ |m, q| m[q.id] = q; m } + return plan.answers.includes({question: :question_format}, :question_options).reduce(0) do |m, a| + if questions_hash[a.question_id].present? && a.is_valid? + m+=1 + end + m end - return n end - ## # deep copy of the given section and all it's associations # diff --git a/app/views/phases/_answer_form.html.erb b/app/views/phases/_answer_form.html.erb index c95732b..566b757 100644 --- a/app/views/phases/_answer_form.html.erb +++ b/app/views/phases/_answer_form.html.erb @@ -1,4 +1,4 @@ - - +
- <% comments = answer.notes.all %> + <% comments = answer.notes.all %> <%= hidden_field_tag :question_id, question.id, class: "question_id" %> <% active_tab = nil %>