Data/Time questions, minimum time

More
2 years 9 months ago #117932 by Marieke123
Marieke123 created the topic: Data/Time questions, minimum time
Hello,
I need some help to restrict the input on a date/time question. I will try to specify the questions with the 3 questions in my survey.
Q1: One radio button question which ask patients whether they think they seldom to never / sometimes / or often suffer from tremors. So for example they choose: seldom to never
Q2: a data/time question (specified to time HH:MM), where patients are asked the amount of time they think they suffer from tremors per day. So for example 1 hrs en 15 minutes.
Q3: Then they will get two follow up questions depending on what they answered in question 1. So imagine you are suffering sometimes from tremors instead of seldom to never, how much time would you then suffer from tremors per day. Again a data/time question HH:MM. But I would like to add a restriction that the amount of time for sometimes suffering should be higher than for seldom to never suffering from tremors. Consequently, the often suffer from tremors time question should be higher than the amount of time for sometimes suffering from tremors.
Is this possible in limesurvey?

Please Log in to join the conversation.

More
2 years 9 months ago #117940 by tpartner
tpartner replied the topic: Data/Time questions, minimum time
You can use separate short-text questions for Q2, each shown conditionally depending on the answer to Q1. Then use JavaScript to insert your own timepicker into the Q2s with different "hourMin" options.

Set up your survey to use JavaScript and place the following script in the source of the Q2 questions, modifying "hourMin" in each as required. (there will be some file-loading redundancy but that shouldn't hurt)
<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() {
 
		// Identify this question
		var thisQuestion = $('#question{QID}');
 
		// Load the timepicker JS and CSS
		var surveyRoot = location.pathname.split('index.php')[0];
		$('<link rel="stylesheet" type="text/css" href="'+surveyRoot+'third_party/jquery-ui-timepicker-addon/jquery-ui-timepicker-addon.css" >').appendTo('head');
		$.getScript(surveyRoot+'third_party/jquery-ui-timepicker-addon/jquery-ui-timepicker-addon.js', function() {
			// Okay, JS is loaded...initialize the timepicker
			$('input[type="text"]', thisQuestion).timepicker({
				timeFormat: "HH:mm",
				hourMin: 2,
				showTime: true,
				onClose: function() {
					$('button.ui-datepicker-current').show();
				}
			});
		});
 
		// Hide the "Now" button and reposition the timepicker
		$('input[type="text"]', thisQuestion).on('click focus', function() {
			$('button.ui-datepicker-current').hide();
			$("#ui-datepicker-div").position({
				my: "left top",
				at: "left-3 top-3",
				of: $(this)
			});
		});
 
	});
</script>

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
2 years 9 months ago - 2 years 9 months ago #117946 by Marieke123
Marieke123 replied the topic: Data/Time questions, minimum time
Hi Tony,
Thanks for your help.
I tried your solution, but still something is going wrong. I do not get a timepicker.. but just the short text box, where I can fill in anything.
The only thing I changed is the QID in the right question number.. (number of Q2).
I am using version 2.0+ Build 131009

And what do you exactly mean with the last part modifying "hourMin" in each as required. (there will be some file-loading redundancy but that shouldn't hurt)

hourMin: 2,

what do I have to fill in here? a question id or a time and in which format?

And don't I have to place anything in the source code of Q3? Because in that question patients have to fill in a time which is higher than the previous filled in time.

Greetings,
Marieke
Last Edit: 2 years 9 months ago by Marieke123. Reason: spelling

Please Log in to join the conversation.

More
2 years 9 months ago #117948 by tpartner
tpartner replied the topic: Data/Time questions, minimum time

The only thing I changed is the QID in the right question number

Do not change that.


Modifying "hourMin" sets the minimum hours allowed in that timepicker.

Because in that question patients have to fill in a time which is higher than the previous filled in time.

Sorry, I missed that requirement - it will require a listener on Q2 which I don't have time for today.

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: Marieke123

Please Log in to join the conversation.

More
2 years 9 months ago - 2 years 9 months ago #117993 by tpartner
tpartner replied the topic: Data/Time questions, minimum time
Okay, I played around with this a bit and here's what I came up with. This script when placed in the first time question (timeQuestion1):
- Automatically detects the second date question (timeQuestion2)
- Initiates a timepicker on timeQuestion1
- Puts a listener on timeQuestion1
- When timeQuestion1 is changed, re-initiates a timepicker on timeQuestion2 with minimum hours greater than timeQuestion1
- Enforces the use of the timepickers for the questions

Note that timeQuestion1 and timeQuestion2 must be sequential short-text questions.

<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() {
 
		// Identify the questions
		var timeQuestion1 = $('#question{QID}');
		var timeQuestion2 = timeQuestion1.nextAll('.text-short:eq(0)');
		var timeQuestions = $(timeQuestion1).add(timeQuestion2);
 
		// Load the timepicker JS and CSS
		var surveyRoot = location.pathname.split('index.php')[0];
		$('<link rel="stylesheet" type="text/css" href="'+surveyRoot+'third_party/jquery-ui-timepicker-addon/jquery-ui-timepicker-addon.css" >').appendTo('head');
		$.getScript(surveyRoot+'third_party/jquery-ui-timepicker-addon/jquery-ui-timepicker-addon.js', function() {
			// Okay, JS is loaded...initialize the Q1 timepicker
			$('input[type="text"]', timeQuestion1).timepicker({
				timeFormat: "HH:mm",
				showTime: true,
				onClose: function() {
					checkconditions(this.value, this.name, this.type);
					q2Timepicker(this.value);
					$('button.ui-datepicker-current').show();
					$(this).blur();
				}
			});
 
			if($('input[type="text"]', timeQuestion1).val() != '') {
				q2Timepicker($('input[type="text"]', timeQuestion1).val());
			}
		});
 
		// Hide the "Now" button and enforce using the timepicker
		$('input[type="text"]', timeQuestions).attr('readonly', 'readonly').on('click focus', function() {
			$('button.ui-datepicker-current').hide();
			$("#ui-datepicker-div").position({
				my: "left top",
				at: "left-3 top-3",
				of: $(this)
			});
		});		
 
		// A function to reset Q2 and initialize a timepicker
		function q2Timepicker(q1Time) {
			var q1Hours = q1Time.split(':')[0];
			var minHours = Number(q1Hours) + 1;
			var q2Hours = '';
			if($('input[type="text"]', timeQuestion2).val() != '') {
				q2Hours = $('input[type="text"]', timeQuestion2).val().split(':')[0];
			}
			if(q1Hours >= q2Hours) {
				$('input[type="text"]', timeQuestion2).val('');
			}
			$('input[type="text"]', timeQuestion2).timepicker('destroy').timepicker({
				timeFormat: "HH:mm",
				hourMin: minHours,
				showTime: true,
				onClose: function() {
					checkconditions(this.value, this.name, this.type);
					$('button.ui-datepicker-current').show();
					$(this).blur();
				}
			});
		}		
	});
</script>

Sample survey:

File Attachment:

File Name: limesurvey...3-06.lss
File Size:16 KB

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
Last Edit: 2 years 9 months ago by tpartner.

Please Log in to join the conversation.

More
2 years 9 months ago #118000 by Marieke123
Marieke123 replied the topic: Data/Time questions, minimum time
Thanks again for your help. As I understand it correctly, I have to make two short free text questions. And in the first short question I have to place the script in the source code.
I don't have to change anything in the script?

because unfortunately it is not working.. I am still not seeing the timepicker. I have tried another template.. Am I not working in a too old build of limemsurvey? 2.0+ Build 131009

Please Log in to join the conversation.

More
2 years 9 months ago #118002 by tpartner
tpartner replied the topic: Data/Time questions, minimum time
Yes, you will need to update to 2.05.

Nothing in the script needs editing as you'll see in the attached survey.


.

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!