DEV Community

Bismark
Bismark

Posted on

LimeSurvey HelloWorld Plugin

The following code is needed to run a simple command on the command line within LimeSurvey.
Your folder structure under upload/plugins should be like this:

  • HelloWorld
    • config.xml
    • HelloWorld.php
<?xml version="1.0" encoding="UTF-8"?>
<config>
    <metadata>
        <name>HelloWorld</name>
        <type>plugin</type>
        <lastUpdate>2022-05-21</lastUpdate>
        <author>My Name</author>
        <authorEmail>My Email Address</authorEmail>
        <authorUrl>MY Homepage</authorUrl>
        <license></license>
        <version>1.0.0</version>
        <description><![CDATA[This Plugin does some magic on the command line]]></description>
    </metadata>

    <compatibility>
        <version>5.0</version>
        <version>4.0</version>
        <version>3.0</version>
    </compatibility>
</config>
Enter fullscreen mode Exit fullscreen mode
class HelloWorld extends PluginBase
{
    protected $storage = 'LimeSurvey\PluginManager\DbStorage';
    static protected $description = 'HelloWorld';
    static protected $name = 'HelloWorld';

    /**
     * subscribe to all needed events
     * @see https://manual.limesurvey.org/Plugin_events
     */
    public function init()
    {
        /** @see https://manual.limesurvey.org/Direct_(command) */
        $this->subscribe('direct');
    }

    /**
     * php application/commands/console.php plugin --target=HelloWorld
     */
    public function direct()
    {
        $event = $this->getEvent();
        $target = $event->get('target');
        if ($target != get_class()) return;

        echo 'HELLO WORLD' . PHP_EOL;
        exit;
    }

}
Enter fullscreen mode Exit fullscreen mode

In LimeSurvey's PluginManager scan files, install and activate the plugin.
Then on the command line run:
php application/commands/console.php plugin --target=HelloWorld

And hopefully you can see: Hello World

Bye!

Top comments (0)