You are here

function MigrateDestinationUserpoints::import in User Points 7

Import a single userpoints transaction.

Parameters

stdClass $userpoints:

stdClass $row:

Return value

array|bool

Overrides MigrateDestination::import

File

./userpoints.migrate.inc, line 33
Migration class for userpoint transactions.

Class

MigrateDestinationUserpoints
Destination class for migrating userpoints.

Code

function import(stdClass $userpoints, stdClass $row) {

  // If updating previously-migrated content.
  if (isset($row->migrate_map_destid1)) {
    $updating = TRUE;
    if (isset($userpoints->txn_id)) {
      if ($userpoints->txn_id != $row->migrate_map_destid1) {
        throw new MigrateException(t("Incoming id !id and map destination id !destid don't match.", array(
          '!id' => $userpoints->txn_id,
          '!destid' => $row->migrate_map_destid1,
        )));
      }
    }
    else {
      $userpoints->txn_id = $row->migrate_map_destid1;
    }
  }
  else {
    $updating = FALSE;

    // Clear out the txn_id, just to be safe.
    unset($userpoints->txn_id);
  }

  // Build params.
  $params = array();
  $fields = $this
    ->fields();
  foreach (array_keys($fields) as $key) {
    if (isset($userpoints->{$key})) {
      $params[$key] = $userpoints->{$key};
    }
  }

  // Call userpoints api to write the entry.
  $result = userpoints_userpointsapi($params);
  if ($result['status'] == TRUE && isset($result['transaction'])) {
    $txn_id = $result['transaction']['txn_id'];
    if ($updating) {
      $this->numUpdated++;
    }
    else {
      $this->numCreated++;
    }

    // Call any complete handlers.
    $this
      ->complete($userpoints, $row);
    return array(
      $txn_id,
    );
  }
  if (isset($result['reason'])) {
    watchdog('userpoints_migrate', $result['reason'], array(), WATCHDOG_WARNING);
  }
  return FALSE;
}