Thanks Darren and everyone else for all of your ongoing great work on this module. Great stuff!!

I was curious about the following use case.

I have an active chat room space on my site that I have affectionately called "The Lodge": www.ithou.org/chatrooms/chat/2

The "who's online" block works great for this page. However, I would also like to have this block show up on *other pages* to show who is chatting away in "The Lodge", in case someone who is *not* already on this page, sees that a friend is in "The Lodge" wants to join them. For example, I would like the "who's online" block for "The Lodge" to show up here: www.ithou.org/tracker/all

Currently, the only way the

chatroom_block_chat_online_list()

shows up is when you are actually viewing the page. Is there a way to create a custom block listing the people in "The Lodge, i.e., Chat#2" ... even when you're not on that page?

Thanks!

PS. I imagine you can do it via a hack to the

function chatroom_block_chat_online_list()

code, though I'm not quite sure what to mix and match. If I figure it out myself, I'll let people know... Best, Albert

Comments

ajwwong’s picture

Title: Create chat_online_list block » Create custom chat_online_list block for a particular chatroom's chat
darren oh’s picture

Status: Active » Closed (works as designed)

I wouldn't recommend using the chat_online_list block for this purpose. It is designed to show who's on-line in the chat on the current page. The reason it doesn't show on other pages is that it's empty, and Drupal doesn't display empty blocks. Instead, create a custom PHP code block, and paste in the code from theme_chatroom_block_chat_online_list(), except for the first four lines, which should be replaced with $chat_id = followed by the ID of the chat whose members you want to show.

ajwwong’s picture

Awesome... thanks for the tip, Darren! Appreciate it... I'll let you know how it goes... :-)

samuelet’s picture

I think that it'd be useful such a block.
I've tested this code on 5.x.1-9 but unfortunately it does not work on my site.
Each user can see only its own name in the block, but not the others and block is empty when user is not in the chat.

ajwwong’s picture

I'll just say, that I tried this out [or what I thought was an equivalent version in 4.7] and it didn't seem to work either... similar behavior as described in #4 above, but I was going to wait to make a full report on success/failure until I tried it out in drupal 5.

thanks samuelet for the heads up.

regards,
albert

ps happy thanksgiving everyone!

darren oh’s picture

My directions were incomplete. You need to add
chatroom_block_settings();
to your code.

darren oh’s picture

Equivalent instructions for 4.7:

  1. Create a PHP block.
  2. Paste in code from chatroom_block_chat_online_list().
  3. Replace the first three lines with matches = array([chat ID]);
  4. Add chatroom_block_add_state_js();
ajwwong’s picture

Thanks Darren! I really appreciate this... I'll try this out and let you know!

Blessings,
Albert

ajwwong’s picture

This looks great and seems like exactly the right way to do it! Thanks, Darren.

I'm including my code here, in case anyone else looking wants it... [I'm still going to probably need to update to Drupal 5 in order to get this commit http://drupal.org/cvs?commit=72486 so that the list is updated when someone leaves the chatroom. But at least for now, I just wanted people to know that this is basically working!]

drupal 4.7
chatroom.module,v 1.50.2.50

Thanks, Darren!

Albert

<?php
chatroom_block_add_state_js();
$chatid = 2; 
$matches[1] = $chatid;
  $title = '';
  $content = '';
  $cache_file = file_directory_temp() .'/drupal_chat_cache/'. chatroom_chat_get_cache_file_name($matches[1]);
  $cache_timestamp = @filemtime($cache_file);
  if ($users = chatroom_chat_get_online_list($matches[1], $cache_timestamp)) {
    $content = '<ul class="menu" id="chatroom-online">';
    foreach ($users as $ol_user) {
      $name = $ol_user->name ? check_plain($ol_user->name) : "guest-{$ol_user->guest_id}";
      $content .= '<li id="'. $ol_user->session_id .'"';
      $content .= $ol_user->away ? ' class="chatroom-user-away" ' : '';
      if ($ol_user->uid) {
        $content .= '><a href="javascript:chatroomSelectUser(\''. $ol_user->name .'\')">'. check_plain($name).'</a></li>';
      }
      else {
        $content .= ">$name</li>";
      }
    }
    $content .= '</ul>';
    $title = t('who is online in') .' <em>'. check_plain($ol_user->chatname) .'</em>';
  }
$preface = 'This block is not completely accurate... but if you want to check out the lodge to chat with people, you might be able to find them <a href="http://www.ithou.org/Lodge">here</a>.<br />';
print_r($preface);
  $output = theme('item_list',array($content));
  print_r($output);  
  
?>
ajwwong’s picture

Just FYI... I pasted in the patch for the commit from http://drupal.org/cvs?commit=72486 into the 4.7 version, to get the more frequent updates to the who's online block, and while the patch does its job on the actual chat page, e.g., www.ithou.org/chatrooms/chat/2... for the block on the "pointer" page www.ithou.org/tracker/all , it also shows the the person who has most recently exited the chatroom as remaining in the room.... interesting....

I'll keep researching this...

Thanks for your help!

samuelet’s picture

To get it work on 5.x.1-9 i had to change:

  $users = chatroom_chat_get_online_list($chat_id, $cache_timestamp, 0);

into:

  $users = chatroom_chat_get_online_list($chat_id, $cache_timestamp, 7);

I think that this could lead to the problem that block list contains always the last user that left the chat:
Probably this should be fixed by the ajax calls of chatroom.js, but loading it with the following block code:

  $chat = chatroom_chat_get_from_id($chat_id);
  chatroom_chat_settings($chat);

fails with this javascript error:

a[j] has no properties
e(Object drupal_base=/var/www/example)jquery.js (line 2)
e(Object global=true ifModified=false type=POST timeout=0)jquery.js (line 2)
e("http://www.example.it/modules/chatroom/chatroomread.php", Object drupal_base=/var/www/example, function(), undefined)jquery.js (line 2)
getUpdates()chatroom.js (line 577)
addEvents()chatroom.js (line 48)
e()
samuelet’s picture

This block code fixes the problem with my above bug. Block is updated only when a page is loaded (no ajax) and it lists users that has updated their chat status in the last $offset seconds.

  chatroom_block_settings();
  //List users that has updated their chat status in the last $offset seconds
  $offset=300;
  //Set your chat id;
  $chat_id = 7;
  $title = 'Users in chat';
  $cache_file = _chatroom_get_cache_file("chat.$chat_id");
  $cache_timestamp = @filemtime($cache_file);
  $users =_users_in_chat($chat_id, $cache_timestamp,$offset);
  if (!empty($users)) {
    $content = '';
    $chatname = db_result(db_query("SELECT chatname FROM {chatroom_chat} WHERE ccid = %d", $chat_id));
    $content = '<ul class="menu" id="chatroom-online">';
    foreach ($users as $user) {
      $content .= '<li id="'. $user['guestId'] .'"';
      $content .= $user['away'] ? ' class="chatroom-user-away">' : '>';
      $content .= '<a href="/user/'.$user['uid'].'">'.$user['user'] .'</a></li>';
    }
  $content .= '</ul>';
  print "<h2>".$title."</h2>";
  print $content;
  }

function _users_in_chat($chat_id, $cache_timestamp,$offset) {
  $users = array();
  if ($chat = chatroom_chat_get_from_id($chat_id, FALSE) && !isset($chat->when_archived)) {
    $result = db_query("
      SELECT
        col.uid,
        col.session_id,
        col.guest_id,
        col.away,
        cc.chatname,
        du.name
      FROM {chatroom_online_list} col
      INNER JOIN {chatroom_chat} cc ON cc.ccid = col.ccid
      LEFT JOIN {users} du ON du.uid = col.uid
      WHERE col.ccid = %d AND col.modified > unix_timestamp()-%d", $chat_id,$offset);
    while ($row = db_fetch_object($result)) {
      $name = $row->name ? check_plain($row->name) : _chatroom_variable_get('chatroom_guest_user_prefix', 'guest-') . $row->guest_id;
      $user = array(
        'user' => $name,
        'uid' => $row->uid,
        'guestId' => $row->guest_id,
        'away' => (int) $row->away,
      );
      if ($row->session_id == session_id()) {
        $user['self'] = TRUE;
        $current_user = $user;
      }
      else {
        $users[] = $user;
      }
    }
    if (isset($current_user)) {
      array_unshift($users, $current_user);
    }
  }
  return $users;
}

astra.satya’s picture

Thanx for the code.

www.zenxcoder.com