You are here

function _realname_rebuild_realnames in Real Name 6

Batchable function used to rebuild realnames

1 string reference to '_realname_rebuild_realnames'
realname_rebuild_confirm_submit in ./realname.admin.inc
Handler for rebuild confirmation

File

./realname.admin.inc, line 480
The RealName module allows the admin to choose fields from the user profile that will be used to add a "realname" element (method) to a user object. Hook_user is used to automatically add this to any user object that is loaded.

Code

function _realname_rebuild_realnames(&$context) {
  if (empty($context['sandbox'])) {

    // Initiate multistep processing.
    $context['sandbox']['progress'] = 0;

    //    $context['sandbox']['current_user'] = 0;
    // Skip user 0 (anonymous).
    $context['sandbox']['current_user'] = 1;
    $context['sandbox']['max'] = db_result(db_query('SELECT COUNT(DISTINCT uid) FROM {users} WHERE uid > 0'));

    // Let's get rid of all existing data and start fresh.
    db_query('TRUNCATE {realname}');
  }

  // Process the next 20 users.
  $limit = 20;
  $result = db_query_range("SELECT u.uid, u.name, u.mail FROM {users} u WHERE uid > %d ORDER BY uid ASC", $context['sandbox']['current_user'], 0, $limit);
  while ($row = db_fetch_object($result)) {
    $realname->uid = $row->uid;
    $realname->realname = _realname_make_name($row);

    // Try to update, if fail - try to insert.
    if (db_result(db_query('SELECT COUNT(uid) FROM {realname} WHERE uid=%d', $realname->uid))) {
      drupal_write_record('realname', $realname, 'uid');
    }
    else {
      drupal_write_record('realname', $realname);
    }
    $context['sandbox']['progress']++;
    $context['sandbox']['current_user'] = $row->uid;
  }

  // Multistep processing : report progress.
  if ($context['sandbox']['progress'] != $context['sandbox']['max'] - 1) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}