Custom question attributes?

More
5 years 3 months ago #75091 by kcivey
kcivey created the topic: Custom question attributes?
I've seen some hints in the documentation that it's possible to create custom question attributes to store metadata associated with a question, but I haven't found any details on how to do this. How can custom attributes be created?

Please Log in to join the conversation.

More
5 years 3 months ago #75092 by TMSWhite
TMSWhite replied the topic: Custom question attributes?
What sort of question attributes are you wanting?

There is a question_attributes table that can store generic attributes, but it is geared to serving up data from the admin GUI screen. So, this is a convenient way to store attributes if you extend the underlying code without having to modify the data model (questions table). However, these attributes are not exposed at run-time (e.g. within the surveys).

There is also discussion of creating a more modular approach to questions to make it easier for people to add new question types (in future releases of LimeSurvey).

Please Log in to join the conversation.

More
5 years 3 months ago #75093 by kcivey
kcivey replied the topic: Custom question attributes?
Yes, I figured I could store data in the question_attributes table (I'm wanting to use it for something external to LimeSurvey), and I not expecting it to be exposed in the surveys. But I'd like to have admins be able to enter it when creating questions. Is there any easy way to include a new attribute in the question editing interface?

Please Log in to join the conversation.

More
5 years 3 months ago #75252 by Mazi
Mazi replied the topic: Custom question attributes?
This way isn't "easy" because there are several files that have to be edited.


Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"

Please Log in to join the conversation.

More
4 years 8 months ago #86523 by Fred01
Fred01 replied the topic: Custom question attributes?
Hi @kcivey, don't know if you ever found the answer, but I had the same problem. If you're just looking to add some "inert" attributes that will simply be displayed in the question editor and save to the DB (like question metadata) then it's a pretty simple edit to the PHP.

There is no reference data in the DB on what the valid attributes are. It's all in the code in the QuestionAttributes() function. In 1.92 this is in common_functions.php. In 2.0 it's in application/helpers/common_helper.php.

Here is a typical block from the function, for the maximum characters attribute. It show the question types, what category to display it in on the question form, etc.
$qattributes["maximum_chars"]=array(
"types"=>"STUNQK:;",
'category'=>$clang->gT('Input'),
'sortorder'=>100,
'inputtype'=>'text',
"help"=>$clang->gT('Maximum characters allowed'),
"caption"=>$clang->gT('Maximum characters'));

I wanted to control the number of decimals to display on reports in an outside system that imports the Limesurvey data. So below is the block I added. Just putting it in the function and saving was all that it took to have it show up on the question edit form (in a separate category called Custom) and it saves and retrieve from the DB. Since all attributes are stored in an "EAV" style table there are no DB changes necessary. Again, I'm not looking for this to affect behavior anywhere else in LS, just to be available for export. Also, anytime there is an update it may overwrite the file and you'll need to edit it again.

$qattributes["decimal_display"]=array(
"types"=>"*KN",
'category'=>$clang->gT('Custom'),
'sortorder'=>80,
'inputtype'=>'singleselect',
'options'=>array(0=>$clang->gT('0'),
1=>$clang->gT('0.0'),
2=>$clang->gT('0.00')),
'default'=>0,
"help"=>$clang->gT('Decimal places to display in reports'),
"caption"=>$clang->gT('Report decimal display'));

Any opinions from the developer team on this?

Please Log in to join the conversation.

More
4 years 8 months ago #86557 by Mazi
Mazi replied the topic: Custom question attributes?
Thanks for these detailed instructions, Fred.

The approach is absolutely correct, but if you want to use a question attribute which is used within a survey, e. g. to manipulate how certain question type(s) are styled, you also need to load the question attribute data.
This is done at "qanda.php" (Limesurvey 1.92 and previous) where you can find a function for each question type, like "do_list_radio()". You have to add such attribute related code to all question types listed at the "types" setting.


Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"

Please Log in to join the conversation.

More
4 years 8 months ago #86622 by Fred01
Fred01 replied the topic: Custom question attributes?
Thanks Marcel. In my case I'm not expecting the new attributes to affect anything in the survey presentation so I didn't add anything to qanda.php. I just want them to be available for export (and to show up in the fieldmap).

I did test that the following work correctly.

- Can add new attributes to q_attribute function in LS or LS2 and the question edit form works correctly
- Can assign existing attributes to additional question types (eg, making prefix an attribute of equations)
- Question works normally for survey taker, new attribute ignored (since I didn't change qanda.php)
- Question attributes export correctly in create_fieldmap()

Please Log in to join the conversation.

More
2 years 1 month ago #120066 by garvsaxena
garvsaxena replied the topic: Custom question attributes?
hi all,

I want to create a new custom question type for our survey, i need a textbox, listbox both in a multiple array format just like a table. can anyone help me out its very urjent!

Please Log in to join the conversation.

More
2 years 1 month ago #120079 by tpartner
tpartner replied the topic: Custom question attributes?

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 1 month ago #120086 by garvsaxena
garvsaxena replied the topic: Custom question attributes?
It works good, thanks tpartner!!
I have one more question that if we want to Perform Sum on two numeric inputs and display the results in the 3rd input box but in the same question, then what should I do??

Please Log in to join the conversation.

More
2 years 1 month ago #120108 by tpartner
tpartner replied the topic: Custom question attributes?
Why do you need the sum in a third input? You can use Expression Manager to pipe the sum into text.

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 1 month ago #120113 by garvsaxena
garvsaxena replied the topic: Custom question attributes?
Ok got it..! I think this should solve my problem. But suppose I have a question with the ID = GE-1, and has options SQ001, SQ002, SQ003 and SQ004. Now we have to do that show the sum of SQ001 + SQ002 + SQ003 will display in the SQ004.

Please guide me the Expression and Relevance equation for the thing I want to achieve.

Please Log in to join the conversation.

More
2 years 1 month ago #120123 by tpartner
tpartner replied the topic: Custom question attributes?
I don't think you can easily do that with Expression Manager but you could do it with a little script. Add this to the question source.

<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() {
 
		// Identify this question
		var thisQuestion = $('#question{QID}');
 
		// Add some classes
		$('input.text', thisQuestion).addClass('active-item');
		$('input.text:last', thisQuestion).removeClass('active-item').addClass('total-item');
 
		// Disable the last input
		$('.total-item', thisQuestion).prop('readonly', true);
 
		// Listener on the inputs
		$('.active-item', thisQuestion).on('keyup change paste', function() {
			setTimeout(function() { // Pause required for paste
				// Calculate and insert the total
				var total = 0;
				$('.active-item', thisQuestion).each(function(i) {
					total = total + Number($(this).val());
				});
				$('.total-item', thisQuestion).val(total);
			}, 100);
		});
	});
</script>

File Attachment:

File Name: limesurvey...5914.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:

Please Log in to join the conversation.

More
2 years 1 month ago #120130 by garvsaxena
garvsaxena replied the topic: Custom question attributes?
Ok thanx for your support..!

but Please guide me where and in which file I have to paste this script which you have send me..!

Please Log in to join the conversation.

More
2 years 1 month ago #120132 by tpartner
tpartner replied the topic: Custom question attributes?
Did you look at the attached sample survey? The script is in the question source.

Please refer to the manual - manual.limesurvey.org/Workarounds:_Manip....29_in_LimeSurvey.3F


.

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