Metadata-Version: 2.5
Name: trytond_etude_collect
Version: 8.0.0.2
Summary: Etude Form Server — Responses and Answers
Author: Clemens Hupka
Maintainer-email: Clemens Hupka <clemens.hupka@backend.at>
License-Expression: GPL-3.0-or-later
License-File: COPYRIGHT
License-File: LICENSE
Keywords: answer,form,response,submit,survey,tryton
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Environment :: Plugins
Classifier: Framework :: Tryton
Classifier: Topic :: Office/Business
Requires-Python: >=3.10
Requires-Dist: trytond-etude<8.1,>=8.0
Requires-Dist: trytond<8.1,>=8.0
Provides-Extra: test
Requires-Dist: trytond-etude-bind<8.1,>=8.0; extra == 'test'
Requires-Dist: trytond-etude-logic<8.1,>=8.0; extra == 'test'
Description-Content-Type: text/x-rst

Etude Collect Module
====================

The ``etude_collect`` module stores what a participant answered: the
``etude.response`` of a form, the ``etude.answer`` rows a response holds and
the submit that closes it.
It is the core module of the collection of the etude group - it depends on
``etude``, the definition engine - and it is what turns a rendered form into an
answer a deployment keeps.

A response pins the version it answers
--------------------------------------

A form is edited, published and republished while answers are given, so a
response does not read the form it answers: it *pins* the version of the
definition it was rendered against, and it is that version - not the working
copy of the form - that the response is validated, reported and rendered
against.

The version is pinned when the response is created, from the version the form
serves at that moment, and the form and the version of a response are written
once and refused afterwards: what a response answers stays what it answered,
even when the designer publishes a definition that renames a question or drops
it.  A form that is not published, that has not opened yet or that is closed
takes no response, and neither does a form that serves no version, because a
form that is prepared rather than served has nothing to render.

What the pinned definition says is what the submit reads: which questions the
response may answer at all, which of them are required, and - through the
companion modules - which of them the form shows.

One typed row per answered question
-----------------------------------

An answer is held in the column of the kind of its question rather than in a
text read back by a join: a short text in ``value_char``, a long text in
``value_text``, a number in ``value_integer`` or ``value_numeric``, a date in
``value_date``, a moment in ``value_datetime``, a yes/no in ``value_boolean``,
a single choice in ``value_option``, a multiple choice in the ``options``
relation and an upload in ``attachment``.

The value is read back as the kind of its question rather than as the column it
is held in: the answer of a choice is the value - or the code - of the option
the participant chose and the answer of a multiple choice is the list of them,
which is what a condition, a formula and a binding of the group read.  A value
that names no option of its question is refused where the answer is stored,
because the option is what the answer points at and not what it copies.

A value that holds nothing is no answer at all: the row of a question that is
not answered is not written, and a row that is not answered any more is
dropped, so that a response holds the answers it was given and nothing else.
A false answer and a zero are answers, and they are told apart from an
unanswered question the way the logic of the group tells them apart.  A
question of the kind display holds no answer of its own, and a question of the
kind matrix, an array or a ranking holds its answers on its subquestions.

The response itself carries what is about the answer as a whole: who answered,
when the response was started and submitted, how long it took, in what
language, from where, where the participant stopped and whether it is complete.
``last_page`` names the page a partially answered response was left on, so that
a participant resumes where the form was left, and ``complete`` says whether
the response holds every answer its form requires.

The submit of a response
------------------------

The submit is one pipeline, run inside the transaction of the caller: the
answers are checked against the definition the response pinned and against the
rules of the form, the records the bindings of the form describe are written,
and the answers are stored, before the response becomes submitted, complete and
counted.

The answers are read against the pinned definition first: an answer of a
question the definition does not ask is dropped rather than stored, and every
question the definition requires must be answered.  The limit of a form is
counted inside the submit transaction against the responses the form already
took, so that a form which reached it refuses the next submit rather than
accepting one response too many.

A step that fails - an unanswered required question, a rule the definition
refuses, a quota the response would break, a binding the running user may not
write - raises before the first record is written and rolls the whole submit
back, so that a refused submit leaves neither an answer nor a record behind.

A partial answer is saved, resumed and timed
--------------------------------------------

A form that takes a partial answer is saved page by page rather than submitted:
``etude.response.save_partial`` stores the answers the response holds, notes the
page the participant stopped on in ``last_page`` and opens the timing of that
page.  A participant who leaves the form and comes back to it comes back to the
response it was answering rather than to a second one: ``get_draft`` reads the
most recent draft of a form and of its user, which is the user a token acts as
when a client is authenticated by one, and the response is resumed with the
answers it holds and the page it stopped on.

The time a participant spent on each page is kept in ``etude.response.page``,
one row per page it went through: the page the participant was on is closed at
the moment the next one is opened, the moment a page was first opened survives a
resume - a participant who comes back to a page carries on the clock of that
page rather than starting a second one - the duration of a page is read from the
moments it was opened and left, and a page still open is read to now.
``get_page_durations`` reads the pages back by their name, and the submit closes
the page the participant submitted from.

A draft a participant never comes back to is abandoned, and a deployment deletes
it once the delay it configures passed: the ``ir.cron`` job of the module runs
``etude.response.expire_drafts``, which reads the retention from ``[etude]
draft_retention`` - in days, 30 by default - and keeps the drafts a participant
is still answering, because a draft one of whose pages was reached after the
delay is not abandoned.  A retention of 0 keeps every draft.

Companions rather than dependencies
-----------------------------------

The rules and the quotas of a form are the logic of the group and the records a
form writes are its bindings, and both are modules installed *beside* this one
rather than depended on: ``etude_logic`` and ``etude_bind`` are declared
``extras_depend``, so a plain survey of a deployment that installs neither
stores its answers alone.  Every companion is reached through the pool and read
where it is installed, so that no module of the group depends on another
group's companion.

When ``etude_logic`` is installed the rules and the quotas of the form are
checked and the answers of the questions the form hides are dropped; when
``etude_bind`` is installed the records the mappings of the form describe are
written through ``etude.form.write_records``.  The count a quota of a form
reads is the responses the form took, which is answered here for the logic:
``etude.quota.get_count`` is extended, and the extension is registered only
where the logic is activated, so that the logic counts nothing back where no
module of the group stores responses.

See ``doc/index.rst`` for the module documentation and ``ETUDE.md`` in
``modules-cch/`` for the architecture of the whole group.
