Creating a question plugin

From LimeSurvey Manual
Jump to: navigation, search

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:

    class YesNoQuestion extends QuestionPluginBase

   {

      protected $questionTypes = array(

           'YesNoQuestionObject'

       );

   }

?>

A question object has several tasks.

  1. It must define its configuration parameters, so the admin interface can allow the user to configure it.
  2. It must define its columns so the storage backend can prepare the table on survey activation.
  3. It must handle updated parameters sent to it via an event from the admin interface.
  4. It must render be able to itself.
  5. 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.

  1. It describes attributes based on an array specified in the question class, implementing the getAttributes fucntion from iQuestion.
  2. It describes columns based on an array specified in the question class, implementing the getColumns function from iQuestion.
  3. 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 get($key = null, $default = null, $language = null, $questionId = null)

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.