Hi all,
I'm attempting to create a plugin to show the last time a user logged into LS and to show that in the menu bar.
First of all, I've used following people's work as the start of my plugin (credit where credit is due!):
Olle Härstedt - Last Visited Surveys
LS Manual topic
I seem to be very close but I cannot figure out how to eventually show that date/time - I suppose it's a rooky mistake somewhere (note: I'm not a PHP programmer at all)...
LastLogon.php:
Code:
<?php
use \ls\menu\MenuItem;
use \ls\menu\Menu;
/**
* Show last date / time user logged on
*
* @author Bernard Deprez
* Inspired by @author Olle Härstedt
*/
class LastLogon extends \ls\pluginmanager\PluginBase
{
static protected $description = 'Show last date / time user logged on.';
static protected $name = 'Last Logon';
protected $storage = 'DbStorage';
public function init()
{
$this->subscribe('beforeAdminMenuRender');
$this->subscribe('beforeActivate');
$this->subscribe('beforeDeactivate');
$this->subscribe('afterSuccessfulLogin');
}
/**
* Create database table to store last visited surveys
*
* @todo Uses MyISAM as engine in MySQL?
* @return void
*/
public function beforeActivate()
{
// Create database table to store visited surveys
// Code copied from updatedb_helper.
// TODO: Include routine in plugin system?
$oDB = Yii::app()->getDb();
$oDB->schemaCachingDuration=0; // Deactivate schema caching
$oTransaction = $oDB->beginTransaction();
try
{
$aFields = array(
'uid' => 'integer primary key',
'logon_date' => 'string',
);
$oDB->createCommand()->createTable('{{plugin_last_logon}}',$aFields);
$oTransaction->commit();
}
catch(Exception $e)
{
$oTransaction->rollback();
// Activate schema caching
$oDB->schemaCachingDuration = 3600;
// Load all tables of the application in the schema
$oDB->schema->getTables();
// Clear the cache of all loaded tables
$oDB->schema->refresh();
$event = $this->getEvent();
$event->set('success', false);
$event->set(
'message',
gT('An non-recoverable error happened during the update. Error details:')
. "<p>"
. htmlspecialchars($e->getMessage())
. "</p>"
);
return;
}
}
public function beforeDeactivate()
{
// Remove table
$oDB = Yii::app()->getDb();
$oDB->schemaCachingDuration=0; // Deactivate schema caching
$oTransaction = $oDB->beginTransaction();
try
{
$oDB->createCommand()->dropTable('{{plugin_last_logon}}');
$oTransaction->commit();
}
catch(Exception $e)
{
$oTransaction->rollback();
// Activate schema caching
$oDB->schemaCachingDuration = 3600;
// Load all tables of the application in the schema
$oDB->schema->getTables();
// Clear the cache of all loaded tables
$oDB->schema->refresh();
$event = $this->getEvent();
$event->set(
'message',
gT('An non-recoverable error happened during the update. Error details:')
. "<p>"
. htmlspecialchars($e->getMessage())
. '</p>'
);
return;
}
}
public function afterSuccessfulLogin()
{
// Get row from database
$userId = Yii::app()->user->getId();
$lastLogon = LastLogonModel::model()->findByPk($userId);
$current_date = date('Y/m/d - H:i:s'). ' UK timezone';
if (!$lastLogon)
{
// First usage after plugin activation
$lastLogon = new LastLogonModel();
$lastLogon->uid = $userId;
$lastLogon->logon_date = $current_date;
$lastLogon->save();
}
$lastLogon->logon_date = $current_date;
$lastLogon->update();
}
public function beforeAdminMenuRender()
{
// Get row from database
$userId = Yii::app()->user->getId();
$lastLogon = LastLogonModel::model()->findByPk($userId);
$lastLogonDateTime = $lastLogon->lastLogonDateTime;
$event = $this->getEvent();
$event->append('extraMenus', array(
new Menu(array(
'label' => 'Last Logon:' . $lastLogonDateTime
))
));
}
}
LastLogonModel.php:
Code:
<?php
class LastLogonModel extends LSActiveRecord
{
public static function model($class = __CLASS__)
{
return parent::model($class);
}
public function tableName()
{
return '{{plugin_last_logon}}';
}
public function primaryKey()
{
return 'uid';
}
public function relations()
{
return array(
'user' => array(self::BELONGS_TO, 'User', 'uid'),
'lastLogonDateTime' => array(self::BELONGS_TO, 'User', 'logon_date'),
);
}
}
I'm also wondering if there were a way to make the Menu item "inactive" (so it doesn't change color when hovered over) - if not, not a biggy...
If anybody could help me out, I would appreciate it a lot!
Bernard.