# class DmptemplatesController

`Project:`

  DMPonline v4

`Description:`

  This controller is responsible for all the actions in the admin interface under templates (e.g. phases, versions, sections, questions, suggested answer) (index; show; create; edit; delete)

`Copyright:`

  Digital Curation Centre

### Public Instance Methods

admin_addphase() click to toggle source

add a new phase to a template

    # File app/controllers/dmptemplates_controller.rb, line 187
    def admin_addphase
            if user_signed_in? && current_user.can_org_admin? then
                    @dmptemplate = Dmptemplate.find(params[:id])
                    @phase = Phase.new
                    if @dmptemplate.phases.count == 0 then
                            @phase.number = '1'
                    else
                            @phase.number = @dmptemplate.phases.count + 1
                    end

                    respond_to do |format|
          format.html
        end
            end
    end

admin_cloneversion() click to toggle source

clone a version of a template

    # File app/controllers/dmptemplates_controller.rb, line 293
    def admin_cloneversion
            if user_signed_in? && current_user.can_org_admin? then
    @old_version = Version.find(params[:version_id])
                    @version = @old_version.amoeba_dup
                    @phase = @version.phase

        respond_to do |format|
          if @version.save
            format.html { redirect_to admin_phase_dmptemplate_path(@phase, :version_id => @version.id, :edit => 'true'), notice: I18n.t('org_admin.templates.updated_message') }
            format.json { head :no_content }
          else
            format.html { render action: "admin_phase" }
            format.json { render json: @version.errors, status: :unprocessable_entity }
          end
        end
            end
    end

admin_create() click to toggle source

POST /dmptemplates POST /dmptemplates.json

    # File app/controllers/dmptemplates_controller.rb, line 81
    def admin_create
      if user_signed_in? && current_user.can_org_admin? then
              @dmptemplate = Dmptemplate.new(params[:dmptemplate])
              @dmptemplate.organisation_id = current_user.organisation.id
              @dmptemplate.description = params['template-desc']

              respond_to do |format|
                if @dmptemplate.save
                  format.html { redirect_to admin_template_dmptemplate_path(@dmptemplate), notice: I18n.t('org_admin.templates.created_message') }
                  format.json { render json: @dmptemplate, status: :created, location: @dmptemplate }
                else
                  format.html { render action: "admin_new" }
                  format.json { render json: @dmptemplate.errors, status: :unprocessable_entity }
                end
              end
      else
                          render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false)
                  end
    end

admin_createphase() click to toggle source

create a phase

    # File app/controllers/dmptemplates_controller.rb, line 204
          def admin_createphase
      if user_signed_in? && current_user.can_org_admin? then
                 @phase = Phase.new(params[:phase])
              @phase.description = params["phase-desc"]
              @version = @phase.versions.build
              @version.title = "#{@phase.title} v.1"
              @version.phase_id = @phase.id
              @version.number = 1
              @version.published = false

              respond_to do |format|
                if @phase.save
                  format.html { redirect_to admin_phase_dmptemplate_path(:id => @phase.id, :version_id => @version.id, :edit => 'true'), notice: I18n.t('org_admin.templates.created_message') }
                  format.json { head :no_content }
                else
                  format.html { render action: "admin_phase" }
                  format.json { render json: @phase.errors, status: :unprocessable_entity }
                end
                          end
                  end
    end

admin_createquestion() click to toggle source

create a question

    # File app/controllers/dmptemplates_controller.rb, line 386
          def admin_createquestion
      if user_signed_in? && current_user.can_org_admin? then
                 @question = Question.new(params[:question])
              @question.guidance = params["new-question-guidance"]
              @question.default_value = params["new-question-default-value"]

              respond_to do |format|
                if @question.save
                  format.html { redirect_to admin_phase_dmptemplate_path(:id => @question.section.version.phase_id, :version_id => @question.section.version_id, :section_id => @question.section_id, :question_id => @question.id, :edit => 'true'), notice: I18n.t('org_admin.templates.created_message') }
                  format.json { head :no_content }
                else
                  format.html { render action: "admin_phase" }
                  format.json { render json: @question.errors, status: :unprocessable_entity }
                end
                          end
                  end
    end

admin_createsection() click to toggle source

create a section

    # File app/controllers/dmptemplates_controller.rb, line 328
          def admin_createsection
      if user_signed_in? && current_user.can_org_admin? then
                 @section = Section.new(params[:section])
              @section.description = params["section-desc"]

              respond_to do |format|
                if @section.save
                  format.html { redirect_to admin_phase_dmptemplate_path(:id => @section.version.phase_id, :version_id => @section.version_id, :section_id => @section.id, :edit => 'true'), notice: I18n.t('org_admin.templates.created_message') }
                  format.json { head :no_content }
                else
                  format.html { render action: "admin_phase" }
                  format.json { render json: @section.errors, status: :unprocessable_entity }
                end
                          end
                  end
    end

admin_createsuggestedanswer() click to toggle source

SUGGESTED ANSWERS create suggested answers

    # File app/controllers/dmptemplates_controller.rb, line 446
       def admin_createsuggestedanswer
       if user_signed_in? && current_user.can_org_admin? then
               @suggested_answer = SuggestedAnswer.new(params[:suggested_answer])

           respond_to do |format|
             if @suggested_answer.save
               format.html { redirect_to admin_phase_dmptemplate_path(:id => @suggested_answer.question.section.version.phase_id, :version_id => @suggested_answer.question.section.version_id, :section_id => @suggested_answer.question.section_id, :question_id => @suggested_answer.question.id, :edit => 'true'), notice: I18n.t('org_admin.templates.created_message') }
               format.json { head :no_content }
             else
               format.html { render action: "admin_phase" }
               format.json { render json: @suggested_answer.errors, status: :unprocessable_entity }
             end
               end
        end
    end

admin_destroy() click to toggle source

DELETE /dmptemplates/1 DELETE /dmptemplates/1.json

    # File app/controllers/dmptemplates_controller.rb, line 105
    def admin_destroy
          if user_signed_in? && current_user.can_org_admin? then
                 @dmptemplate = Dmptemplate.find(params[:id])
              @dmptemplate.destroy

              respond_to do |format|
                format.html { redirect_to admin_index_dmptemplate_path }
                format.json { head :no_content }
              end
                 else
                          render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false)
                  end
          end

admin_destroyphase() click to toggle source

delete a version, sections and questions

    # File app/controllers/dmptemplates_controller.rb, line 246
    def admin_destroyphase
    if user_signed_in? && current_user.can_org_admin? then
           @phase = Phase.find(params[:phase_id])
           @dmptemplate = @phase.dmptemplate
        @phase.destroy

        respond_to do |format|
          format.html { redirect_to admin_template_dmptemplate_path(@dmptemplate), notice: I18n.t('org_admin.templates.destroyed_message') }
          format.json { head :no_content }
        end
           end
    end

admin_destroyquestion() click to toggle source

delete a version, sections and questions

    # File app/controllers/dmptemplates_controller.rb, line 428
    def admin_destroyquestion
    if user_signed_in? && current_user.can_org_admin? then
           @question = Question.find(params[:question_id])
           @section = @question.section
                    @version = @section.version
           @phase = @version.phase
        @question.destroy

        respond_to do |format|
          format.html { redirect_to admin_phase_dmptemplate_path(:id => @phase.id, :version_id => @version.id, :section_id => @section.id, :edit => 'true'), notice: I18n.t('org_admin.templates.destroyed_message') }
          format.json { head :no_content }
        end
           end
    end

admin_destroysection() click to toggle source

delete a section and questions

    # File app/controllers/dmptemplates_controller.rb, line 368
    def admin_destroysection
    if user_signed_in? && current_user.can_org_admin? then
           @section = Section.find(params[:section_id])
           @version = @section.version
           @phase = @version.phase
        @section.destroy

        respond_to do |format|
          format.html { redirect_to admin_phase_dmptemplate_path(:id => @phase.id, :version_id => @version.id,  :edit => 'true' ), notice: I18n.t('org_admin.templates.destroyed_message') }
          format.json { head :no_content }
        end
           end
    end

admin_destroysuggestedanswer() click to toggle source

delete a suggested answer

    # File app/controllers/dmptemplates_controller.rb, line 484
    def admin_destroysuggestedanswer
    if user_signed_in? && current_user.can_org_admin? then
           @suggested_answer = SuggestedAnswer.find(params[:suggested_answer])
           @question = @suggested_answer.question
           @section = @question.section
                    @version = @section.version
           @phase = @version.phase
        @suggested_answer.destroy

        respond_to do |format|
          format.html { redirect_to admin_phase_dmptemplate_path(:id => @phase.id, :version_id => @version.id, :section_id => @section.id, :edit => 'true'), notice: I18n.t('org_admin.templates.destroyed_message') }
          format.json { head :no_content }
        end
           end
    end

admin_destroyversion() click to toggle source

delete a version, sections and questions

    # File app/controllers/dmptemplates_controller.rb, line 312
    def admin_destroyversion
    if user_signed_in? && current_user.can_org_admin? then
           @version = Version.find(params[:version_id])
           @phase = @version.phase
        @version.destroy

        respond_to do |format|
          format.html { redirect_to admin_phase_dmptemplate_path(@phase), notice: I18n.t('org_admin.templates.destroyed_message') }
          format.json { head :no_content }
        end
           end
    end

admin_index() click to toggle source

GET /dmptemplates GET /dmptemplates.json

    # File app/controllers/dmptemplates_controller.rb, line 9
    def admin_index
      if user_signed_in? && current_user.can_org_admin? then
          #institutional templates

              @dmptemplates_own = Dmptemplate.own_institutional_templates(current_user.organisation_id)

              #funders templates

              @dmptemplates_funders = Dmptemplate.funders_templates

       respond_to do |format|
                format.html # index.html.erb

             end
      else
                          render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false)
                  end
    end

admin_new() click to toggle source

GET /dmptemplates/new GET /dmptemplates/new.json

    # File app/controllers/dmptemplates_controller.rb, line 66
    def admin_new
      if user_signed_in? && current_user.can_org_admin? then
              @dmptemplate = Dmptemplate.new

              respond_to do |format|
                format.html # new.html.erb

                format.json { render json: @dmptemplate }
              end
      else
                          render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false)
                  end
    end

admin_phase() click to toggle source

show and edit a phase of the template

    # File app/controllers/dmptemplates_controller.rb, line 124
    def admin_phase
            if user_signed_in? && current_user.can_org_admin? then

                    @phase = Phase.find(params[:id])

                    if !params.has_key?(:version_id) then
                            @edit = 'false'
                            #check for the most recent published version, if none is available then return the most recent one

                            versions = @phase.versions.where('published = ?', true).order('updated_at DESC')
                            if versions.any?() then
                                    @version = versions.first
                            else
                                    @version = @phase.versions.order('updated_at DESC').first
                            end
                            # When the version_id is passed as an argument

                    else
                            @edit = params[:edit]
                            @version = Version.find(params[:version_id])
                    end

                    #verify if there are any sections if not create one

                    @sections = @version.sections
                    if !@sections.any?() || @sections.count == 0 then
                            @section = @version.sections.build
                            @section.title = ''
                            @section.version_id = params[:version_id]
                            @section.number = 1
                            @section.organisation_id = current_user.organisation.id
                            @section.published = true
                            @section.save
                    end

                    #verify if section_id has been passed, if so then open that section

                    if params.has_key?(:section_id) then
                            @open = true
                            @section_id = params[:section_id].to_i
                    end

                    if params.has_key?(:question_id) then
                            @question_id = params[:question_id].to_i
                    end

                    respond_to do |format|
                            format.html
                    end
            end
    end

admin_previewphase() click to toggle source

preview a phase

    # File app/controllers/dmptemplates_controller.rb, line 173
    def admin_previewphase
            if user_signed_in? && current_user.can_org_admin? then

                    @version = Version.find(params[:id])

                    respond_to do |format|
                            format.html
                    end
            end
    end

admin_template() click to toggle source

GET /dmptemplates/1 GET /dmptemplates/1.json

    # File app/controllers/dmptemplates_controller.rb, line 27
    def admin_template
      if user_signed_in? && current_user.can_org_admin? then
              @dmptemplate = Dmptemplate.find(params[:id])

              respond_to do |format|
                format.html # show.html.erb

                format.json { render json: @dmptemplate }
              end
      else
                          render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false)
                  end
    end

admin_update() click to toggle source

PUT /dmptemplates/1 PUT /dmptemplates/1.json

    # File app/controllers/dmptemplates_controller.rb, line 44
    def admin_update
          if user_signed_in? && current_user.can_org_admin? then
                  @dmptemplate = Dmptemplate.find(params[:id])
                  @dmptemplate.description = params["template-desc"]

                    respond_to do |format|
                if @dmptemplate.update_attributes(params[:dmptemplate])
                  format.html { redirect_to admin_template_dmptemplate_path(params[:dmptemplate]), notice: I18n.t('org_admin.templates.updated_message') }
                  format.json { head :no_content }
                else
                  format.html { render action: "edit" }
                  format.json { render json: @dmptemplate.errors, status: :unprocessable_entity }
                end
                 end
          else
                          render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false)
          end
    end

admin_updatephase() click to toggle source

update a phase of a template

    # File app/controllers/dmptemplates_controller.rb, line 228
    def admin_updatephase
            if user_signed_in? && current_user.can_org_admin? then
            @phase = Phase.find(params[:id])
            @phase.description = params["phase-desc"]

        respond_to do |format|
          if @phase.update_attributes(params[:phase])
            format.html { redirect_to admin_phase_dmptemplate_path(@phase), notice: I18n.t('org_admin.templates.updated_message') }
            format.json { head :no_content }
          else
            format.html { render action: "admin_phase" }
            format.json { render json: @phase.errors, status: :unprocessable_entity }
          end
        end
            end
    end

admin_updatequestion() click to toggle source

update a question of a template

    # File app/controllers/dmptemplates_controller.rb, line 406
    def admin_updatequestion
            if user_signed_in? && current_user.can_org_admin? then
                   @question = Question.find(params[:id])
                            @question.guidance = params["question-guidance-#{params[:id]}"]
                            @question.default_value = params["question-default-value-#{params[:id]}"]
           @section = @question.section
                            @version = @section.version
                            @phase = @version.phase

                            respond_to do |format|
                  if @question.update_attributes(params[:question])
                    format.html { redirect_to admin_phase_dmptemplate_path(:id => @phase.id, :version_id => @version.id, :section_id => @section.id, :question_id => @question.id, :edit => 'true'), notice: I18n.t('org_admin.templates.updated_message') }
                    format.json { head :no_content }
                  else
                    format.html { render action: "admin_phase" }
                    format.json { render json: @question.errors, status: :unprocessable_entity }
                  end
                end
                    end
            end

admin_updatesection() click to toggle source

update a section of a template

    # File app/controllers/dmptemplates_controller.rb, line 347
    def admin_updatesection
            if user_signed_in? && current_user.can_org_admin? then
                   @section = Section.find(params[:id])
                   @section.description = params["section-desc-#{params[:id]}"]
           @version = @section.version
                            @phase = @version.phase

                            respond_to do |format|
                  if @section.update_attributes(params[:section])
                    format.html { redirect_to admin_phase_dmptemplate_path(:id => @phase.id, :version_id => @version.id, :section_id => @section.id , :edit => 'true'), notice: I18n.t('org_admin.templates.updated_message') }
                    format.json { head :no_content }
                  else
                    format.html { render action: "admin_phase" }
                    format.json { render json: @section.errors, status: :unprocessable_entity }
                  end
                end
                    end
    end

admin_updatesuggestedanswer() click to toggle source

update a suggested answer of a template

    # File app/controllers/dmptemplates_controller.rb, line 463
    def admin_updatesuggestedanswer
            if user_signed_in? && current_user.can_org_admin? then
                   @suggested_answer = SuggestedAnswer.find(params[:id])
        @question = @suggested_answer.question
        @section = @question.section
        @version = @section.version
        @phase = @version.phase

                            respond_to do |format|
                  if @suggested_answer.update_attributes(params[:suggested_answer])
                    format.html { redirect_to admin_phase_dmptemplate_path(:id => @phase.id, :version_id => @version.id, :section_id => @section.id, :question_id => @question.id, :edit => 'true'), notice: I18n.t('org_admin.templates.updated_message') }
                    format.json { head :no_content }
                  else
                    format.html { render action: "admin_phase" }
                    format.json { render json: @suggested_answer.errors, status: :unprocessable_entity }
                  end
                end
                    end
            end

admin_updateversion() click to toggle source

update a version of a template

    # File app/controllers/dmptemplates_controller.rb, line 262
    def admin_updateversion
            if user_signed_in? && current_user.can_org_admin? then
                   @version = Version.find(params[:id])
        @version.description = params["version-desc"]
        @phase = @version.phase

        if @version.published && !@phase.dmptemplate.published then
            @phase.dmptemplate.published = true
        end

        @all_versions = @phase.versions.where('published = ?', true)
        @all_versions.each do |v|
            if v.id != @version.id && v.published == true then
                v.published = false
                v.save
            end
        end

                respond_to do |format|
                  if @version.update_attributes(params[:version])
                    format.html { redirect_to admin_phase_dmptemplate_path(@phase, :version_id =>  @version.id, :edit => 'false'), notice: I18n.t('org_admin.templates.updated_message') }
                    format.json { head :no_content }
                  else
                    format.html { render action: "admin_phase" }
                    format.json { render json: @version.errors, status: :unprocessable_entity }
                  end
                end
                    end
            end