You are here

function activity_get_activity in Activity 5.3

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

The API supports:

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, target_role
  • values are arrays of possible values for the keys. The key of the array of possible values can be 'include' or 'exclude' to indicate if the filter is positive or negative For example: array('target_role' => array('include' => 'Author'), 'operation' => array('include' => 'delete')) this would find activity where the author had deleted something. Example 2: array('target_role' => array('include' => array('Requester', 'Requestee'))) This shows that the values can be arrays as well. Example 3: array('module' => array('include' => array('nodeactivity', 'commentactivity')), 'operation' => array('exclude' => array('delete', 'unpublish')))

$limit: The number of results desired

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

8 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
create a block for display
activity_feed in ./activity.module
menu callback to return a feed of a signed in user's activity page
activity_json in ./activity.module
output our activity as json $arg[0] = ACTIVITY_ALL or $uid $arg[1] = number of activities to retreive

... 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 253
Activity module: Allow users to see their friends' activity on the site.

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 {
    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)) {
    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) || !array_key_exists('include', $filter) && !array_key_exists('exclude', $filter)) {
        $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 . ($criteria == 'exclude' ? ' != ' : ' = ') . "'%s'";

          // $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']['created'] = $row['created'];
    $row['data']['activity_id'] = $row['aid'];
    $row['data']['module'] = $row['module'];
    $row['data']['type'] = $row['type'];
    $row['data']['operation'] = $row['data']['operation'] ? $row['data']['operation'] : $row['operation'];

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