You are here

function _fb_register_register_users in Drupal for Facebook 6.2

Register a chunk of users.

3 calls to _fb_register_register_users()
fb_register_all_users in contrib/fb_register.module
This callback will register a chunk of users, then redirect to itself to register another chunk. And so on, to quickly register all users of a site, rather than waiting for cron jobs to take care of it all.
fb_register_cron in contrib/fb_register.module
Implementation of hook_cron().
fb_register_now_form_submit in contrib/fb_register.module

File

contrib/fb_register.module, line 151
This code aims to prevent duplicate accounts.

Code

function _fb_register_register_users($fb_app, $how_many) {

  // Use cache to keep track of how many users we've already registered.
  // Joining {users} and {fb_register} tables does not give us this
  // information, because we must register users for each application
  // hosted on this Drupal.
  $cache = cache_get('fb_register_cache');
  if (!$cache) {
    $cache = new stdClass();
    $cache->data = array(
      $fb_app->apikey => 0,
    );
  }
  $last_uid_registered = $cache->data[$fb_app->apikey];

  // Initialize facebook api for this app.
  $fb = fb_api_init($fb_app);
  $result = db_query("SELECT u.uid, u.name, u.mail, fbr.email_hash FROM {users} u LEFT JOIN {fb_register} fbr ON fbr.uid=u.uid WHERE (fbr.uid IS NULL OR u.uid > %d) AND u.mail IS NOT NULL AND u.mail <> '' ORDER BY u.uid LIMIT %d, %d", $last_uid_registered, 0, $how_many);
  while ($account = db_fetch_object($result)) {
    $register_data[] = array(
      'email_hash' => fb_register_email_hash($account->mail),
      'account_url' => url('user/' . $account->uid, array(
        'absolute' => TRUE,
      )),
    );
    $user_data[] = $account;
  }
  $success_count = 0;
  $error_count = 0;
  if (isset($register_data) && count($register_data)) {
    try {
      $success_data = $fb->api_client
        ->connect_registerUsers(json_encode($register_data));

      // Check results
      foreach ($register_data as $i => $data) {
        $account = $user_data[$i];
        if (isset($success_data[$i]) && $success_data[$i] == $data['email_hash']) {

          // Success
          db_query("DELETE FROM {fb_register} WHERE uid=%d", $account->uid);
          db_query("INSERT INTO {fb_register} (uid, email_hash) VALUES (%d, '%s')", $account->uid, $data['email_hash']);
          $success_count++;
          $last_uid_registered = max($last_uid_registered, $account->uid);
        }
        else {

          // Failure
          if (fb_verbose()) {
            watchdog('fb_register', '%application failed to register email hash %hash for user %uid (%name)', array(
              '%application' => $fb_app->title,
              '%hash' => $data['email_hash'],
              '%uid' => $account->uid,
              '%name' => $account->name,
            ), WATCHDOG_ERROR);
          }
          $error_count++;

          // Make certain we try this user again next time.
          db_query("DELETE FROM {fb_register} WHERE uid=%d", $account->uid);
        }
      }

      // debugging

      //dpm($register_data, "register data");

      //dpm($success_data, "success data");
    } catch (Exception $e) {
      fb_log_exception($e, t('Failed to register users via connect_registerUsers'));
      $error_count++;
    }
    fb_report_errors($fb, t('Failed to register email hash'));
  }
  else {
    drupal_set_message(t('Found no users to register.'));
  }

  // Remember any progress we've made.
  $cache->data[$fb_app->apikey] = $last_uid_registered;
  cache_set('fb_register_cache', $cache->data, 'cache', CACHE_PERMANENT);
  if ($error_count > 0) {
    return -1 * $error_count;
  }
  else {
    return $success_count;
  }
}