You are here

function flag_get_content_flags in Flag 7.2

Same name and namespace in other branches
  1. 5 flag.module \flag_get_content_flags()
  2. 6.2 flag.module \flag_get_content_flags()
  3. 6 flag.module \flag_get_content_flags()

Return a list of users who have flagged a piece of content.

Parameters

$content_type: The type of content that will be retrieved. Usually 'node'.

$content_id: The content ID to check for flagging.

$flag_name: Optional. The name of a flag if wanting a list specific to a single flag.

Return value

If no flag name is given, an array of flagged content, keyed by the user ID that flagged the content. Each flagged content array is structured as an array of flag information for each flag, keyed by the flag name. If a flag name is specified, only the information for that flag is returned. If no flags were found an empty array is returned.

1 string reference to 'flag_get_content_flags'
flag_flag::flag in ./flag.inc
Flags, or unflags, an item.

File

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

Code

function flag_get_content_flags($content_type, $content_id, $flag_name = NULL) {
  $content_flags =& drupal_static(__FUNCTION__, array());
  if (!isset($content_flags[$content_type][$content_id])) {
    $flag_names = _flag_get_flag_names();
    $result = db_select('flag_content', 'fc')
      ->fields('fc')
      ->condition('content_type', $content_type)
      ->condition('content_id', $content_id)
      ->orderBy('timestamp', 'DESC')
      ->execute();
    $content_flags[$content_type][$content_id] = array();
    foreach ($result as $flag_content) {

      // Build a list of flaggings for all flags by user.
      $content_flags[$content_type][$content_id]['users'][$flag_content->uid][$flag_names[$flag_content->fid]] = $flag_content;

      // Build a list of flaggings for each individual flag.
      $content_flags[$content_type][$content_id]['flags'][$flag_names[$flag_content->fid]][$flag_content->uid] = $flag_content;
    }
  }
  if (empty($content_flags[$content_type][$content_id])) {
    return array();
  }
  if (isset($flag_name)) {
    if (isset($content_flags[$content_type][$content_id]['flags'][$flag_name])) {
      return $content_flags[$content_type][$content_id]['flags'][$flag_name];
    }
    return array();
  }
  return $content_flags[$content_type][$content_id]['users'];
}