Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Executing the survey Exit Link after an error

  • JEfromCanada
  • JEfromCanada's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
8 months 2 weeks ago #245747 by JEfromCanada
Executing the survey Exit Link after an error was created by JEfromCanada
Please help us help you and fill where relevant:
Your LimeSurvey version: url=https://bizxawards.ca/polls/index.php/surveyAdministration/view?surveyid=389677#modalSystemInformation]Version 5.6.33+230808[/url
Own server or LimeSurvey hosting: Own Server
Survey theme/template: extends_fruity
==================
Because I'm using an extended version of Fruity, I'll start by saying the only modifications to standard fruity are:
1) Implementing suggested code for displaying available surveys in Alphabetical Order (this works)
2) Adding a custom image to replace logo.png (this works)

My issue:
We annually create a "voting poll" consisting of 24 categories.  These voting categories are implemented as a set of 24 different Limesurvey Surveys with a single-answer list-style question. The URL's for these 24 surveys are displayed on the client's landing page (and because of an issue with last year's survey, where CloudFlare was interfering with some devices accessing the survey, we decided this year to host the survey database on a separate domain - not connected with CloudFlare). 

When someone tries to vote for a candidate in a specific category (i.e. a specific Limesurvey Survey), the answer registers, and the End of Survey URL is executed after submission, returning the voter to the previous landing page on the client's website.  This is working perfectly.

However, when someone tries voting again in the same category, we catch that as an attempt to execute the same survey as has already been completed.  This results in the standard error message:

ERROR
You have already completed this survey.
For further information please contact ....

What I want to do is add a link to the template (I assume this will be done by editing the file /limesurvey/application/controllers/survey/index.php) to provide an easy method of returning to the standard End-of-survey URL after the user encounters this error.

I don't know what format the PHP will take to do this, as I have not located the code that normally is used to return from the SUBMIT process.

 

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 months 1 week ago - 8 months 1 week ago #245753 by tpartner
Replied by tpartner on topic Executing the survey Exit Link after an error
Do not edit the core files, extend the theme and edit the Twig files.

Perhaps, warnings.twig.

- manual.limesurvey.org/New_Template_System_in_LS3.x/en
github.com/LimeSurvey/LimeSurvey/blob/ma...ssages/warnings.twig

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 8 months 1 week ago by tpartner.
The following user(s) said Thank You: JEfromCanada

Please Log in to join the conversation.

  • JEfromCanada
  • JEfromCanada's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
8 months 1 week ago - 8 months 1 week ago #245754 by JEfromCanada
Replied by JEfromCanada on topic Executing the survey Exit Link after an error
Tony, while waiting for a response, I did a search for the error string and located the .twig file responsible for generating that error. So, I was able to modify the .twig file.

upload/themes/survey/[mytheme]/views/layout_errors.twig

and placed a button into the code to perform a browser-back operation.

Unfortunately, I was NOT able to locate a .twig file that could be used to generate the content of the file:
/application/controllers/survey/index.php, so I ended up modifying the error header and message by hand in the core file.
Last edit: 8 months 1 week ago by JEfromCanada.

Please Log in to join the conversation.

  • JEfromCanada
  • JEfromCanada's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
8 months 1 week ago #245792 by JEfromCanada
Replied by JEfromCanada on topic Executing the survey Exit Link after an error
When code was available in a .twig file, I extended the theme and edited the .twig file. However, I saw no way to edit a core file in a way that preserves the original file. There was some customization that needed to be done on the LIST question type (when submitting an answer) and I was able to do that with a modification to the navigator.twig file. The file "/limesurvey/application/controllers/survey/index.php" needed to be updated to modify the error and message text for surveys that had already been taken, and I saw no way to "extend" a core file. If you can point me to the method of extending core files, I would be grateful, since I would like to be able to apply security updates without losing my customizations.

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 months 1 week ago - 8 months 1 week ago #245802 by tpartner
Replied by tpartner on topic Executing the survey Exit Link after an error
I don't understand why you need to modify "/limesurvey/application/controllers/survey/index.php".

Placing something like this in your layout_errors.twig file results in the screenshot below.

(of course, you may want to extend it for different languages)

Code:
{{ registerTemplateCssFile('css/errors.css') }}
{% set aError     = aSurveyInfo.aError    %}
 
{% set customContent = false %}
{% if aError.type == 'survey-notstart' %}
  {% set customContent = true %}
{% endif %}
 
<!DOCTYPE html>
<html lang="{{ aSurveyInfo.languagecode }}" dir="{{ aSurveyInfo.dir }}" class="{{ aSurveyInfo.languagecode }} dir-{{ aSurveyInfo.dir }} {{ aSurveyInfo.class.html }}" {{ aSurveyInfo.attr.html }}>
 
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 
        <title>
            {% if customContent == true %}
                My custom title.
            {% elseif aError.error %}
                {{ aError.error }}
            {% else %}
                Error {{ aSurveyInfo.surveyls_title }}
            {% endif %}
        </title>
 
        <meta name="generator" content="LimeSurvey http://www.limesurvey.org" />
        {% if(imageSrc('./files/favicon.ico')) %}
            <link rel="shortcut icon" href="{{ imageSrc('./files/favicon.ico') }}" />
        {% endif %}
    </head>
 
    <body  class="{{ aSurveyInfo.class.body }} lang-{{aSurveyInfo.languagecode}} {{surveyformat}}" marginwidth="0" marginheight="0" {{ aSurveyInfo.attr.body }}>
    <article id="block_error">
            <div>
                {% if aError.error %}
                    <h1>{{ aError.error }}</h1>
                {% endif %}
                {% if customContent == true %}
                    <h2>My custom error header</h2>
                    <p>
                        Oops, it looks like you have already completed this survey.
                    </p>
                    <p>
                        <a href='https://google.com'>Please click here to select another survey.</a>
                    </p>
                {% else %}
                    <h2>{{ aError.title }}</h2>
                    <p>
                        {{ aError.message }}
                    </p>
                    <p>
                        {% if aError.contact %}
                            {{ aError.contact }}
                        {% else %}
                            {{gT("For further information please contact %s:")|format (aSurveyInfo.admin)}}
                            {% if aSurveyInfo.adminemail %}
                                <br>
                                <a href='mailto:{{ aSurveyInfo.adminemail }}'>{{ aSurveyInfo.adminemail }}</a>
                            {% endif %}
                        {% endif %}
                    </p>
                {% endif %}
            </div>
            {% if aError.trace is not empty %}
                <pre>
                    {{ aError.trace }}
                </pre>
            {% endif %}
            <div>
            </div>
        </article>
        {% block footer %}
            {{ include('./subviews/footer/footer.twig') }}
        {% endblock %}
    </body>
</html>

 

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 8 months 1 week ago by tpartner.
The following user(s) said Thank You: JEfromCanada

Please Log in to join the conversation.

  • JEfromCanada
  • JEfromCanada's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
8 months 1 week ago - 8 months 1 week ago #245813 by JEfromCanada
Replied by JEfromCanada on topic Executing the survey Exit Link after an error
Wow! I had looked at the layout_errors.twig file, recognizing this was the module that was ultimately generating the error message. But my lack of knowledge of the .twig programming system (the syntax of lines of code above) as well as the lack of realization that I could embed my custom header and text into the .twig file without fetching strings from the translation system (since I had never seen an example of "hard coded" strings in any prior .twig file) somehow led me to believe the .twig file was the wrong place for this customization.

I will need to study your solution to see whether it still will function for "general" errors (ones where an ERROR header and message from the .mo file are still appropriate).

So, thank you for this amazing solution.


Jack


EDIT:

Alright, I've compared this code to the original .twig file and see where you've suggested changes, and in the context of seeing the original and modified code, I'm beginning to understand what you've done here.  I do have a question.  In the index file, the error type 'survey-notstart' is triggered in one of three situations.  My custom code would only be relevant in one of those situations (a previously completed survey).  That means the test for whether I need to use the custom header and message depends on the error type ('survey-notstart') as well as the error reason ('alreadyCompleted').  Can I assume that the data structure aError also contains the reason (so that I would be doing a compound test like: {% if (aError.type == 'survey-notstart' && aError.reason == 'alreadyCompleted') %}

Please excuse if I don't understand the syntax of the lines, but I definitely think I need the compound condition.
 
Last edit: 8 months 1 week ago by JEfromCanada. Reason: Proposing that I need a compound condition to set the custom message flag

Please Log in to join the conversation.

  • JEfromCanada
  • JEfromCanada's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
8 months 1 week ago #245814 by JEfromCanada
Replied by JEfromCanada on topic Executing the survey Exit Link after an error
I get a fatal error when I try making the if statement into a compound test. I tried with and without brackets around the compound statement, so I can only assume I have completely wrong syntax, or the assumption that 'aError.reason' has a value is wrong.

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 months 1 week ago #245820 by tpartner
Replied by tpartner on topic Executing the survey Exit Link after an error
I don't think the aError array contains the reason. (responding from mobile so can't check that)

You can see the array contents with:
Code:
{{ dump(aError) }}

You may need to make it conditional on the error text.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

  • JEfromCanada
  • JEfromCanada's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
8 months 1 week ago - 8 months 1 week ago #245838 by JEfromCanada
Replied by JEfromCanada on topic Executing the survey Exit Link after an error
Thank you, Tony. I'm not yet clear as to what the double-brace-brackets imply.

To see the array contents, where do I place the "{{ dump(aError) }}" string? Is it self-executing? Does it need to be embedded in a specific tag (like a SPAN tag or P tag)?

I've tried placing that string in various places in the existing .twig file, and I cannot see any indication that something was generated (there wasn't even any additional white space evident).


EDIT:

OK, I started reading more about .twig files and learned that the dump command doesn't work until debug is enabled.  So I've done that and I'm now seeing the "message" string I need to test. Unfortunately, the message string has lots of trailing spaces, and when I tried to test the message using:  " if aError.message|trim == 'The original string as passed from the index file' ", the TRIM filter didn't seem to do anything. The comparison failed because my comparison string (in quotes) was shorter than the string passed through the index file (which included a bunch of trailing blanks).  I tried putting double-braces around the variable {{ aError.message|trim }} and that led to a fatal error. So, I'm not sure what the proper method is of stripping whitespace from the error message.

I even tried assigning the string to my own variable like:
{% set myVariable = aError.message|trim %} and then using myVariable in the comparison statement  (if myVariable == 'The original string as passed from the index file' ) and that didn't work either. I guess I'm close - just don't quite know the syntax well enough.
Last edit: 8 months 1 week ago by JEfromCanada.

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 months 1 week ago #245853 by tpartner
Replied by tpartner on topic Executing the survey Exit Link after an error
You can use the TWIG "in" operator - twig.symfony.com/doc/3.x/templates.html#containment-operator

Code:
{% set customContent = false %}
{% if 'You have already completed this survey' in aError.message %}
  {% set customContent = true %}
{% endif %}

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: JEfromCanada

Please Log in to join the conversation.

  • JEfromCanada
  • JEfromCanada's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
8 months 1 week ago - 8 months 1 week ago #245879 by JEfromCanada
Replied by JEfromCanada on topic Executing the survey Exit Link after an error
Well, my friend, you have solved the last remaining issue. Now, I just need to muster the nerve to do a Comfort Update on the LimeSurvey installation to update it.

No core file changes remain, and I assume the Comfort Update will not alter the files in the UPLOADS folder. I suppose I will need to do a file-to-file comparison between my altered .twig files and the newly updated ones to verify there haven't been changes to the original .twig files that I should be migrating over.

EDIT:
Just for my own satisfaction, can you tell me what I did wrong in trying to assign the "trimmed" value of the aError.message variable to a new variable for comparison purposes?  I had seen samples of the |trim filter and thought I had followed the example correctly.
Last edit: 8 months 1 week ago by JEfromCanada. Reason: Added a question about my use of syntax for the |trim filter

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 months 1 week ago - 8 months 1 week ago #245880 by tpartner
Replied by tpartner on topic Executing the survey Exit Link after an error
ComfortUpdate will not overwrite anything in the /uploads directory but it is always recommended to make a backup of the LimeSurvey directory and database before any update.

I'm on mobile for the next few days so cannot debug your |trim issue.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 8 months 1 week ago by tpartner.

Please Log in to join the conversation.

Lime-years ahead

Online-surveys for every purse and purpose