Actions

Plugin system architecture: Difference between revisions

From LimeSurvey Manual

No edit summary
 
m (Text replacement - " enclose="div"" to "")
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
A plugin can extend functionality of LimeSurvey. The plugin registers on what events it wants to extend. This could be adding a button to the menu, do something when a respondent starts or finishes the survey... the possibilities are endless.


Some random stuff on plugin architecture.
For the plugin to be able to do it's magic it might need some special configuration. Below is the 'lifetime of a plugin':


{|
! Event name || Description
|-
|<code>beforeInstall</code> || When the plugin is added to the system, this event is fired on the plugin. This way the plugin can set a default config, of check for dependencies. The method can return success or failure and an optional message. This method is also fired when a new version of the plugin is uploaded. This way the plugin can handle upgrades when neccessary.
|-
|<code>beforeActivate</code> || Like the beforeInstall event, but this time it is fired when the plugin is activated.
|-
|<code>beforeDeactivate</code> || You guessed right, on deactivation. Might show a warning about dependencies of other plugins, lost functionaity etc.
|-
|<code>beforeDeinstall</code>|| This should handle cleanup of extra tables, config etc.
|}
The plugins will extend a base class.
The plugins will extend a base class.


The base plugin class will initialize a storage interface and create wrapper functions for the get and set implemented in the interface.
The base plugin class will initialize a storage interface and create wrapper functions for the get and set implemented in the interface.


Code below might contain mistakes, textarea doesnt have syntax checking :p
<syntaxhighlight lang="php">interface PluginStorage {
 
<syntaxhighlight lang="php" enclose="div">interface PluginStorage {
 
/**
/**
 Returns plugin data.
 Returns plugin data.
 @param object $plugin The plugin object getting its data.
 @param object $plugin The plugin object getting its data.
 @param string | null $key The storage key, if null will return all data for the plugin.
 @param string | null $key The storage key, if null will return all data for the plugin.
 @param string $model Name of a model in case its model specific plugin data, like for a specific question or survey.
 @param string $model Name of a model in case its model specific plugin data, like for a specific question or survey.
 @param int $id Id of the model for which the data is retreived
 @param int $id Id of the model for which the data is retreived
 @return mixed The data stored.
 @return mixed The data stored.
*/
*/
 
public function get($plugin, $key = null, $model = null, $id = null)
public function get($plugin, $key = null, $model = null, $id = null);


/**
/**
 Stores plugin data.
 Stores plugin data.
 @param object $plugin The plugin object getting its data.
 @param object $plugin The plugin object getting its data.
 @param string $key The storage key to identify the data.
 @param string $key The storage key to identify the data.
 @param mixed $data The data to be stored, serialized using serialize.
 @param mixed $data The data to be stored, serialized using serialize.
 @param string $model Name of a model in case its model specific plugin data, like for a specific question or survey.
 @param string $model Name of a model in case its model specific plugin data, like for a specific question or survey.
 @param int $id Id of the model for which the data is retreived
 @param int $id Id of the model for which the data is retreived
*/
*/
public function set ($plugin, $key, $data, $model = null, $id = null)
</syntaxhighlight>


public function set ($plugin, $key, $data, $model = null, $id = null);
<syntaxhighlight lang="php">
 
}
 
class PluginBase {
class PluginBase {
   protected $storage = 'PluginStorage';
   protected $storage = 'PluginStorage';
   private $store = null;
   private $store = null;
   public function '''construct()
   public function '''construct()
   {
   {
       $this->store = Plugin::getStore($this->storage);
       $this->store = Plugin::getStore($this->storage);
   }
   }
   /**
   /**
     This function stores plugin data.
     This function stores plugin data.
   */
   */
   protected function set($key, $data, $model = null, $id = null)
   protected function set($key, $data, $model = null, $id = null)
   {
   {
       if (isset($this->store))
       if (isset($this->store))
       {
       {
           return $this->store->set($this, $key, $model, $id);
           return $this->store->set($this, $key, $model, $id);
       }
       }
       return false;
       return false;
   }
   }
   /**
   /**
     This function retrieves plugin data. Do not cache this data; the plugin storage
     This function retrieves plugin data. Do not cache this data; the plugin storage
     engine will handling caching. After the first call to this function, subsequent
     engine will handling caching. After the first call to this function, subsequent
     calls will only consist of a few function calls and array lookups.
     calls will only consist of a few function calls and array lookups.
   */
   */
   protected function get($key = null, $model = null, $id = null)
   protected function get($key = null, $model = null, $id = null)
   {
   {
       if (isset($this->store))
       if (isset($this->store))
       {
       {
           return $this->store->get($this, $key, $model, $id);
           return $this->store->get($this, $key, $model, $id);
       }
       }
       return false;
       return false;
   }
   }
}</syntaxhighlight>


}</syntaxhighlight>
To see how to add a new menu item and display a page when the menu item is clicked see [[Add_new_menu_and_view_by_a_plugin]]

Latest revision as of 15:04, 16 February 2022

A plugin can extend functionality of LimeSurvey. The plugin registers on what events it wants to extend. This could be adding a button to the menu, do something when a respondent starts or finishes the survey... the possibilities are endless.

For the plugin to be able to do it's magic it might need some special configuration. Below is the 'lifetime of a plugin':

Event name Description
beforeInstall When the plugin is added to the system, this event is fired on the plugin. This way the plugin can set a default config, of check for dependencies. The method can return success or failure and an optional message. This method is also fired when a new version of the plugin is uploaded. This way the plugin can handle upgrades when neccessary.
beforeActivate Like the beforeInstall event, but this time it is fired when the plugin is activated.
beforeDeactivate You guessed right, on deactivation. Might show a warning about dependencies of other plugins, lost functionaity etc.
beforeDeinstall This should handle cleanup of extra tables, config etc.

The plugins will extend a base class.

The base plugin class will initialize a storage interface and create wrapper functions for the get and set implemented in the interface.

interface PluginStorage {
/**
 Returns plugin data.
 @param object $plugin The plugin object getting its data.
 @param string | null $key The storage key, if null will return all data for the plugin.
 @param string $model Name of a model in case its model specific plugin data, like for a specific question or survey.
 @param int $id Id of the model for which the data is retreived
 @return mixed The data stored.
*/
public function get($plugin, $key = null, $model = null, $id = null)

/**
 Stores plugin data.
 @param object $plugin The plugin object getting its data.
 @param string $key The storage key to identify the data.
 @param mixed $data The data to be stored, serialized using serialize.
 @param string $model Name of a model in case its model specific plugin data, like for a specific question or survey.
 @param int $id Id of the model for which the data is retreived
*/
public function set ($plugin, $key, $data, $model = null, $id = null)
class PluginBase {
   protected $storage = 'PluginStorage';
   private $store = null;
   public function '''construct()
   {
       $this->store = Plugin::getStore($this->storage);
   }
   /**
     This function stores plugin data.
   */
   protected function set($key, $data, $model = null, $id = null)
   {
       if (isset($this->store))
       {
           return $this->store->set($this, $key, $model, $id);
       }
       return false;
   }
   /**
     This function retrieves plugin data. Do not cache this data; the plugin storage
     engine will handling caching. After the first call to this function, subsequent
     calls will only consist of a few function calls and array lookups.
   */
   protected function get($key = null, $model = null, $id = null)
   {
       if (isset($this->store))
       {
           return $this->store->get($this, $key, $model, $id);
       }
       return false;
   }
}

To see how to add a new menu item and display a page when the menu item is clicked see Add_new_menu_and_view_by_a_plugin