function drupalchat_poll in DrupalChat 7.2
Same name and namespace in other branches
- 6.2 drupalchat.module \drupalchat_poll()
- 6 drupalchat.module \drupalchat_poll()
- 7 drupalchat.module \drupalchat_poll()
Process and get messages
1 string reference to 'drupalchat_poll'
- drupalchat_menu in ./
drupalchat.module - Implements hook_menu().
File
- ./
drupalchat.module, line 737 - Module code for DrupalChat.
Code
function drupalchat_poll() {
global $user;
$initial_time = time();
$message_count = 0;
/*if (isset($_GET['drupalchat_last_timestamp'])) {
$last_timestamp = check_plain($_GET['drupalchat_last_timestamp']);
}*/
if ($_GET['drupalchat_last_timestamp'] > 0) {
$last_timestamp = check_plain($_GET['drupalchat_last_timestamp']);
}
else {
$last_timestamp = $initial_time;
//$last_timestamp = 1;
}
$buddylist = _drupalchat_buddylist($user->uid);
$buddylist_online_old = _drupalchat_buddylist_online($buddylist);
$polling_method = check_plain(variable_get('drupalchat_polling_method', DRUPALCHAT_AJAX));
//echo $polling_method;
//die;
$json['messages'] = array();
if ($polling_method == DRUPALCHAT_AJAX) {
/*$messages = db_query('SELECT u.name, m.uid1, m.uid2, m.message, m.timestamp FROM {drupalchat_msg} m INNER JOIN {users} u ON m.uid1 = u.uid WHERE m.uid2 = :uid2 AND m.timestamp > :timestamp ORDER BY m.timestamp ASC', array(':uid2' => $user->uid, ':timestamp' => $last_timestamp));
foreach ($messages as $message) {
$account = user_load($message->uid1);
$json['messages'][] = array('message' => $message->message, 'timestamp' => date("H:i", $message->timestamp), 'uid1' => $message->uid1, 'name' => check_plain(format_username($account)));
if ($message->timestamp > $last_timestamp) {
$last_timestamp = $message->timestamp;
}
}*/
//echo 'SELECT u.name, m.uid1, m.uid2, m.message, m.timestamp FROM {drupalchat_msg} m INNER JOIN {users} u ON m.uid1 = u.uid WHERE m.uid2 = '.$user->uid.' AND m.timestamp > '.$last_timestamp.' ORDER BY m.timestamp ASC';
_drupalchat_touch_user($user->uid);
module_invoke_all('drupalchat_ajaxpoll');
// AJAX poll hook
}
elseif ($polling_method == DRUPALCHAT_LONGPOLL) {
do {
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();
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)
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)
AND m.timestamp > :timestamp', array(
':uid2' => '0-' . _drupalchat_get_sid(),
':timestamp' => $last_timestamp,
))
->fetchField();
}
_drupalchat_touch_user($user->uid);
module_invoke_all('drupalchat_longpoll');
// Long poll hook
} while (time() - $initial_time < ini_get('max_execution_time') - 5 && $message_count == 0 && _drupalchat_buddylist_diff($buddylist_online_old, $buddylist_online));
}
if ($message_count > 0 || $polling_method == DRUPALCHAT_AJAX) {
if ($user->uid > 0) {
$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,
));
}
//while ($message = db_fetch_object($messages)) {
// Drupal 7
foreach ($messages as $message) {
//$arr = explode("-", $message->uid1, 2);
if (!strpos($message->uid1, '-') && $message->uid1 != $user->uid || strpos($message->uid1, '-') && $message->uid1 != '0-' . _drupalchat_get_sid()) {
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 (check_plain(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 (check_plain(variable_get('drupalchat_user_picture', 1)) == 1) {
$temp_msg['p'] = drupalchat_return_pic_url_any_user(user_load('0'));
}
$json['messages'][] = $temp_msg;
}
}
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 (check_plain(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 (check_plain(variable_get('drupalchat_user_picture', 1)) == 1) {
$temp_msg['p'] = drupalchat_return_pic_url_any_user(user_load('0'));
}
$json['messages'][] = $temp_msg;
}
}
//if($message->
if ($message->timestamp > $last_timestamp) {
$last_timestamp = $message->timestamp;
}
}
}
$json['status'] = 0;
$json['total_messages'] = $message_count;
$json['last_timestamp'] = $last_timestamp;
$json['buddylist'] = isset($buddylist_online) ? $buddylist_online : $buddylist_online_old;
/*echo '<pre>';
print_r($json);
echo '</pre>';*/
drupal_json_output($json);
}