How to calculate age

Topics: General '15, Source Code & Change Sets, Survey Project Team
Jul 30, 2012 at 11:49 AM
Edited Jul 30, 2012 at 11:50 AM

Hello, I'm new in the Survey Project. I must say I find it interesting so far. What I want to ask is the following:

 

I have a question where I ask the date of birth of the user. Bellow that, I want to verify his age by asking him:

So your age is [age], correct?

- Yes

- No

Yes and No are radio buttons. What I don't know how to do is how to automatically show the age of the person there, calculated from the date of birth he submitted in previous question. Can it be done? And how?

Coordinator
Jul 30, 2012 at 4:53 PM

Hello,

You can use the answer piping options for this. There is a live demo available at http://www.surveyproject.org/Samples/SurveyAnswerPiping/tabid/127/Default.aspx to see what the effect is.

Part of the Survey Project 2.0 installation files are two sample survey. One of these called: AnswerPiping_SurveySample_SP20.xml. It can be found in the _SampleSurveys directory. If you import this survey into your installation you can see how the answer piping is set up.

Hopefully this will help you out.

Kind regards,
Survey Project Coordinator

Aug 1, 2012 at 7:30 AM

Yes, I understand about piping. Thank you for the answer.

I decided that it will be better to add a query string in the question, which will calculate the age through the date of birth. I tried putting the query code in the body of its corresponding question but after I press 'update', it comments out the code.

for example I want to do this:

So your age is "age-calculation-query", correct?

- Yes

- No

So how could I add an SQL query in the question without it being commented out? Is there something relative to the ##yourquerystringvariablename##?

Coordinator
Aug 1, 2012 at 7:22 PM

Hi,

Sorry for misunderstanding the first time. You could try using a javascript and add it to the second question to calculate the age based on the answer that is piped to it. It would be something like below (just get rid of the button and alert box)

Based on your birthdate [[age]], your age is [scriptresult]:

<button onclick="getAge('[[age]]')">Your Age</button>

<script language="javascript" type="text/javascript">
<!--
function getAge(dateString) {
    var today = new Date();
    var birthDate = new Date(dateString);
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
        age--;
    }
   alert(age);
}

//-->
</script>

Kind regards
Survey Project Coordinator

Aug 2, 2012 at 8:02 AM
Edited Aug 2, 2012 at 10:56 AM

That worked perfectly. Much appreciate your help. One more question: If I want to take that value of age and store it in a hidden field, is there a way to do it?

 

(I am documenting everything for future reference btw)

 

EDIT: Also if its possible can you explain to me how the answers of each question are stored?

I have this question:

Question: How long have you been in the city?

Answers: [integer_number] + Days[days_radio_button] + Months[months_radio_button]

So how can this be in a somewhat readable form in the reports? I have tried to work with reports but being new to this I get kind of confused.

Coordinator
Aug 5, 2012 at 8:35 AM

Hello,

Re Hidden Field: it's probably possible but you need a (java) scripting expert for this. Maybe one of the other projectmembers can help you out. Otherwise try some of the java script forums.

Re Reporting: all answers are stored in the database and can be exported if needed. The Graphical reporting option will not show all answers/ answertypes. In your example you probably use a Field Basic answer type to save the [Integer number]. Field answer types contain free text and therefore will not show in the Graphical reports. To see free text results use the Voter Reports options.

Kind regards
Survey Project Coordinator