Drupal Tip #5: Add a "Who is online?" block

We're back with our Drupal Tips, and this is the Tip #5, that talks about how to create a Who's online block, very interesting to have an idea of who is connected on your forum right now.

Who is online block Screenshot

This article assumes that you have skills in Drupal themes and/or modules editing. If not, follow this guide with care, and regular test that you don't break something.

This time you have to create your own function, by slightly modifying the existing Who's online block that Drupal provides by default.

  1. Here is the main snippet, that you can put wherever you want, in your own module, or directly in the template.php file of your current theme.

    template.php

    function user_statistics() {
     // Count users active within the defined period.
     $interval = time() - variable_get('user_block_seconds_online', 900);
     
     // Perform database queries to gather online user lists.  We use s.timestamp
     // rather than u.access because it is much faster.
     $anonymous_count = sess_count($interval);
     $authenticated_users = db_query('SELECT DISTINCT u.uid, u.name, s.timestamp FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.timestamp >= %d AND s.uid > 0 ORDER BY s.timestamp DESC', $interval);
     $authenticated_count = 0;
     $administrator_count = 0;
     $users = array();
     while ($account = db_fetch_object($authenticated_users)) {
      $users[] = $account;
      if ($account->uid == 1) {
       $administrator_count++;
      }
      else {
       $authenticated_count++;
      }
     }
     
     // Format the output with proper grammar.
     if ($anonymous_count == 1 && $authenticated_count == 1 && $administrator_count == 1)
      $verb = "is";
     else
      $verb = "are";
     
     $count_description = t('There ' . $verb . ' currently %admins, %members and %visitors online.', array(
      '%admins' => format_plural($administrator_count, '1 admin', '@count admins'),
      '%members' => format_plural($authenticated_count, '1 user', '@count users'), 
      '%visitors' => format_plural($anonymous_count, '1 guest', '@count guests'),
     ));
     
     $user_statistics = new stdClass();
     $user_statistics->anonymous_count = $anonymous_count;
     $user_statistics->authenticated_count = $authenticated_count;
     $user_statistics->administrator_count = $administrator_count;
     $user_statistics->count_description = $count_description;
     $user_statistics->accounts = $users;
     
     return $user_statistics;
    }
     
     
    function who_is_online() {
     $user_statistics = user_statistics();
     
     $output = "";
     
     $output .= "<div id='who-online'>";
     $output .= "<h3>Who is online?</h3>";
     $output .= $user_statistics->count_description;
     $output .= " Connected users: ";
     
     $account_names = array();
     foreach ($user_statistics->accounts as $account) {
      if ($account->uid == 1) {
       $account_names[] = "<span style='color:#C60B0B; font-weight: bold'>$account->name</span>";
      }
      else {
       $account_names[] = $account->name;
      }
     
     }
     
     $output .= join(", ", $account_names) . ".";
     $output .= "</div>";
     
     return $output;
    }

    The user_statistics() function creates an object containing information about the current users. It is used here by our who_is_online() function, be you can reuse it if you wish, for some other reasons.

  2. Once added, you simply have to append a new variable containing the result of who_is_online() in the right template preprocessors:

    template.php

    function yourtheme_preprocess_page(&$variables) {
     $node = $variables["node"];
     
     }
     // See page-node-forum.tpl.php
     if ($node->type == "forum") {
      $variables['template_files'][0] = 'page-node-forum';
     
      ...
      $variables["who_is_online"] = who_is_online();
     
      ...
     
     }
     
    }
     
     
    function yourtheme_preprocess_forums(&$variables) {
     ...
     
     // See forums.tpl.php
     $variables["who_is_online"] = who_is_online();
     
     ...
    }

  3. Last step, you of course need to use this variable in your templates, like this:

    page-node-forum.tpl.php

     ...
     
     <?php print $content ?>
     
     <?php print $who_is_online; ?>
     
     ...

    forums.tpl.php

    <?php
    // $Id: forums.tpl.php,v 1.4 2007/08/07 08:39:35 goba Exp $
     
    /**
     * @file forums.tpl.php
     * Default theme implementation to display a forum which may contain forum
     * containers as well as forum topics.
     *
     * Variables available:
     * - $links: An array of links that allow a user to post new forum topics.
     *   It may also contain a string telling a user they must log in in order
     *   to post.
     * - $forums: The forums to display (as processed by forum-list.tpl.php)
     * - $topics: The topics to display (as processed by forum-topic-list.tpl.php)
     * - $forums_defined: A flag to indicate that the forums are configured.
     *
     * @see template_preprocess_forums()
     * @see theme_forums()
     */
    ?>
     
    <div class="links-forum-container">
      <?php print theme('links', $links); ?>
      <?php print $search_form_box; ?>
    </div>
     
    <div style="clear:both"></div>
     
    <div class='forum-topic-wrapper'>
    <div id="forum">
     
    <?php if ($forums_defined): ?>
      <?php print $forums; ?>
      <?php print $topics; ?>
    <?php endif; ?>
     
    </div>
    </div>
     
    <div class="links-forum-container">
      <?php print theme('links', $links); ?>
    </div>
     
    <?php print $who_is_online; ?>

  4. Here is the CSS stylesheet we use to style this block

    style.css

    #who-online {
     margin:     60px 0 3px;
     clear:     left;
    }
     
    #who-online h3 {
     color:      #115098;
     border-bottom:    1px solid #CCC;
     font-weight:    bold;
     font-size:    14px;
     text-transform:   uppercase; 
     
    }

Keywords:
Comments are closed.

Comments (4)

Hello, great article. I would

Hello,

great article. I would like to inform you that i have already build a module called viewers which does the same thing.

I would appreciate your feedback,
apanag

Top
lastnico's picture

Yes, indeed, my article and

Yes, indeed, my article and your module do the same, even if your module is at least, could be installed to other places in the Drupal layout (as a block for example). The core function presented here should look like yours.

Thanks for pointing this interesting module

FSR Admin - Vote for 2012.

Top

Do you have an update for

Do you have an update for drupal 7?

Top

is there any port of the

is there any port of the views module to D7 for some of us that depend on modules to get functionality lol

Top
  Top Top