EclipseGc

Untitled

Mar 16th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.21 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupal\user;
  4.  
  5. use Drupal\block\AbstractBlock;
  6.  
  7. /**
  8.  * @file
  9.  *   System module block for rendering the help for a page.
  10.  */
  11. class UserOnlineBlock extends AbstractBlock {
  12.  
  13.   /**
  14.    * Implements BlockInterface::info().
  15.    */
  16.   public function info() {
  17.     return array(
  18.       'info' => t('Who\'s online'),
  19.       'cache' => DRUPAL_NO_CACHE,
  20.       'properties' => array(
  21.         'administrative' => TRUE,
  22.       ),
  23.     );
  24.   }
  25.  
  26.   /**
  27.    * Implements BlockInterface::access().
  28.    */
  29.   public function access() {
  30.     return user_access('access content');
  31.   }
  32.  
  33.   /**
  34.    * Implements BlockInterface::configure().
  35.    */
  36.   public function configure($form, &$form_state) {
  37.     $form = parent::configure($form, $form_state);
  38.     $period = drupal_map_assoc(array(30, 60, 120, 180, 300, 600, 900, 1800, 2700, 3600, 5400, 7200, 10800, 21600, 43200, 86400), 'format_interval');
  39.     $form['user_block_seconds_online'] = array(
  40.       '#type' => 'select',
  41.       '#title' => t('User activity'),
  42.       '#default_value' => $this->config['seconds_online'],
  43.       '#options' => $period,
  44.       '#description' => t('A user is considered online for this long after they have last viewed a page.')
  45.     );
  46.     $form['user_block_max_list_count'] = array(
  47.       '#type' => 'select',
  48.       '#title' => t('User list length'),
  49.       '#default_value' => $this->config['max_list_count'],
  50.       '#options' => drupal_map_assoc(array(0, 5, 10, 15, 20, 25, 30, 40, 50, 75, 100)),
  51.       '#description' => t('Maximum number of currently online users to display.')
  52.     );
  53.     return $form;
  54.   }
  55.  
  56.   /**
  57.    * Implements BlockInterface::configureSubmit().
  58.    */
  59.   public function configureSubmit($form, &$form_state) {
  60.     parent::configureSubmit($form, &$form_state);
  61.     $this->config['seconds_online'] = $form_state['values']['user_block_seconds_online'];
  62.     $this->config['max_list_count'] = $form_state['values']['user_block_max_list_count'];
  63.   }
  64.  
  65.   /**
  66.    * Implements BlockInterface::build().
  67.    */
  68.   public function build() {
  69.     // Count users active within the defined period.
  70.     $interval = REQUEST_TIME - $this->config['max_list_count'];
  71.  
  72.     // Perform database queries to gather online user lists. We use s.timestamp
  73.     // rather than u.access because it is much faster.
  74.     $authenticated_count = db_query("SELECT COUNT(DISTINCT s.uid) FROM {sessions} s WHERE s.timestamp >= :timestamp AND s.uid > 0", array(':timestamp' => $interval))->fetchField();
  75.  
  76.     $output = '<p>' . format_plural($authenticated_count, 'There is currently 1 user online.', 'There are currently @count users online.') . '</p>';
  77.  
  78.     // Display a list of currently online users.
  79.     $max_users = $this->config['max_list_count'];
  80.     if ($authenticated_count && $max_users) {
  81.       $items = db_query_range('SELECT u.uid, u.name, MAX(s.timestamp) AS max_timestamp FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.timestamp >= :interval AND s.uid > 0 GROUP BY u.uid, u.name ORDER BY max_timestamp DESC', 0, $max_users, array(':interval' => $interval))->fetchAll();
  82.       $output .= theme('user_list', array('users' => $items));
  83.     }
  84.  
  85.     return array(
  86.       '#block' => $this->config,
  87.       '#children' => $output,
  88.     );
  89.   }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment