Creating a question plugin
Creating a basic question plugin.
A question type in LimeSurvey 2.1 will always be part of a plugin.
A plugin can contain multiple question types, the plugin takes care of registering the question types.
A simple plugin contianing just one question could look like this:
protected $questionTypes = array(
A question object has several tasks.
- It must define its configuration parameters, so the admin interface can allow the user to configure it.
- It must define its columns so the storage backend can prepare the table on survey activation.
- It must handle updated parameters sent to it via an event from the admin interface.
- It must render be able to itself.
- It must handle response data from a user submission.
Each question object must implement the iQuestion (https://github.com/LimeSurvey/LimeSurvey/blob/Plugin/application/libraries/PluginManager/Question/iQuestion.php) interface. It is strongly recommended to simply extends the QuestionBase class, this class offers a lot of useful functionality simplifying creation of new question types.
The questionbase class handles the following tasks.
- It describes attributes based on an array specified in the question class, implementing the getAttributes fucntion from iQuestion.
- It describes columns based on an array specified in the question class, implementing the getColumns function from iQuestion.
- It handles configuration data by implementing the saveAttributes function from iQuestion.
Besides handling these base function it also exposes 2 very useful functions:
protected function set($key, $value, $language = null, $questionId = null)
These functions allow the question object to save and restore data without knowing about storage details. Each datum is stored using a key and a value; by default the datum is related to the question type itself, for example when configuring global options like allowed file types for a file upload question.
When a question object is created it may be passed a question id, if it is it uses this id to relate the data it saves or retrieves to the specific instance of the question.
The getter and setter functions support localization by passing an optional language identifier.