Actions

Add new menu and view by a plugin: Difference between revisions

From LimeSurvey Manual

No edit summary
No edit summary
Line 1: Line 1:
{{Alert|OBS: These instructions are for 2.06, not 2.50.x.}}
How to write a plugin that adds a menu to the admin menubar & when clicked renders some content  
How to write a plugin that adds a menu to the admin menubar & when clicked renders some content  
{{Alert|OBS: These instructions are for 2.06, not 2.50.x.}}


1. Create a new plugin. let us call it Rewards
1. Create a new plugin. let us call it Rewards

Revision as of 10:25, 9 September 2016

  OBS: These instructions are for 2.06, not 2.50.x.


How to write a plugin that adds a menu to the admin menubar & when clicked renders some content

1. Create a new plugin. let us call it Rewards

2. register for events afterAdminMenuLoad and newDirectRequest in the constructor

3. implement the above methods methods in the new plugin class

4. In the method afterAdminMenuLoad, add a new menu item and link it to

   admin/plugins/direct?plugin=<plugin_name>&function=<plugin_action>
   e.g
   admin/plugins/direct?plugin=rewards&function=assignRewards

The best way to to this would be to make use of the createURL method:

   'href' => $this->api->createUrl('plugins/direct', array('plugin' => 'Rewards', 'function' => 'assignRewards')),

5. implement the method newDirectRequest

   public function newDirectRequest(){
       $event = $this->event;
       
       // you can get other params from the request object
       $request = $event->get('request');
       
       //get the function name to call and use the method call_user_func
       $functionToCall = $event->get('function');        
       $content = call_user_func(array($this,$functionToCall)); 
       //set the content on the event      
       $event->setContent($this, $content);        
   }


6. implement the method plugin_action, in our case it is assignRewards(). In this method currently i am just trying to return some dummy content, you can have sophisticated page rendered.

   public function assignRewards() {
        $content = "<h1> plugin content set succesfully </h1>";
        return $content;        
    }


7. The full Rewards.php File:Rewards1.zip


8. This is how the UI looks now.