Sha256: bc5392c05ba510b9634cdc2a7bdd34ff5dab474303b3b8ea2ee33cb73919222f

Contents?: true

Size: 1.93 KB

Versions: 12

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

module Decidim
  module Forms
    module Admin
      #
      # Presenter for questionnaire response
      #
      class QuestionnaireParticipantPresenter < SimpleDelegator
        def participant
          __getobj__.fetch(:participant)
        end

        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
          I18n.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.not_title_and_description.count * 100
        end

        private

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

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
decidim-forms-0.27.9 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.27.8 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.27.7 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.27.6 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.27.5 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.27.4 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.27.3 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.27.2 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.27.1 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.27.0 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.27.0.rc2 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.27.0.rc1 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb