You are here

function activity_batch_access_rebuild_process in Activity 7

Batch API processing operation. Rebuilding Access table.

Parameters

array $context: The batch api context array.

Return value

none

1 string reference to 'activity_batch_access_rebuild_process'
activity_access_batch_set in ./activity.admin.inc
FAPI form submit function for the activity_rebuild button.

File

./activity.batch.inc, line 15
: Contains Batch API functions for long running processing.

Code

function activity_batch_access_rebuild_process(&$context) {
  if (!isset($context['sandbox']['last_aid'])) {

    // Set up the sandbox for the first time.
    $context['sandbox']['last_aid'] = 0;
    $context['sandbox']['progress'] = 0;

    // Activity can be happening on the site. Any Activity happening after this point
    // will not be rebuilt. This is ok, as in that case, the new Activity will receive
    // any and all new Activity Access Realms.
    $context['sandbox']['max'] = db_query("SELECT COUNT(aid) FROM {activity}")
      ->fetchField();
  }

  // Process 100 Activities at a time.
  $limit = 100;
  $activities = db_select('activity', 'a')
    ->fields('a')
    ->condition('aid', $context['sandbox']['last_aid'], '>')
    ->range(0, $limit)
    ->execute()
    ->fetchAll();
  foreach ($activities as $activity) {
    $grants = activity_get_grants($activity);

    // Delete existing records.
    db_delete('activity_access')
      ->condition('aid', $activity->aid)
      ->execute();

    // Insert new ones.
    foreach ($grants as $realm => $values) {
      foreach ($values as $value) {
        $perm = new stdClass();
        $perm->aid = $activity->aid;
        $perm->realm = $realm;
        $perm->value = $value;
        drupal_write_record('activity_access', $perm);
      }
    }

    // Update sandbox variables.
    $context['sandbox']['last_aid'] = $activity->aid;
    $context['sandbox']['progress']++;
  }

  // Check if not finished.
  if ($context['sandbox']['progress'] < $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
  else {

    // If finished, delete the sandbox.
    unset($context['sandbox']);
  }
}