DEV Community

Bismark
Bismark

Posted on

LimeSurvey Plugin Settings

In my last article I created a simple plugin for LimeSurvey,
that is used at the command line, which can be useful for cronjobs.

The next step could be some plugin settings. There are two types.

Global Plugin Settings

Global Plugin Settings are defined in a variable $settings

protected $settings = [
    'setting1' => [
        'type' => 'string',
        'label' => 'Setting One',
        'help' => 'This might be a useful hint',
        'default' => '' 
    ]
];
Enter fullscreen mode Exit fullscreen mode

You can access this setting via:

var_dump($this->get('setting1'));
Enter fullscreen mode Exit fullscreen mode

Survey Level Plugin Settings

In order to create some settings on survey level, you have to subscribe two events in your init() method.

public function init()
{
    /* @see https://manual.limesurvey.org/BeforeSurveySettings */
    $this->subscribe('beforeSurveySettings');
    /* @see https://manual.limesurvey.org/NewSurveySettings */
    $this->subscribe('newSurveySettings');
}
Enter fullscreen mode Exit fullscreen mode

Next we define a field isActive of type boolean.
There are several types available. See some examples here:

public function beforeSurveySettings()
{
    $event = $this->event;
    $surveyId = $event->get('survey');

    $event->set("surveysettings.{$this->id}", [
        'name' => get_class($this),
        'settings' => [
            'isActive' => [
                'type' => 'boolean',
                'label' => 'enable plugin for this survey',
                'current' => $this->get('isActive', 'Survey', $surveyId, false),
                'help' => 'some useful hint'
            ]
        ]
     ]);
}
Enter fullscreen mode Exit fullscreen mode

The following code is needed to save your survey settings:

public function newSurveySettings()
{
    $event = $this->event;
    foreach ($event->get('settings') as $name => $value) {
        $this->set($name, $value, 'Survey', $event->get('survey'));
    }
}
Enter fullscreen mode Exit fullscreen mode

That's it.
Bye!

Top comments (0)