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

TOPIC: Custom question attributes?

Custom question attributes? 4 years 5 months ago #75091

  • kcivey
  • kcivey's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 2
  • Karma: 0
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?
The administrator has disabled public write access.

Custom question attributes? 4 years 5 months ago #75092

  • TMSWhite
  • TMSWhite's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 758
  • Thank you received: 83
  • Karma: 37
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).
The administrator has disabled public write access.

Custom question attributes? 4 years 5 months ago #75093

  • kcivey
  • kcivey's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 2
  • Karma: 0
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?
The administrator has disabled public write access.

Custom question attributes? 4 years 4 months ago #75252

  • Mazi
  • Mazi's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5974
  • Thank you received: 366
  • Karma: 260
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)limesurvey.org'"
The administrator has disabled public write access.

Custom question attributes? 3 years 9 months ago #86523

  • Fred
  • Fred's Avatar
  • Offline
  • Expert Lime
  • Posts: 147
  • Thank you received: 5
  • Karma: 3
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?
The administrator has disabled public write access.

Custom question attributes? 3 years 9 months ago #86557

  • Mazi
  • Mazi's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5974
  • Thank you received: 366
  • Karma: 260
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)limesurvey.org'"
The administrator has disabled public write access.

Custom question attributes? 3 years 9 months ago #86622

  • Fred
  • Fred's Avatar
  • Offline
  • Expert Lime
  • Posts: 147
  • Thank you received: 5
  • Karma: 3
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()
The administrator has disabled public write access.

Custom question attributes? 1 year 2 months ago #120066

  • garvsaxena
  • garvsaxena's Avatar
  • Offline
  • Senior Lime
  • Posts: 41
  • Karma: 1
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!
The administrator has disabled public write access.

Custom question attributes? 1 year 2 months ago #120079

  • tpartner
  • tpartner's Avatar
  • Online
  • LimeSurvey Team
  • Posts: 6122
  • Thank you received: 1357
  • Karma: 533
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.

Custom question attributes? 1 year 2 months ago #120086

  • garvsaxena
  • garvsaxena's Avatar
  • Offline
  • Senior Lime
  • Posts: 41
  • Karma: 1
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??
The administrator has disabled public write access.

Custom question attributes? 1 year 2 months ago #120108

  • tpartner
  • tpartner's Avatar
  • Online
  • LimeSurvey Team
  • Posts: 6122
  • Thank you received: 1357
  • Karma: 533
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.
The administrator has disabled public write access.

Custom question attributes? 1 year 2 months ago #120113

  • garvsaxena
  • garvsaxena's Avatar
  • Offline
  • Senior Lime
  • Posts: 41
  • Karma: 1
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.
The administrator has disabled public write access.

Custom question attributes? 1 year 2 months ago #120123

  • tpartner
  • tpartner's Avatar
  • Online
  • LimeSurvey Team
  • Posts: 6122
  • Thank you received: 1357
  • Karma: 533
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.
The administrator has disabled public write access.

Custom question attributes? 1 year 2 months ago #120130

  • garvsaxena
  • garvsaxena's Avatar
  • Offline
  • Senior Lime
  • Posts: 41
  • Karma: 1
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..!
The administrator has disabled public write access.

Custom question attributes? 1 year 2 months ago #120132

  • tpartner
  • tpartner's Avatar
  • Online
  • LimeSurvey Team
  • Posts: 6122
  • Thank you received: 1357
  • Karma: 533
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.
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.666 seconds
Imprint                   Data Protection Statement                  Revocation information and revocation form