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

UHF-10741: Force user to visit all pages before submitting application #1533

Merged
merged 4 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions public/modules/custom/grants_handler/grants_handler.module
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,11 @@ function grants_handler_preprocess_webform_submission_form(&$variables) {
$navigationHelper = \Drupal::service('grants_handler.navigation_helper');
// Get errors for printing.
$variables['errors'] = $navigationHelper->getAllErrors($webform_submission);

$variables['unvisited_pages'] = $navigationHelper->getUnvisitedPages($webform_submission);
$variables['display_visit_all_pages_warning'] =
$navigationHelper->getCurrentPage($webform_submission) === 'webform_preview' &&
count($variables['unvisited_pages']) > 0;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public function __construct(
public function getCurrentPage(WebformSubmissionInterface $webformSubmission): string {
$pages = $webformSubmission->getWebform()
->getPages('edit', $webformSubmission);

return empty($webformSubmission->getCurrentPage()) ? array_keys($pages)[0] : $webformSubmission->getCurrentPage();
}

Expand Down Expand Up @@ -267,6 +268,29 @@ public function getPageVisits(WebformSubmissionInterface $webformSubmission): ar
return $submission_log;
}

/**
* Check if user has visited each page.
*
* @param \Drupal\webform\WebformSubmissionInterface $webformSubmission
* The submission to check.
*
* @return array
* Array of unvisited pages.
*/
public function getUnvisitedPages(WebformSubmissionInterface $webformSubmission) : array {
$page_visits = array_map(function ($entry) {
return $entry->page;
}, $this->getPageVisits($webformSubmission));
$ignore_visits = ['webform_preview', 'webform_confirmation'];
return array_filter(
$webformSubmission->getWebform()->getPages('edit', $webformSubmission),
function ($name) use ($ignore_visits, $page_visits) {
return !in_array($name, $ignore_visits, TRUE) && !in_array($name, $page_visits, TRUE);
},
ARRAY_FILTER_USE_KEY
);
}

/**
* Logs the current submission page.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,13 @@ public function alterForm(array &$form, FormStateInterface $form_state, WebformS
}

GrantsErrorStorage::setErrors($errors);

if (
$this->grantsFormNavigationHelper->getCurrentPage($webform_submission) == 'webform_preview' &&
count($this->grantsFormNavigationHelper->getUnvisitedPages($webform_submission)) > 0
) {
$form['actions']['submit']['#disabled'] = TRUE;
}
}

/**
Expand Down Expand Up @@ -1269,12 +1276,19 @@ public function validateForm(
$webform_submission
);

if ($violations->count() === 0) {
$allPagesVisited = count(
$this->grantsFormNavigationHelper->getUnvisitedPages($webform_submission)
) === 0;

if ($allPagesVisited && $violations->count() === 0) {
// If we have no violations clear all errors.
$form_state->clearErrors();
$this->grantsFormNavigationHelper->deleteSubmissionLogs($webform_submission, GrantsHandlerNavigationHelper::ERROR_OPERATION);
}
else {
if (!$allPagesVisited) {
$form_state->setErrorByName('unvisited-pages', $this->t('You must visit all pages in the form before you can submit the application.', [], $tOpts));
}
if ($violations->count() > 0) {
// If we HAVE errors, then refresh them from the.
$this->messenger()
->addError($this->t('The application cannot be submitted because not all
Expand Down
8 changes: 8 additions & 0 deletions public/modules/custom/grants_handler/translations/fi.po
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,14 @@ msgctxt "grants_handler"
msgid "Refresh the page"
msgstr "Lataa sivu uudelleen"

msgctxt "grants_handler"
msgid "You must visit all pages in the form before you can submit the application"
msgstr "Käy kaikilla sivuilla ennen kuin lähetät hakemuksen"

msgctxt "grants_handler"
msgid "Unvisited pages:"
msgstr "Sivut, joilla ei ole käyty:"

msgctxt "grants_handler"
msgid "Leave the application"
msgstr "Poistu hakemukselta"
Expand Down
8 changes: 8 additions & 0 deletions public/modules/custom/grants_handler/translations/sv.po
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,14 @@ msgstr "%mail är inte en giltig e-postadress. Använd formen [email protected]."
msgid "Remove @item @number"
msgstr "Ta bort @item @number"

msgctxt "grants_handler"
msgid "You must visit all pages in the form before you can submit the application"
msgstr "Du måste besöka alla sidor i formuläret innan du kan skicka in ansökan."

msgctxt "grants_handler"
msgid "Unvisited pages:"
msgstr "Obesökta sidor:"

msgctxt "grants_handler"
msgid "Refresh the page"
msgstr "Uppdatera sidan"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,48 @@
{% endfor %}

{% if has_errors is same as true %}
<section aria-label="Notification" class="hds-notification hds-notification--error">
<div class="hds-notification__content">
<div class="hds-notification__label" role="heading" aria-level="2">
<span class="hel-icon hel-icon--alert-circle-fill" aria-hidden="true"></span>
<span>{{ 'Missing or incomplete information'|t }}</span>
<section aria-label="Notification" class="hds-notification hds-notification--error">
<div class="hds-notification__content">
<div class="hds-notification__label" role="heading" aria-level="2">
<span class="hel-icon hel-icon--alert-circle-fill" aria-hidden="true"></span>
<span>{{ 'Missing or incomplete information'|t }}</span>
</div>
<div class="hds-notification__body">
<ul>
{% for page in errors %}
{% for name,element in page %}
{% if name is not same as 'title' %}
<li>
{{ 'Error in page'|t }} {{ page['title'] }}: {{ element }}
</li>
{% endif %}
{% endfor %}
{% endfor %}
</ul>
</div>
</div>
<div class="hds-notification__body">
<ul>
{% for page in errors %}
{% for name,element in page %}
{% if name is not same as 'title' %}
<li>
{{ 'Error in page'|t }} {{ page['title'] }}: {{ element }}
</li>
{% endif %}
</section>
{% endif %}

{% if display_visit_all_pages_warning %}
<section aria-label="Notification" class="hds-notification hds-notification--error">
<div class="hds-notification__content">
<div class="hds-notification__label" role="heading" aria-level="2">
<span class="hel-icon hel-icon--alert-circle-fill" aria-hidden="true"></span>
<span>{{ 'You must visit all pages in the form before you can submit the application'|trans({}, {'context:': 'grants_handler'}) }}</span>
</div>
<div class="hds-notification__body">
<span>{{ 'Unvisited pages:'|trans({}, {'context': 'grants_handler'}) }}</span>
<ul>
{% for page in unvisited_pages %}
<li>
{{ page['#title'] }}
</li>
{% endfor %}
{% endfor %}
</ul>
</ul>
</div>
</div>
</div>
</section>
</section>
{% endif %}

{{ form }}
Expand Down