-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmate_user_content.module
executable file
·318 lines (277 loc) · 7.8 KB
/
mate_user_content.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
/**
* @file
* Mate User Content Module - Audits of Users Production.
*/
/**
* Implements hook_menu().
*/
function mate_user_content_menu() {
$items['admin/content/users'] = array(
'title' => 'Users content',
'page callback' => 'mate_user_content_list',
'access callback' => 'user_access',
'access arguments' => TRUE,
'access arguments' => array('access administration menu'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/user/%user/content'] = array(
'title' => 'Content',
'page callback' => 'mate_user_content_detail',
'page arguments' => array(2),
'access callback' => 'user_access',
'access arguments' => TRUE,
'access arguments' => array('access administration menu'),
);
return $items;
}
/**
* Returns default time in unix time (today).
* @return int
*/
function mate_user_content_default_from() {
return strtotime('today');
}
/**
* Returns default time in unix time (today's last second of last minute of last hour).
* @return int
*/
function mate_user_content_default_until() {
return strtotime('today') + 86400 - 1;
}
/**
* Content by user page callback.
* @return string
*/
function mate_user_content_list() {
$from = mate_user_content_default_from();
if (!empty($_GET['from']['date'])) {
$from = mate_user_content_get_unixtime_from_date($_GET['from']['date'] . ' 00:00:00');
}
$until = mate_user_content_default_until();
if (!empty($_GET['until']['date'])) {
$until = mate_user_content_get_unixtime_from_date($_GET['until']['date'] . ' 23:59:59');
}
$roles = array_keys(mate_user_content_allowed_roles());
if (!empty($_GET['roles'])) {
$roles = $_GET['roles'];
}
$users = mate_user_get_users_production($from, $until, $roles);
foreach ($users as $key => $user) {
$content = mate_user_get_user_content($from, $until, $user->uid);
$line = '';
foreach ($content as $c) {
$line .= l(t($c->title), "node/$c->nid", array('attributes' => array('title' => $c->title, 'target'=> '_blank'))) . '</br >';
}
if (count($content) == 40) {
$line .= l('Ver mas',
'admin/user/' . $user->uid . '/content',
array(
'attributes' => array('title' => $user->name),
'query' => array(
'from' => date('Y-m-d', $from),
'until' => date('Y-m-d', $until),
)
)
);
}
$users[$key]->content = $line;
}
return theme('mate_user_content_list', array(
'users' => $users,
'from' => $from,
'until' => $until,
));
}
/**
* User's content list page callback.
*
* @param $user
* @return string
*/
function mate_user_content_detail($user) {
$from = mate_user_content_default_from();
if (!empty($_GET['from'])) {
$from = $_GET['from'];
}
$until = mate_user_content_default_until();
if (!empty($_GET['until'])) {
$until = $_GET['until'];
}
$user_content = mate_user_get_user_content(strtotime($from['date']), strtotime($until['date']), $user->uid);
return theme('mate_user_content_detail', array(
'nodes' => $user_content,
'from' => $from,
'until' => $until,
'username' => $user->name,
));
}
/**
* Gets the content produced by an user from until date.
*
* @param $from
* @param $until
* @param $uid
*
* @return mixed
*/
function mate_user_get_user_content($from, $until, $uid) {
$query = db_select('node', 'n')->extend('PagerDefault')->limit(40);
$query
->fields('n', array('nid', 'type', 'title', 'status', 'created', 'changed'))
->where('uid = :uid AND created BETWEEN :from AND :until', array(
':uid' => $uid,
':from' => $from,
':until' => $until,
))
->orderBy('created', 'DESC');
return $query->execute()->fetchAll();
}
/**
* Gets all users and his production in a date range.
*
* @param $from
* @param $until
* @param $roles
*
* @return Array
*/
function mate_user_get_users_production($from, $until, $roles) {
$content_sql = 'SELECT u.uid, u.name, count(n.nid) nodes
FROM {node} n
INNER JOIN {users} u ON n.uid = u.uid
INNER JOIN {users_roles} ur ON u.uid = ur.uid
WHERE ur.rid IN (:roles) AND n.created BETWEEN :from AND :until
GROUP BY u.uid, u.name
ORDER BY u.name';
$users = db_query($content_sql, array(
':roles' => $roles,
':from' => $from,
':until' => $until,
));
$users_data = $users->fetchAllAssoc('name');
return $users_data;
}
/**
* Implements hook_theme()
*
* @param $existing
* @param $type
* @param $theme
* @param $path
* @return array
*/
function mate_user_content_theme($existing, $type, $theme, $path) {
$base = array(
'path' => drupal_get_path('module', 'mate_user_content'),
);
return array(
'mate_user_content_list' => $base + array(
'template' => 'mate_user_content_list',
),
'mate_user_content_detail' => $base + array(
'template' => 'mate_user_content_detail',
),
);
}
/**
* Form to select a period of time.
* @param $form
* @param $form_state
* @return mixed
*
*/
function mate_user_content_from_until_form($form, &$form_state) {
$default_from = $default_until = date('Y-m-d');
if (!empty($_GET['from']['date'])) {
$default_from = $_GET['from']['date'];
}
if (!empty($_GET['from']['date'])) {
$default_until = $_GET['until']['date'];
}
$form['from'] = array(
'#type' => 'datepicker',
'#date_format' => 'Y-m-d',
'#title' => t('From'),
'#default_value' => $default_from,
'#required' => TRUE,
'#pre_render' => array('mate_user_content_limit_year_in_date'),
);
$form['until'] = array(
'#type' => 'datepicker',
'#date_format' => 'Y-m-d',
'#title' => t('Until'),
'#default_value' => $default_until,
'#required' => TRUE,
'#pre_render' => array('mate_user_content_limit_year_in_date'),
);
// Add roles field if page is total of content by user.
if ('admin' === arg(0) && 'content' === arg(1) && 'users' === arg(2)) {
$roles = mate_user_content_allowed_roles();
$form['roles'] = array(
'#type' => 'checkboxes',
'#options' => $roles,
'#title' => t('Roles'),
'#default_value' => (!empty($_GET['roles'])) ? array_keys($_GET['roles']) : array_keys($roles),
);
}
$form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Search'),
);
$form['#method'] = 'get';
return $form;
}
/**
* Restricts Year range in date form widget.
* @param $element
* @return mixed
*/
function mate_user_content_limit_year_in_date($element) {
$element['year']['#options'] = drupal_map_assoc(
range(
date('Y', strtotime('-2 Year')),
date('Y')
)
);
return $element;
}
function mate_user_content_from_until_form_submit($form, &$form_state) {
$form_state['redirect'] = FALSE;
}
/**
* Returns unix time from provided date or current unix time if $date is not valid. $date should have the following format
* array(
* 'date' => 'YYYY-MM-DD HH:MM:SS',
* )
* @param $date
* @return int
*/
function mate_user_content_get_unixtime_from_date($date = NULL) {
if (NULL !== $date) {
return strtotime($date);
}
return time();
}
/**
* Returns all roles different from Anonymous, Authenticated nor Administrator
* @return array
*/
function mate_user_content_allowed_roles() {
$query = db_select('role', 'r');
$query->fields('r', array('rid', 'name'));
$query->orderBy('name');
$result = $query->execute();
$roles = array();
foreach ($result as $role) {
switch ($role->rid) {
case DRUPAL_ANONYMOUS_RID:
case DRUPAL_AUTHENTICATED_RID:
continue;
break;
default:
$roles[$role->rid] = t($role->name);
}
}
return $roles;
}