You are here

function flag_get_flag_counts in Flag 7.3

Same name and namespace in other branches
  1. 6.2 flag.module \flag_get_flag_counts()
  2. 7.2 flag.module \flag_get_flag_counts()

Gets the count of entities flagged by the given flag.

For example, with a 'report abuse' flag, this returns the number of entities that have been reported, not the total number of reports. In other words, an entity that has been reported multiple times will only be counted once.

When called during a flagging or unflagging (such as from a hook implementation or from Rules), the count this returns takes into account the the flagging or unflagging that is in the process of being performed.

Parameters

$flag_name: The flag name for which to retrieve a flag count.

$reset: (optional) Reset the internal cache and execute the SQL query another time.

Return value

int The number of entities that are flagged with the flag.

3 calls to flag_get_flag_counts()
flag_form in includes/flag.admin.inc
Add/Edit flag page.
flag_rules_action_fetch_overall_flag_count in ./flag.rules.inc
Base action implementation: Fetch overall count for a particular flag.
_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_flag_counts'
flag_flag::_decrease_count in includes/flag/flag_flag.inc
Decreases the flag count for an object and clears the static counts cache.
flag_flag::_increase_count in includes/flag/flag_flag.inc
Increases the flag count for an object and clears the static counts cache.

File

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

Code

function flag_get_flag_counts($flag_name, $reset = FALSE) {
  $counts =& drupal_static(__FUNCTION__);
  if ($reset) {
    $counts = array();
  }
  if (!isset($counts[$flag_name])) {
    $flag = flag_get_flag($flag_name);
    $counts[$flag_name] = db_select('flag_counts', 'fc')
      ->fields('fc', array(
      'fid',
    ))
      ->condition('fid', $flag->fid)
      ->countQuery()
      ->execute()
      ->fetchField();
  }
  return $counts[$flag_name];
}