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

Backend Changes for Customizable Confirmation Screen #5112

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

rajpatel24
Copy link
Contributor

@rajpatel24 rajpatel24 commented Sep 17, 2024

Checklist

  1. If you've added code that should be tested, add tests
  2. If you've changed APIs, update (or create!) the documentation
  3. Ensure the tests pass
  4. Make sure that your code lints and that you've followed our coding style
  5. Write a title and, if necessary, a description of your work suitable for publishing in our release notes
  6. Mention any related issues in this repository (as #ISSUE) and in other repositories (as kobotoolbox/other#ISSUE)
  7. Open an issue in the docs if there are UI/UX changes

Description

This PR implements the backend changes for a customizable confirmation screen feature in Enketo, enhancing the user experience by providing personalized feedback based on their survey responses. The changes enable OpenRosa to dynamically extract and return customized confirmation messages, leveraging the flexibility of XLSForm's calculation capabilities.

The feature utilizes the kobo:submitResponseMessage attribute, allowing form designers to specify the XPath to the element containing the confirmation message. This PR modifies OpenRosa to recognize and extract this XPath, retrieve the corresponding message from the XML submission payload, and return it in the 201 response.

Notes

  • Modified OpenRosa to recognize and extract the XPath from kobo:submitResponseMessage attribute
  • Used extracted XPath to retrieve confirmation message from XML submission payload
  • Updated OpenRosa's 201 response to include the extracted confirmation message
  • Handled edge cases where the attribute is missing or references a non-existent XML element

@rajpatel24 rajpatel24 force-pushed the customizable_confirmation_screen branch from d8f8458 to f11a723 Compare September 17, 2024 12:37
@tiritea
Copy link

tiritea commented Sep 17, 2024

Note, it is probably worth double-checking that the ODK COllect/KoboCollect mobile client ignores - or is otherwise not negatively impacted - by having a non-empty <message> in the <OpenRosaResponse> body; see https://bitbucket.org/javarosa/javarosa/wiki/FormSubmissionAPI

Specifically, the <message> should either be completely ignored by Collect (eg Collect always just presents a boilerplate post-submission success text) or Collect just displays whatever text is returned in this string, markup chars and all. If Collect does anything else, that probably suggests an ODK Collect bug/issue that needs further investigation.

@noliveleger noliveleger self-requested a review October 7, 2024 18:23
@noliveleger noliveleger self-assigned this Oct 7, 2024
@noliveleger noliveleger changed the base branch from beta-refactored to main October 7, 2024 18:30
@noliveleger noliveleger changed the base branch from main to npm-nonsense-workaround October 7, 2024 18:50
@noliveleger noliveleger changed the base branch from npm-nonsense-workaround to main October 7, 2024 18:50
noliveleger and others added 3 commits October 7, 2024 15:20
 # Conflicts:
 #	kobo/apps/audit_log/tests/test_models.py
 #	kobo/apps/audit_log/tests/test_one_time_auth.py
 #	kobo/apps/openrosa/apps/api/viewsets/xform_submission_api.py
 #	kobo/apps/openrosa/apps/logger/models/attachment.py
 #	kobo/apps/openrosa/apps/main/tests/test_process.py
Copy link

Comment on lines 66 to 106
def extract_confirmation_message(xml_string: str) -> str | None:
"""
Extracts the confirmation message from the XML string based on the
`kobo:submitMessage` attribute.
"""
if isinstance(xml_string, str):
xml_string = xml_string.encode('utf-8')
parser = etree.XMLParser(recover=True)
root = etree.fromstring(xml_string, parser=parser)
namespaces = root.nsmap

# Extract the kobo:submitMessage attribute from the root element
try:
confirmation_message_xpath = root.xpath(
'@kobo:submitMessage', namespaces=namespaces
)
except etree.XPathEvalError:
return

if not confirmation_message_xpath:
return

# Blocked by kpi#5137, this block below works as-is but won't work
# when kpi#5137 is merged.
confirmation_message_xpath = confirmation_message_xpath[0].replace(
'/data', f'/{root.tag}'
).strip()

try:
# Evaluate the XPath expression to find the message
confirmation_message_element = root.xpath(confirmation_message_xpath)
except etree.XPathEvalError as e:
logging.error(
'Failed to extract confirmation message: ' + str(e),
exc_info=True
)
else:
if confirmation_message_element:
return confirmation_message_element[0].text

return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the liberty to make some changes to understand how it worked and to make it work.
The tests were passing because in your XML, submitMessage starts with the root tag.

In real world, Kobo uses the id_string for the XForm which always changes and cannot be set in the settings for the XLS file (i.e. attribute::kobo:submitMessage)

See https://chat.kobotoolbox.org/#narrow/stream/4-Kobo-Dev/topic/Customizable.20confirmation.20screen.20in.20Enketo/near/469885 , IMO this PR is blocked by kpi#5137.

We should add a test to be sure everything work if no attribute::kobo:submitMessage is present.

@rajpatel24 rajpatel24 force-pushed the customizable_confirmation_screen branch from a4df9e4 to 7375725 Compare October 17, 2024 14:19
@rajpatel24 rajpatel24 force-pushed the customizable_confirmation_screen branch from 7375725 to a60fede Compare October 17, 2024 14:31
jnm added a commit that referenced this pull request Nov 4, 2024
## Checklist

1. [x] If you've added code that should be tested, add tests
2. [ ] If you've changed APIs, update (or create!) the documentation
3. [x] Ensure the tests pass
4. [x] Run `./python-format.sh` to make sure that your code lints and
that you've followed [our coding
style](https://github.com/kobotoolbox/kpi/blob/main/CONTRIBUTING.md)
5. [x] Write a title and, if necessary, a description of your work
suitable for publishing in our [release
notes](https://community.kobotoolbox.org/tag/release-notes)
6. [x] Mention any related issues in this repository (as #ISSUE) and in
other repositories (as kobotoolbox/other#ISSUE)
7. [x] Open an issue in the
[docs](https://github.com/kobotoolbox/docs/issues/new) if there are
UI/UX changes

## Description

This is an advanced feature for unusual cases where it's necessary to
[override the root XML node
name](https://xlsform.org/en/#specify-xforms-root-node-name) used in
submissions. This may be used for an upcoming feature to allow
customizable confirmation messages in Enketo.

## Notes

- Implemented logic to check and set the name attribute from XLSForms in
the survey object.
- The existing behavior where name was not utilized has been modified to
honor the name setting, defaulting to the id_string if name is not
provided or is invalid.

## Related issues

Part of #5112

---------

Co-authored-by: John N. Milner <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants