Actions

NewQuestionAttributes: Difference between revisions

From LimeSurvey Manual

No edit summary
No edit summary
Line 47: Line 47:
         $questionAttributes = array(
         $questionAttributes = array(
             'exampleOne'=>array(
             'exampleOne'=>array(
                 "types"=>"L",
                 "types"=>"SRTU",
                 '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 66: Line 66:
                 'default'=>0,
                 'default'=>0,
                 "help"=>'An example for singleselect.',
                 "help"=>'An example for singleselect.',
                 "caption"=>'A text attribute'
                 "caption"=>'A dropdown attribute'
             ),
             ),
         );
         );

Revision as of 15:09, 1 August 2016

  This event is new actually, usage of question attribute can be updated and break plugins compatibility in future release.


When

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

Input

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
   'options' : optionnal options if input type need it*
    'default' : the defaumt value
   '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"=>"SRTU",
                '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
         */

    }
}