- Posts: 53
- Thank you received: 5
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
LastLogon - my first attempt at creating a plugin
- bdeprez
- Topic Author
- Offline
- Senior Member
Less
More
5 years 10 months ago #168350
by bdeprez
LastLogon - my first attempt at creating a plugin was created by bdeprez
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:
LastLogonModel.php:
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.
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.
The topic has been locked.
- DenisChenu
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 13935
- Thank you received: 2551
5 years 10 months ago #168583
by DenisChenu
Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
Replied by DenisChenu on topic LastLogon - my first attempt at creating a plugin
I don't think there are a way to show Menu item «inactive» except with some js hack, you can put this in feature request and make a pull request.
About your table : maybe it's best to use API
See example here:
gitlab.com/DRAAF_Bourgogne-Franche-Comte...AnyResponse.php#L267
gitlab.com/DRAAF_Bourgogne-Franche-Comte...responseLink.php#L26 (for table name)
About your table : maybe it's best to use API
See example here:
gitlab.com/DRAAF_Bourgogne-Franche-Comte...AnyResponse.php#L267
gitlab.com/DRAAF_Bourgogne-Franche-Comte...responseLink.php#L26 (for table name)
Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.