You are here

function fb_actions_cron_per_user in Drupal for Facebook 5

Same name and namespace in other branches
  1. 5.2 fb_actions.module \fb_actions_cron_per_user()
  2. 6.2 contrib/fb_actions.module \fb_actions_cron_per_user()

File

./fb_actions.module, line 361
Actions defined here interact with Facebook's API. This makes it possible to notify facebook of various activities as they happen.

Code

function fb_actions_cron_per_user($obj, $values) {

  //$args = func_get_args();

  //watchdog('fb_action_debug', "fb_actions_cron_per_user" . dpr($args, 1), WATCHDOG_ERROR);
  foreach ($values['fb_app_nids'] as $nid) {
    $fb_app = fb_get_app(array(
      'nid' => $nid,
    ));

    // Set paths as if on a canvas page.
    // perform per-user actions
    // Save current settings
    $before_fb = $GLOBALS['fb'];
    $before_fb_app = $GLOBALS['fb_app'];
    $before_user = $GLOBALS['user'];

    // Find some users of the app, for whom cron has not run recently.
    $result = db_query("SELECT * FROM {fb_user_app} WHERE apikey='%s' AND fbu > 0 AND added > 0 ORDER BY time_cron ASC LIMIT %d", $fb_app->apikey, $values['throttle']);
    while ($data = db_fetch_object($result)) {

      // Log into facebook as the user from the database
      $account = fb_user_get_local_user($data->fbu, $fb_app);
      if (variable_get('fb_actions_verbose', FALSE)) {
        watchdog('fb_action_debug', "fb_actions_cron_per_user fbu is {$data->fbu}, local user is " . theme('username', $account), WATCHDOG_ERROR);
      }
      if (!$account || !$account->uid) {
        watchdog('fb cron', t('Facebook user %fbu does not correspond to a local account.', array(
          '%fbu' => $data->fbu,
        )));

        //db_query("DELETE FROM {fb_user_app} WHERE apikey='%s' AND fbu = %d",

        //         $fb_app->apikey,
        //         $data->fbu);
      }
      else {

        // If here, local user has been found.
        $fb = fb_api_init($fb_app, $data->fbu);
        if (!$fb || !$fb->api_client
          ->users_getLoggedInUser()) {

          // Failed to log in as the current user, fallback to infinite session.
          $fb = fb_api_init($fb_app, FB_FBU_INFINITE_SESSION);
        }
        if (!$fb || !$fb->api_client
          ->users_getLoggedInUser()) {
          watchdog('fb cron', t('Failed to log into %app during cron.  Try testing infinite session key.', array(
            '%app' => $fb_app->title,
          )));
        }
        else {

          // if here, we're logged into facebook.
          // Set things up as if this were a canvas page.
          $GLOBALS['user'] = $account;
          $GLOBALS['fb'] = $fb;
          $GLOBALS['fb_app'] = $fb_app;

          // Invoke any actions that we've been configured to invoke.
          actions_do($values['actions'], $fb_app);
        }

        // end if able to log into facebook
      }

      // end if local user found.
      // Record how recently cron was run for this user.  We do this even if
      // we failed to log in, because we don't want persistent problems to
      // clog the cron queue.  We'll get to this user again, eventually.
      db_query("UPDATE {fb_user_app} SET time_cron=%d WHERE apikey='%s' AND fbu=%d", time(), $fb_app->apikey, $data->fbu);
    }

    // Restore global variables
    $GLOBALS['user'] = $before_user;
    $GLOBALS['fb'] = $before_fb;
    $GLOBALS['fb_app'] = $before_fb_app;
  }
}