You are here

function flag_get_user_flag_counts in Flag 7.3

Gets the count of the flaggings made by a user with a flag.

For example, with a 'bookmarks' flag, this returns the number of bookmarks a user has created.

When called during a flagging or unflagging (such as from a hook implementation or from Rules), the flagging or unflagging that is in the process of being performed:

  • will be included during a flagging operation
  • will STILL be included during an unflagging operation. That is, the count will not yet have been decreased.

This is because this queries the {flagging} table, which only has its record deleted at the very end of the unflagging process.

However, it should be noted that this method does not serves global flags.

Parameters

$flag: The flag.

$user: The user object.

Return value

int The number of flaggings for the given flag and user.

2 calls to flag_get_user_flag_counts()
flag_rules_action_fetch_user_flag_count in ./flag.rules.inc
Base action implementation: Fetch user's flag count.
_flag_hook_test_record_invocation in tests/flag_hook_test/flag_hook_test.module
Store the hook name and parameters into a variable for retrieval by the test.
2 string references to 'flag_get_user_flag_counts'
flag_flag::flagging_delete in includes/flag/flag_flag.inc
Unflag an entity by deleting a Flagging.
flag_flag::flagging_insert in includes/flag/flag_flag.inc
Create a new Flagging to flag an entity.

File

./flag.module, line 1773
The Flag module.

Code

function flag_get_user_flag_counts($flag, $user) {
  $counts =& drupal_static(__FUNCTION__);

  // We check to see if the flag count is already in the cache,
  // if it's not, run the query.
  if (!isset($counts[$flag->name][$user->uid])) {
    $counts[$flag->name][$user->uid] = array();
    $result = db_select('flagging', 'f')
      ->fields('f', array(
      'fid',
    ))
      ->condition('fid', $flag->fid)
      ->condition('uid', $user->uid)
      ->countQuery()
      ->execute()
      ->fetchField();
    $counts[$flag->name][$user->uid] = $result;
  }
  return $counts[$flag->name][$user->uid];
}