x

Main chapters

  1. LimeSurvey Cloud vs LimeSurvey CE
  2. LimeSurvey Cloud - Quick start guide
  3. LimeSurvey CE - Installation
  4. How to design a good survey (Guide)
  5. Getting started
  6. LimeSurvey configuration
  7. Introduction - Surveys
  8. View survey settings
  9. View survey menu
  10. View survey structure
  11. Introduction - Questions
  12. Introduction - Question Groups
  13. Introduction - Surveys - Management
  14. Survey toolbar options
  15. Multilingual survey
  16. Quick start guide - ExpressionScript
  17. Advanced features
  18. General FAQ
  19. Troubleshooting
  20. Workarounds
  21. License
  22. Version change log
  23. Plugins - Advanced
 Actions

NewQuestionAttributes: Difference between revisions

From LimeSurvey Manual

No edit summary
DenisChenu (talk | contribs)
No edit summary
Line 19: Line 19:
     'types' : Apply to this question type
     'types' : Apply to this question type
     'category' : Where to put it
     'category' : Where to put it
  'sortorder' : Qort order in this category
    'sortorder' : Qort order in this category
  'inputtype' : type of input
    'inputtype' : type of input : [buttongroup,columns,integer,singleselect,switch,text,textarea]
  'options' : optionnal options if input type need it*
    'options' : optionnal options if input type need it (for switch, singleselect and buttongroup)
     'default' : the defaumt value
     'default' : the default value
  'caption' : the label
    'i18n' : (optionnal) set to true for translatable attribute
  'help' : an help]
    'caption' : the label
    'help' : an help
]
]
</pre>
</pre>

Revision as of 15:58, 31 August 2016

 Hint: This features is available starting in version 2.50
  This event is new actually, usage of question attribute can be updated and break plugins compatibility in future release. This allow to add Question attribute with the core question. Question Object can improve the system, but broke plugin compatibility.


When

This settings happen each time the attribute definition is readed for the installation. In general : it happen one time only.

Input

None.

Possible output

  • Adding new attribute : $this->getEvent()->append('questionAttributes', $array); where arry is the new attribute.

New attribute definition array:

attributeName=>[
    'types' : Apply to this question type
    'category' : Where to put it
    'sortorder' : Qort order in this category
    'inputtype' : type of input : [buttongroup,columns,integer,singleselect,switch,text,textarea]
    'options' : optionnal options if input type need it (for switch, singleselect and buttongroup)
    'default' : the default value
    'i18n' : (optionnal) set to true for translatable attribute
    'caption' : the label
    'help' : an help
]

Exemple of usage

<?php
class addAnAttribute extends PluginBase
{
    protected $storage = 'DbStorage';

    static protected $name = 'addAnAttribute';
    static protected $description = 'Add an attribute for any question';
    public function init()
    {
        $this->subscribe('newQuestionAttributes');
        $this->subscribe('beforeQuestionRender');
    }
    /**
   * We add the attribute here
   */
    public function newQuestionAttributes()
    {
        $event = $this->getEvent();
        $questionAttributes = array(
            'exampleOne'=>array(
                "types"=>"STU",
                'category'=>gT('Other'),
                'sortorder'=>1,
                'inputtype'=>'text',
                'default'=>'',
                "help"=>'An example for short, long and huge text.',
                "caption"=>'A text attribute'
            ),
            'exampleTwo'=>array(
                "types"=>"L",
                'category'=>gT('Other'),
                'sortorder'=>2,
                'inputtype'=>'singleselect',
                'options'=>array(
                    0=>gT('No'),
                    1=>gT('No'),
                ),
                'default'=>0,
                "help"=>'An example for singleselect.',
                "caption"=>'A dropdown attribute'
            ),
        );
        $event->append('questionAttributes', $questionAttributes);
    }

   /**
  * We can use the attribute like this , for example
  */
    public function beforeQuestionRender()
    {
        $oAttributeOne=QuestionAttribute::model()->find("qid=:qid AND attribute=:attribute",array(":qid"=>$oEvent->get('qid'),":attribute"=>"exampleOne"));
        $oAttributeTwo=QuestionAttribute::model()->find("qid=:qid AND attribute=:attribute",array(":qid"=>$oEvent->get('qid'),":attribute"=>"exampleTwo"));
        /* OR */
        $aAttributes=QuestionAttribute::model()->getQuestionAttributes($oEvent->get('qid'));
        $soAttributeOne=$aAttributes["exampleOne"];
        $soAttributeTwo=$aAttributes["exampleTwo"];
        /**
         * Do something
         */

    }
}