user_block
- Versions
- 4.6 – 6
user_block($op = 'list', $delta = 0, $edit = array())
Implementation of hook_block().
Code
modules/user.module, line 479
<?php
function user_block($op = 'list', $delta = 0, $edit = array()) {
global $user;
if ($op == 'list') {
$blocks[0]['info'] = t('User login');
$blocks[1]['info'] = t('Navigation');
$blocks[2]['info'] = t('Who\'s new');
$blocks[3]['info'] = t('Who\'s online');
return $blocks;
}
else if ($op == 'configure' && $delta == 3) {
$period = drupal_map_assoc(array(30, 60, 120, 180, 300, 600, 900, 1800, 2700, 3600, 5400, 7200, 10800, 21600, 43200, 86400), 'format_interval');
$output = form_select(t('User activity'), 'user_block_seconds_online', variable_get('user_block_seconds_online', 900), $period, t('A user is considered online for this long after they have last viewed a page.'));
$output .= form_select(t('User list length'), 'user_block_max_list_count', variable_get('user_block_max_list_count', 10), drupal_map_assoc(array(0, 5, 10, 15, 20, 25, 30, 40, 50, 75, 100)), t('Maximum number of currently online users to display.'));
return $output;
}
else if ($op == 'save' && $delta == 3) {
variable_set('user_block_seconds_online', $edit['user_block_seconds_online']);
variable_set('user_block_max_list_count', $edit['user_block_max_list_count']);
}
else if ($op == 'view') {
$block = array();
switch ($delta) {
case 0:
// For usability's sake, avoid showing two login forms on one page.
if (!$user->uid && !(arg(0) == 'user' && !is_numeric(arg(1)))) {
$edit = $_POST['edit'];
$output = "<div class=\"user-login-block\">\n";
// NOTE: special care needs to be taken because on pages with forms,
// such as node and comment submission pages, the $edit variable
// might already be set.
$output .= form_textfield(t('Username'), 'name', $edit['name'], 15, 64);
$output .= form_password(t('Password'), 'pass', $pass, 15, 64);
$output .= form_submit(t('Log in'));
$output .= "</div>\n";
$output = form($output, 'post', url('user/login', drupal_get_destination()));
if (variable_get('user_register', 1)) {
$items[] = l(t('Create new account'), 'user/register', array('title' => t('Create a new user account.')));
}
$items[] = l(t('Request new password'), 'user/password', array('title' => t('Request new password via e-mail.')));
$output .= theme('item_list', $items);
$block['subject'] = t('User login');
$block['content'] = $output;
}
return $block;
case 1:
if ($menu = theme('menu_tree')) {
$block['subject'] = $user->uid ? check_plain($user->name) : t('Navigation');
$block['content'] = '<div class="menu">'. $menu .'</div>';
}
return $block;
case 2:
if (user_access('access content')) {
$result = db_query_range('SELECT uid, name FROM {users} WHERE status != 0 ORDER BY uid DESC', 0, 5);
while ($account = db_fetch_object($result)) {
$items[] = format_name($account);
}
$output = theme('user_list', $items);
$block['subject'] = t('Who\'s new');
$block['content'] = $output;
}
return $block;
case 3:
if (user_access('access content')) {
// Count users with activity in the past defined period.
$time_period = variable_get('user_block_seconds_online', 2700);
// Perform database queries to gather online user lists.
$guests = db_fetch_object(db_query('SELECT COUNT(sid) AS count FROM {sessions} WHERE timestamp >= %d AND uid = 0', time() - $time_period));
$users = db_query('SELECT DISTINCT(uid), MAX(timestamp) AS max_timestamp FROM {sessions} WHERE timestamp >= %d AND uid != 0 GROUP BY uid ORDER BY max_timestamp DESC', time() - $time_period );
$total_users = db_num_rows($users);
// Format the output with proper grammar.
if ($total_users == 1 && $guests->count == 1) {
$output = t('There is currently %members and %visitors online.', array('%members' => format_plural($total_users, '1 user', '%count users'), '%visitors' => format_plural($guests->count, '1 guest', '%count guests')));
}
else {
$output = t('There are currently %members and %visitors online.', array('%members' => format_plural($total_users, '1 user', '%count users'), '%visitors' => format_plural($guests->count, '1 guest', '%count guests')));
}
// Display a list of currently online users.
$max_users = variable_get('user_block_max_list_count', 10);
if ($max_users) {
$items = array();
while ($max_users-- && $uid = db_fetch_object($users)) {
$items[] = format_name(user_load(array('uid' => $uid->uid)));
}
if ($items) {
if (db_fetch_object($users)) {
$items[] = '...';
}
$output .= theme('item_list', $items, t('Online users:'));
}
}
$block['subject'] = t('Who\'s online');
$block['content'] = $output;
}
return $block;
}
}
}
?>Login or register to post comments 