Support part descriptions
This commit is contained in:
parent
143c0cf421
commit
4faa75ca27
|
@ -31,6 +31,9 @@
|
||||||
<% if !isnothing(survey[page].label) %>
|
<% if !isnothing(survey[page].label) %>
|
||||||
<h2>$(survey[page].label)</h2>
|
<h2>$(survey[page].label)</h2>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
<% if !isnothing(survey[page].description) %>
|
||||||
|
<p>$(survey[page].description)</p>
|
||||||
|
<% end %>
|
||||||
<form method="POST" action="/submit" enctype="application/x-www-form-urlencoded" novalidate>
|
<form method="POST" action="/submit" enctype="application/x-www-form-urlencoded" novalidate>
|
||||||
<section>
|
<section>
|
||||||
<input type="hidden" name="uid" value="$(uid)" />
|
<input type="hidden" name="uid" value="$(uid)" />
|
||||||
|
|
|
@ -133,6 +133,7 @@ end
|
||||||
|
|
||||||
struct SurveyPart
|
struct SurveyPart
|
||||||
label::Union{AbstractString, Nothing}
|
label::Union{AbstractString, Nothing}
|
||||||
|
description::Union{AbstractString, Nothing}
|
||||||
questions::Vector{Question}
|
questions::Vector{Question}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -141,9 +142,12 @@ Base.getindex(p::SurveyPart, index::Integer) = p.questions[index]
|
||||||
|
|
||||||
Base.length(p::SurveyPart) = length(p.questions)
|
Base.length(p::SurveyPart) = length(p.questions)
|
||||||
|
|
||||||
|
SurveyPart(label::Union{AbstractString, Nothing}, description::Union{AbstractString, Nothing}, questions::Question...) =
|
||||||
|
SurveyPart(label, description, questions |> collect)
|
||||||
SurveyPart(label::Union{AbstractString, Nothing}, questions::Question...) =
|
SurveyPart(label::Union{AbstractString, Nothing}, questions::Question...) =
|
||||||
SurveyPart(label, questions |> collect)
|
SurveyPart(label, nothing, questions |> collect)
|
||||||
SurveyPart(questions::Question...) = SurveyPart(nothing, questions |> collect)
|
SurveyPart(questions::Question...) =
|
||||||
|
SurveyPart(nothing, nothing, questions |> collect)
|
||||||
|
|
||||||
# Survey
|
# Survey
|
||||||
|
|
||||||
|
@ -153,11 +157,11 @@ struct Survey
|
||||||
id::SurveyID
|
id::SurveyID
|
||||||
name::AbstractString
|
name::AbstractString
|
||||||
description::Union{AbstractString, Nothing}
|
description::Union{AbstractString, Nothing}
|
||||||
parts::Vector{Pair{Union{AbstractString, Nothing}, Vector{Symbol}}}
|
parts::Vector{Pair{Tuple{Union{AbstractString, Nothing}, Union{AbstractString, Nothing}}, Vector{Symbol}}}
|
||||||
questions::Dict{Symbol, Question}
|
questions::Dict{Symbol, Question}
|
||||||
function Survey(name::AbstractString,
|
function Survey(name::AbstractString,
|
||||||
description::Union{AbstractString, Nothing},
|
description::Union{AbstractString, Nothing},
|
||||||
parts::Vector{<:Pair{<:Union{<:AbstractString, Nothing}, <:Vector{Symbol}}},
|
parts::Vector{<:Pair{<:Any, <:Vector{Symbol}}},
|
||||||
questions::Dict{Symbol, Question})
|
questions::Dict{Symbol, Question})
|
||||||
# Create an id that only depends on:
|
# Create an id that only depends on:
|
||||||
# 1. Question IDs
|
# 1. Question IDs
|
||||||
|
@ -171,13 +175,14 @@ struct Survey
|
||||||
end
|
end
|
||||||
id = xor(map(qhash, values(questions))...) |>
|
id = xor(map(qhash, values(questions))...) |>
|
||||||
h -> xor(reinterpret(SurveyID, [h])...)
|
h -> xor(reinterpret(SurveyID, [h])...)
|
||||||
new(id, name, description, parts, questions)
|
typedparts = parts |> Vector{Pair{Tuple{Union{AbstractString, Nothing}, Union{AbstractString, Nothing}}, Vector{Symbol}}}
|
||||||
|
new(id, name, description, typedparts, questions)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Base.getindex(s::Survey, id::Symbol) = s.questions[id]
|
Base.getindex(s::Survey, id::Symbol) = s.questions[id]
|
||||||
Base.getindex(s::Survey, part::Integer) =
|
Base.getindex(s::Survey, part::Integer) =
|
||||||
SurveyPart(s.parts[part].first,
|
SurveyPart(s.parts[part].first[1], s.parts[part].first[2],
|
||||||
getindex.(Ref(s.questions), s.parts[part].second))
|
getindex.(Ref(s.questions), s.parts[part].second))
|
||||||
|
|
||||||
Base.length(s::Survey) = length(s.parts)
|
Base.length(s::Survey) = length(s.parts)
|
||||||
|
@ -186,7 +191,9 @@ Survey(name::AbstractString,
|
||||||
description::Union{AbstractString, Nothing},
|
description::Union{AbstractString, Nothing},
|
||||||
parts::SurveyPart...) =
|
parts::SurveyPart...) =
|
||||||
Survey(name, description,
|
Survey(name, description,
|
||||||
map(p -> p.label => getfield.(p.questions, :id), parts) |> collect,
|
map(parts) do p
|
||||||
|
(p.label, p.description) => getfield.(p.questions, :id)
|
||||||
|
end |> collect,
|
||||||
Dict(q.id => q for q in
|
Dict(q.id => q for q in
|
||||||
Iterators.flatten(getfield.(parts, :questions))))
|
Iterators.flatten(getfield.(parts, :questions))))
|
||||||
Survey(name::AbstractString, parts::SurveyPart...) =
|
Survey(name::AbstractString, parts::SurveyPart...) =
|
||||||
|
@ -690,6 +697,9 @@ function show(io::IO, m::MIME"text/plain", part::SurveyPart)
|
||||||
printstyled(io, " -- ", if isnothing(part.label)
|
printstyled(io, " -- ", if isnothing(part.label)
|
||||||
"Unlabeled part"
|
"Unlabeled part"
|
||||||
else part.label end, " --\n", color=:yellow)
|
else part.label end, " --\n", color=:yellow)
|
||||||
|
if !isnothing(part.description)
|
||||||
|
printstyled(io, " ", part.description, '\n', color=:yellow)
|
||||||
|
end
|
||||||
for q in part.questions
|
for q in part.questions
|
||||||
show(io, m, q)
|
show(io, m, q)
|
||||||
q === last(part.questions) || print(io, "\n")
|
q === last(part.questions) || print(io, "\n")
|
||||||
|
|
Loading…
Reference in New Issue