I would like add a comma separated list of active channels with links under the 'who is chatting' block in Drupal 4.7.x. Any hint on how to do it?

Comments

jasonwhat’s picture

Along these lines, where are the list of channels? I don't know how to get users into channels so they can keep chat going while moving around the site. Seems like a block would be the place for this, but at any rate, I have no idea how to find the sitewide chat rooms.

udvranto’s picture

Jason,
I guess you can not create a site wide chatroom. You can have nodes with chats, accessible from any where in the site.

My requirement is similar to yours. I am approaching the problem by adding chatroom name just under the "Chat room users" (or something similar) block. Or another approach could be to add a new block just to display the available chatrooms and number of users in it.

udvranto’s picture

I solved the problem partially. I added another block for active channels. But I would like to add a link to the correct node where the channel is. I don't know how to do it.

Heres is the modified phpfreechat_block function

/**
 * Implementation of hook_block().
 * S M Mahbub Murshed, June 26, 2007
 */
function phpfreechat_block($op = 'list', $delta = 0, $edit = array()) {
  global $user, $base_url;
  if ($op == 'list') {
    $block[0]['info'] = t('Who is chatting');
    $block[1]['info'] = t('Who is chatting on...');
    $block[2]['info'] = t('Latest from...');
    $block[3]['info'] = t('Active channels');
    return $block;
  }
  else if ($op == 'configure' && $delta > 0) {
    $form['phpfreechat_block'] = array(
      '#type' => 'textfield',
      '#title' => t('Channel Name'),
      '#default_value' => variable_get('phpfreechat_block_channel_'.$delta, ''),
      '#description' => t('Enter the channel name to use')
    );
    return $form;
  }
  else if ($op == 'save' && $delta > 0) {
    variable_set('phpfreechat_block_channel_'.$delta, $edit['phpfreechat_block']);
  }
  else if ($op == 'view') {
    if (user_access('access content')) {
      if (!phpfreechat_check_install()) {
        $block['content'] = phpfreechat_not_found();
        return $block;
      }
      $params = phpfreechat_load_params();
      require_once 'phpfreechat/src/pfcinfo.class.php';
      $info  = new pfcInfo(md5($base_url), $params['data_private_path']);

      if ($delta == 0) {
        $users = $info->getOnlineNick(NULL);
        $nd_users = count($users);
        $block['content'] .= theme_item_list($users);
        $block['subject'] = t('who is chatting').' ('.t($nd_users).')';
      }
      if ($delta == 1) {
        $channel = variable_get('phpfreechat_block_channel_1', '');
        $users = $info->getOnlineNick($channel);
        $nd_users = count($users);
        $block['content'] .= theme_item_list($users);
        $block['subject'] = t('who is chatting on ').$channel.' ('.t($nd_users).')';
      }
      if ($delta == 2) {
        $channel = variable_get('phpfreechat_block_channel_2', '');
        $lastmsg_raw = $info->getLastMsg($channel, 10);
        $output = '';
        foreach($lastmsg_raw["data"] as $m) {
          $output .= $m["sender"].': ';
          $output .= '<em>' . $m["param"].'</em><br />';
          $date = $m["date"];
          $time = $m["time"];
        }
        $output .= t('Last chat: ');
        // Convert to US style date
        $date = preg_replace("/^\s*([0-9]{1,2})[\/\. -]+([0-9]{1,2})[\/\. -]+([0-9]{1,4})/", "\\2/\\1/\\3", $date);
        $output .= format_interval(time() - strtotime($date.' '.$time));
        $output .= t(' ago');
        $block['content'] .= $output;
        $block['subject'] = t('latest from ').$channel;
      }
      if ($delta == 3) {
        if(is_array($params['channels'])) {
          $nd_chan = count($params['channels']);
          for($i = 0; $i < $nd_chan; $i++) {
            $users = $info->getOnlineNick($params['channels'][$i]);
            $nd_users = count($users);
            $block['content'] .= $params['channels'][$i].' ('.t($nd_users).')';
            $block['content'] .= theme_item_list($users);
            if($i!=$nd_chan-1)
              $block['content'] .= '<br />';
          }
        }
        else {
            $users = $info->getOnlineNick($params['channels']);
            $nd_users = count($users);
            $block['content'] .= $params['channels'].' ('.t($nd_users).')';
            $block['content'] .= theme_item_list($users);
        }
        $block['subject'] = t('Active channels');
      }
      return $block;
    }
  }
}
udvranto’s picture

There is a small correction. I don't know why it is happening, but the $params['channels'] is not and array! That why I did what I did in my previous code. I have only one active channel.

        else {
            $users = $info->getOnlineNick($params[0]['channels']);
            $nd_users = count($users);
udvranto’s picture

Alright this one works perfectly

/**
 * Implementation of hook_block().
 * S M Mahbub Murshed June 26, 2007
 * udvranto@yahoo.com  
 */
function phpfreechat_block($op = 'list', $delta = 0, $edit = array()) {
  global $user, $base_url;
  if ($op == 'list') {
    $block[0]['info'] = t('Who is chatting');
    $block[1]['info'] = t('Who is chatting on...');
    $block[2]['info'] = t('Latest from...');
    $block[3]['info'] = t('Active channels');
    return $block;
  }
  else if ($op == 'configure' && $delta > 0) {
    $form['phpfreechat_block'] = array(
      '#type' => 'textfield',
      '#title' => t('Channel Name'),
      '#default_value' => variable_get('phpfreechat_block_channel_'.$delta, ''),
      '#description' => t('Enter the channel name to use')
    );
    return $form;
  }
  else if ($op == 'save' && $delta > 0) {
    variable_set('phpfreechat_block_channel_'.$delta, $edit['phpfreechat_block']);
  }
  else if ($op == 'view') {
    if (user_access('access content')) {
      if (!phpfreechat_check_install()) {
        $block['content'] = phpfreechat_not_found();
        return $block;
      }
      $params = phpfreechat_load_params();
      require_once 'phpfreechat/src/pfcinfo.class.php';
      $info  = new pfcInfo(md5($base_url), $params['data_private_path']);

      if ($delta == 0) {
        $users = $info->getOnlineNick(NULL);
        $nd_users = count($users);
        $block['content'] .= theme_item_list($users);
        $block['subject'] = t('who is chatting').' ('.t($nd_users).')';
      }
      if ($delta == 1) {
        $channel = variable_get('phpfreechat_block_channel_1', '');
        $users = $info->getOnlineNick($channel);
        $nd_users = count($users);
        $block['content'] .= theme_item_list($users);
        $block['subject'] = t('who is chatting on ').$channel.' ('.t($nd_users).')';
      }
      if ($delta == 2) {
        $channel = variable_get('phpfreechat_block_channel_2', '');
        $lastmsg_raw = $info->getLastMsg($channel, 10);
        $output = '';
        foreach($lastmsg_raw["data"] as $m) {
          $output .= $m["sender"].': ';
          $output .= '<em>' . $m["param"].'</em><br />';
          $date = $m["date"];
          $time = $m["time"];
        }
        $output .= t('Last chat: ');
        // Convert to US style date
        $date = preg_replace("/^\s*([0-9]{1,2})[\/\. -]+([0-9]{1,2})[\/\. -]+([0-9]{1,4})/", "\\2/\\1/\\3", $date);
        $output .= format_interval(time() - strtotime($date.' '.$time));
        $output .= t(' ago');
        $block['content'] .= $output;
        $block['subject'] = t('latest from ').$channel;
      }
      if ($delta == 3) {
        $channels = db_query('SELECT nid, phpfreechat_title FROM {phpfreechat} WHERE phpfreechat_enabled = 1 ORDER BY nid DESC');
        while ($channel = db_fetch_object($channels)) {
          $users = $info->getOnlineNick($channel->phpfreechat_title);
          $nd_users = count($users);
          $block['content'] .= l($channel->phpfreechat_title, 'node/'. $channel->nid, array('title' => t('Chat on %channel',array('%channel' => $channel->phpfreechat_title)))).' ('.t($nd_users).')';
          $block['content'] .= theme_item_list($users);              
        }
        
        $block['subject'] = t('Active channels');
      }
      return $block;
    }
  }
}
udvranto’s picture

A patch


      if ($delta == 3) {
        $channels = db_query('SELECT nid, phpfreechat_title, phpfreechat_channels FROM {phpfreechat} WHERE phpfreechat_enabled = 1 ORDER BY nid DESC');
        while ($channel = db_fetch_object($channels)) {
          $block['content'] .= l($channel->phpfreechat_title, 'node/'. $channel->nid, array('target'=> '_blank', 'title' => t('Chat on %channel',array('%channel' => $channel->phpfreechat_title))));
          $block['content'] .= "<br>";
          $ichannels = explode(",", $channel->phpfreechat_channels);
          $nc = count(ichannels);
          for($i=0; $i<$nc; $i++) {            
            $users = $info->getOnlineNick($ichannels[$i]);
            $nd_users = count($users);
            $block['content'] .= $ichannels[$i].' ('.t($nd_users).')';
            $block['content'] .= theme_item_list($users);
          }              
        }
        
        $block['subject'] = t('Active channels');
      }
udvranto’s picture

This is an update. Another block is added for unique users

/**
 * Implementation of hook_block().
 * Updated by 
 * S M Mahbub Murshed June 26, 2007
 * udvranto@yahoo.com  
 */
function phpfreechat_block($op = 'list', $delta = 0, $edit = array()) {
  global $user, $base_url;
  if ($op == 'list') {
    $block[0]['info'] = t('Who is chatting');
    $block[1]['info'] = t('Who is chatting on...');
    $block[2]['info'] = t('Latest from...');
    $block[3]['info'] = t('Active channels');
    $block[4]['info'] = t('Unique users of active channels');
    return $block;
  }
  else if ($op == 'configure' && $delta > 0) {
    $form['phpfreechat_block'] = array(
      '#type' => 'textfield',
      '#title' => t('Channel Name'),
      '#default_value' => variable_get('phpfreechat_block_channel_'.$delta, ''),
      '#description' => t('Enter the channel name to use')
    );
    return $form;
  }
  else if ($op == 'save' && $delta > 0) {
    variable_set('phpfreechat_block_channel_'.$delta, $edit['phpfreechat_block']);
  }
  else if ($op == 'view') {
    if (user_access('access content')) {
      if (!phpfreechat_check_install()) {
        $block['content'] = phpfreechat_not_found();
        return $block;
      }
      $params = phpfreechat_load_params();
      require_once 'phpfreechat/src/pfcinfo.class.php';
      $info  = new pfcInfo(md5($base_url), $params['data_private_path']);

      if ($delta == 0) {
        $users = $info->getOnlineNick(NULL);
        $nd_users = count($users);
        $block['content'] .= theme_item_list($users);
        $block['subject'] = t('who is chatting').' ('.t($nd_users).')';
      }
      if ($delta == 1) {
        $channel = variable_get('phpfreechat_block_channel_1', '');
        $users = $info->getOnlineNick($channel);
        $nd_users = count($users);
        $block['content'] .= theme_item_list($users);
        $block['subject'] = t('who is chatting on ').$channel.' ('.t($nd_users).')';
      }
      if ($delta == 2) {
        $channel = variable_get('phpfreechat_block_channel_2', '');
        $lastmsg_raw = $info->getLastMsg($channel, 10);
        $output = '';
        foreach($lastmsg_raw["data"] as $m) {
          $output .= $m["sender"].': ';
          $output .= '<em>' . $m["param"].'</em><br />';
          $date = $m["date"];
          $time = $m["time"];
        }
        $output .= t('Last chat: ');
        // Convert to US style date
        $date = preg_replace("/^\s*([0-9]{1,2})[\/\. -]+([0-9]{1,2})[\/\. -]+([0-9]{1,4})/", "\\2/\\1/\\3", $date);
        $output .= format_interval(time() - strtotime($date.' '.$time));
        $output .= t(' ago');
        $block['content'] .= $output;
        $block['subject'] = t('latest from ').$channel;
      }
      if ($delta == 3) {
        $channels = db_query('SELECT nid, phpfreechat_title, phpfreechat_channels FROM {phpfreechat} WHERE phpfreechat_enabled = 1 ORDER BY nid DESC');        
        while ($channel = db_fetch_object($channels)) {
          $channel_line = l($channel->phpfreechat_title, 'node/'. $channel->nid, array('target'=> '_blank', 'title' => t('Chat on %channel',array('%channel' => $channel->phpfreechat_title))));
          $channel_line .= "<br>";
          $ichannels = strtok($channel->phpfreechat_channels, ", \n\t");
          while ($ichannels !== false) {
            $users = $info->getOnlineNick($ichannels);
            $nd_users = count($users);
            $channel_line .= $ichannels.' ('.t($nd_users).')';
            $channel_line .= theme_item_list($users);
            $ichannels = strtok(", \n\t");
          }
          $channels_list[] = $channel_line;            
        }
        $block['content'] .= theme_item_list($channels_list);        
        $block['subject'] = t('Active channels');
      }
      if ($delta == 4) {
        $channels = db_query('SELECT nid, phpfreechat_title, phpfreechat_channels FROM {phpfreechat} WHERE phpfreechat_enabled = 1 ORDER BY nid DESC');        
        while ($channel = db_fetch_object($channels)) {
          $ichannels = strtok($channel->phpfreechat_channels, ", \n\t");
          $users = array();
          while ($ichannels !== false) {
            $users = array_merge($users, $info->getOnlineNick($ichannels));
            $ichannels = strtok(", \n\t");
          }
          $users = array_unique ($users);
          $nd_users = count($users);
          $channels_display[] .= l($channel->phpfreechat_title.' ('.t($nd_users).')', 'node/'. $channel->nid, array('target'=> '_blank', 'title' => t('Chat on %channel',array('%channel' => $channel->phpfreechat_title))))
          . theme_item_list($users);                  
        }
        $block['content'] .= theme_item_list($channels_display);
        $block['subject'] = t('Unique users of active channels');
      }
      return $block;
    }
  }
}

permutations’s picture

Status: Active » Closed (fixed)

The current version uploaded today (5.x-1.2) has a block that lists people who are chatting.