| 1 |
<?php |
<?php |
| 2 |
// $Id: user_stats.module,v 1.2.2.16.2.9 2008/11/16 18:40:42 liammcdermott Exp $ |
// $Id: user_stats.module,v 1.2.2.16.2.10 2008/11/18 22:24:43 liammcdermott Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 37 |
'access arguments' => array('administer user stats'), |
'access arguments' => array('administer user stats'), |
| 38 |
'type' => MENU_NORMAL_ITEM, |
'type' => MENU_NORMAL_ITEM, |
| 39 |
); |
); |
| 40 |
$items['admin/settings/user_stats/reset'] = array( |
$items['admin/settings/user_stats/reset_post_count'] = array( |
| 41 |
'title' => 'reset user post stats', |
'title' => 'reset user post stats', |
| 42 |
'page callback' => 'drupal_get_form', |
'page callback' => 'user_stats_reset_post_count', |
|
'page arguments' => array('user_stats_reset_postcount_confirm'), |
|
| 43 |
'access arguments' => array('administer user stats'), |
'access arguments' => array('administer user stats'), |
| 44 |
'type' => MENU_CALLBACK, |
'type' => MENU_CALLBACK, |
| 45 |
); |
); |
| 46 |
$items['admin/settings/user_stats/reset_logins'] = array( |
$items['admin/settings/user_stats/reset_login_count'] = array( |
| 47 |
'title' => 'reset user login stats', |
'title' => 'reset user login stats', |
| 48 |
'page callback' => 'drupal_get_form', |
'page callback' => 'user_stats_reset_login_count', |
|
'page arguments' => array('user_stats_reset_logincount_confirm'), |
|
| 49 |
'access arguments' => array('administer user stats'), |
'access arguments' => array('administer user stats'), |
| 50 |
'type' => MENU_CALLBACK, |
'type' => MENU_CALLBACK, |
| 51 |
); |
); |
| 98 |
$profile_fields[$row->name] = $row->title; |
$profile_fields[$row->name] = $row->title; |
| 99 |
} |
} |
| 100 |
|
|
| 101 |
$form['post_count_options']['user_stats_postcount_profile_field'] = array( |
$form['post_count_options']['user_stats_post_count_profile_field'] = array( |
| 102 |
'#type' => 'select', |
'#type' => 'select', |
| 103 |
'#title' => t('User stats post count profile field'), |
'#title' => t('User stats post count profile field'), |
| 104 |
'#description' => t('This is the profile field that holds the postcount for a user. Changing this can be useful if you have multiple sites using the same database (or you just do not like the default field used for post counts). <strong>The default setting should work for most people.</strong>'), |
'#description' => t('This is the profile field that holds the post count for a user. Changing this can be useful if you have multiple sites using the same database (or you just do not like the default field used for post counts). <strong>The default setting should work for most people.</strong>'), |
| 105 |
'#options' => $profile_fields, |
'#options' => $profile_fields, |
| 106 |
'#default_value' => variable_get('user_stats_postcount_profile_field', 'user_post_count'), |
'#default_value' => variable_get('user_stats_post_count_profile_field', 'user_post_count'), |
| 107 |
'#required' => TRUE, |
'#required' => TRUE, |
| 108 |
); |
); |
| 109 |
|
|
| 121 |
'#collapsed' => TRUE, |
'#collapsed' => TRUE, |
| 122 |
); |
); |
| 123 |
|
|
| 124 |
$form['post_count_options']['post_count_reset']['user_stats_reset_count'] = array( |
$form['post_count_options']['post_count_reset']['user_stats_reset_post_count'] = array( |
| 125 |
'#type' => 'submit', |
'#type' => 'submit', |
| 126 |
'#value' => t('Reset all post counts'), |
'#value' => t('Reset all post counts'), |
| 127 |
); |
); |
| 151 |
'#type' => 'submit', |
'#type' => 'submit', |
| 152 |
'#value' => t('Reset all login counts'), |
'#value' => t('Reset all login counts'), |
| 153 |
); |
); |
| 154 |
|
$form['#submit'][] = 'user_stats_settings_change'; |
| 155 |
|
|
| 156 |
return system_settings_form($form); |
return system_settings_form($form); |
| 157 |
} |
} |
| 158 |
|
|
| 159 |
/** |
/** |
| 160 |
* Validate callback. |
* Settings change callback. |
| 161 |
|
* TODO: make this check a setting has been changed before resetting counts. |
| 162 |
*/ |
*/ |
| 163 |
function user_stats_admin_settings_validate($form, &$form_state) { |
function user_stats_settings_change($form, &$form_state) { |
| 164 |
if ($form_state['values']['op'] == t('Reset all post counts')) { |
$op = isset($form_state['values']['op']) ? $form_state['values']['op'] : ''; |
| 165 |
drupal_goto('admin/settings/user_stats/reset'); |
|
| 166 |
|
// Give user feedback that post counts have been reset as requested. |
| 167 |
|
if ($op == t('Reset all post counts')) { |
| 168 |
|
drupal_goto('admin/settings/user_stats/reset_post_count'); |
| 169 |
} |
} |
| 170 |
else if ($form_state['values']['op'] == t('Reset all login counts')) { |
else if ($op == t('Reset all login counts')) { |
| 171 |
drupal_goto('admin/settings/user_stats/reset_logins'); |
drupal_goto('admin/settings/user_stats/reset_login_count'); |
| 172 |
} |
} |
|
} |
|
| 173 |
|
|
| 174 |
/** |
// Rebuild post count stats when settings change. |
| 175 |
* reset post count confim callback. |
variable_set('user_stats_rebuild_stats', TRUE); |
| 176 |
*/ |
user_stats_reset_counts('user_post_count'); |
|
function user_stats_reset_postcount_confirm() { |
|
|
return confirm_form(array(), t('Are you sure you want to reset post counts?'), |
|
|
'admin/settings/user_stats', '', t('Reset all post counts'), t('Cancel')); |
|
|
} |
|
|
|
|
|
/** |
|
|
* reset login count confim callback. |
|
|
*/ |
|
|
function user_stats_reset_logincount_confirm() { |
|
|
return confirm_form(array(), t('Are you sure you want to reset login counts?'), |
|
|
'admin/settings/user_stats', '', t('Reset all login counts'), t('Cancel')); |
|
| 177 |
} |
} |
| 178 |
|
|
| 179 |
/** |
/** |
| 180 |
* reset post count handler. |
* reset post count handler. |
| 181 |
*/ |
*/ |
| 182 |
function user_stats_reset_postcount_confirm_submit($form, &$form_state) { |
function user_stats_reset_post_count() { |
| 183 |
if ($form_state['values']['confirm']) { |
variable_set('user_stats_rebuild_stats', TRUE); |
| 184 |
variable_set('user_stats_rebuild_stats', TRUE); |
user_stats_reset_counts('user_post_count'); |
| 185 |
user_stats_reset_counts('user_post_count'); |
drupal_set_message(t('Post counts have been reset.')); |
| 186 |
drupal_set_message(t('The post counts will be reset during the next cron run.')); |
drupal_goto('admin/settings/user_stats'); |
|
drupal_goto('admin/settings/user_stats'); |
|
|
} |
|
| 187 |
} |
} |
| 188 |
|
|
| 189 |
/** |
/** |
| 190 |
* reset login count handler. |
* reset login count handler. |
| 191 |
*/ |
*/ |
| 192 |
function user_stats_reset_logincount_confirm_submit($form, &$form_state) { |
function user_stats_reset_login_count() { |
| 193 |
if ($form_state['values']['confirm']) { |
user_stats_reset_counts('user_login_count'); |
| 194 |
user_stats_reset_counts('user_login_count'); |
drupal_set_message(t('User login counts have been reset.')); |
| 195 |
drupal_set_message(t('The user login counts have been reset')); |
drupal_goto('admin/settings/user_stats'); |
|
drupal_goto('admin/settings/user_stats'); |
|
|
} |
|
| 196 |
} |
} |
| 197 |
|
|
| 198 |
/** |
/** |
| 245 |
return 'n/a'; |
return 'n/a'; |
| 246 |
} |
} |
| 247 |
// If the post count for this user hasn't been set then update it. |
// If the post count for this user hasn't been set then update it. |
| 248 |
$post_count_profile_field = variable_get('user_stats_postcount_profile_field', 'user_post_count'); |
$post_count_profile_field = variable_get('user_stats_post_count_profile_field', 'user_post_count'); |
| 249 |
if (!isset($user->$post_count_profile_field)) { |
if (!isset($user->$post_count_profile_field)) { |
| 250 |
user_stats_post_count_update($user, 'reset'); |
user_stats_post_count_update($user, 'reset'); |
| 251 |
} |
} |
| 283 |
* User id of user required from cache. |
* User id of user required from cache. |
| 284 |
* @param $flush |
* @param $flush |
| 285 |
* Defaults to FALSE. If TRUE the $uid is flushed from the cache. |
* Defaults to FALSE. If TRUE the $uid is flushed from the cache. |
| 286 |
* If $uid is set to 0 and $flush is TRUE the entire cache will |
* If $uid is set to -1 and $flush is TRUE the entire cache will |
| 287 |
* be purged. |
* be purged. |
| 288 |
* @return |
* @return |
| 289 |
* If the $uid is other than -1 and $flush is FALSE the user object. |
* If the $uid is other than -1 and $flush is FALSE the user object. |
| 327 |
if (!in_array($op, array('insert', 'delete', 'update'))) { |
if (!in_array($op, array('insert', 'delete', 'update'))) { |
| 328 |
return; |
return; |
| 329 |
} |
} |
| 330 |
$postcount_content_types = variable_get('user_stats_included_content_types', array()); |
$post_count_content_types = variable_get('user_stats_included_content_types', array()); |
| 331 |
if ((empty($postcount_content_types) || |
if ((empty($post_count_content_types) || |
| 332 |
in_array($node->type, $postcount_content_types)) && |
in_array($node->type, $post_count_content_types)) && |
| 333 |
variable_get('user_stats_count_posts', TRUE)) { |
variable_get('user_stats_count_posts', TRUE)) { |
| 334 |
|
|
| 335 |
switch ($op) { |
switch ($op) { |
| 391 |
// check to see if comments should be counted at all. |
// check to see if comments should be counted at all. |
| 392 |
if (variable_get('user_stats_count_comments', TRUE)) { |
if (variable_get('user_stats_count_comments', TRUE)) { |
| 393 |
$comment = (object)$a1; |
$comment = (object)$a1; |
| 394 |
$postcount_content_types = variable_get('user_stats_included_content_types', array()); |
$post_count_content_types = variable_get('user_stats_included_content_types', array()); |
| 395 |
$node = node_load(array('nid' => $comment->nid)); |
$node = node_load(array('nid' => $comment->nid)); |
| 396 |
|
|
| 397 |
if ((empty($postcount_content_types) || |
if ((empty($post_count_content_types) || |
| 398 |
in_array($node->type, $postcount_content_types)) && |
in_array($node->type, $post_count_content_types)) && |
| 399 |
variable_get('user_login_count', TRUE)) { |
variable_get('user_login_count', TRUE)) { |
| 400 |
|
|
| 401 |
switch ($op) { |
switch ($op) { |
| 432 |
function user_stats_cron() { |
function user_stats_cron() { |
| 433 |
if (variable_get('user_stats_rebuild_stats', TRUE) && (variable_get('user_stats_count_posts', TRUE) || variable_get('user_stats_count_comments', TRUE))) { |
if (variable_get('user_stats_rebuild_stats', TRUE) && (variable_get('user_stats_count_posts', TRUE) || variable_get('user_stats_count_comments', TRUE))) { |
| 434 |
$sql = "SELECT fid FROM {profile_fields} WHERE name='%s'"; |
$sql = "SELECT fid FROM {profile_fields} WHERE name='%s'"; |
| 435 |
$fid = db_result(db_query($sql, variable_get('user_stats_postcount_profile_field', 'user_post_count'))); |
$fid = db_result(db_query($sql, variable_get('user_stats_post_count_profile_field', 'user_post_count'))); |
| 436 |
|
|
| 437 |
// Unfortunately this cannot be done with a JOIN because of the need to match on fid. |
// Unfortunately this cannot be done with a JOIN because of the need to match on fid. |
| 438 |
$sql = "SELECT uid FROM {users} WHERE uid NOT IN |
$sql = "SELECT uid FROM {users} WHERE uid NOT IN |
| 536 |
*/ |
*/ |
| 537 |
function _user_stats_last_post($account) { |
function _user_stats_last_post($account) { |
| 538 |
$sql = "SELECT MAX(created) FROM {node} WHERE status=%d AND uid=%d"; |
$sql = "SELECT MAX(created) FROM {node} WHERE status=%d AND uid=%d"; |
| 539 |
$postcount_content_types = variable_get('user_stats_included_content_types', array()); |
$post_count_content_types = variable_get('user_stats_included_content_types', array()); |
| 540 |
if (!empty($postcount_content_types)) { |
if (!empty($post_count_content_types)) { |
| 541 |
$content_types = "'". implode("','", $postcount_content_types) ."'"; |
$content_types = "'". implode("','", $post_count_content_types) ."'"; |
| 542 |
$where = ' AND type IN ('. $content_types .')'; |
$where = ' AND type IN ('. $content_types .')'; |
| 543 |
$sql .= $where; |
$sql .= $where; |
| 544 |
} |
} |
| 546 |
$sql = "SELECT MAX(timestamp) FROM {comments} c |
$sql = "SELECT MAX(timestamp) FROM {comments} c |
| 547 |
INNER JOIN {node} n ON c.nid=n.nid |
INNER JOIN {node} n ON c.nid=n.nid |
| 548 |
WHERE c.status=%d AND c.uid=%d"; |
WHERE c.status=%d AND c.uid=%d"; |
| 549 |
if (!empty($postcount_content_types)) { |
if (!empty($post_count_content_types)) { |
| 550 |
$where = ' AND n.type IN ('. $content_types .')'; |
$where = ' AND n.type IN ('. $content_types .')'; |
| 551 |
$sql .= $where; |
$sql .= $where; |
| 552 |
} |
} |
| 769 |
$sql = "UPDATE {profile_values} SET value=%d |
$sql = "UPDATE {profile_values} SET value=%d |
| 770 |
WHERE fid = (SELECT fid FROM {profile_fields} WHERE name = '%s') |
WHERE fid = (SELECT fid FROM {profile_fields} WHERE name = '%s') |
| 771 |
AND uid=%d"; |
AND uid=%d"; |
| 772 |
$post_count_profile_field = variable_get('user_stats_postcount_profile_field', 'user_post_count'); |
$post_count_profile_field = variable_get('user_stats_post_count_profile_field', 'user_post_count'); |
| 773 |
|
|
| 774 |
switch ($op) { |
switch ($op) { |
| 775 |
case 'increment': |
case 'increment': |
| 822 |
} |
} |
| 823 |
if (variable_get('user_stats_count_posts', TRUE)) { |
if (variable_get('user_stats_count_posts', TRUE)) { |
| 824 |
$sql = "SELECT COUNT(*) FROM {node} WHERE uid=%d AND status=1"; |
$sql = "SELECT COUNT(*) FROM {node} WHERE uid=%d AND status=1"; |
| 825 |
$postcount_content_types = variable_get('user_stats_included_content_types', array()); |
$post_count_content_types = variable_get('user_stats_included_content_types', array()); |
| 826 |
if (!empty($postcount_content_types)) { |
if (!empty($post_count_content_types)) { |
| 827 |
$content_types = "'". implode("','", $postcount_content_types) ."'"; |
$content_types = "'". implode("','", $post_count_content_types) ."'"; |
| 828 |
$where = ' AND type IN ('. $content_types .')'; |
$where = ' AND type IN ('. $content_types .')'; |
| 829 |
$sql .= $where; |
$sql .= $where; |
| 830 |
} |
} |
| 833 |
} |
} |
| 834 |
if (variable_get('user_stats_count_comments', TRUE)) { |
if (variable_get('user_stats_count_comments', TRUE)) { |
| 835 |
$sql = "SELECT COUNT(*) FROM {comments} c INNER JOIN {node} n ON c.nid=n.nid WHERE c.uid=%d AND c.status=0 AND n.status=1"; |
$sql = "SELECT COUNT(*) FROM {comments} c INNER JOIN {node} n ON c.nid=n.nid WHERE c.uid=%d AND c.status=0 AND n.status=1"; |
| 836 |
if (!empty($postcount_content_types)) { |
if (!empty($post_count_content_types)) { |
| 837 |
$where = ' AND n.type IN ('. $content_types .')'; |
$where = ' AND n.type IN ('. $content_types .')'; |
| 838 |
$sql .= $where; |
$sql .= $where; |
| 839 |
} |
} |
| 923 |
* This must be the name of the profile_fields field. |
* This must be the name of the profile_fields field. |
| 924 |
*/ |
*/ |
| 925 |
function user_stats_reset_counts($statistic = 'user_post_count') { |
function user_stats_reset_counts($statistic = 'user_post_count') { |
| 926 |
$fid = db_result(db_query("SELECT fid FROM {profile_fields} WHERE name = '%s'", variable_get('user_stats_postcount_profile_field', 'user_post_count'))); |
$sql = "SELECT fid FROM {profile_fields} WHERE name = '%s'"; |
| 927 |
|
if ($statistic == 'user_post_count') { |
| 928 |
|
$fid = db_result(db_query($sql, variable_get('user_stats_post_count_profile_field', 'user_post_count'))); |
| 929 |
|
} |
| 930 |
|
else { |
| 931 |
|
$fid = db_result(db_query($sql, $statistic)); |
| 932 |
|
} |
| 933 |
if ($fid) { |
if ($fid) { |
| 934 |
db_query('DELETE FROM {profile_values} WHERE fid = %d', $fid); |
db_query('DELETE FROM {profile_values} WHERE fid = %d', $fid); |
| 935 |
} |
} |