Welcome to the LimeSurvey Community Forum

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

How do I change the page requesting a token for authentication? (copied)

  • eschneider
  • eschneider's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
10 years 5 months ago #100520 by eschneider
We solved this by changing the terminology in our invite to say 'Token' rather than what we had used previously 'PIN/Personal Identification Number'.

We would have preferred changing the text in Lime but we wanted to avoid changing our local version of Lime.
The topic has been locked.
More
10 years 4 months ago #101916 by NetMarie
In case somebody stumbles upon this thread looking for another answer: due to lack of knowledge, we asked the limesurvey-support-people to change the related file for us. We didn't customize the whole message but had two spelling mistakes corrected.
The topic has been locked.
More
5 years 7 months ago - 5 years 7 months ago #172456 by socius
Hi,

time to go on with this thread ;-)

I'd like to use survey specific messages for undefined tokenlabel, tokentext, tokenerror, i.e. the page that comes up when someone enters a sid that does not exist. I use LS 2.6.7LTS.

I tried to use an if-condition to discriminate between a closed (only participants with token can take part) survey with sid = 111111 and all the other surveys - but that failed. I'm still a novice in JS and probably overlook something decisive. Who can help?

I started with Denis' Code ( www.limesurvey.org/forum/can-i-do-this-w...ication-copied#78077 ) which works perfectly. It replaces the given messages by the ones entered for tokenlabel tokentext, tokenerror.

Code:
$(document).ready(function(){
    if (typeof tokenlabel === "undefined"){
        tokenlabel ="YOUR TOKEN LABEL : ";
    }
    if (typeof tokentext === "undefined"){
        tokentext ="YOUR TOKEN TEXT DESCRIPTION";
    }
    if (typeof tokenerror === "undefined"){
        tokenerror ="<br><br><strong class='error'>YOUR ERROR MESSAGE.</strong>";
    }
 
   if ($("#tokenform").length )
   {
        $("#tokenmessage").html(tokentext);
        $("label[for='token']").text(tokenlabel);
   }
   else
   {
       $("#tokenmessage").html(tokentext+tokenerror);
   }
 
})


Now my idea was to split this into two. I tried with two if conditions - one for SID = 111111 and one for the other cases. I thought it's possible to get the SID and use it in the if. (I put in a little window.alert to show me the content of this_surveyId, that should contain the SID)

Code:
$(document).ready(function(){
 
  var this_surveyId = '{SID}'; /*gets the SID */
  window.alert(this_surveyId); /*returns the string "{SID}" and not the actual SID - I guess, I'm the only surprised here :-)*/
 
  if (this_surveyId === 111111){
 
    if (typeof tokenlabel === "undefined"){
        tokenlabel ="1 YOUR TOKEN LABEL : ";
    }
 
    if (typeof tokentext === "undefined"){
        tokentext ="1 YOUR TOKEN TEXT DESCRIPTION {SID}";
    }
 
    if (typeof tokenerror === "undefined"){
        tokenerror ="1 <br><br><strong class='error'>YOUR ERROR MESSAGE.</strong>";
    }
  }
 
  if (this_surveyId !== 111111){   
 
    if (typeof tokenlabel === "undefined"){
        tokenlabel ="2 YOUR TOKEN LABEL : ";
    }
 
    if (typeof tokentext === "undefined"){
        tokentext ="2 YOUR TOKEN TEXT DESCRIPTION {SID}";
    }
 
    if (typeof tokenerror === "undefined"){
        tokenerror ="2 <br><br><strong class='error'>YOUR ERROR MESSAGE.</strong>";
    }
 
  }
 
   if ($("#tokenform").length )
   {
        $("#tokenmessage").html(tokentext);
        $("label[for='token']").text(tokenlabel);
   }
   else
   {
       $("#tokenmessage").html(tokentext+tokenerror);
   }
 
})


But: When I enter the survey url, e.g. www.foo.com/ls/index.php/survey/index/sid/111111 - I do not get the expected "1 YOUR TOKEN TEXT DESCRIPTION 1 {SID}" and "1 YOUR TOKEN LABEL : ", but "2 YOUR TOKEN TEXT DESCRIPTION {SID}";




The problem seems: I somehow fail to get the SID - as the window.alert and the output shows. It returns "{SID}" as a string but not the SID itself.

Hm... how could I achieve this? Sorry, if I overlook something obvious.


I appreciate any help!
Thanks for your time and all the best,
G
Last edit: 5 years 7 months ago by socius. Reason: added comment
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 7 months ago - 5 years 7 months ago #172461 by tpartner
A couple of things...

1) You cannot access Expression Manager variables from template.js - they are only available in the survey. Try this to access the SID:

Code:
  // Find the survey and group IDs
  if($( 'input#fieldnames' ).length != 0) {
    var fieldNames = $('input#fieldnames').attr('value');
    var tmp = fieldNames.split('X');
    var sID = tmp[0];
    var gID = tmp[1];
  }


2) Using the triple-equals sign searches for an exact comparison, including variable type, and since you have enclosed {SID} in quotes, it will be a string and then you try to compare it to a number in your IF() statement. Try this (given the var from above):

Code:
if (sID == 111111){

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 5 years 7 months ago by tpartner.
The following user(s) said Thank You: socius
The topic has been locked.
More
5 years 7 months ago - 5 years 7 months ago #172506 by socius
Hi,

thanks so much @tpartner!

I tried to get the SID with the given code, but it did not return the SID, since console.log($('input#fieldnames' ).length); . returns 0, i.e. there are no fieldnames on the page where participants can enter their token. Right?
Code:
$( document ).ready( function() {
  // Find the survey and group IDs
  if($( 'input#fieldnames' ).length != 0) {
    var fieldNames = $('input#fieldnames').attr('value');
    var tmp = fieldNames.split('X');
    var sID = tmp[0];
    var gID = tmp[1];
  }
 
  // But: this does not work on this page, because ...
  console.log($('input#fieldnames' ).length);  // ... returns 0, i.e. there are no fieldnames on the page where participants can enter their token. Right?
});

But that brought me to the idea to extract the SID from the URL - unfortunately it's hardcoded, but it works in my case: I get the survey specific token messages I wanted.
Code:
$(document).ready(function(){
 
  /* Extract SID from URL */
  var pathname = window.location.pathname; /* Get the pathname from the URL */
  var pathArray = window.location.pathname.split( '/' );
  var sID = pathArray[3]; /* in my case (LSdirectory/index.php/111111/newtest/Y#) the SID sits at position 3 */
  console.log(sID);  /* returns the SID extracted from the URL */
 
  if (sID == 111111){
 
    if (typeof tokenlabel === "undefined"){
        tokenlabel ="1 YOUR TOKEN LABEL : ";
    }
 
    if (typeof tokentext === "undefined"){
        tokentext ="1 YOUR TOKEN TEXT DESCRIPTION {SID}";
    }
 
    if (typeof tokenerror === "undefined"){
        tokenerror ="1 <br><br><strong class='error'>YOUR ERROR MESSAGE.</strong>";
    }
  }
 
  if (sID != 111111){
 
    document.getElementById("token").style.display="none";  /* hides the tokenform */
 
    if (typeof tokenlabel === "undefined"){
        tokenlabel ="2 YOUR TOKEN LABEL : ";
    }
 
    if (typeof tokentext === "undefined"){
        tokentext ="2 YOUR TOKEN TEXT DESCRIPTION {SID}";
    }
 
    if (typeof tokenerror === "undefined"){
        tokenerror ="2 <br><br><strong class='error'>YOUR ERROR MESSAGE.</strong>";
    }
 
  }
 
   if ($("#tokenform").length )
   {
        $("#tokenmessage").html(tokentext);
        $("label[for='token']").text(tokenlabel);
   }
   else
   {
       $("#tokenmessage").html(tokentext+tokenerror);
   }
 
})

Also thanks a lot for your hint with the "===" exact comparison - it's decisive in this case - thanks a lot!

Is the upper code "OK"? Or is there something I should avoid? (probably hardcoding :-)


Thanks and all the best,
G
Last edit: 5 years 7 months ago by socius. Reason: Add question
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 7 months ago - 5 years 7 months ago #172508 by tpartner
Oh, yeah, sorry I forgot that you weren't in the survey yet so there is no fieldnames input.

Parsing the URL works but you could get the sID from the action attribute of the form:



Code:
var sID = $('#limesurvey').attr('action').split('sid=')[1];

Regarding your code, it may be slightly more efficient to use an ELSE statement instead of two IFs when testing for the sID.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 5 years 7 months ago by tpartner.
The following user(s) said Thank You: socius
The topic has been locked.
More
5 years 7 months ago #172524 by socius
In version 2.6.7LTS I see
Code:
</script><form id="tokenform" autocomplete="off" action="/fragen/index.php/111111" method="post">
If that's correct I simply stay with the extraction from the URL.

I replace the second
Code:
if (sID != 111111){
by
Code:
else {
which works! Thanks again!

Best,
G
The topic has been locked.
More
5 years 7 months ago #172717 by socius
Hi,

Just for the record: I found another solution to get the sid which is a little less hardcoded (it does not fix the position, at least). I had to look for such a solution since I just ran into an error caused by this ;-)

Best, G

Code:
$(document).ready(function(){
 
  /* Extract SID from URL */
  var pathname = window.location.pathname; /* Get the pathname from the URL */
  var pathArrayStep1 = window.location.pathname.split( 'sid/' )[1];  /* Split the pathname in two (at the sid) and get the second (=1) element of the array */
  var sID = pathArrayStep1.split( '/' )[0];  /* Split the pathname at the "/" and get the first (=0) element of the array */
  console.log(sID);  /* returns the SID extracted from the URL */
 
 
  if (sID == 111111){
 
    if (typeof tokenlabel === "undefined"){
        tokenlabel ="1 YOUR TOKEN LABEL : ";
    }
 
    if (typeof tokentext === "undefined"){
        tokentext ="1 YOUR TOKEN TEXT DESCRIPTION {SID}";
    }
 
    if (typeof tokenerror === "undefined"){
        tokenerror ="1 <br><br><strong class='error'>YOUR ERROR MESSAGE.</strong>";
    }
  }
 
  if (sID != 111111){
 
    document.getElementById("token").style.display="none";  /* hides the tokenform */
 
    if (typeof tokenlabel === "undefined"){
        tokenlabel ="2 YOUR TOKEN LABEL : ";
    }
 
    if (typeof tokentext === "undefined"){
        tokentext ="2 YOUR TOKEN TEXT DESCRIPTION {SID}";
    }
 
    if (typeof tokenerror === "undefined"){
        tokenerror ="2 <br><br><strong class='error'>YOUR ERROR MESSAGE.</strong>";
    }
 
  }
 
   if ($("#tokenform").length )
   {
        $("#tokenmessage").html(tokentext);
        $("label[for='token']").text(tokenlabel);
   }
   else
   {
       $("#tokenmessage").html(tokentext+tokenerror);
   }
 
})
 
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose