Check out the LimeSurvey source code on GitHub!
Welcome, Guest
Username: Password:

TOPIC: Using Expressions Manager to auto-insert survey, group and question ID in js

Using Expressions Manager to auto-insert survey, group and question ID in js 4 years 5 months ago #75428

I have a survey that uses javascript to manage the import of information from an external csv file. After much trial and error, the script works flawlessly - with one exception: It has to be manually updated for each copy or import of the survey. I am also using css to hide the question because the info is being imported so that it can be used in subsequent questions.

A separate process generates a csv file and inserts the survey id at the start of the csv file name so that it can be referenced uniquely for each survey. I need to know if Expressions Manager can be used insted of the specific reference for each survey element. If so, do you have any suggestions on the best way to revise the script? Here is the javascript code:

<script type="text/javascript" charset="utf-8">
$(document).ready(function() {

/* todo can we pull this automagically from the PHP later? */
/**** UPDATE THESE VALUES *****/
var surveyID = 79322;
var questiongroupID = 145;
var questionID = 3186;
/**** END UPDATE THESE VALUES *****/


/* Calculate everything else from that info above */
var myAnswerStub = "answer"+surveyID+"X"+questiongroupID+"X"+questionID;
var url = "../upload/metadata/survey_"+surveyID+".csv";


/* Create an array to hold the names */
var namesArr = new Array();
$.get(url, function(data){
/*alert("Data Loaded: " + data); */ /* DEBUG: see the CSV file contents*/

/* load the CSV file into the array */
/* NOTE: need to include the new js file /scripts/jquery/jquery.csv.min.js */
fullArray = jQuery.csv()(data);
if (fullArray.length > 1) {
/* get each row. */
/* assume row never goes above 9. if it does will need to increment differently. */
for (i = 1; i < fullArray.length; i++ ) {
columnArray = fullArray;
if (!!columnArray && !!columnArray[0] && !!document.getElementById(myAnswerStub+"SQ00" +i + "_SQ001")) {
document.getElementById(myAnswerStub+"SQ00" +i + "_SQ001").value = columnArray[0];
document.getElementById(myAnswerStub+"SQ00" +i + "_SQ002").value = columnArray[1];
document.getElementById(myAnswerStub+"SQ00"+ i + "_SQ003").value = columnArray[2];
}
}
}
});

});
</script>
The administrator has disabled public write access.

Using Expressions Manager to auto-insert survey, group and question ID in js 4 years 5 months ago #75499

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6212
  • Thank you received: 1376
  • Karma: 539
I'll let others speak to the EM capabilities but with JavaScript you can automatically find the survey and group IDs:
// 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];
}

And the question ID can be found using the question class. For example the ID of the first array on a page will be returned by:
var qID = $('.array-flexible-row:eq(0)').attr('id').split('question')[1];
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.

Using Expressions Manager to auto-insert survey, group and question ID in js 4 years 5 months ago #75550

Thanks for your help on this. I have two instances where I need the code to work. It worked, with some revision, on the first but the second instance isn't loading the content from the csv file as it should without a specific reference to the questionID.

Any help would would be appreciated.
The administrator has disabled public write access.

Using Expressions Manager to auto-insert survey, group and question ID in js 4 years 5 months ago #75552

Let me be more specific. Here is the code for the first implementation, which works:

Information for this Bid Request:<script type="text/javascript" charset="utf-8">

$(document).ready(function() {

/* todo can we pull this automagically from the PHP later? */
/**** UPDATE THESE VALUES *****/

// GTF-1 (dynamic questionID)
if($( 'input#fieldnames' ).length != 0) {
var fieldNames = $( 'input#fieldnames' ).attr('value');
var tmp = fieldNames.split(/\D/);
var surveyID = tmp[0];
var questiongroupID = tmp[1];
var questionID = tmp[2];
}

/**** END UPDATE THESE VALUES *****/

/* Calculate everything else from that info above */
var myAnswerStub = "answer"+surveyID+"X"+questiongroupID+"X"+questionID;
var url = "../upload/metadata/survey_extra_"+surveyID+".csv";

/* Create an array to hold the names */
var namesArr = new Array();
$.get(url, function(data){
/*alert("Data Loaded: " + data); */ /* DEBUG: see the CSV file contents*/

/* load the CSV file into the array */
/* NOTE: need to include the new js file /scripts/jquery/jquery.csv.min.js */
fullArray = jQuery.csv()(data);
if (fullArray.length > 1) {
/* get each row. */
/* assume row never goes above 9. if it does will need to increment differently. */
for (i = 1; i < fullArray.length; i++ ) {
columnArray = fullArray;
if (!!columnArray && !!columnArray[0] && !!document.getElementById(myAnswerStub+"SQ00" +i + "_SQ001")) {
document.getElementById(myAnswerStub+"SQ00" +i + "_SQ001").value = columnArray[0];
document.getElementById(myAnswerStub+"SQ00" +i + "_SQ002").value = columnArray[1];
document.getElementById(myAnswerStub+"SQ00"+ i + "_SQ003").value = columnArray[2];
}
}
}
});

});
</script>


And I've used the same code with changes for the file name/url but it's not functioning unless I hard code the question ID into the variables, which defeats the purpose. Do the two questions have to be in separate question qroups to function?
The administrator has disabled public write access.

Using Expressions Manager to auto-insert survey, group and question ID in js 4 years 5 months ago #75555

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6212
  • Thank you received: 1376
  • Karma: 539
Do the two questions have to be in separate question qroups to function?
No but your code - var questionID = tmp[2]; - will just grab the ID of the first question in the fieldNames input.

You need something like this (to grab the ID of the second array on the page):
var q2ID = $('.array-flexible-row:eq(1)').attr('id').split('question')[1];
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.

Using Expressions Manager to auto-insert survey, group and question ID in js 4 years 5 months ago #75557

Thanks for the tip. Actually, once I moved the question to its own group, the load worked perfectly. Which leads me to my next question: I am hiding the questions with css but need a way to dynamically insert the question ID within the CSS so that the survey can be copied without having to manually enter the number. Is there any code or solution that can accomplish that?

Thanks,
Norman
The administrator has disabled public write access.

Using Expressions Manager to auto-insert survey, group and question ID in js 4 years 5 months ago #75560

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6212
  • Thank you received: 1376
  • Karma: 539
No, but you can hide them with JavaScript:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() { 
 
		$('#question'+questionID+'').hide();
	});
 
</script>
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.

Using Expressions Manager to auto-insert survey, group and question ID in js 4 years 5 months ago #75561

thanks. The problem is that the questions have to load content that will be used in subsequent questions. The content is loading from a csv file, so the page actually has to load the question first so that the fields can populate with the data. Would the {SGQ} code work for this purpose? Haven't tried it yet, but it seems like it should be do-able.
The administrator has disabled public write access.

Using Expressions Manager to auto-insert survey, group and question ID in js 4 years 5 months ago #75562

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6212
  • Thank you received: 1376
  • Karma: 539
Yup, it should.

It shouldn't matter whether you hide them with CSS or JavaScript - the JavaScript just applies element level CSS to hide them.
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.

Using Expressions Manager to auto-insert survey, group and question ID in js 4 years 5 months ago #75667

Here is the code that I am currently using the hide the question:

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

Can you tell me how I would alter this code to incorporate the {SGQ} reference? All of the combinations that I have tried are not working.
The administrator has disabled public write access.

Using Expressions Manager to auto-insert survey, group and question ID in js 4 years 5 months ago #75668

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6212
  • Thank you received: 1376
  • Karma: 539
If you have defined a variable questionID with the code above you can do something like:
<script>
    $(document).ready(function() {
        $('#question'+questionID+'').hide();
    });
</script>
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.

Using Expressions Manager to auto-insert survey, group and question ID in js 4 years 5 months ago #75672

Maybe I am placing the code in the wrong area but the javascript seems to be having no impact. I've placed the previous code in the question edit box and the below code in the help field edit box:

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

I am using the same reference for questionID (questiongroupID) that I used in the question editor box and questionID2 is also a reference from the question edit box code. However, I have tried several variations and the only thing that seems to work is code that includes the specific question number - which then works seamlessly. Am I doing something wrong? Do I need to combine the two scripts? Any advice would be helpful.
The administrator has disabled public write access.

Using Expressions Manager to auto-insert survey, group and question ID in js 4 years 5 months ago #75676

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6212
  • Thank you received: 1376
  • Karma: 539
Use my code as an example.

This: #questiongroupID

Should just be: #question
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.

Using Expressions Manager to auto-insert survey, group and question ID in js 4 years 5 months ago #75680

Thanks for your help with this. I should have started the previous post by saying that I started with the code that you provided but it had no effect. I believe that the script is not capturing the question id. What I posted was my third attempt at restructing the script to try to make it work with no success. Could this be a placement issue as far as where the script is located? Should I try inserting it into one of the subquestion fields instead of the Help area?
The administrator has disabled public write access.

Using Expressions Manager to auto-insert survey, group and question ID in js 4 years 5 months ago #75681

You're awesome! I got it to work. I needed to insert the line of code into the larger code block that appeared earlier in the page. Since the earlier code block already establishes the questionID, it worked perfectly. There is one last component that I need help with. I have a series of datepickers that populates a series of fields in an array question. As with the previous, I need to alter the javascript to identify the field name so that the survey can be copied without the need for direct intervention. Here is that code:

<script type="text/javascript" charset="utf-8">

$(document).ready(function() {
var qID = 3173;

// Add column-specific classes to array cells
$('#question'+qID+' table.question tr').each(function(i, el){
$('> *', this).each(function(i, el){
$(el).addClass('col-'+i);
});
});

// Apply datepicker to text inputs
$('#question'+qID+' .col-1 input[type="text"], #question'+qID+' .col-2 input[type="text"]').datepicker({
dateFormat: 'mm/dd/yy'
});

});

</script>

Can you provide quideance on where and how the code should be altered so that the question ID is handled by the javascript?

Thanks in advance for your help.
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.269 seconds
Imprint                   Data Protection Statement                  Revocation information and revocation form