- Posts: 53
- Thank you received: 5
Ask the community, share ideas, and connect with other LimeSurvey users!
<?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 )) )); } }
<?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'), ); } }