You are here

function user_relationships_api_cron in User Relationships 6

Same name and namespace in other branches
  1. 5.3 user_relationships_api/user_relationships_api.module \user_relationships_api_cron()

hook_cron()

File

user_relationships_api/user_relationships_api.module, line 238
User Relationships API. Module shell. @author Jeff Smick (creator) @author Alex Karshakevich (maintainer) http://drupal.org/user/183217

Code

function user_relationships_api_cron() {
  $now = time();

  // only expire relationships once a day
  $last_cron = variable_get('user_relationships_last_expire', 0);
  if ($now < $last_cron + 86400) {
    return FALSE;
  }
  $result = db_query(" SELECT ur.rid\n      FROM {user_relationships} ur\n        INNER JOIN {user_relationship_types} urt ON ur.rtid = urt.rtid\n      WHERE ur.approved = 0\n        AND urt.expires_val > 0\n        AND ur.updated_at < (%d - (urt.expires_val * 86400))\n      GROUP BY ur.rid", $now);
  $expired_requests = array();
  while ($request = db_fetch_object($result)) {
    $expired_requests[] = $request->rid;
  }
  if (count($expired_requests)) {
    db_query('DELETE FROM {user_relationships} WHERE rid IN (' . db_placeholders($expired_requests) . ')', $expired_requests);

    //FIXME: this will trigger various hook_user_relationships() but will not pass a valid relationship object. E.g. in mailer.
    _user_relationships_invoke('delete', $expired_requests);
  }

  // remember when we last expired relationships
  variable_set('user_relationships_last_expire', $now);
  return TRUE;
}