Actions

NewQuestionAttributes: Difference between revisions

From LimeSurvey Manual

(Page create)
 
(13 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{FeatureStarting|2.50}}
=The event=
'''When'''
'''When'''


Line 4: Line 8:


'''Input'''
'''Input'''
None.


'''Possible output'''
'''Possible output'''
Line 9: Line 15:
* Adding new attribute : $this->getEvent()->append('questionAttributes', $array); where arry is the new attribute.
* Adding new attribute : $this->getEvent()->append('questionAttributes', $array); where arry is the new attribute.


New attribute definition array:
=Detail on definition=
 
==New attribute definition==
It's an array:
<pre>
<pre>
attributeName=>[
attributeName=>[
     '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' : Sort 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 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>
'''Exemple of usage'''
 
==Input type==
 
* switch : Switch (on|off) value send 0 or 1
* buttongroup : Button group, need options array
* singleselect : Single select in a DropDown
* text : a short text witout line feed
* integer : an integer
* columns : An integer betwwen 1 to 12
* textarea : text with line feed
 
=Exemple of usage=


<source lang="php">
<source lang="php">
Line 45: Line 66:
         $questionAttributes = array(
         $questionAttributes = array(
             'exampleOne'=>array(
             'exampleOne'=>array(
                 "types"=>"L",
                 "types"=>"STU",
                 'category'=>gT('Other'),
                 'category'=>gT('Other'),
                 'sortorder'=>1,
                 'sortorder'=>1,
                 'inputtype'=>'text',
                 'inputtype'=>'text',
                 'default'=>'',
                 'default'=>'',
                 "help"=>'An example for text.',
                 "help"=>'An example for short, long and huge text.',
                 "caption"=>'A text attribute'
                 "caption"=>'A text attribute'
             ),
             ),
Line 64: Line 85:
                 'default'=>0,
                 'default'=>0,
                 "help"=>'An example for singleselect.',
                 "help"=>'An example for singleselect.',
                 "caption"=>'A text attribute'
                 "caption"=>'A dropdown attribute'
             ),
             ),
         );
         );
Line 75: Line 96:
     public function beforeQuestionRender()
     public function beforeQuestionRender()
     {
     {
         $oAttributeOne=QuestionAttribute::model()->find("qid=:qid AND attribute=:attribute",array(":qid"=>$oEvent->get('qid'),":attribute"=>"exampleOne"));
         $oAttributeOne=QuestionAttribute::model()->find("qid=:qid AND attribute=:attribute",array(":qid"=>$this->getEvent()->get('qid'),":attribute"=>"exampleOne"));
         $oAttributeTwo=QuestionAttribute::model()->find("qid=:qid AND attribute=:attribute",array(":qid"=>$oEvent->get('qid'),":attribute"=>"exampleTwo"));
         $oAttributeTwo=QuestionAttribute::model()->find("qid=:qid AND attribute=:attribute",array(":qid"=>$this->getEvent()->get('qid'),":attribute"=>"exampleTwo"));
         /* OR */
         /* OR */
         $aAttributes=QuestionAttribute::model()->getQuestionAttributes($oEvent->get('qid'));
         $aAttributes=QuestionAttribute::model()->getQuestionAttributes($this->getEvent()->get('qid'));
         $soAttributeOne=$aAttributes["exampleOne"];
         $soAttributeOne=$aAttributes["exampleOne"];
         $soAttributeTwo=$aAttributes["exampleTwo"];
         $soAttributeTwo=$aAttributes["exampleTwo"];
Line 88: Line 109:
}
}
</source>
</source>
[[Category:Plugins events]]

Revision as of 17:19, 14 February 2020

 Hint: This features is available starting in version 2.50


The event

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.

Detail on definition

New attribute definition

It's an array:

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

Input type

  • switch : Switch (on|off) value send 0 or 1
  • buttongroup : Button group, need options array
  • singleselect : Single select in a DropDown
  • text : a short text witout line feed
  • integer : an integer
  • columns : An integer betwwen 1 to 12
  • textarea : text with line feed

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"=>$this->getEvent()->get('qid'),":attribute"=>"exampleOne"));
        $oAttributeTwo=QuestionAttribute::model()->find("qid=:qid AND attribute=:attribute",array(":qid"=>$this->getEvent()->get('qid'),":attribute"=>"exampleTwo"));
        /* OR */
        $aAttributes=QuestionAttribute::model()->getQuestionAttributes($this->getEvent()->get('qid'));
        $soAttributeOne=$aAttributes["exampleOne"];
        $soAttributeTwo=$aAttributes["exampleTwo"];
        /**
         * Do something
         */

    }
}