Sha256: f94172d421388c71ff3f2aef887e17f8219c8cf0604e96700ca0dd86f1183abf

Contents?: true

Size: 1.81 KB

Versions: 13

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

module Decidim
  module Forms
    module Admin
      #
      # Presenter for questionnaire response
      #
      class QuestionnaireParticipantPresenter < Rectify::Presenter
        attribute :participant, Decidim::Forms::Answer

        def session_token
          participant.session_token || "-"
        end

        def ip_hash
          participant.ip_hash || "-"
        end

        def answered_at
          participant.created_at
        end

        delegate :questionnaire, to: :participant

        def registered?
          participant.decidim_user_id.present?
        end

        def status
          t(registered? ? "registered" : "unregistered", scope: "decidim.forms.user_answers_serializer")
        end

        def answers
          sibilings.map { |answer| QuestionnaireAnswerPresenter.new(answer: answer) }
        end

        def first_short_answer
          short = sibilings.where(decidim_forms_questions: { question_type: %w(short_answer) })
          short.first
        end

        def completion
          with_body = sibilings.where(decidim_forms_questions: { question_type: %w(short_answer long_answer) })
                               .where.not(body: "").count
          with_choices = sibilings.where.not("decidim_forms_questions.question_type in (?)", %w(short_answer long_answer))
                                  .where("decidim_forms_answers.id IN (SELECT decidim_answer_id FROM decidim_forms_answer_choices)").count

          (with_body + with_choices).to_f / questionnaire.questions.not_separator.count * 100
        end

        private

        def sibilings
          Answer.not_separator.where(questionnaire: questionnaire, session_token: participant.session_token).joins(:question).order("decidim_forms_questions.position ASC")
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-forms-0.25.2 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.25.1 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.25.0 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.25.0.rc4 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.25.0.rc3 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.25.0.rc2 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.25.0.rc1 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.24.3 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.24.2 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.24.1 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.24.0 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.24.0.rc2 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.24.0.rc1 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb