Newer
Older
dmpopidor / app / controllers / phases_controller.rb
class PhasesController < ApplicationController
  require 'pp'

  after_action :verify_authorized


  # GET /plans/:plan_id/phases/:id/edit
  def edit
    plan = Plan.load_for_phase(params[:plan_id], params[:id])

    # authorization done on plan so found in plan_policy
    authorize plan

    phase_id = params[:id].to_i
    phase = plan.template.phases.select {|p| p.id == phase_id}.first
    readonly = !plan.editable_by?(current_user.id)

    # Now we need to get all the themed guidance for the plan.
    # TODO: think this through again, there may be a better way to do this.
    #
    # Ultimately we are heading to a map from question id to theme to guidance.
    #
    # get the ids of the dynamically selected guidance groups
    # and keep a map of them so we can extract the names later
    guidance_groups_ids = plan.guidance_groups.map{|pgg| pgg.id}
    guidance_groups =  GuidanceGroup.includes({guidances: :themes}).where(published: true, id: guidance_groups_ids)

    # create a map from theme to array of guidances
    # where guidance is a hash with the text and the org name
    theme_guidance = {}

    guidance_groups.includes(guidances:[:themes]).each do |guidance_group|
      guidance_group.guidances.each do |guidance|
        if guidance.published
          guidance.themes.each do |theme|
            title = theme.title
            if !theme_guidance.has_key?(title)
              theme_guidance[title] = Array.new
            end
            theme_guidance[title] << {
              text: guidance.text,
              org: guidance_group.name + ':'
            }
          end
        end
      end
    end

    # create hash from question id to theme to guidance array
    # so when we arerendering a question we can grab the guidance out of this
    #
    # question_guidance = {
    #              question.id => {
    #                      theme => [ {text: "......", org: "....."} ]
    #              }
    # }
    question_guidance = {}
    plan.questions.each do |question|
      qg = {}
      question.themes.each do |t|
        title = t.title
        qg[title] = theme_guidance[title] if theme_guidance.has_key?(title)
      end
      if !question_guidance.has_key?(question.id)
        question_guidance[question.id] = Array.new
      end
      question_guidance[question.id] = qg
    end

    if !user_signed_in? then
      respond_to do |format|
        format.html { redirect_to edit_user_registration_path }
      end
    else 
      render('/phases/edit', locals: { plan: plan, phase: phase, readonly: readonly, question_guidance: question_guidance })
    end
  end


    # GET /plans/PLANID/phases/PHASEID/status.json
  def status
    @plan = Plan.eager_load(params[:plan_id])
    authorize @plan
    if user_signed_in? && @plan.readable_by?(current_user.id) then
      respond_to do |format|
        format.json { render json: @plan.status }
      end
    else
      render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false)
    end
  end

  #show and edit a phase of the template
  def admin_show
    @phase = Phase.includes(:sections).order(:number).find(params[:id])
    authorize @phase

    @current = Template.current(@phase.template.dmptemplate_id)
    @edit = (@phase.template.org == current_user.org) && (@phase.template == @current)

    if params.has_key?(:question_id)
      @question_id = params[:question_id].to_i
    end
    if @phase.template.customization_of.present?
      @original_org = Template.where(dmptemplate_id: @phase.template.customization_of).first.org
    else
      @original_org = @phase.template.org
    end
    render('/templates/container',
      locals: {
        partial_path: 'admin_show',
        phase: @phase,
        template: @phase.template,
        edit: @edit,
        current_section: params.has_key?(:section_id) ? params[:section_id].to_i : nil
      })
  end


  #preview a phase
  def admin_preview
    @phase = Phase.find(params[:id])
    authorize @phase
    @template = @phase.template
  end


  #add a new phase to a passed template
  def admin_add
    @template = Template.find(params[:id])
    @phase = Phase.new
    @phase.template = @template
    authorize @phase
    @phase.number = @template.phases.count + 1
    render('/templates/container',
      locals: {
        partial_path: 'admin_add',
        template: @template
      })
  end


  #create a phase
  def admin_create
    @phase = Phase.new(params[:phase])
    authorize @phase

    @phase.description = params["phase-desc"]
    @phase.modifiable = true
    if @phase.save
      @phase.template.dirty = true
      @phase.template.save!

      redirect_to admin_show_phase_path(id: @phase.id), notice: success_message(_('phase'), _('created'))
    else
      flash[:alert] = failed_create_error(@phase, _('phase'))
      @template = @phase.template
      redirect_to admin_template_template_path(id: @phase.template_id)
    end
  end


  #update a phase of a template
  def admin_update
    @phase = Phase.find(params[:id])
    authorize @phase
    @phase.description = params["phase-desc"]
    if @phase.update_attributes(params[:phase])
      @phase.template.dirty = true
      @phase.template.save!

      redirect_to admin_show_phase_path(@phase), notice: success_message(_('phase'), _('saved'))
    else
      @sections = @phase.sections
      @template = @phase.template
      # These params may not be available in this context so they may need
      # to be set to true without the check
      @edit = true
      @open = !params[:section_id].nil?
      @section_id = (params[:section_id].nil? ? nil : params[:section_id].to_i)
      @question_id = (params[:question_id].nil? ? nil : params[:question_id].to_i)
      flash[:alert] = failed_update_error(@phase, _('phase'))
      if @phase.template.customization_of.present?
        @original_org = Template.where(dmptemplate_id: @phase.template.customization_of).first.org
      else
        @original_org = @phase.template.org
      end
      redirect_to admin_show_phase_path(@phase)
    end
  end

  #delete a phase
  def admin_destroy
    @phase = Phase.find(params[:phase_id])
    authorize @phase
    @template = @phase.template
    if @phase.destroy
      @template.dirty = true
      @template.save!

      redirect_to admin_template_template_path(@template), notice: success_message(_('phase'), _('deleted'))
    else
      @sections = @phase.sections

      # These params may not be available in this context so they may need
      # to be set to true without the check
      @edit = true
      @open = !params[:section_id].nil?
      @section_id = (params[:section_id].nil? ? nil : params[:section_id].to_i)
      @question_id = (params[:question_id].nil? ? nil : params[:question_id].to_i)
      flash[:alert] = failed_destroy_error(@phase, _('phase'))
      if @phase.template.customization_of.present?
        @original_org = Template.where(dmptemplate_id: @phase.template.customization_of).first.org
      else
        @original_org = @phase.template.org
      end
      render 'admin_show'
    end
  end

end