Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

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 2 years 6 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.

Re: Using Expressions Manager to auto-insert survey, group and question ID in js 2 years 6 months ago #75499

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4192
  • Thank you received: 788
  • Karma: 359
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Using Expressions Manager to auto-insert survey, group and question ID in js 2 years 6 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.

Re: Using Expressions Manager to auto-insert survey, group and question ID in js 2 years 6 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.

Re: Using Expressions Manager to auto-insert survey, group and question ID in js 2 years 6 months ago #75555

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4192
  • Thank you received: 788
  • Karma: 359
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Using Expressions Manager to auto-insert survey, group and question ID in js 2 years 6 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.

Re: Using Expressions Manager to auto-insert survey, group and question ID in js 2 years 6 months ago #75560

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4192
  • Thank you received: 788
  • Karma: 359
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Using Expressions Manager to auto-insert survey, group and question ID in js 2 years 6 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.

Re: Using Expressions Manager to auto-insert survey, group and question ID in js 2 years 6 months ago #75562

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4192
  • Thank you received: 788
  • Karma: 359
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Using Expressions Manager to auto-insert survey, group and question ID in js 2 years 6 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.
  • Page:
  • 1
  • 2
Moderators: ITEd
Time to create page: 0.140 seconds
Donation Image