Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Retrieving previous answers based on login

Hi there,

I have a survey where i've defined a login field. Each user can answer the survey a number of times. Can i, upon the user logging in, retrieve the number of times that this user answered the survey previously? And if this is possible can i list the dates that those surveys were answered?

Thanks.
asked Jul 7, 2016 by cdaniel (445 points)

1 Answer

+1 vote
You can accomplish this via a free format question, but it's going to get a bit tricky.  Please see the disclaimers at the bottom of this forum post.

Start by creating a free format question on the page immediately following the Start page.  Give the free format a single variable, with variable type "hidden" and data type "text."  Give the free format this HTML:

<input name="FreeFormatQ_FFVar" id="FreeFormatQ_FFVar" type="hidden" value="[% GetValue(FreeFormatQ_FFVar) %]">

<script>
$(document).ready(function(){
    var datetimes = $('#FreeFormatQ_FFVar').val();
    if (datetimes != '') {
        datetimes += ';';
    }
    datetimes += new Date();
    $('#FreeFormatQ_FFVar').val(datetimes);
})
</script>


"FreeFormatQ" must be replaced with the name of the free format question.  "FFVar" must be replaced with the name of the free format's variable.

The date-times that the free format was viewed is now stored in the variable, separated by semicolons.  With a bit of code, you can do whatever you want with these values in Perl or JavaScript.  Here's an example of how to print out the number of entries and date-times while on the same page as the free format question:

<span id="entry">
    <span>-1</span>
</span>

<script>
$(document).ready(function(){
    var entriesStr = SSI_GetValue('FreeFormatQ_FFVar');
    var entries = entriesStr.split(';');
    
    $('#entry span').text(entries.length);
    for (var i = 0; i < entries.length; i++) {
        $('#entry').append('<br><span>' + entries[i] + '</span>');
    }
})
</script>


And here's an example of how to print the number of entries and date-times while on a different page than the free format question:

<span id="entry">
    <span>-1</span>
</span>

<script>
$(document).ready(function(){
    var entriesStr = '[% GetValue(FreeFormatQ_FFVar) %]';
    var entries = entriesStr.split(';');
    
    $('#entry span').text(entries.length);
    for (var i = 0; i < entries.length; i++) {
        $('#entry').append('<br><span>' + entries[i] + '</span>');
    }
})
</script>


Be sure to replace "FreeFormatQ" and "FFVar" just like before.

Disclaimer #1: A survey entry is recorded every time the free format question is viewed, so if the respondent returns to the question via a skip or by pressing the Back button, another entry will be recorded.

One potential workaround to this problem is to hide the previous button on the page that comes after the free format question.

<script>
$(document).ready(function(){
    $('#previous_button').hide();
})
</script>


Disclaimer #2: The free format question must be seen when the respondent enters the study each time.  If they are skipped to the question they were last on, they won't see the free format question and their entry will not be recorded.

One potential workaround to this problem is to force respondents to return to the free format question on every entry.  You could then use a select question as a sort of table of contents, letting the respondent skip back to where they left off.  I will include an example of how to do this in a response to this answer.
answered Jul 7, 2016 by Zachary Platinum Sawtooth Software, Inc. (123,825 points)
By default, Lighthouse skips respondents to their last viewed page when they enter the survey with a password.  To prevent this default behavior, we're going to have to make modifications to the Perl file.

You may want to make a backup before modifying your Perl files.  I would recommend you test the survey carefully after making any Perl changes just in case.

(i) Locate your study's /cgi-bin folder.

(ii) Locate the file with the name to the effect of "ciwlib.pl" - it will likely have some other numbers in the name.  Open this file with a simple text editor, like Notepad.

(iii) Using your text editor's tools, search for the text "sys_LastQuestion" in the file.

(iv) Delete the surrounding area from the file.  It should look like this, with a few changes to "9_9_9" and "xx"/"xxx":

authlib9_9_9::_xxx([["sys_LastQuestion", $__xx]], $__xx, 0);


Don't delete anything before the "authlib" and don't delete anything after that semicolon.

(v) Save your changes to the .pl file.

Every respondent who enters the survey should now be put on the first page, where we've put the free format question.
select query
...