commit 43400e9482429bba4a231465dc48a8c5fbcbd6cb
Author: bhawanac <bhawanac@cybage.com>
Date:   Thu Apr 28 16:16:58 2016 +0530

    Applied og patch to chat group

diff --git a/docroot/sites/all/modules/contrib/drupalchat/drupalchat.admin.inc b/docroot/sites/all/modules/contrib/drupalchat/drupalchat.admin.inc
index 6075503..12154c5 100644
--- a/docroot/sites/all/modules/contrib/drupalchat/drupalchat.admin.inc
+++ b/docroot/sites/all/modules/contrib/drupalchat/drupalchat.admin.inc
@@ -431,7 +431,9 @@ function drupalchat_settings_form($form, &$form_state) {
     $form['drupalchat_chatlist_cont']['drupalchat_rel']['#options'][DRUPALCHAT_REL_FF] = t('Flag Friend module');
   }
   if (module_exists('og')) {
-    $form['drupalchat_chatlist_cont']['drupalchat_rel']['#options'][DRUPALCHAT_REL_OG] = t('Organic Groups module');
+    $form['drupalchat_chatlist_cont']['drupalchat_rel']['#options'][DRUPALCHAT_REL_OG] = t('Organic Group members as buddies');
+    $form['drupalchat_chatlist_cont']['drupalchat_rel']['#options'][DRUPALCHAT_REL_OG_CHATROOM] = t('Organic Groups as chatrooms');
+    $form['drupalchat_chatlist_cont']['drupalchat_rel']['#options'][DRUPALCHAT_REL_OG_AND_CHATROOM] = t('Organic Group members as buddies and each og as chatroom');
   }
   $form['drupalchat_chatlist_cont']['drupalchat_ur_name'] = array(
     '#type' => 'textfield',
diff --git a/docroot/sites/all/modules/contrib/drupalchat/drupalchat.module b/docroot/sites/all/modules/contrib/drupalchat/drupalchat.module
index f784c49..0a089a6 100644
--- a/docroot/sites/all/modules/contrib/drupalchat/drupalchat.module
+++ b/docroot/sites/all/modules/contrib/drupalchat/drupalchat.module
@@ -17,7 +17,9 @@ define('DRUPALCHAT_USER_IDLE', 2);
 define('DRUPALCHAT_REL_AUTH', 0);
 define('DRUPALCHAT_REL_UR', 1);
 define('DRUPALCHAT_REL_FF', 2);
-define('DRUPALCHAT_REL_OG', 3);
+define('DRUPALCHAT_REL_OG', 3);  // Only og members as buddies
+define('DRUPALCHAT_REL_OG_CHATROOM', 4); // Each og as a separate chatroom
+define('DRUPALCHAT_REL_OG_AND_CHATROOM', 5); // Og members as buddies as well as each og as a separate chatroom
 
 define('DRUPALCHAT_EXTERNAL_HOST', 'http://api'.variable_get('drupalchat_ext_d_i','1').'.iflychat.com');
 define('DRUPALCHAT_EXTERNAL_PORT', '80');
@@ -541,6 +543,16 @@ function drupalchat_init() {
       'changeurl' => url('drupalchat/change-guest-name', array('query' => array('t' => time(),),)),
       'allowSmileys' => variable_get('drupalchat_enable_smiley', 1),
     );
+	
+	 if (variable_get('drupalchat_rel', DRUPALCHAT_REL_AUTH) == DRUPALCHAT_REL_OG_CHATROOM || variable_get('drupalchat_rel', DRUPALCHAT_REL_AUTH) == DRUPALCHAT_REL_OG_AND_CHATROOM) {
+          $groups = _drupalchat_get_groups($user->uid);
+          $groups[] = 0; // Adding public chat
+          foreach ($groups as $key => $group) {
+            $groups[$key] = 'c-' . $group;
+          }
+          $my_settings['chatrooms'] = $groups;
+        }
+
     if($polling_method == DRUPALCHAT_AJAX) {
       $my_settings['refresh_rate'] = variable_get('drupalchat_refresh_rate', 2);
     }
@@ -775,7 +787,11 @@ function drupalchat_poll() {
 
   $initial_time = time();
   $message_count = 0;
-
+	$groups = _drupalchat_get_groups($user->uid);
+	$groups[] = 0; // Adding public chat
+	foreach ($groups as $key => $group) {
+		$groups[$key] = 'c-' . $group;
+	}
 
   /*if (isset($_GET['drupalchat_last_timestamp'])) {
     $last_timestamp = check_plain($_GET['drupalchat_last_timestamp']);
@@ -821,17 +837,18 @@ function drupalchat_poll() {
       sleep(3);
 	  /*watchdog('drupalchat', '%a - %b - OLD - %c - CURRENT %e - %d', array('%a' => $user->uid, '%b' => time(), '%b' => time(), '%c' => print_r($buddylist_online_old,true), '%d' => print_r(_drupalchat_buddylist_diff($buddylist_online_old, $buddylist_online),true), '%e' => print_r($buddylist_online,true)), WATCHDOG_DEBUG);*/
       $buddylist_online = _drupalchat_buddylist_online($buddylist);
-      //$message_count = db_query('SELECT COUNT(*) FROM {drupalchat_msg} m INNER JOIN {users} u ON m.uid1 = u.uid WHERE m.uid2 = :m.uid2 AND m.timestamp > :m.timestamp', array(':m.uid2' => $user->uid, ':m.timestamp' => $last_timestamp))->fetchField();
+     
+	 //$message_count = db_query('SELECT COUNT(*) FROM {drupalchat_msg} m INNER JOIN {users} u ON m.uid1 = u.uid WHERE m.uid2 = :m.uid2 AND m.timestamp > :m.timestamp', array(':m.uid2' => $user->uid, ':m.timestamp' => $last_timestamp))->fetchField();
       if($user->uid > 0) {
 	    $message_count = db_query(' SELECT COUNT(*)
                                     FROM {drupalchat_msg} m
-                                    WHERE (m.uid2 IN (:uid2,\'c-0\') OR m.uid1 = :uid2)
+                                    WHERE (m.uid2 IN (:uid2,\'' . implode('\', \'', $groups) . '\') OR m.uid1 = :uid2)
                                     AND m.timestamp > :timestamp', array(':uid2' => $user->uid, ':timestamp' => $last_timestamp))->fetchField();
       }
 	  else {
 	    $message_count = db_query(' SELECT COUNT(*)
                                     FROM {drupalchat_msg} m
-                                    WHERE (m.uid2 IN (:uid2,\'c-0\') OR m.uid1 = :uid2)
+                                     WHERE (m.uid2 IN (:uid2,\'' . implode('\', \'', $groups) . '\') OR m.uid1 = :uid2)
                                     AND m.timestamp > :timestamp', array(':uid2' => '0-'._drupalchat_get_sid(), ':timestamp' => $last_timestamp))->fetchField();
 	  }
 	  _drupalchat_touch_user($user->uid);
@@ -843,8 +860,8 @@ function drupalchat_poll() {
       $messages = db_query('SELECT m.message_id, m.uid1, m.uid2, m.message, m.timestamp FROM {drupalchat_msg} m WHERE (m.uid2 IN (:uid2,\'c-0\') OR m.uid1 = :uid2) AND m.timestamp > :timestamp ORDER BY m.timestamp ASC', array(':uid2' => $user->uid, ':timestamp' => $last_timestamp));
     }
 	else {
-	  $messages = db_query('SELECT m.message_id, m.uid1, m.uid2, m.message, m.timestamp FROM {drupalchat_msg} m WHERE (m.uid2 IN (:uid2,\'c-0\') OR m.uid1 = :uid2) AND m.timestamp > :timestamp ORDER BY m.timestamp ASC', array(':uid2' => '0-'._drupalchat_get_sid(), ':timestamp' => $last_timestamp));
-	}
+	   $messages = db_query('SELECT m.message_id, m.uid1, m.uid2, m.message, m.timestamp FROM {drupalchat_msg} m WHERE (m.uid2 IN (:uid2,\'' . implode('\', \'', $groups) . '\') OR m.uid1 = :uid2) AND m.timestamp > :timestamp ORDER BY m.timestamp ASC', array(':uid2' => '0-'._drupalchat_get_sid(), ':timestamp' => $last_timestamp));
+}
     //while ($message = db_fetch_object($messages)) {
     // Drupal 7
     foreach ($messages as $message) {
@@ -952,6 +969,27 @@ function _drupalchat_buddylist($uid) {
     foreach ($result as $friend) {
       $uid == $friend->uid ? $users[] = $friend->friend_uid : $users[] = $friend->uid;
     }
+  } elseif ($drupalchat_rel == DRUPALCHAT_REL_OG || $drupalchat_rel == DRUPALCHAT_REL_OG_AND_CHATROOM) {
+    $user = user_load($uid);
+
+    // Create an array of all groups this user belongs to
+    $og_array = _drupalchat_get_groups($uid);
+
+    // Query the database for all users belonging to those groups
+    if (!empty($og_array)) {
+      $query = db_select('og_membership', 'ogm');
+      $query
+        ->fields('ogm', array('etid'))
+        ->condition('entity_type', 'user')
+        ->condition('gid', $og_array, 'IN')
+        ->condition('etid', $uid, '<>')
+        ->distinct();
+      $results = $query->execute()->fetchAllAssoc('etid');
+    }
+    // Create a flat array of user IDs
+    foreach ($results as $member) {
+      $users[] = $member->etid;
+    }
   }
   return $users;
 }
@@ -960,79 +998,94 @@ function _drupalchat_buddylist_online($buddylist) {
   global $user, $base_url;
 
   $users = array();
-  if(variable_get('drupalchat_enable_chatroom', 1) == 1) {
+  if (variable_get('drupalchat_enable_chatroom', 1) == 1) {
     $users['c-0'] = array('name' => t('Public Chatroom'), 'status' => '1');
-	if(variable_get('drupalchat_user_picture', 1) == 1) {
-	  $users['c-0']['p'] = $base_url . '/' . drupal_get_path('module', 'drupalchat') . '/themes/' . variable_get('drupalchat_theme', 'light') . '/images/default_room.png';
-	}
-  }
-  if (variable_get('drupalchat_rel', DRUPALCHAT_REL_AUTH) > DRUPALCHAT_REL_AUTH) {
-    // Return empty on an empty buddylist
-    if (empty($buddylist)) {
-      $users['total'] = 0;
-      return $users;
+    if (variable_get('drupalchat_rel', DRUPALCHAT_REL_AUTH) == DRUPALCHAT_REL_OG_CHATROOM || variable_get('drupalchat_rel', DRUPALCHAT_REL_AUTH) == DRUPALCHAT_REL_OG_AND_CHATROOM) {
+      $og_array = _drupalchat_get_groups($user->uid);
+
+      if (!empty($og_array)) {
+        $groups = db_select('node', 'n')
+            ->fields('n', array('nid', 'title'))
+            ->condition('nid', $og_array, 'IN')
+            ->execute()
+            ->fetchAllAssoc('nid');
+        foreach ($groups as $group) {
+          $users['c-' . $group->nid] = array('name' => t($group->title), 'status' => '1');
+          if (variable_get('drupalchat_user_picture', 1) == 1) {
+            $users['c-' . $group->nid]['p'] = $base_url . '/' . drupal_get_path('module', 'drupalchat') . '/themes/' . variable_get('drupalchat_theme', 'light') . '/images/default_room.png';
+          }
+        }
+      }
     }
-	$result = db_select('drupalchat_users', 'n')
-      ->fields('n', array('uid', 'name', 'status'))
-      ->condition('timestamp', (time() - variable_get('drupalchat_user_latency', 2)), '>=')
-      ->condition('uid', $buddylist, 'IN')
-      ->execute();
-  }
-  else {
-    if($user->uid > 0) {
-	  $result = db_select('drupalchat_users', 'n')
-	    ->fields('n', array('uid', 'name', 'status', 'session'))
-        ->condition('timestamp', (time() - variable_get('drupalchat_user_latency', 2)), '>=')
-        ->condition('uid', $user->uid, '<>')
-        ->execute();
-	}
-	else {
-	  $result = db_select('drupalchat_users', 'n')
-	    ->fields('n', array('uid', 'name', 'status', 'session'))
-        ->condition('timestamp', (time() - variable_get('drupalchat_user_latency', 2)), '>=')
-        ->condition('session', _drupalchat_get_sid(), '<>')
-        ->execute();
-	}
-  }
-  foreach ($result as $buddy) {
-    if($buddy->uid > 0) {
-	  $account = user_load($buddy->uid);
-	  $users[$buddy->uid] = array('name' => check_plain(format_username($account)), 'status' => $buddy->status);
-	  if(variable_get('drupalchat_user_picture', 1) == 1) {
-	    $users[$buddy->uid]['p'] = drupalchat_return_pic_url_any_user(user_load($buddy->uid));
-	  }
-	}
-	else {
-	  $users[$buddy->uid . '-' . $buddy->session] = array('name' => check_plain($buddy->name), 'status' => $buddy->status);
-	  if(variable_get('drupalchat_user_picture', 1) == 1) {
-	    $users[$buddy->uid . '-' . $buddy->session]['p'] = drupalchat_return_pic_url_any_user(user_load('0'));
-	  }
-	}
-  }
-  $users['total'] = count($users);
-  if(variable_get('drupalchat_enable_chatroom', 1) == 1) {
-    $users['total']--;
+    if (variable_get('drupalchat_rel', DRUPALCHAT_REL_AUTH) != DRUPALCHAT_REL_AUTH) {
+      // Return empty on an empty buddylist
+      if (empty($buddylist)) {
+        $users['total'] = 0;
+        return $users;
+      }
+      $result = db_select('drupalchat_users', 'n')
+          ->fields('n', array('uid', 'name', 'status'))
+          ->condition('timestamp', (time() - variable_get('drupalchat_user_latency', 2)), '>=')
+          ->condition('uid', $buddylist, 'IN')
+          ->execute();
+    }
+    else {
+      if ($user->uid > 0) {
+        $result = db_select('drupalchat_users', 'n')
+            ->fields('n', array('uid', 'name', 'status', 'session'))
+            ->condition('timestamp', (time() - variable_get('drupalchat_user_latency', 2)), '>=')
+            ->condition('uid', $user->uid, '<>')
+            ->execute();
+      }
+      else {
+        $result = db_select('drupalchat_users', 'n')
+            ->fields('n', array('uid', 'name', 'status', 'session'))
+            ->condition('timestamp', (time() - variable_get('drupalchat_user_latency', 2)), '>=')
+            ->condition('session', _drupalchat_get_sid(), '<>')
+            ->execute();
+      }
+    }
+    foreach ($result as $buddy) {
+      if ($buddy->uid > 0) {
+        $account = user_load($buddy->uid);
+        $users[$buddy->uid] = array('name' => check_plain(format_username($account)), 'status' => $buddy->status);
+        if (variable_get('drupalchat_user_picture', 1) == 1) {
+          $users[$buddy->uid]['p'] = drupalchat_return_pic_url_any_user(user_load($buddy->uid));
+        }
+      }
+      else {
+        $users[$buddy->uid . '-' . $buddy->session] = array('name' => check_plain($buddy->name), 'status' => $buddy->status);
+        if (variable_get('drupalchat_user_picture', 1) == 1) {
+          $users[$buddy->uid . '-' . $buddy->session]['p'] = drupalchat_return_pic_url_any_user(user_load('0'));
+        }
+      }
+    }
+    $users['total'] = count($users);
+    if (variable_get('drupalchat_enable_chatroom', 1) == 1) {
+      $users['total'] --;
+    }
+    return $users;
   }
-  return $users;
 }
 
 function _drupalchat_buddylist_diff($ar1, $ar2) {
-  if ($ar1['total'] != $ar2['total']) {
-    return FALSE;
-  }
-
-  foreach ($ar1 as $key => $value) {
-    if (!isset($ar2[$key])) {
+    if ($ar1['total'] != $ar2['total']) {
       return FALSE;
     }
-    if ($value['status'] != $ar2[$key]['status']) {
-      return FALSE;
+
+    foreach ($ar1 as $key => $value) {
+      if (!isset($ar2[$key])) {
+        return FALSE;
+      }
+      if ($value['status'] != $ar2[$key]['status']) {
+        return FALSE;
+      }
     }
+
+    return TRUE;
   }
 
-  return TRUE;
-}
-/**
+  /**
  * @todo Please document this function.
  * @see http://drupal.org/node/1354
  */
@@ -1543,74 +1596,76 @@ function _drupalchat_get_groups($uid) {
 }
 
 function drupalchat_get_thread_history() {
-  global $user;
-  $json = array();
-  if(isset($_POST['drupalchat_open_chat_uids'])) {
-    $chat_ids = explode(',', check_plain($_POST['drupalchat_open_chat_uids']));
+    global $user;
+    $json = array();
+    if (isset($_POST['drupalchat_open_chat_uids'])) {
+      $chat_ids = explode(',', check_plain($_POST['drupalchat_open_chat_uids']));
 
-    $json['messages'] = array();
-    foreach($chat_ids as $chat_id) {
-      $messages = '';
+      $json['messages'] = array();
+      foreach ($chat_ids as $chat_id) {
+        $messages = '';
 
-      if($user->uid > 0) {
-        $current_uid = $user->uid;
-      }
-      else {
-        $current_uid = '0-'._drupalchat_get_sid();
-      }
+        if ($user->uid > 0) {
+          $current_uid = $user->uid;
+        }
+        else {
+          $current_uid = '0-' . _drupalchat_get_sid();
+        }
 
-      if($chat_id == 'c-0') {
-        $messages = db_query('SELECT m.message_id, m.uid1, m.uid2, m.message, m.timestamp FROM {drupalchat_msg} m WHERE m.uid2 = \'c-0\' ORDER BY m.timestamp DESC LIMIT 30', array(':uid1' => $current_uid, ':uid2' => $chat_id))->fetchAll();
-      }
-      else {
-        $messages = db_query('SELECT m.message_id, m.uid1, m.uid2, m.message, m.timestamp FROM {drupalchat_msg} m WHERE (m.uid2 = :uid2 AND m.uid1 = :uid1) OR (m.uid2 = :uid1 AND m.uid1 = :uid2) ORDER BY m.timestamp DESC LIMIT 30', array(':uid1' => $current_uid, ':uid2' => $chat_id))->fetchAll();
-      }
 
-      foreach ($messages as $message) {
-      //print_r($message);
-        if((((!strpos($message->uid1,'-')) && ($message->uid1 != $user->uid)) || ((strpos($message->uid1,'-')) && ($message->uid1 != '0-'._drupalchat_get_sid()))) || ($message->uid2 == 'c-0')) {
-          if(!strpos($message->uid1,'-')) {
-            $account = user_load($message->uid1);
-          $temp_msg = array('message' => check_plain($message->message), 'timestamp' => date("H:i", $message->timestamp), 'uid1' => $message->uid1, 'name' => check_plain(format_username($account)), 'uid2' => $message->uid2, 'message_id' => check_plain($message->message_id),);
-          if(variable_get('drupalchat_user_picture', 1) == 1) {
-          $temp_msg['p'] = drupalchat_return_pic_url_any_user($account);
-          }
-          $json['messages'][] = $temp_msg;
-          }
-          else {
-            $arr = explode("-", $message->uid1, 2);
-          $sid = $arr[1];
-            $name = db_query('SELECT name FROM {drupalchat_users} WHERE uid = :uid AND session = :sid', array(':uid' => '0', ':sid' => $sid))->fetchField();
-            $temp_msg = array('message' => check_plain($message->message), 'timestamp' => date("H:i", $message->timestamp), 'uid1' => $message->uid1, 'name' => $name, 'uid2' => $message->uid2, 'message_id' => check_plain($message->message_id),);
-          if(variable_get('drupalchat_user_picture', 1) == 1) {
-          $temp_msg['p'] = drupalchat_return_pic_url_any_user(user_load('0'));
-          }
-          $json['messages'][] = $temp_msg;
-          }
+        if (substr($chat_id, 0, 2) == 'c-') {
+          $messages = db_query('SELECT m.message_id, m.uid1, m.uid2, m.message, m.timestamp FROM {drupalchat_msg} m WHERE m.uid2 = \'' . $chat_id . '\' ORDER BY m.timestamp DESC LIMIT 30', array(':uid1' => $current_uid, ':uid2' => $chat_id))->fetchAll();
         }
         else {
-          if(!strpos($message->uid2,'-')) {
-            $account = user_load($message->uid2);
-          $temp_msg = array('message' => check_plain($message->message), 'timestamp' => date("H:i", $message->timestamp), 'uid1' => $message->uid1, 'name' => check_plain(format_username($account)), 'uid2' => $message->uid2, 'message_id' => check_plain($message->message_id),);
-          if(variable_get('drupalchat_user_picture', 1) == 1) {
-          $temp_msg['p'] = drupalchat_return_pic_url_any_user($account);
-          }
-          $json['messages'][] = $temp_msg;
+          $messages = db_query('SELECT m.message_id, m.uid1, m.uid2, m.message, m.timestamp FROM {drupalchat_msg} m WHERE (m.uid2 = :uid2 AND m.uid1 = :uid1) OR (m.uid2 = :uid1 AND m.uid1 = :uid2) ORDER BY m.timestamp DESC LIMIT 30', array(':uid1' => $current_uid, ':uid2' => $chat_id))->fetchAll();
+        }
+
+        foreach ($messages as $message) {
+          //print_r($message);
+          if ((((!strpos($message->uid1, '-')) && ($message->uid1 != $user->uid)) || ((strpos($message->uid1, '-')) && ($message->uid1 != '0-' . _drupalchat_get_sid()))) || (substr($message->uid2, 0, 2) == 'c-')) {
+            // if((((!strpos($message->uid1,'-')) && ($message->uid1 != $user->uid)) || ((strpos($message->uid1,'-')) && ($message->uid1 != '0-'._drupalchat_get_sid()))) || ($message->uid2 == 'c-0')) {
+            if (!strpos($message->uid1, '-')) {
+              $account = user_load($message->uid1);
+              $temp_msg = array('message' => check_plain($message->message), 'timestamp' => date("H:i", $message->timestamp), 'uid1' => $message->uid1, 'name' => check_plain(format_username($account)), 'uid2' => $message->uid2, 'message_id' => check_plain($message->message_id),);
+              if (variable_get('drupalchat_user_picture', 1) == 1) {
+                $temp_msg['p'] = drupalchat_return_pic_url_any_user($account);
+              }
+              $json['messages'][] = $temp_msg;
+            }
+            else {
+              $arr = explode("-", $message->uid1, 2);
+              $sid = $arr[1];
+              $name = db_query('SELECT name FROM {drupalchat_users} WHERE uid = :uid AND session = :sid', array(':uid' => '0', ':sid' => $sid))->fetchField();
+              $temp_msg = array('message' => check_plain($message->message), 'timestamp' => date("H:i", $message->timestamp), 'uid1' => $message->uid1, 'name' => $name, 'uid2' => $message->uid2, 'message_id' => check_plain($message->message_id),);
+              if (variable_get('drupalchat_user_picture', 1) == 1) {
+                $temp_msg['p'] = drupalchat_return_pic_url_any_user(user_load('0'));
+              }
+              $json['messages'][] = $temp_msg;
+            }
           }
           else {
-            $arr = explode("-", $message->uid2, 2);
-          $sid = $arr[1];
-            $name = db_query('SELECT name FROM {drupalchat_users} WHERE uid = :uid AND session = :sid', array(':uid' => '0', ':sid' => $sid))->fetchField();
-            $temp_msg = array('message' => check_plain($message->message), 'timestamp' => date("H:i", $message->timestamp), 'uid1' => $message->uid1, 'name' => $name, 'uid2' => $message->uid2, 'message_id' => check_plain($message->message_id),);
-          if(variable_get('drupalchat_user_picture', 1) == 1) {
-          $temp_msg['p'] = drupalchat_return_pic_url_any_user(user_load('0'));
-          }
-          $json['messages'][] = $temp_msg;
+            if (!strpos($message->uid2, '-')) {
+              $account = user_load($message->uid2);
+              $temp_msg = array('message' => check_plain($message->message), 'timestamp' => date("H:i", $message->timestamp), 'uid1' => $message->uid1, 'name' => check_plain(format_username($account)), 'uid2' => $message->uid2, 'message_id' => check_plain($message->message_id),);
+              if (variable_get('drupalchat_user_picture', 1) == 1) {
+                $temp_msg['p'] = drupalchat_return_pic_url_any_user($account);
+              }
+              $json['messages'][] = $temp_msg;
+            }
+            else {
+              $arr = explode("-", $message->uid2, 2);
+              $sid = $arr[1];
+              $name = db_query('SELECT name FROM {drupalchat_users} WHERE uid = :uid AND session = :sid', array(':uid' => '0', ':sid' => $sid))->fetchField();
+              $temp_msg = array('message' => check_plain($message->message), 'timestamp' => date("H:i", $message->timestamp), 'uid1' => $message->uid1, 'name' => $name, 'uid2' => $message->uid2, 'message_id' => check_plain($message->message_id),);
+              if (variable_get('drupalchat_user_picture', 1) == 1) {
+                $temp_msg['p'] = drupalchat_return_pic_url_any_user(user_load('0'));
+              }
+              $json['messages'][] = $temp_msg;
+            }
           }
         }
       }
+      $json['messages'] = array_reverse($json['messages']);
     }
-    $json['messages'] = array_reverse($json['messages']);
+    drupal_json_output($json);
   }
-  drupal_json_output($json);
-}
diff --git a/docroot/sites/all/modules/contrib/drupalchat/js/drupalchat.js b/docroot/sites/all/modules/contrib/drupalchat/js/drupalchat.js
index b7fa334..f741a74 100644
--- a/docroot/sites/all/modules/contrib/drupalchat/js/drupalchat.js
+++ b/docroot/sites/all/modules/contrib/drupalchat/js/drupalchat.js
@@ -560,7 +560,8 @@ function drupalchatMain() {
   			  drupalselfmessage = true;
   			}
   			//Add div if required.
-  			if(value.uid2=="c-0") {
+if(value.uid2.substr(0, 2)=="c-") {
+
   			  drupalchatroom = true;
   			}
               else {
@@ -568,7 +569,7 @@ function drupalchatMain() {
               }			
   			chatboxtitle = (drupalchatroom || drupalselfmessage)?value.uid2:value.uid1;
   			if (jQuery("#chatbox_"+chatboxtitle).length <= 0) {
-  				createChatBox(chatboxtitle, drupalchatroom?"Public Chatroom":value.name, 1);
+  				createChatBox(chatboxtitle, drupalchatroom ? Drupal.settings.drupalchat.chatrooms[value.uid2] : value.name, 1);
   			}
   			else if (jQuery("#chatbox_"+chatboxtitle+" .subpanel").is(':hidden')) {
   				if (jQuery("#chatbox_"+chatboxtitle).css('display') == 'none') {
