1
|
<?php
|
2
|
/**
|
3
|
* @file
|
4
|
* Most active poster.
|
5
|
*/
|
6
|
|
7
|
/**
|
8
|
* Callback function to supply a list of content types.
|
9
|
*/
|
10
|
function advanced_forum_forum_most_active_poster_ctools_content_types() {
|
11
|
if (module_exists('nodecomment')) {
|
12
|
return array(
|
13
|
'single' => TRUE,
|
14
|
'title' => t('Most active forum poster'),
|
15
|
'icon' => 'icon_forum.png',
|
16
|
'description' => t(
|
17
|
'A block with information about the most active forum poster.'
|
18
|
),
|
19
|
'required context' => new ctools_context_required(t('Forum'), 'forum'),
|
20
|
'category' => t('Forum'),
|
21
|
'defaults' => array('period' => 'ever', 'exclude' => 0, 'uidone' => TRUE),
|
22
|
);
|
23
|
}
|
24
|
}
|
25
|
|
26
|
/**
|
27
|
* Render the content.
|
28
|
*/
|
29
|
function advanced_forum_forum_most_active_poster_content_type_render(
|
30
|
$subtype,
|
31
|
$conf,
|
32
|
$panel_args,
|
33
|
$context
|
34
|
) {
|
35
|
if (!empty($context) && empty($context->data)) {
|
36
|
return;
|
37
|
}
|
38
|
|
39
|
$tid = 0;
|
40
|
if (!empty($context)) {
|
41
|
$tid = $context->data->tid;
|
42
|
}
|
43
|
|
44
|
$period = empty($conf['period']) ? 'ever' : $conf['period'];
|
45
|
|
46
|
switch ($period) {
|
47
|
case 'day':
|
48
|
$period_arg = 60 * 60 * 24;
|
49
|
break;
|
50
|
|
51
|
case 'week':
|
52
|
$period_arg = 60 * 60 * 24 * 7;
|
53
|
break;
|
54
|
|
55
|
case 'month':
|
56
|
$period_arg = 60 * 60 * 24 * 30;
|
57
|
break;
|
58
|
|
59
|
case 'quarter':
|
60
|
$period_arg = 60 * 60 * 24 * 91;
|
61
|
break;
|
62
|
|
63
|
case 'year':
|
64
|
$period_arg = 60 * 60 * 24 * 365;
|
65
|
break;
|
66
|
}
|
67
|
|
68
|
$block = new stdClass();
|
69
|
$block->module = 'forum-active-poster';
|
70
|
$block->delta = $tid;
|
71
|
_advanced_forum_add_files();
|
72
|
|
73
|
$args = array();
|
74
|
$query = "SELECT n.uid, COUNT(DISTINCT n.nid) AS posts FROM {node} n ";
|
75
|
$query .= "LEFT JOIN {node_comments} nc ON n.nid = nc.cid ";
|
76
|
$query .= "INNER JOIN {node} n2 ON COALESCE(nc.nid, n.nid) = n2.nid ";
|
77
|
$query .= "INNER JOIN {forum} f ON f.vid = n2.vid ";
|
78
|
|
79
|
// Add a join to exclude our roles if needed.
|
80
|
if (!empty($conf['exclude'])) {
|
81
|
$query .= "LEFT JOIN {users_roles} ur ON n.uid = ur.uid AND ur.rid = %d ";
|
82
|
$args[] = $conf['exclude'];
|
83
|
}
|
84
|
|
85
|
$query .= "WHERE n2.status <> 0 AND n.uid <> 0 ";
|
86
|
|
87
|
if ($tid) {
|
88
|
$forum = taxonomy_term_load($tid);
|
89
|
$block->title = t(
|
90
|
'Most active poster in @forum',
|
91
|
array('@forum' => $forum->name)
|
92
|
);
|
93
|
$query .= "AND f.tid = %d ";
|
94
|
$args = array($tid);
|
95
|
}
|
96
|
else {
|
97
|
$block->title = t('Most active poster');
|
98
|
}
|
99
|
|
100
|
// Add a WHERE to make sure our excluded role is not present.
|
101
|
if (!empty($conf['exclude'])) {
|
102
|
$query .= "AND ur.rid IS NULL ";
|
103
|
}
|
104
|
|
105
|
// Exclude the superuser if configged to do so.
|
106
|
if (!empty($conf['uidone'])) {
|
107
|
$query .= "AND n.uid <> 1 ";
|
108
|
}
|
109
|
|
110
|
if (!empty($period_arg)) {
|
111
|
$query .= "AND n2.created >= %d ";
|
112
|
$args[] = REQUEST_TIME - $period_arg;
|
113
|
}
|
114
|
|
115
|
$query .= "GROUP BY n.uid ";
|
116
|
$query .= "ORDER BY COUNT(DISTINCT n.nid) DESC LIMIT 1";
|
117
|
|
118
|
$info = db_fetch_object(db_query($query, $args));
|
119
|
|
120
|
// This can happen if there are no posts.
|
121
|
if (!$info) {
|
122
|
return;
|
123
|
}
|
124
|
|
125
|
$account = user_load($info->uid);
|
126
|
|
127
|
// Now that we have a user and that user's # of posts, get # of topics for
|
128
|
// this forum.
|
129
|
$args = array($info->uid);
|
130
|
$query = "SELECT COUNT(n.nid) AS posts FROM {node} n ";
|
131
|
$query .= "INNER JOIN {forum} f ON f.vid = n.vid ";
|
132
|
$query .= "WHERE n.status <> 0 AND n.uid = %d";
|
133
|
if ($tid) {
|
134
|
$query .= " AND f.tid = %d";
|
135
|
$args[] = $tid;
|
136
|
}
|
137
|
|
138
|
if (!empty($period_arg)) {
|
139
|
$query .= " AND n.created >= %d";
|
140
|
$args[] = REQUEST_TIME - $period_arg;
|
141
|
}
|
142
|
$topics = db_result(db_query($query, $args));
|
143
|
|
144
|
// And get their last post.
|
145
|
$args = array($info->uid);
|
146
|
$query = "SELECT n.nid FROM {node} n ";
|
147
|
$query .= "LEFT JOIN {node_comments} nc ON n.nid = nc.cid ";
|
148
|
$query .= "INNER JOIN {node} n2 ON COALESCE(nc.nid, n.nid) = n2.nid ";
|
149
|
$query .= "INNER JOIN {forum} f ON f.vid = n2.vid ";
|
150
|
$query .= "WHERE n.status <> 0 AND n.uid = %d ";
|
151
|
if ($tid) {
|
152
|
$query .= " AND f.tid = %d ";
|
153
|
$args[] = $tid;
|
154
|
}
|
155
|
|
156
|
$query .= "ORDER BY n.created DESC LIMIT 1";
|
157
|
|
158
|
$last_post = node_load(db_result(db_query($query, $args)));
|
159
|
$block->content = theme(
|
160
|
'advanced_forum_active_poster',
|
161
|
array(
|
162
|
'forum' => $tid,
|
163
|
'account' => $account,
|
164
|
'posts' => $info->posts,
|
165
|
'topics' => $topics,
|
166
|
'last_post' => $last_post,
|
167
|
)
|
168
|
);
|
169
|
$block->more = array(
|
170
|
'title' => t('More posts'),
|
171
|
'href' => 'forum/user/' . $info->uid,
|
172
|
);
|
173
|
|
174
|
return $block;
|
175
|
}
|
176
|
|
177
|
/**
|
178
|
* Returns an edit form for the custom type.
|
179
|
*/
|
180
|
function advanced_forum_forum_most_active_poster_content_type_edit_form(
|
181
|
$form,
|
182
|
&$form_state
|
183
|
) {
|
184
|
// Make sure conf contains newer defaults. CTools should do this for us.
|
185
|
$conf = $form_state['conf'];
|
186
|
$conf += $form_state['plugin']['defaults'];
|
187
|
|
188
|
$form['period'] = array(
|
189
|
'#type' => 'select',
|
190
|
'#title' => t('Period'),
|
191
|
'#options' => array(
|
192
|
'day' => t('In the last day'),
|
193
|
'week' => t('In the last week'),
|
194
|
'month' => t('In the last month'),
|
195
|
'quarter' => t('In the last quarter'),
|
196
|
'year' => t('In the last year'),
|
197
|
'ever' => t('Ever'),
|
198
|
),
|
199
|
'#description' => t(
|
200
|
'Select what period you want to search for the most active poster.'
|
201
|
),
|
202
|
'#default_value' => $conf['period'],
|
203
|
);
|
204
|
|
205
|
$roles = array('0' => t('Allow all roles')) + user_roles(TRUE);
|
206
|
unset($roles[DRUPAL_AUTHENTICATED_RID]);
|
207
|
|
208
|
$form['exclude'] = array(
|
209
|
'#type' => 'select',
|
210
|
'#title' => t('Exclude role'),
|
211
|
'#options' => $roles,
|
212
|
'#description' => t(
|
213
|
'Choose a role that will be excluded from being an active poster.'
|
214
|
),
|
215
|
'#default_value' => $conf['exclude'],
|
216
|
);
|
217
|
|
218
|
$form['uidone'] = array(
|
219
|
'#type' => 'checkbox',
|
220
|
'#title' => t('Exclude UID 1'),
|
221
|
'#description' => t(
|
222
|
'If checked the super user will not be a possible most active poster.'
|
223
|
),
|
224
|
'#default_value' => $conf['uidone'],
|
225
|
);
|
226
|
|
227
|
return $form;
|
228
|
}
|
229
|
|
230
|
/**
|
231
|
* Submit callback.
|
232
|
*/
|
233
|
function advanced_forum_forum_most_active_poster_content_type_edit_form_submit(
|
234
|
$form,
|
235
|
&$form_state
|
236
|
) {
|
237
|
// Copy everything from our defaults.
|
238
|
foreach (array_keys($form_state['plugin']['defaults']) as $key) {
|
239
|
$form_state['conf'][$key] = $form_state['values'][$key];
|
240
|
}
|
241
|
}
|
242
|
|
243
|
/**
|
244
|
* Callback for admin title.
|
245
|
*/
|
246
|
function advanced_forum_forum_most_active_poster_content_type_admin_title(
|
247
|
$subtype,
|
248
|
$conf,
|
249
|
$context
|
250
|
) {
|
251
|
return t('"@s" most active poster', array('@s' => $context->identifier));
|
252
|
}
|