Social Engine Demo Site

Social Engine 4 Hook Example

Marco Enrico
2 min readFeb 7, 2020

Introduction

Following up on my previous post about SE4 hooks. On this post we create a simple hook. For example we want to create a reward system. We reward a user a point per log-in per day i.e. a user gets a point if he logs in in a particular day but the point is only rewarded once per day. We encapsulate this feature in a theoretical plugin called “point-module.”

Letting SE4 Know

We need to let SE4 know that we plan on hooking to an event. In particular the event when the user successfully logged in. SE4 has an aptly named event “onUserLoginBefore” which is triggered when the user successfully logs in but before the lastlogin_date field is updated. On our module’s manifest — the file application/modules/<module_name>/settings/manifest.php we put

array (
'type' => 'module',
'name' => 'point',
'version' => '4.0.0',
'path' => 'application/modules/Point',
'title' => 'Points',
'description' => 'Points',
'author' => 'Marco Enrico',
'callback' =>
array (
'class' => 'Engine_Package_Installer_Module',
),
'actions' =>
array (
0 => 'install',
1 => 'upgrade',
2 => 'refresh',
3 => 'enable',
4 => 'disable',
),
'directories' =>
array (
0 => 'application/modules/Point',
),
'files' =>
array (
0 => 'application/languages/en/point.csv',
),
),
// Hooks ---------------------------------------------------------------------
'hooks' => array(
array(
'event' => 'onUserLoginBefore',
'resource' => 'Point_Plugin_Core'
)
)
);

This lets SE4 know that we plan on handling the “onUserLoginBefore” event with an instance of the class Point_Plugin_Core.

The Handler

Next we create the handler for the event. In application/modules/Point/Plugin/Core.php:

class Point_Plugin_Core extends Core_Plugin_Abstract
{
public function onUserLoginBefore($event)
{
$user = $event->getPayload();
if (date('d', strtotime($user->lastlogin_date)) != date('d')) {
// user's first time to login today
// add points...
// ...
$this->addPoints(1);
}
}
public function addPoints($points, $user = null)
{
//...

}
}

Summary

This example illustrates that it’s pretty simple to implement a social engine hook. All you really have to know is which hook best serves the task you’re trying to accomplish. “What if none of the hooks apply to a customization I’m trying to accomplish?”, you ask. There are other ways which is a subject of another post.

Originally published in https://social-engine-tutorials.blogsplot.com.

--

--

No responses yet