batch_birthdays_sync_p2b

Versions
6
batch_birthdays_sync_p2b($field, &$context)

Profile to Birthdays batch copying process.

Code

contributions/birthdays/birthdays.sync.inc, line 109

<?php
function batch_birthdays_sync_p2b($field, &$context) {
  // First run, set up the sandbox.
  if (empty($context['sandbox'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_user'] = 0;
    // Count how many users are going to be processed.
    $context['sandbox']['max'] = db_result(db_query('SELECT COUNT(DISTINCT uid) FROM {users} WHERE uid > 0'));
  }

  // I'm not sure how many a slow server can process within the batch execution
  // limit, but 25 seems not too much. Faster machines won't notice the limit
  // thanks to the Batch API.
  $limit = 25;

  // Get next batch of users.
  $result = db_query_range('SELECT uid FROM {users} WHERE uid > %d ORDER BY uid ASC', $context['sandbox']['current_user'], 0, $limit);

  // For each user id.
  while ($uid = db_fetch_object($result)) {
    // Get the user object.
    $account = user_load(array('uid' => $uid->uid));
    // If the date of birth was set
    if ($account->{$field->name}) {
      // Save it, so birthdays_user() will be called and all necessary actions
      // will be performed.
      user_save($account, array($field->name => $account->{$field->name}));
    }

    // Updating the sandbox.
    $context['sandbox']['progress']++;
    $context['sandbox']['current_user'] = $account->uid;
  }

  // Update the message.
  $context['message'] = t('Synchronized @current of @total users.', array('@current' => $context['sandbox']['progress'], '@total' => $context['sandbox']['max']));

  // If not yet finished, calculate the proportion of items that are.
  if ($context['sandbox']['progress'] < $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}
?>

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters (without spaces) shown in the image.