Actions

Plugin system architecture

From LimeSurvey Manual

Revision as of 09:53, 18 January 2013 by Mdekker (talk | contribs)

Some random stuff on plugin architecture.

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':

onInstall 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 of 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.

onActivate Like the onInstall event, but this time it is fired when the plugin is activated.

onDeactivate You guessed right, on deactivation. Might show a warning about dependencies of other plugins, lost functionaity etc.

onDeinstall 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.

Code below might contain mistakes, textarea doesnt have syntax checking :p

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;

   }

}