You are here

function flag_get_user_flags in Flag 6

Same name and namespace in other branches
  1. 5 flag.module \flag_get_user_flags()
  2. 6.2 flag.module \flag_get_user_flags()
  3. 7.3 flag.module \flag_get_user_flags()
  4. 7.2 flag.module \flag_get_user_flags()

Find what a user has flagged, either a single node or on the entire site.

Parameters

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

$content_id: Optional. The content ID to check for flagging. If none given, all content flagged by this user will be returned.

$uid: Optional. The user ID whose flags we're checking. If none given, the current user will be used.

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

Return value

$flags If returning a single node's flags, an array of the structure [flag_name] => (fcid => [fcid], uid => [uid], content_id => [content_id], timestamp => [timestamp], ...)

If returning all nodes, an array of arrays for each flag: [flag_name] => [content_id] => Object from above.

2 calls to flag_get_user_flags()
flag_flag::flag in ./flag.inc
Flags, on unflags, an item.
flag_flag::is_flagged in ./flag.inc
Returns TRUE if a certain user has flagged this content.

File

./flag.module, line 1008

Code

function flag_get_user_flags($content_type, $content_id = NULL, $uid = NULL, $reset = FALSE) {
  static $flagged_content;
  if ($reset) {
    $flagged_content = array();
    if (!isset($content_type)) {
      return;
    }
  }
  $uid = !isset($uid) ? $GLOBALS['user']->uid : $uid;
  if (isset($content_id)) {
    if (!isset($flagged_content[$uid][$content_type][$content_id])) {
      $flag_names = _flag_get_flag_names();
      $flagged_content[$uid][$content_type][$content_id] = array();
      $result = db_query("SELECT * FROM {flag_content} WHERE content_type = '%s' AND content_id = %d AND (uid = %d OR uid = 0)", $content_type, $content_id, $uid);
      while ($flag_content = db_fetch_object($result)) {
        $flagged_content[$uid][$content_type][$content_id][$flag_names[$flag_content->fid]] = $flag_content;
      }
    }
    return $flagged_content[$uid][$content_type][$content_id];
  }
  else {
    if (!isset($flagged_content[$uid][$content_type]['all'])) {
      $flag_names = _flag_get_flag_names();
      $flagged_content[$uid][$content_type]['all'] = array();
      $result = db_query("SELECT * FROM {flag_content} WHERE content_type = '%s' AND (uid = %d OR uid = 0)", $content_type, $uid);
      while ($flag_content = db_fetch_object($result)) {
        $flagged_content[$uid][$content_type]['all'][$flag_names[$flag_content->fid]][$flag_content->content_id] = $flag_content;
      }
    }
    return $flagged_content[$uid][$content_type]['all'];
  }
}