Sha256: 571b9248a9835ffa2a84794638062a1a7c56cbfddf9b27537371bd94be2fdbba

Contents?: true

Size: 1.91 KB

Versions: 7

Compression:

Stored size: 1.91 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:) }
        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:, session_token: participant.session_token)
                .joins(:question).order("decidim_forms_questions.position ASC")
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
decidim-forms-0.28.4 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.28.3 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.28.2 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.28.1 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.28.0 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.28.0.rc5 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb
decidim-forms-0.28.0.rc4 app/presenters/decidim/forms/admin/questionnaire_participant_presenter.rb