Drupal: Last Login details

This CodeLet will store user's login history and creates a block. Admin can place this block on user pages or on pages. Future ammendments to this code will allow to use the stored login details to be displayed using a theme function.
This CodeLet will be submitted to Drupal.org community soon.

Module Info

name = Last login details
description = Shows user's last login time stamp.
core = 6.x
package = Login

.Install file

/**
* @file
*
* Schema file for Last login
*
* DrupalD
*/

/**
* Implementation of hook_install
*
* DrupalD
*/
function last_login_install() {
drupal_install_schema('last_login');
}

/**
* Implemetation of hook_uninstall
*
* DrupalD
*/
function last_login_uninstall() {
drupal_uninstall_schema('last_login');
}

/**
* Implemeantation of hook_schema
*
* DrupalD
*/
function last_login_schema() {
$schema = array();
$schema['last_login'] = array(
'fields' => array(
'id' => array(
'type' => 'serial',
'length' => 11,
'not null' => TRUE,
),

'uid' => array(
'type' => 'int',
'length' => 11,
'not null' => TRUE,
),

'last_logged' => array(
'type' => 'int',
'length' => 11,
'not null' => TRUE,
),

),
'primary key' => array('id'),
);

return $schema;
}

CodeLet: 

/**
* @file
*
* Logs user's logins and display last login details
*
* DrupalD
*/

/**
* Implementation of hook_user
*
* @param $op
* @param $edit
* @param $account
* @param $category
* DrupalD
*/
function last_login_user($op, &$edit, &$account, $category = NULL) {
global $user;
switch ($op) {
case 'login':
case 'register':
db_query('INSERT INTO {last_login VALUES(id, %d, %d)', $user->uid, time('now'));
break;
}
}

/**
* Implementation of hook_block
*
* @param unknown_type $op
* @param unknown_type $delta
* @param unknown_type $edit
* DrupalD
*/
function last_login_block($op = 'list', $delta = 0, $edit = array()) {
if ($op == 'list') {
$blocks[0] = array(
'info' => t('Last login'),
'status' => 1,
'weight' => 0,
'region' => 'right',
);
return $blocks;
}
if ($op == 'view') {
switch ($delta) {
case 0:
$output = _get_last_login();
echo $output;
$block = array(
'subject' => t("Last login"),
'content' => $output,
);
break;
}
return $block;
}
}

/**
* _get_last_login
*
* Get last login details of the user
* DrupalD
*/
function _get_last_login() {
global $user;
$sn__last_login = db_result(db_query('SELECT last_logged FROM {last_login} WHERE uid = %d ORDER BY id DESC LIMIT 1, 1', $user->uid));
if (empty($sn__last_login)) {
$sn__last_login = db_result(db_query('SELECT last_logged FROM {last_login} WHERE uid = %d ORDER BY id DESC LIMIT 0, 1', $user->uid));
}
if (!empty($sn__last_login)) {
$output = t('Last login at @time on @date', array('@time' => format_date($sn__last_login, 'custom', 'h:m A T'), '@date' => format_date($sn__last_login, 'custom','m/d/Y')));
}
else {
$output = t('Last login details not available');
}
return $output;
}

Drupal: Last Login details | Drupal Developer

By DrupalD on Thu, 06/02/2014 - 5:32pm

Add comment

Log in or register to post comments
QR code for this page URL This page URL
Feedback