Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/questionnaire exports #28

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion decidim-core/app/jobs/decidim/export_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ def perform(user, component, name, format)

collection = export_manifest.collection.call(component, user)
serializer = export_manifest.serializer
options = export_manifest.options

export_data = Decidim::Exporters.find_exporter(format).new(collection, serializer).export

ExportMailer.export(user, name, export_data).deliver_now
ExportMailer.export(user, name, export_data, options).deliver_now
end
end
end
11 changes: 9 additions & 2 deletions decidim-core/app/mailers/decidim/export_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@ class ExportMailer < ApplicationMailer
# user - The user to be notified.
# export_name - The name of the export.
# export_data - The data containing the result of the export.
# options - Options regarding the export (zip: whether to send the attachment compressed in a .zip file).
#
# Returns nothing.
def export(user, export_name, export_data)
def export(user, export_name, export_data, options = { zip: true })
@user = user
@organization = user.organization

filename = export_data.filename(export_name)
filename_without_extension = export_data.filename(export_name, extension: false)

attachments["#{filename_without_extension}.zip"] = FileZipper.new(filename, export_data.read).zip
if options[:zip]
attachments["#{filename_without_extension}.zip"] = FileZipper.new(filename, export_data.read).zip
@zipped = true
else
attachments[filename] = export_data.read
@zipped = false
end

with_user(user) do
mail(to: "#{user.name} <#{user.email}>", subject: I18n.t("decidim.export_mailer.subject", name: filename))
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= t(".ready") %>
<%= @zipped ? t(".ready_zipped") : t(".ready") %>
3 changes: 2 additions & 1 deletion decidim-core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,8 @@ en:
click_button: 'Click the next button to download your data. <br/> You will have the file available until %{date}. <br/> You will need <a href=''https://www.7-zip.org/''>7-Zip</a> to open it. Password: %{password}'
download: Download
export:
ready: Please find attached a zipped version of your export.
ready_zipped: Please find attached a zipped version of your export.
ready: Please find your export attached.
subject: Your export "%{name}" is ready
filters:
linked_classes:
Expand Down
12 changes: 12 additions & 0 deletions decidim-core/lib/decidim/exporters/export_manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ def formats(formats = nil)
@formats ||= formats || DEFAULT_FORMATS
end

DEFAULT_OPTIONS = { zip: true }.freeze

# Public: Defines options for the export.
#
# options - The hash containing the options.
#
# Returns the stored options if previously stored, or
# the default empty hash.
def options(options = DEFAULT_OPTIONS)
@options ||= options
end

private

# Private: Loads the given exporters when formats argument is provided.
Expand Down
18 changes: 18 additions & 0 deletions decidim-core/spec/lib/exporters/export_manifest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,23 @@ module Decidim
end
end
end

describe "#options" do
context "when no options are specified" do
it "returns an empty hash" do
expect(subject.options).to eq(described_class::DEFAULT_OPTIONS)
end
end

context "when some options are specified" do
before do
subject.options option: "value"
end

it "returns the options hash" do
expect(subject.options).to eq(option: "value")
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,14 @@

onFulfilled(id, fulfilled) {
this.conditions[id].fulfilled = fulfilled;
const $fulfilled = this.wrapperField.find("input[name$=\\[display_conditions_fulfilled\\]]");

if (this.mustShow()) {
$fulfilled.val("true");
this.showQuestion();
}
else {
$fulfilled.val("false");
this.hideQuestion();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,68 +1,113 @@
$foreground: #202751;
$background: #fafafa;
$background-alt: darken($background, 5%);
$white: white;

$border-radius-top: 4px 4px 0 0;
$border-radius-bottom: 0 0 4px 4px;

$border: 1px solid $foreground;

$small-padding: 10px;
$padding: 25px;

.questionnaire-answers{
font-family: 'Source Sans Pro', Helvetica, Roboto, Arial, sans-serif;

.header{
h1{
margin: 0;
padding: 25px;
background: rgb(59, 69, 87);
color: white;
border-radius: 4px 4px 0 0;
.logo{
height: 1rem;
width: auto;
}

.title,
.subtitle,
.description{
margin: 0;
padding: 25px;
background: #f6f6f6;
color: #202751;
border-radius: 0 0 4px 4px;
padding: $small-padding $padding;
background: $foreground;
color: $white;
border-radius: $border-radius-top;
}

.title{
padding: $padding;
}

.subtitle{
border-radius: 0;
padding-top: $padding;
}

.subtitle,
.description,
.answers-count{
background: $background-alt;
color: $foreground;
}

.answers-count{
margin: -1px 0;
padding: $padding;
}

.description{
border-radius: $border-radius-bottom;
}
}

.answer{
margin-top: 25px;
background: #f6f6f6;
border-radius: 0 0 4px 4px;
margin-top: $padding;
background: $background;
border-radius: $border-radius-bottom;

.title{
page-break-inside: avoid;
border-radius: 4px 4px 0 0;
padding: 10px 25px;
background: rgb(59, 69, 87);
color: white;
border-radius: $border-radius-top;
padding: $small-padding $padding;
margin: 0;
background: $foreground;
color: $white;
}

.participant-info{
margin-top: 0;
padding: 10px 25px;
padding: $small-padding $padding;
width: 100%;
text-align: center;
background: #eee;
color: #202751;
background: $background-alt;
color: $foreground;
border-bottom: $border;
page-break-inside: avoid;

th:first-child{
text-align: left;
}

td:first-child{
text-align: left;
}
td,
th{
&:first-child{
text-align: left;
}

th:last-child{
text-align: right;
}

td:last-child{
text-align: right;
&:last-child{
text-align: right;
}
}
}

.answers{
padding: 25px;

.response{
dt{
font-weight: bold;
}
dd, dt{
margin: $small-padding 0;
}
}

.question{
page-break-inside: avoid;
font-size: inherit;
color: #202751;
color: $foreground;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def call

def answer_questionnaire
Answer.transaction do
form.responses_by_step.flatten.select(&:display_conditions_fulfilled?).each do |form_answer|
form.responses_by_step.flatten.select(&:display_conditions_fulfilled?).reject(&:separator?).each do |form_answer|
answer = Answer.new(
user: @current_user,
questionnaire: @questionnaire,
Expand Down
14 changes: 4 additions & 10 deletions decidim-forms/app/forms/decidim/forms/answer_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class AnswerForm < Decidim::Form

attribute :question_id, String
attribute :body, String
attribute :display_conditions_fulfilled, Boolean, default: true
attribute :choices, Array[AnswerChoiceForm]
attribute :matrix_choices, Array[AnswerChoiceForm]

Expand All @@ -18,7 +19,7 @@ class AnswerForm < Decidim::Form
validate :all_choices, if: -> { question.question_type == "sorting" }
validate :min_choices, if: -> { question.matrix? && question.mandatory? }

delegate :mandatory_body?, :mandatory_choices?, :matrix?, to: :question
delegate :mandatory_body?, :mandatory_choices?, :matrix?, :separator?, to: :question

attr_writer :question

Expand Down Expand Up @@ -49,21 +50,14 @@ def selected_choices
choices.select(&:body)
end

def display_conditions_fulfilled?
question.display_conditions.all? do |condition|
answer = question.questionnaire.answers.find_by(question: condition.condition_question)
condition.fulfilled?(answer)
end
end

private

def mandatory_body?
question.mandatory_body? if display_conditions_fulfilled?
question.mandatory_body? if display_conditions_fulfilled
end

def mandatory_choices?
question.mandatory_choices? if display_conditions_fulfilled?
question.mandatory_choices? if display_conditions_fulfilled
end

def grouped_choices
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def perform(user, title, answers)
serializer = Decidim::Forms::UserAnswersSerializer
export_data = Decidim::Exporters::FormPDF.new(answers, serializer).export

ExportMailer.export(user, title, export_data).deliver_now
ExportMailer.export(user, title, export_data, zip: false).deliver_now
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,53 @@ class QuestionnaireAnswerPresenter < Rectify::Presenter
attribute :answer, Decidim::Forms::Answer

def question
translated_attribute(answer.question.body)
translated_attribute(answer.question.body, organization)
end

def body
return answer.body if answer.body.present?
return simple_format answer.body if answer.body.present?
return "-" if answer.choices.empty?

choices = answer.choices.map do |choice|
choice.try(:custom_body) || choice.try(:body)
end
return answer.choices.first.body if answer.question.question_type == "single_option"

return choices.first if answer.question.question_type == "single_option"
present_choices
end

content_tag(:ul) do
safe_join(choices.map { |c| choice(c) })
end
def text?
%w(short_answer long_answer).include? answer.question.question_type.to_s
end

private

def choice(choice_body)
content_tag :li do
choice_body
def organization
answer.questionnaire.questionnaire_for&.organization
end

def choice_body(choice)
choice.try(:custom_body) || choice.try(:body)
end

def present_choices
if answer.question.matrix?
content_tag :dl do
safe_join(
answer.choices.map do |c|
matrix_row = answer.question.matrix_rows.find_by(id: c.matrix_row.id)
safe_join([
content_tag(:dt, translated_attribute(matrix_row.body)),
content_tag(:dd, choice_body(c))
])
end
)
end
else
content_tag(answer.question.question_type == "sorting" ? :ol : :ul) do
safe_join(
answer.choices.map do |c|
content_tag(:li, choice_body(c))
end
)
end
end
end
end
Expand Down
Loading