/[drupal]/contributions/modules/user_stats/user_stats.module
ViewVC logotype

Diff of /contributions/modules/user_stats/user_stats.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.2.2.19, Sun Nov 16 18:38:36 2008 UTC revision 1.2.2.20, Sun Feb 1 03:44:11 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: user_stats.module,v 1.2.2.18 2008/11/16 18:19:12 liammcdermott Exp $  // $Id: user_stats.module,v 1.2.2.19 2008/11/16 18:38:36 liammcdermott Exp $
3    
4  /**  /**
5   * Implementation of hook_perm()   * @file
6     * User Stats provides commonly requested user statistics for themers.
7     * These are:
8     *  - days registered;
9     *  - join date;
10     *  - days since last login;
11     *  - days since last post;
12     *  - post count;
13     *  - login count;
14     *  - user online/offline;
15     *  - IP address;
16     */
17    
18    /**
19     * Implementation of hook_perm().
20   */   */
21  function user_stats_perm() {  function user_stats_perm() {
22    return array('administer user stats', 'View statistics', 'View IP addresses');    return array('administer user stats', 'View statistics', 'View IP addresses');
# Line 28  function user_stats_menu($may_cache) { Line 42  function user_stats_menu($may_cache) {
42      $items[] = array(      $items[] = array(
43        'path' => 'admin/settings/user_stats/reset',        'path' => 'admin/settings/user_stats/reset',
44        'title' => t('reset user post stats'),        'title' => t('reset user post stats'),
45        'callback' => 'drupal_get_form',        'callback' => 'user_stats_reset_post_count',
46        'callback arguments' => array('user_stats_reset_postcount_confirm'),        //'callback arguments' => array('user_stats_reset_postcount_confirm'),
47        'access' => user_access('administer user stats'),        'access' => user_access('administer user stats'),
48        'type' => MENU_CALLBACK,        'type' => MENU_CALLBACK,
49      );      );
50      $items[] = array(      $items[] = array(
51        'path' => 'admin/settings/user_stats/reset_logins',        'path' => 'admin/settings/user_stats/reset_logins',
52        'title' => t('reset user login stats'),        'title' => t('reset user login stats'),
53        'callback' => 'drupal_get_form',        'callback' => 'user_stats_reset_login_count',
54        'callback arguments' => array('user_stats_reset_logincount_confirm'),        //'callback arguments' => array('user_stats_reset_logincount_confirm'),
55        'access' => user_access('administer user stats'),        'access' => user_access('administer user stats'),
56        'type' => MENU_CALLBACK,        'type' => MENU_CALLBACK,
57      );      );
# Line 71  function user_stats_admin_settings() { Line 85  function user_stats_admin_settings() {
85      '#default_value' => variable_get('user_stats_count_comments', TRUE),      '#default_value' => variable_get('user_stats_count_comments', TRUE),
86    );    );
87    
88    foreach(node_get_types() as $types) {    foreach (node_get_types() as $types) {
89      $options[$types->type] = $types->name;      $options[$types->type] = $types->name;
90    }    }
91    
# Line 91  function user_stats_admin_settings() { Line 105  function user_stats_admin_settings() {
105      $profile_fields[$row->name] = $row->title;      $profile_fields[$row->name] = $row->title;
106    }    }
107    
108    $form['post_count_options']['user_stats_postcount_profile_field'] = array(    $form['post_count_options']['user_stats_post_count_profile_field'] = array(
109      '#type' => 'select',      '#type' => 'select',
110      '#title' => t('User stats post count profile field'),      '#title' => t('User stats post count profile field'),
111      '#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>'),
112      '#options' => $profile_fields,      '#options' => $profile_fields,
113      '#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'),
114      '#required' => TRUE,      '#required' => TRUE,
115    );    );
116    
# Line 114  function user_stats_admin_settings() { Line 128  function user_stats_admin_settings() {
128      '#collapsed' => TRUE,      '#collapsed' => TRUE,
129    );    );
130    
131    $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(
132      '#type' => 'submit',      '#type' => 'submit',
133      '#value' => t('Reset all post counts'),      '#value' => t('Reset all post counts'),
134    );    );
# Line 153  function user_stats_admin_settings() { Line 167  function user_stats_admin_settings() {
167   */   */
168  function user_stats_admin_settings_validate($form_id, $form_values) {  function user_stats_admin_settings_validate($form_id, $form_values) {
169    if ($form_values['op'] == t('Reset all post counts')) {    if ($form_values['op'] == t('Reset all post counts')) {
170      drupal_goto('admin/settings/user_stats/reset');      drupal_goto('admin/settings/user_stats/reset_post_count');
171    }    }
172    else if ($form_values['op'] == t('Reset all login counts')) {    else if ($form_values['op'] == t('Reset all login counts')) {
173      drupal_goto('admin/settings/user_stats/reset_logins');      drupal_goto('admin/settings/user_stats/reset_login_count');
174    }    }
 }  
175    
176  /**    // Rebuild post count stats when settings change.
177   * reset post count confim callback    variable_set('user_stats_rebuild_stats', TRUE);
178   */    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'));  
179  }  }
180    
181  /**  /**
182   * reset login count confim callback   * Reset post count handler.
183   */   */
184  function user_stats_reset_logincount_confirm() {  function user_stats_reset_post_count_confirm_submit() {
185    return confirm_form(array(), t('Are you sure you want to reset login counts?'),    variable_set('user_stats_rebuild_stats', TRUE);
186      'admin/settings/user_stats', '', t('Reset all login counts'), t('Cancel'));    user_stats_reset_counts('user_post_count');
187      drupal_set_message(t('Post counts have been reset.'));
188      drupal_goto('admin/settings/user_stats');
189  }  }
190    
191  /**  /**
192   * reset post count handler   * Reset login count handler.
193   */   */
194  function user_stats_reset_postcount_confirm_submit($form_id, &$form) {  function user_stats_reset_login_count() {
195    if ($form['confirm']) {    user_stats_reset_counts('user_login_count');
196      variable_set('user_stats_rebuild_stats', TRUE);    drupal_set_message(t('User login counts have been reset.'));
197      user_stats_reset_counts('user_post_count');    drupal_goto('admin/settings/user_stats');
     drupal_set_message(t('The post counts will be reset during the next cron run.'));  
     drupal_goto('admin/settings/user_stats');  
   }  
198  }  }
199    
200  /**  /**
201   * reset login count handler   * Returns user stats.
  */  
 function user_stats_reset_logincount_confirm_submit($form_id, &$form) {  
   if ($form['confirm']) {  
     user_stats_reset_counts('user_login_count');  
     drupal_set_message(t('The user login counts have been reset'));  
     drupal_goto('admin/settings/user_stats');  
   }  
 }  
   
 /**  
  * Returns user stats  
202   *   *
203   * @param $type   * @param $type
204   *   The statistic to return. Possible values are:   *   The statistic to return. Possible values are:
# Line 212  function user_stats_reset_logincount_con Line 210  function user_stats_reset_logincount_con
210   *   - "post_days"   *   - "post_days"
211   *   - "reg_days"   *   - "reg_days"
212   *   - "online"   *   - "online"
213     *   - "profile"
214   * @param $uid   * @param $uid
215   *   The user id who's stats should be retrieved.   *   The user id who's stats should be retrieved.
216   *   *
# Line 247  function user_stats_get_stats($type, $ui Line 246  function user_stats_get_stats($type, $ui
246          if (!variable_get('user_stats_count_posts', TRUE) && !variable_get('user_stats_count_comments', TRUE)) {          if (!variable_get('user_stats_count_posts', TRUE) && !variable_get('user_stats_count_comments', TRUE)) {
247            return 'n/a';            return 'n/a';
248          }          }
249          // 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.
250          $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');
251          if (!isset($user->$post_count_profile_field)) {          if (!isset($user->$post_count_profile_field)) {
252            user_stats_post_count_update($user, 'reset');            user_stats_post_count_update($user, 'reset');
253          }          }
# Line 257  function user_stats_get_stats($type, $ui Line 256  function user_stats_get_stats($type, $ui
256          $last_post = _user_stats_last_post($user);          $last_post = _user_stats_last_post($user);
257          if ($last_post !== FALSE) {          if ($last_post !== FALSE) {
258            return floor((time() - $last_post) / 86400);            return floor((time() - $last_post) / 86400);
259          } else {          }
260            else {
261            return 'n/a';            return 'n/a';
262          }          }
263        case 'reg_days':        case 'reg_days':
# Line 277  function user_stats_get_stats($type, $ui Line 277  function user_stats_get_stats($type, $ui
277  }  }
278    
279  /**  /**
280   * User cache for optimisation, this needs to be a seperate   * User cache for optimisation, this needs to be a seperate
281   * function so cache can be flushed by user_stats_post_count_update   * function so cache can be flushed by user_stats_post_count_update
282   * below (and possibly other places).   * below (and possibly other places).
283   *   *
# Line 285  function user_stats_get_stats($type, $ui Line 285  function user_stats_get_stats($type, $ui
285   *   User id of user required from cache.   *   User id of user required from cache.
286   * @param $flush   * @param $flush
287   *   Defaults to FALSE. If TRUE the $uid is flushed from the cache.   *   Defaults to FALSE. If TRUE the $uid is flushed from the cache.
288   *   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
289   *   be purged.   *   be purged.
290   * @return   * @return
291   *   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.
292   *   If the $uid is -1 and/or $flush is TRUE nothing.   *   If the $uid is -1 and/or $flush is TRUE nothing.
293   */   */
294  function _user_stats_user_cache($uid, $flush = FALSE) {  function _user_stats_user_cache($uid, $flush = FALSE) {
# Line 309  function _user_stats_user_cache($uid, $f Line 309  function _user_stats_user_cache($uid, $f
309  }  }
310    
311  /**  /**
312   * Drupal hook implementations   * Drupal hook implementations.
313   */   */
314    
315  /**  /**
316   * Implementation of hook_nodeapi()   * Implementation of hook_nodeapi().
317   *   *
318   * Increments post count on insert or publish   * Increments post count on insert or publish.
319   * Decrements post count on delete or unpublish   * Decrements post count on delete or unpublish.
320   * Checks and updates users without post counts on view   * Checks and updates users without post counts on view.
321   *   *
322   * @param &$node   * @param &$node
323   *   The node the action is being performed on.   *   The node the action is being performed on.
324   * @param $op   * @param $op
325   *   The operation being performed. We are interested in insert, delete, update and view   *   The operation being performed. We are interested in insert, delete, update and view.
326   */   */
327  function user_stats_nodeapi(&$node, $op) {  function user_stats_nodeapi(&$node, $op) {
328    // We stop before any work is done if the $op isn't one of the ones we need    // We stop before any work is done if the $op isn't one of the ones we need
329    if (!in_array($op, array('insert', 'delete', 'update'))) {    if (!in_array($op, array('insert', 'delete', 'update'))) {
330      return;      return;
331    }    }
332    $postcount_content_types = variable_get('user_stats_included_content_types', array());    $post_count_content_types = variable_get('user_stats_included_content_types', array());
333    if ((empty($postcount_content_types) ||    if ((empty($post_count_content_types) ||
334      in_array($node->type, $postcount_content_types)) &&      in_array($node->type, $post_count_content_types)) &&
335      variable_get('user_stats_count_posts', TRUE)) {      variable_get('user_stats_count_posts', TRUE)) {
336    
337      switch ($op) {      switch ($op) {
# Line 353  function user_stats_nodeapi(&$node, $op) Line 353  function user_stats_nodeapi(&$node, $op)
353      }      }
354    }    }
355    
356    // Do IP Address update    // Do IP Address update.
357    switch ($op) {    switch ($op) {
358      case 'insert':      case 'insert':
359        global $user;        global $user;
360        // User IP addresses are only interesting if they are posting the content        // User IP addresses are only interesting if they are posting the content.
361        if ($node->uid == $user->uid) {        if ($node->uid == $user->uid) {
362          user_stats_ip_address_update($user, $_SERVER['REMOTE_ADDR']);          user_stats_ip_address_update($user, $_SERVER['REMOTE_ADDR']);
363        }        }
# Line 372  function user_stats_nodeapi(&$node, $op) Line 372  function user_stats_nodeapi(&$node, $op)
372  }  }
373    
374  /**  /**
375   * Implementation of hook_comment()   * Implementation of hook_comment().
376   *   *
377   * Increments post count on insert or publish   * Increments post count on insert or publish.
378   * Decrements post count on delete or unpublish   * Decrements post count on delete or unpublish.
379   * Updates users with no post count on view   * Updates users with no post count on view.
380   *   *
381   * @param $a1   * @param $a1
382   *   Either the form values being submitted, the module form to be displayed,   *   Either the form values being submitted, the module form to be displayed,
# Line 385  function user_stats_nodeapi(&$node, $op) Line 385  function user_stats_nodeapi(&$node, $op)
385   *   What kind of action is being performed.   *   What kind of action is being performed.
386   */   */
387  function user_stats_comment(&$a1, $op) {  function user_stats_comment(&$a1, $op) {
388    // We stop before any work is done if the $op isn't one of the ones we need    // We stop before any work is done if the $op isn't one of the ones we need.
389    if (!in_array($op, array('insert', 'delete', 'update'))) {    if (!in_array($op, array('insert', 'delete', 'update'))) {
390      return;      return;
391    }    }
392    
393    // check to see if comments should be counted at all    // check to see if comments should be counted at all.
394    if (variable_get('user_stats_count_comments', TRUE)) {    if (variable_get('user_stats_count_comments', TRUE)) {
395      $comment = (object)$a1;      $comment = (object)$a1;
396      $postcount_content_types = variable_get('user_stats_included_content_types', array());      $post_count_content_types = variable_get('user_stats_included_content_types', array());
397      $node = node_load(array('nid' => $comment->nid));      $node = node_load(array('nid' => $comment->nid));
398    
399      if ((empty($postcount_content_types) ||      if ((empty($post_count_content_types) ||
400        in_array($node->type, $postcount_content_types)) &&        in_array($node->type, $post_count_content_types)) &&
401        variable_get('user_login_count', TRUE)) {        variable_get('user_login_count', TRUE)) {
402    
403        switch ($op) {        switch ($op) {
# Line 417  function user_stats_comment(&$a1, $op) { Line 417  function user_stats_comment(&$a1, $op) {
417      }      }
418    }    }
419    
420    // Do IP address update    // Do IP address update.
421    global $user;    global $user;
422    if ($op == 'insert' && $comment->uid == $user->uid) {    if ($op == 'insert' && $comment->uid == $user->uid) {
423      // User IP addresses are only interesting if they are posting the content      // User IP addresses are only interesting if they are posting the content.
424      user_stats_ip_address_update($user, $_SERVER['REMOTE_ADDR']);      user_stats_ip_address_update($user, $_SERVER['REMOTE_ADDR']);
425    }    }
426  }  }
427    
428  /**  /**
429   * Implementation of hook_cron()   * Implementation of hook_cron().
430   *   *
431   * We slowly work through all users without a post count   * We slowly work through all users without a post count
432   * updating them.   * updating them.
# Line 434  function user_stats_comment(&$a1, $op) { Line 434  function user_stats_comment(&$a1, $op) {
434  function user_stats_cron() {  function user_stats_cron() {
435    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))) {
436      $sql = "SELECT fid FROM {profile_fields} WHERE name='%s'";      $sql = "SELECT fid FROM {profile_fields} WHERE name='%s'";
437      $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')));
438    
439      // 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.
440      $sql  = "SELECT uid FROM {users} WHERE uid NOT IN ( ";      $sql  = "SELECT uid FROM {users} WHERE uid NOT IN
441      $sql .= "  SELECT uid FROM {profile_values} ";        (SELECT uid FROM {profile_values} WHERE fid=%d)";
442      $sql .= "  WHERE fid=%d ";      // Update 25 users per cron run.
     $sql .= ") ";  
     // Update 25 users per cron run  
443      $result = db_query_range($sql, $fid, 0, variable_get('user_stats_user_per_cron', array('25')));      $result = db_query_range($sql, $fid, 0, variable_get('user_stats_user_per_cron', array('25')));
444      // If all users have been updated we'll avoid running this expensive      // If all users have been updated we'll avoid running this expensive
445      // query again by setting the following flag!      // query again by setting the following flag!
446      if (db_num_rows($result) == 0) {      if (db_num_rows($result) == 0) {
447        variable_set('user_stats_rebuild_stats', FALSE);        variable_set('user_stats_rebuild_stats', FALSE);
# Line 453  function user_stats_cron() { Line 451  function user_stats_cron() {
451        user_stats_post_count_update($user, 'reset');        user_stats_post_count_update($user, 'reset');
452      }      }
453    }    }
454    // Fire workflow_ng daily anniversary event    // Fire workflow_ng daily anniversary event.
455    if (module_exists('workflow_ng')) {    if (module_exists('workflow_ng')) {
456      $sql  = "SELECT uid FROM {users} u ";      $sql  = "SELECT uid FROM {users} u ";
457      // ((last cron - created) - (time() - created)) > one day      // ((last cron - created) - (time() - created)) > one day
458      $sql .= "WHERE ";      $sql .= "WHERE (FLOOR((%d-created)/(60*60*24))-FLOOR((%d-created)/(60*60*24)))>0
459      $sql .= "(FLOOR((%d-created)/(60*60*24))-FLOOR((%d-created)/(60*60*24)))>0 ";        AND uid>0";
     $sql .= "AND uid>0 ";  
460      $result = db_query($sql, time(), variable_get('cron_last', time()));      $result = db_query($sql, time(), variable_get('cron_last', time()));
461      $reset_user_count = 0;      $reset_user_count = 0;
462      while ($update_user = db_fetch_object($result)) {      while ($update_user = db_fetch_object($result)) {
463        $user = user_stats_user_load($update_user->uid);        $user = user_stats_user_load($update_user->uid);
464        // We stop at 50 reset users (they'll just have to wait) so cron doesn't get timed out        // We stop at 50 reset users (they'll just have to wait) so cron doesn't get timed out.
465        if (!isset($user->user_post_count) && $reset_user_count < 50) {        if (!isset($user->user_post_count) && $reset_user_count < 50) {
466          user_stats_post_count_update($user, 'reset');          user_stats_post_count_update($user, 'reset');
467          $reset_user_count++;          $reset_user_count++;
# Line 475  function user_stats_cron() { Line 472  function user_stats_cron() {
472  }  }
473    
474  /**  /**
475   * Implementation of hook_user()   * Implementation of hook_user().
476   */   */
477  function user_stats_user($op, &$edit, &$account) {  function user_stats_user($op, &$edit, &$account) {
478    if (variable_get('user_stats_count_logins', TRUE) && $op == 'login') {    if (variable_get('user_stats_count_logins', TRUE) && $op == 'login') {
# Line 486  function user_stats_user($op, &$edit, &$ Line 483  function user_stats_user($op, &$edit, &$
483          AND uid=%d";          AND uid=%d";
484      }      }
485      else {      else {
486        // If there isn't a value insert it        // If there isn't a value insert it.
487        $sql = "INSERT INTO {profile_values} (fid, value, uid)        $sql = "INSERT INTO {profile_values} (fid, value, uid)
488          SELECT fid, %d AS value, %d AS uid FROM {profile_fields}          SELECT fid, %d AS value, %d AS uid FROM {profile_fields}
489          WHERE name='user_login_count'";          WHERE name='user_login_count'";
490      }      }
# Line 499  function user_stats_user($op, &$edit, &$ Line 496  function user_stats_user($op, &$edit, &$
496      }      }
497      db_query($sql, $login_count, $account->uid);      db_query($sql, $login_count, $account->uid);
498    }    }
499    // Update IP Address    // Update IP Address.
500    if ($op == 'login' || $op == 'logout') {    if ($op == 'login' || $op == 'logout') {
501      user_stats_ip_address_update($account, $_SERVER['REMOTE_ADDR']);      user_stats_ip_address_update($account, $_SERVER['REMOTE_ADDR']);
502    }    }
503  }  }
504    
505  /**  /**
506   * Implementation of hook_form_alter()   * Implementation of hook_form_alter().
507   */   */
508  function user_stats_form_alter($form_id, &$form) {  function user_stats_form_alter($form_id, &$form) {
509    if ($form_id == 'user_edit') {    if ($form_id == 'user_edit') {
# Line 514  function user_stats_form_alter($form_id, Line 511  function user_stats_form_alter($form_id,
511        if (isset($form['Statistics'])) {        if (isset($form['Statistics'])) {
512          foreach ($form['Statistics'] as $field => $value) {          foreach ($form['Statistics'] as $field => $value) {
513            if (is_array($value) && $value['#type'] == 'textfield') {            if (is_array($value) && $value['#type'] == 'textfield') {
514              $form['Statistics'][$field]['#disabled'] = true;              $form['Statistics'][$field]['#disabled'] = TRUE;
515              $form['Statistics'][$field]['#description'] = 'Field not editable.';              $form['Statistics'][$field]['#description'] = 'Field not editable.';
516            }            }
517          }          }
# Line 539  function user_stats_form_alter($form_id, Line 536  function user_stats_form_alter($form_id,
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    }    }
545    $max_node = db_result(db_query($sql, 1, $account->uid));    $max_node = db_result(db_query($sql, 1, $account->uid));
546    $sql  = "SELECT MAX(timestamp) FROM {comments} c INNER JOIN {node} n ON c.nid=n.nid WHERE c.status=%d AND c.uid=%d";    $sql  = "SELECT MAX(timestamp) FROM {comments} c
547    if (!empty($postcount_content_types)) {      INNER JOIN {node} n ON c.nid=n.nid
548        WHERE c.status=%d AND c.uid=%d";
549      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    }    }
# Line 565  function _user_stats_last_post($account) Line 564  function _user_stats_last_post($account)
564  }  }
565    
566  /**  /**
567   * Workflow-ng hooks and implementing functions   * Workflow-ng hooks and implementing functions.
568   */   */
569    
570  /**  /**
571  * Implementation of hook_event_info()   * Implementation of hook_event_info().
572  */   */
573  function user_stats_event_info() {  function user_stats_event_info() {
574    return array(    return array(
575      'user_stats_post_count_increment' => array(      'user_stats_post_count_increment' => array(
# Line 605  function user_stats_event_info() { Line 604  function user_stats_event_info() {
604  }  }
605    
606  /**  /**
607   * Implementation of hook_action_info()   * Implementation of hook_action_info().
608   */   */
609  function user_stats_action_info() {  function user_stats_action_info() {
610    return array(    return array(
# Line 686  function user_stats_action_post_count_re Line 685  function user_stats_action_post_count_re
685    
686  /**  /**
687   * Implementation of hook_preprocess_author_pane().   * Implementation of hook_preprocess_author_pane().
688   * A preprocess function for the author pane used by   * A preprocess function for the author pane used by
689   * Advanced Forum and Advanced Profile Kit.   * Advanced Forum and Advanced Profile Kit.
690   */   */
691  function user_stats_preprocess_author_pane(&$variables) {  function user_stats_preprocess_author_pane(&$variables) {
# Line 704  function user_stats_preprocess_author_pa Line 703  function user_stats_preprocess_author_pa
703  }  }
704    
705  /**  /**
706   * Token hook implementations   * Token hook implementations.
707   */   */
708    
709  /**  /**
710   * Implementation of hook_token_values()   * Implementation of hook_token_values().
711   */   */
712  function user_stats_token_values($type, $object = NULL) {  function user_stats_token_values($type, $object = NULL) {
713    switch ($type) {    switch ($type) {
# Line 730  function user_stats_token_values($type, Line 729  function user_stats_token_values($type,
729        $values['post-days']              = check_plain(user_stats_get_stats('post_days', $uid));        $values['post-days']              = check_plain(user_stats_get_stats('post_days', $uid));
730        $values['post-count']             = check_plain(user_stats_get_stats('post_count', $uid));        $values['post-count']             = check_plain(user_stats_get_stats('post_count', $uid));
731        $values['ip-address']             = check_plain(user_stats_get_stats('ip_address', $uid));        $values['ip-address']             = check_plain(user_stats_get_stats('ip_address', $uid));
732          return $values;
733    }    }
   
   return $values;  
734  }  }
735    
736  /**  /**
737   * Implementation of hook_token_list()   * Implementation of hook_token_list().
738   */   */
739  function user_stats_token_list($type = 'all') {  function user_stats_token_list($type = 'all') {
740    if ($type == 'user' || $type == 'all') {    if ($type == 'user' || $type == 'all') {
# Line 745  function user_stats_token_list($type = ' Line 743  function user_stats_token_list($type = '
743      $tokens['user']['post-days']              = t('Number of days since the user posted');      $tokens['user']['post-days']              = t('Number of days since the user posted');
744      $tokens['user']['post-count']             = t('User\'s post count');      $tokens['user']['post-count']             = t('User\'s post count');
745      $tokens['user']['ip-address']             = t('User\'s IP address');      $tokens['user']['ip-address']             = t('User\'s IP address');
746        return $tokens;
747    }    }
   return $tokens;  
748  }  }
749    
750  /**  /**
751   * Manage the post count of a given user   * Manage the post count of a given user.
752   *   *
753   * @param $user   * @param $user
754   *   Reference to user object   *   Reference to user object.
755   * @param $op   * @param $op
756   *   Whether the user post count should be incremented, decremented, or reset.   *   Whether the user post count should be incremented, decremented, or reset.
757   *   The default is to increment. Possible values are:   *   The default is to increment. Possible values are:
# Line 763  function user_stats_token_list($type = ' Line 761  function user_stats_token_list($type = '
761   */   */
762  function user_stats_post_count_update(&$user, $op = 'increment') {  function user_stats_post_count_update(&$user, $op = 'increment') {
763    // The lock stops infinite recursions, the action will still happen, but the    // The lock stops infinite recursions, the action will still happen, but the
764    // corresponding event (the reason for an infinite loop) will not be fired    // corresponding event (the reason for an infinite loop) will not be fired.
765    static $locked;    static $locked;
766    // Shared SQL for increment and decrement    // Shared SQL for increment and decrement
767    $sql  = "UPDATE {profile_values} SET value=%d ";    $sql  = "UPDATE {profile_values} SET value=%d
768    $sql .= "WHERE fid = (SELECT fid FROM {profile_fields} ";      WHERE fid = (SELECT fid FROM {profile_fields} WHERE name = '%s')
769    $sql .= "WHERE name = '%s') AND uid=%d ";      AND uid=%d";
770    $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');
771    
772    switch ($op) {    switch ($op) {
773      case 'increment':      case 'increment':
# Line 781  function user_stats_post_count_update(&$ Line 779  function user_stats_post_count_update(&$
779          db_query($sql, $user->$post_count_profile_field, $post_count_profile_field, $user->uid);          db_query($sql, $user->$post_count_profile_field, $post_count_profile_field, $user->uid);
780          // Flush user cache.          // Flush user cache.
781          _user_stats_user_cache($user->uid, TRUE);          _user_stats_user_cache($user->uid, TRUE);
782          // Flush token cache          // Flush token cache.
783          if (module_exists('token')) {          if (module_exists('token')) {
784            token_get_values('user', NULL, TRUE);            token_get_values('user', NULL, TRUE);
785          }          }
786          // We use $locked to stop infinite loops          // We use $locked to stop infinite loops.
787          if (!$locked && module_exists('workflow_ng')) {          if (!$locked && module_exists('workflow_ng')) {
788            $locked = TRUE;            $locked = TRUE;
789            workflow_ng_invoke_event('user_stats_post_count_increment', $user);            workflow_ng_invoke_event('user_stats_post_count_increment', $user);
# Line 802  function user_stats_post_count_update(&$ Line 800  function user_stats_post_count_update(&$
800          db_query($sql, max($user->$post_count_profile_field, 0), $post_count_profile_field, $user->uid);          db_query($sql, max($user->$post_count_profile_field, 0), $post_count_profile_field, $user->uid);
801          // Flush user cache.          // Flush user cache.
802          _user_stats_user_cache($user->uid, TRUE);          _user_stats_user_cache($user->uid, TRUE);
803          // Flush token cache          // Flush token cache.
804          if (module_exists('token')) {          if (module_exists('token')) {
805            token_get_values('user', NULL, TRUE);            token_get_values('user', NULL, TRUE);
806          }          }
# Line 822  function user_stats_post_count_update(&$ Line 820  function user_stats_post_count_update(&$
820        }        }
821        if (variable_get('user_stats_count_posts', TRUE)) {        if (variable_get('user_stats_count_posts', TRUE)) {
822          $sql  = "SELECT COUNT(*) FROM {node} WHERE uid=%d AND status=1";          $sql  = "SELECT COUNT(*) FROM {node} WHERE uid=%d AND status=1";
823          $postcount_content_types = variable_get('user_stats_included_content_types', array());          $post_count_content_types = variable_get('user_stats_included_content_types', array());
824          if (!empty($postcount_content_types)) {          if (!empty($post_count_content_types)) {
825            $content_types = "'". implode("','", $postcount_content_types) ."'";            $content_types = "'". implode("','", $post_count_content_types) ."'";
826            $where = ' AND type IN ('. $content_types .')';            $where = ' AND type IN ('. $content_types .')';
827            $sql .= $where;            $sql .= $where;
828          }          }
# Line 833  function user_stats_post_count_update(&$ Line 831  function user_stats_post_count_update(&$
831        }        }
832        if (variable_get('user_stats_count_comments', TRUE)) {        if (variable_get('user_stats_count_comments', TRUE)) {
833          $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";
834          if (!empty($postcount_content_types)) {          if (!empty($post_count_content_types)) {
835            $where = ' AND n.type IN ('. $content_types .')';            $where = ' AND n.type IN ('. $content_types .')';
836            $sql .= $where;            $sql .= $where;
837          }          }
838          $comments_count = db_result(db_query($sql, $user->uid));          $comments_count = db_result(db_query($sql, $user->uid));
839          $total_count += $comments_count;          $total_count += $comments_count;
840        }        }
841        $sql =  "DELETE FROM {profile_values} ";        $sql =  "DELETE FROM {profile_values}
842        $sql .= "WHERE fid=%d AND uid=%d";          WHERE fid=%d AND uid=%d";
843        db_query($sql, $fid, $user->uid);        db_query($sql, $fid, $user->uid);
844        $sql  = "INSERT INTO {profile_values} (fid, uid, value) VALUES ( ";        $sql  = "INSERT INTO {profile_values} (fid, uid, value)
845        $sql .= "%d, %d, %d ";          VALUES ( %d, %d, %d )";
       $sql .= ") ";  
846        db_query($sql, $fid, $user->uid, $total_count);        db_query($sql, $fid, $user->uid, $total_count);
847        $user->user_post_count = $total_count;        $user->user_post_count = $total_count;
848        // Flush user cache.        // Flush user cache.
# Line 874  function user_stats_post_count_update(&$ Line 871  function user_stats_post_count_update(&$
871  function user_stats_ip_address_update(&$user, $ip_address) {  function user_stats_ip_address_update(&$user, $ip_address) {
872    profile_load_profile($user);    profile_load_profile($user);
873    if (isset($user->user_ip_address)) {    if (isset($user->user_ip_address)) {
874      $sql  = "UPDATE {profile_values} SET value='%s' ";      $sql  = "UPDATE {profile_values} SET value='%s' WHERE fid =
875      $sql .= "WHERE fid = (SELECT fid FROM {profile_fields} ";        (SELECT fid FROM {profile_fields} WHERE name = 'user_ip_address')
876      $sql .= "WHERE name = 'user_ip_address') AND uid=%d ";        AND uid=%d";
877    }    }
878    else {    else {
879      $sql  = "INSERT INTO {profile_values} (fid, value, uid) ";      $sql  = "INSERT INTO {profile_values} (fid, value, uid)
880      $sql .= "SELECT fid, '%s' AS value, %d AS uid FROM {profile_fields} ";        SELECT fid, '%s' AS value, %d AS uid FROM {profile_fields}
881      $sql .= "WHERE name='user_ip_address' ";        WHERE name='user_ip_address'";
882    }    }
883    db_query($sql, $ip_address, $user->uid);    db_query($sql, $ip_address, $user->uid);
884  }  }
# Line 918  function user_stats_user_load($uid) { Line 915  function user_stats_user_load($uid) {
915    
916  /**  /**
917   * Resets statistics. Full stop.   * Resets statistics. Full stop.
918   *   *
919   * @param $statistic   * @param $statistic
920   *   The name of the statistic to be reset.   *   The name of the statistic to be reset.
921   *   This must be the name of the profile_fields field.   *   This must be the name of the profile_fields field.
922   */   */
923  function user_stats_reset_counts($statistic = 'user_post_count') {  function user_stats_reset_counts($statistic = 'user_post_count') {
924    $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'";
925      if ($statistic == 'user_post_count') {
926        $fid = db_result(db_query($sql, variable_get('user_stats_post_count_profile_field', 'user_post_count')));
927      }
928      else {
929        $fid = db_result(db_query($sql, $statistic));
930      }
931    if ($fid) {    if ($fid) {
932      db_query('DELETE FROM {profile_values} WHERE fid = %d', $fid);      db_query('DELETE FROM {profile_values} WHERE fid = %d', $fid);
933    }    }

Legend:
Removed from v.1.2.2.19  
changed lines
  Added in v.1.2.2.20

  ViewVC Help
Powered by ViewVC 1.1.2