Welcome, Guest
Username: Password: Remember me

TOPIC: Custom question attributes?

Custom question attributes? 2 years 7 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.

Re: Custom question attributes? 2 years 7 months ago #75092

  • TMSWhite
  • TMSWhite's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 759
  • Thank you received: 82
  • Karma: 36
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.

Re: Custom question attributes? 2 years 7 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.

Re: Custom question attributes? 2 years 7 months ago #75252

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5331
  • Thank you received: 296
  • Karma: 249
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.

Re: Custom question attributes? 2 years 2 weeks ago #86523

  • Fred
  • Fred's Avatar
  • OFFLINE
  • Gold Lime
  • Posts: 163
  • 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.

Re: Custom question attributes? 2 years 2 weeks ago #86557

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5331
  • Thank you received: 296
  • Karma: 249
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.

Re: Custom question attributes? 2 years 1 week ago #86622

  • Fred
  • Fred's Avatar
  • OFFLINE
  • Gold Lime
  • Posts: 163
  • 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.
Moderators: ITEd
Time to create page: 0.129 seconds
Donation Image