user_stats.admin.inc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * @file
  4. * Commonly requested user statistics for themers administration interface.
  5. */
  6. /**
  7. * Implements hook_settings().
  8. */
  9. function user_stats_admin_settings($form, &$form_state) {
  10. $form['post_count_options'] = array(
  11. '#type' => 'fieldset',
  12. '#title' => t('Post count options'),
  13. '#collapsible' => TRUE,
  14. '#collapsed' => (variable_get('user_stats_count_posts', TRUE) || variable_get('user_stats_count_comments', TRUE) ? FALSE : TRUE),
  15. );
  16. $form['post_count_options']['user_stats_count_posts'] = array(
  17. '#type' => 'checkbox',
  18. '#title' => t('Count posts'),
  19. '#description' => t('If checked user post counts will be calculated.'),
  20. '#default_value' => variable_get('user_stats_count_posts', TRUE),
  21. );
  22. $form['post_count_options']['user_stats_count_comments'] = array(
  23. '#type' => 'checkbox',
  24. '#title' => t('Count comments'),
  25. '#description' => t('If checked user comments counts will be included in the total user post count.'),
  26. '#default_value' => variable_get('user_stats_count_comments', TRUE),
  27. );
  28. foreach (node_type_get_types() as $types) {
  29. $options[$types->type] = $types->name;
  30. }
  31. $form['post_count_options']['user_stats_included_content_types'] = array(
  32. '#type' => 'select',
  33. '#title' => t('Content types to include in post count'),
  34. '#description' => t('Select the content types to include in the user post count (hold ctrl or shift to select multiple types). Both nodes and comments will be included in the post count. If you do not select any content types, then all types will be counted.'),
  35. '#options' => $options,
  36. '#default_value' => variable_get('user_stats_included_content_types', array()),
  37. '#multiple' => TRUE,
  38. '#size' => 10,
  39. );
  40. $form['post_count_options']['user_stats_user_per_cron'] = array(
  41. '#type' => 'select',
  42. '#title' => t('Number of users to update per cron run'),
  43. '#options' => array(
  44. '10' => '10',
  45. '25' => '25',
  46. '50' => '50',
  47. '100' => '100',
  48. '200' => '200',
  49. ),
  50. '#default_value' => variable_get('user_stats_user_per_cron', array('25')),
  51. );
  52. $form['post_count_options']['post_count_reset'] = array(
  53. '#type' => 'fieldset',
  54. '#title' => t('Post count reset'),
  55. '#collapsible' => TRUE,
  56. '#collapsed' => TRUE,
  57. );
  58. $form['post_count_options']['post_count_reset']['user_stats_reset_post_count'] = array(
  59. '#type' => 'submit',
  60. '#value' => t('Reset all post counts'),
  61. );
  62. $form['login_count_options'] = array(
  63. '#type' => 'fieldset',
  64. '#title' => t('Login count options'),
  65. '#collapsible' => TRUE,
  66. '#collapsed' => (variable_get('user_stats_count_logins', TRUE) ? FALSE : TRUE),
  67. );
  68. $form['login_count_options']['user_stats_count_logins'] = array(
  69. '#type' => 'checkbox',
  70. '#title' => t('Count logins'),
  71. '#description' => t('If checked user login counts will be calculated.'),
  72. '#default_value' => variable_get('user_stats_count_logins', TRUE),
  73. );
  74. $form['login_count_options']['login_count_reset'] = array(
  75. '#type' => 'fieldset',
  76. '#title' => t('Login count reset'),
  77. '#collapsible' => TRUE,
  78. '#collapsed' => TRUE,
  79. );
  80. $form['login_count_options']['login_count_reset']['user_stats_reset_login_count'] = array(
  81. '#type' => 'submit',
  82. '#value' => t('Reset all login counts'),
  83. );
  84. $form['ip_tracking_options'] = array(
  85. '#type' => 'fieldset',
  86. '#title' => t('IP address tracking'),
  87. '#collapsible' => TRUE,
  88. '#collapsed' => (variable_get('user_stats_track_ips', TRUE) ? FALSE : TRUE),
  89. );
  90. $form['ip_tracking_options']['user_stats_track_ips'] = array(
  91. '#type' => 'checkbox',
  92. '#title' => t('Track IP addresses'),
  93. '#description' => t('If checked the IP addresses of users will be logged. This may be a privacy issue for some people, but is very useful for discovering spammers on community sites.'),
  94. '#default_value' => variable_get('user_stats_track_ips', TRUE),
  95. );
  96. $period = drupal_map_assoc(array(604800, 4838400, 15724800, 31536000, 157680000, 315360000), 'format_interval');
  97. $form['ip_tracking_options']['user_stats_flush_ips_timer'] = array(
  98. '#type' => 'select',
  99. '#title' => t('Discard IP logs older than'),
  100. '#description' => t('Older IP log entries will be automatically discarded. (Requires a correctly configured <a href="@cron">cron maintenance task</a>.)', array('@cron' => url('admin/reports/status'))),
  101. '#options' => $period,
  102. '#default_value' => variable_get('user_stats_flush_ips_timer', 31536000),
  103. );
  104. $form['online_user_options'] = array(
  105. '#type' => 'fieldset',
  106. '#title' => t('Online users'),
  107. '#collapsible' => TRUE,
  108. '#collapsed' => FALSE,
  109. );
  110. // Following blatantly nicked from /modules/user/user.module and should be updated if that changes.
  111. $period = drupal_map_assoc(array(30, 60, 120, 180, 300, 600, 900, 1800, 2700, 3600, 5400, 7200, 10800, 21600, 43200, 86400), 'format_interval');
  112. $form['online_user_options']['user_block_seconds_online'] = array(
  113. '#type' => 'select',
  114. '#title' => t('User activity'),
  115. '#default_value' => variable_get('user_block_seconds_online', 900),
  116. '#options' => $period,
  117. '#description' => t('A user is considered online for this long after they have last viewed a page.'),
  118. );
  119. $form['#submit'][] = 'user_stats_settings_change';
  120. return system_settings_form($form);
  121. }
  122. /**
  123. * Settings change callback.
  124. * TODO: make this check a setting has been changed before resetting counts.
  125. */
  126. function user_stats_settings_change($form, &$form_state) {
  127. $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
  128. // Give user feedback that post counts have been reset as requested.
  129. if ($op == t('Reset all post counts')) {
  130. drupal_goto('admin/config/people/user_stats/reset_post_count');
  131. }
  132. else if ($op == t('Reset all login counts')) {
  133. drupal_goto('admin/config/people/user_stats/reset_login_count');
  134. }
  135. // If the user has decided not to track IPs, we delete all the logged entries.
  136. if (!$form_state['values']['user_stats_track_ips'] &&
  137. variable_get('user_stats_track_ips', TRUE) &&
  138. db_query("SELECT COUNT(*) FROM {user_stats_ips}")->fetchField()) {
  139. db_query("TRUNCATE TABLE {user_stats_ips}");
  140. }
  141. // We also need to rebuild the menus, as some Views we provide in the
  142. // Reports section rely on IP address tracking.
  143. if ($form_state['values']['user_stats_track_ips'] != variable_get('user_stats_track_ips', TRUE)) {
  144. // We have to save the form value for user_stats_track_ips before the menus
  145. // are rebuilt.
  146. variable_set('user_stats_track_ips', $form_state['values']['user_stats_track_ips']);
  147. menu_rebuild();
  148. }
  149. // Rebuild post count stats when settings change.
  150. variable_set('user_stats_rebuild_stats', TRUE);
  151. user_stats_reset_counts('post_count');
  152. }
  153. /**
  154. * Reset post count handler.
  155. */
  156. function user_stats_reset_post_count() {
  157. variable_set('user_stats_rebuild_stats', TRUE);
  158. user_stats_reset_counts('post_count');
  159. drupal_set_message(t('Post counts have been reset.'));
  160. drupal_goto('admin/config/people/user_stats');
  161. }
  162. /**
  163. * Reset login count handler.
  164. */
  165. function user_stats_reset_login_count() {
  166. user_stats_reset_counts('login_count');
  167. drupal_set_message(t('User login counts have been reset.'));
  168. drupal_goto('admin/config/people/user_stats');
  169. }