You are here

function activity_get_activity in Activity 6

Same name and namespace in other branches
  1. 5.4 activity.module \activity_get_activity()
  2. 5.3 activity.module \activity_get_activity()

API function.

Retrieve activity from the database.

@todo This should be replaced with views integration. Perhaps in 6.x.

Parameters

$uids:

  • a single uid
  • an array of uids
  • can include the special uid ACTIVITY_ALL

$filters:

  • an array where keys are one of module, type, operation, created, target_role
  • values are arrays of possible values for the keys. For example: array('target_role' => 'Author', 'operation' => 'delete') this would find activity where the author had deleted something. Example 2: array('target_role' => array('Requester', 'Requestee')) This shows that the values can be arrays as well.

$limit: The number of results desired

$tablesort_headers: An array that determines the sorting of the result set.

11 calls to activity_get_activity()
ActivityAPITest::testGetActivity1 in tests/ActivityAPITests.test
Test activity_get_activity activity_get_activity($uids = ACTIVITY_ALL, $filters = NULL, $limit = NULL, $tablesort_headers = NULL)
ActivityAPITest::testGetActivity2 in tests/ActivityAPITests.test
activity_block in ./activity.module
Implementation of hook_block().
activity_feed in ./activity.module
Menu callback for displaying site or user activity as an RSS feed.
activity_json in ./activity.module
Output activity as JSON.

... See full list

1 string reference to 'activity_get_activity'
ActivityAPITest::testGetActivity1 in tests/ActivityAPITests.test
Test activity_get_activity activity_get_activity($uids = ACTIVITY_ALL, $filters = NULL, $limit = NULL, $tablesort_headers = NULL)

File

./activity.module, line 606
activity.module

Code

function activity_get_activity($uids = ACTIVITY_ALL, $filters = NULL, $limit = NULL, $tablesort_headers = NULL) {
  $wheres = array();

  // Build the WHERE clause for user id.
  if (!is_array($uids)) {
    $wheres[] = "activity_targets.target_uid = %d";
    $params[] = $uids;
  }
  else {
    if (!empty($uids)) {
      foreach ($uids as $uid) {
        $nums[] = "%d";
        $params[] = $uid;
      }
      $wheres[] = 'activity_targets.target_uid IN (' . implode(',', $nums) . ')';
    }
  }

  // Build sql limiting query to on filtered fields
  if (!empty($filters) && is_array($filters)) {
    $ops = array(
      'include' => " = '%s'",
      'exclude' => " != '%s'",
      'lt' => ' < %d',
      'gt' => ' > %d',
      'lte' => ' <= %d',
      'gte' => ' >= %d',
    );
    foreach ($filters as $column => $filter) {

      // Of the possible columns, role is in the at table and all others in the
      // a table. Prefix the column name with the appropriate table.
      if ($column == 'target_role') {
        $column = 'activity_targets.target_role';
      }
      else {
        $column = "activity.{$column}";
      }

      // attempt to rewrite old filters to the new format
      if (!is_array($filter) || count(array_intersect(array_keys($ops), array_keys($filter))) == 0) {
        $filter = array(
          'include' => $filter,
        );
      }
      foreach ($filter as $criteria => $values) {
        if (is_array($values)) {
          $strings = array();
          foreach ($values as $value) {
            $strings[] = "'%s'";
            $params[] = $value;
          }
          $wheres[] = $column . ($criteria == 'exclude' ? ' NOT IN ' : ' IN ') . '(' . implode(',', $strings) . ')';
        }
        else {
          $wheres[] = $column . $ops[$criteria];

          // $values is a string with the single value.
          $params[] = $values;
        }
      }
    }
  }
  if (count($wheres) > 0) {
    $where = implode(' AND ', $wheres);
    $where = "WHERE {$where}";
  }

  // We always include tablesort_sql in the query so that this API is friendly
  // to sortable tables. If no headers were passed in, use the default headers.
  if (empty($tablesort_headers)) {
    $tablesort_headers = activity_get_tablesort_headers();
    $tablesort_headers['activity.created']['sort'] = 'desc';
  }

  // Build the sql and do the query. Wrapping it in db_rewrite_sql allows other
  // modules to impose access restrictions on activity listings.
  $sql = "SELECT activity.*, activity_targets.target_uid, activity_targets.target_role\n    FROM {activity_targets} activity_targets INNER JOIN {activity} activity ON activity.aid = activity_targets.aid\n    {$where} ";
  $tablesort_sql = tablesort_sql($tablesort_headers);
  $sql = db_rewrite_sql("{$sql} {$tablesort_sql}", 'activity_targets', 'aid', array(
    'uids' => $uids,
  ));
  if (is_numeric($limit)) {
    $result = pager_query($sql, $limit, 0, NULL, $params);
  }
  else {
    $result = db_query($sql, $params);
  }
  $activity = array();
  while ($row = db_fetch_array($result)) {
    $row['data'] = unserialize($row['data']);
    $row['data']['aid'] = $row['aid'];
    $row['data']['uid'] = $row['uid'];
    $row['data']['module'] = $row['module'];
    $row['data']['type'] = $row['type'];
    $row['data']['operation'] = isset($row['data']['operation']) ? $row['data']['operation'] : $row['operation'];
    $row['data']['created'] = $row['created'];

    // Load Activity comments if user can view comments
    // Use a permissions check here to save comment loading if user cannot view
    if (user_access('view activity comments')) {
      $row['comments'] = activity_comments_load($row['aid']);
    }

    // Invoke activityapi
    activity_invoke_activityapi($row, 'load');
    if (!empty($row)) {
      $activity[] = $row;
    }
  }
  return $activity;
}