Commit 8bacdc2a authored by webchick's avatar webchick

Issue #2020399 by mr.baileys, oadaeh, dawehner, yanniboi, Xano, Andi-D:...

Issue #2020399 by mr.baileys, oadaeh, dawehner, yanniboi, Xano, Andi-D: Convert 'Who's online' block to a View.
parent d166be36
......@@ -48,7 +48,7 @@
))
->execute();
// Adds some role visibility settings on the who's online block for the long
// Adds some role visibility settings on the "Powered by" block for the long
// role.
db_insert('block_role')->fields(array(
'module',
......@@ -56,8 +56,8 @@
'rid',
))
->values(array(
'module' => 'user',
'delta' => 'online',
'module' => 'system',
'delta' => 'powered',
'rid' => '5',
))
->execute();
base_field: uid
base_table: users
core: 8.x
description: 'Shows the user names of the most recently active users, and the total number of active users.'
status: '1'
display:
default:
display_plugin: default
id: default
display_title: Master
position: '1'
display_options:
access:
type: perm
options:
perm: 'access user profiles'
cache:
type: none
options: { }
query:
type: views_query
options:
disable_sql_rewrite: '0'
distinct: '0'
slave: '0'
query_comment: ''
query_tags: { }
exposed_form:
type: basic
options:
submit_button: Apply
reset_button: '0'
reset_button_label: Reset
exposed_sorts_label: 'Sort by'
expose_sort_order: '1'
sort_asc_label: Asc
sort_desc_label: Desc
pager:
type: some
options:
items_per_page: '10'
offset: '0'
style:
type: html_list
options:
grouping: { }
row_class: ''
default_row_class: '1'
row_class_special: '1'
type: ul
wrapper_class: item-list
class: ''
row:
type: fields
fields:
name:
id: name
table: users
field: name
label: ''
alter:
alter_text: '0'
make_link: '0'
absolute: '0'
trim: '0'
word_boundary: '0'
ellipsis: '0'
strip_tags: '0'
html: '0'
hide_empty: '0'
empty_zero: '0'
link_to_user: '1'
overwrite_anonymous: '0'
relationship: none
group_type: group
admin_label: ''
exclude: '0'
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: '1'
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: '1'
empty: ''
hide_alter_empty: '1'
anonymous_text: ''
format_username: '1'
filters:
status:
value: '1'
table: users
field: status
id: status
expose:
operator: '0'
group: '1'
access:
id: access
table: users
field: access
relationship: none
group_type: group
admin_label: ''
operator: '>='
value:
min: ''
max: ''
value: '-15 minutes'
type: offset
group: '1'
exposed: '0'
expose:
operator_id: access_op
label: 'Last access'
description: 'A user is considered online for this long after they have last viewed a page.'
use_operator: '0'
operator: access_op
identifier: access
required: '0'
remember: '0'
multiple: '0'
remember_roles:
authenticated: authenticated
anonymous: '0'
administrator: '0'
is_grouped: '0'
group_info:
label: ''
description: ''
identifier: ''
optional: '1'
widget: select
multiple: '0'
remember: '0'
default_group: All
default_group_multiple: { }
group_items: { }
plugin_id: date
sorts:
access:
id: access
table: users
field: access
order: DESC
relationship: none
group_type: group
admin_label: ''
exposed: '0'
expose:
label: ''
granularity: second
provider: views
plugin_id: date
title: 'Who''s online'
header:
result:
id: result
table: views
field: result
relationship: none
group_type: group
admin_label: ''
empty: '0'
content: 'There are currently @total users online.'
plugin_id: result
footer: { }
empty:
area_text_custom:
id: area_text_custom
table: views
field: area_text_custom
relationship: none
group_type: group
admin_label: ''
empty: '1'
tokenize: '0'
content: 'There are currently 0 users online.'
plugin_id: text_custom
relationships: { }
arguments: { }
who_s_online_block:
display_plugin: block
id: who_s_online_block
display_title: 'Who''s online'
position: '1'
display_options:
block_description: 'Who''s online'
display_description: 'A list of users that are currently logged in.'
label: 'Who''s online block'
module: views
id: who_s_online
tag: default
uuid: 67a78cad-cf14-4f0d-9705-05d50cd84eaa
langcode: en
<?php
/**
* @file
* Contains \Drupal\user\Plugin\Block\UserOnlineBlock.
*/
namespace Drupal\user\Plugin\Block;
use Drupal\block\BlockBase;
use Drupal\block\Annotation\Block;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Session\AccountInterface;
/**
* Provides a "Who's online" block.
*
* @todo Move this block to the Statistics module and remove its dependency on
* {users}.access.
*
* @Block(
* id = "user_online_block",
* admin_label = @Translation("Who's online"),
* category = @Translation("Lists (Views)")
* )
*/
class UserOnlineBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return array(
'properties' => array(
'administrative' => TRUE
),
'seconds_online' => 900,
'max_list_count' => 10
);
}
/**
* {@inheritdoc}
*/
public function access(AccountInterface $account) {
return $account->hasPermission('access content');
}
/**
* Overrides \Drupal\block\BlockBase::blockForm().
*/
public function blockForm($form, &$form_state) {
$period = drupal_map_assoc(array(30, 60, 120, 180, 300, 600, 900, 1800, 2700, 3600, 5400, 7200, 10800, 21600, 43200, 86400), 'format_interval');
$form['user_block_seconds_online'] = array(
'#type' => 'select',
'#title' => t('User activity'),
'#default_value' => $this->configuration['seconds_online'],
'#options' => $period,
'#description' => t('A user is considered online for this long after they have last viewed a page.')
);
$form['user_block_max_list_count'] = array(
'#type' => 'select',
'#title' => t('User list length'),
'#default_value' => $this->configuration['max_list_count'],
'#options' => drupal_map_assoc(array(0, 5, 10, 15, 20, 25, 30, 40, 50, 75, 100)),
'#description' => t('Maximum number of currently online users to display.')
);
return $form;
}
/**
* Overrides \Drupal\block\BlockBase::blockSubmit().
*/
public function blockSubmit($form, &$form_state) {
$this->configuration['seconds_online'] = $form_state['values']['user_block_seconds_online'];
$this->configuration['max_list_count'] = $form_state['values']['user_block_max_list_count'];
}
/**
* {@inheritdoc}
*/
public function build() {
// Count users active within the defined period.
$interval = REQUEST_TIME - $this->configuration['seconds_online'];
// Perform database queries to gather online user lists.
$authenticated_count = db_query("SELECT COUNT(uid) FROM {users} WHERE access >= :timestamp", array(':timestamp' => $interval))->fetchField();
$build = array(
'#theme' => 'item_list__user__online',
'#prefix' => '<p>' . format_plural($authenticated_count, 'There is currently 1 user online.', 'There are currently @count users online.') . '</p>',
);
// Display a list of currently online users.
$max_users = $this->configuration['max_list_count'];
if ($authenticated_count && $max_users) {
$uids = db_query_range('SELECT uid FROM {users} WHERE access >= :interval AND uid > 0 ORDER BY access DESC', 0, $max_users, array(':interval' => $interval))->fetchCol();
foreach (user_load_multiple($uids) as $account) {
$username = array(
'#theme' => 'username',
'#account' => $account,
);
$build['#items'][] = drupal_render($username);
}
}
return $build;
}
}
......@@ -19,7 +19,7 @@ class UserBlocksTests extends WebTestBase {
*
* @var array
*/
public static $modules = array('block');
public static $modules = array('block', 'views');
/**
* The admin user used in this test.
......@@ -80,8 +80,7 @@ function testUserLoginBlock() {
* Test the Who's Online block.
*/
function testWhosOnlineBlock() {
$block = $this->drupalPlaceBlock('user_online_block');
$config = $block->get('settings');
$block = $this->drupalPlaceBlock('views_block:who_s_online-who_s_online_block');
// Generate users.
$user1 = $this->drupalCreateUser(array());
......@@ -94,7 +93,7 @@ function testWhosOnlineBlock() {
// Insert an inactive user who should not be seen in the block, and ensure
// that the admin user used in setUp() does not appear.
$inactive_time = REQUEST_TIME - $config['seconds_online'] - 1;
$inactive_time = REQUEST_TIME - (15 * 60) - 1;
$this->updateAccess($user3->id(), $inactive_time);
$this->updateAccess($this->adminUser->id(), $inactive_time);
......
......@@ -571,9 +571,6 @@ function user_preprocess_block(&$variables) {
case 'user_new_block':
$variables['attributes']['role'] = 'complementary';
break;
case 'user_online_block':
$variables['attributes']['role'] = 'complementary';
break;
}
}
}
......
......@@ -112,6 +112,11 @@ protected function setUp() {
);
entity_create('comment', $comment)->save();
}
// Some views, such as the "Who's Online" view, only return results if at
// least one user is logged in.
$account = $this->drupalCreateUser(array());
$this->drupalLogin($account);
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment