-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.module
81 lines (62 loc) · 1.65 KB
/
chat.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
* Implementation of hook_permission().
*/
function chat_permission() {
return array(
'access chat' => array(
'title' => t('Access Chat'),
'description' => t('Allow access to the chat subsystem.'),
),
);
}
/**
* Implementation of hook_menu().
*/
function chat_menu() {
$items = array();
$items['chat'] = array(
'title' => 'Chat',
'access arguments' => array('access chat'),
'page callback' => 'chat_page',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implementation of hook_theme().
*/
function chat_theme() {
return array(
'chat' => array(
'template' => 'chat',
'arguments' => array('username' => NULL),
),
);
}
/**
* Page callback for /chat menu item.
*/
function chat_page() {
global $user;
return theme('chat', array('user' => $user, 'users' => online_users()));
}
function online_users(){
global $user;
$query = db_select('chat_messages', 'm');
$db_or = db_or();
$db_or->condition('m.sender', $user->uid);
$db_or->condition('m.receiver', $user->uid);
$query->condition($db_or);
$query->join('users', 'u', 'u.uid = m.receiver OR u.uid = m.sender');
$query->condition('u.uid', $user->uid, '!=');
$query->groupBy('u.uid');
$query->fields('u', array('uid'));
$query->fields('m', array('id', 'message', 'sender', 'receiver'));
$query->fields('u', array('name', 'mail'));
$query->addExpression('MAX(timestamp)');
$query->orderBy('m.timestamp', 'DESC')
->range(0, 30);
//echo '<pre>'; print_r($query->execute()->fetchAll()); exit;
return $query->execute()->fetchAll();
}