NewQuestionAttributes: Difference between revisions
From LimeSurvey Manual
DenisChenu (talk | contribs) |
|||
(6 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{{FeatureStarting|2.50}} | {{FeatureStarting|2.50}} | ||
=The event= | =The event= | ||
'''When''' | '''When''' | ||
This | This event happens each time the attribute definition is read for the installation. In general it happens one time only. | ||
'''Input''' | '''Input''' | ||
Line 24: | Line 23: | ||
'category' : Where to put it | 'category' : Where to put it | ||
'sortorder' : Sort order in this category | 'sortorder' : Sort order in this category | ||
'inputtype' : type of input : [buttongroup,columns,integer,singleselect,switch,text,textarea] | 'inputtype' : type of input : [buttongroup,columns,integer,float,singleselect,switch,text,textarea] | ||
'options' : optionnal options if input type need it (for singleselect and buttongroup) | 'options' : optionnal options if input type need it (for singleselect and buttongroup) | ||
'default' : the default value | 'default' : the default value | ||
Line 30: | Line 29: | ||
'caption' : the label | 'caption' : the label | ||
'help' : an help | 'help' : an help | ||
'min' : (optionnal) use for integer type for min value to be set | 'min' : (optionnal) use for integer or float type for min value to be set | ||
'max' : (optionnal) use for integer type for max value to be set | 'max' : (optionnal) use for integer or float type for max value to be set | ||
] | ] | ||
</pre> | </pre> | ||
Line 37: | Line 36: | ||
==Input type== | ==Input type== | ||
* switch | * switch - Switch (on|off) value send 0 or 1 | ||
* buttongroup | * buttongroup - Button group, need options array | ||
* singleselect | * singleselect - Single select in a DropDown | ||
* text | * text - Short text without line feed | ||
* integer | * integer - Integer value | ||
* columns | * float - Float value | ||
* textarea | * columns - Integer value between 1 to 12 (can be empty) | ||
* textarea - Text with line feed | |||
= | =Usage example= | ||
<source lang="php"> | <source lang="php"> | ||
Line 94: | Line 94: | ||
/** | /** | ||
* We can use the attribute like this , for example | * We can use the attribute like this, for example | ||
*/ | */ | ||
public function beforeQuestionRender() | public function beforeQuestionRender() |
Latest revision as of 14:35, 19 November 2024
The event
When
This event happens each time the attribute definition is read for the installation. In general it happens 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,float,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 'min' : (optionnal) use for integer or float type for min value to be set 'max' : (optionnal) use for integer or float type for max value to be set ]
Input type
- switch - Switch (on|off) value send 0 or 1
- buttongroup - Button group, need options array
- singleselect - Single select in a DropDown
- text - Short text without line feed
- integer - Integer value
- float - Float value
- columns - Integer value between 1 to 12 (can be empty)
- textarea - Text with line feed
Usage example
<?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
*/
}
}