From 6bbeab62150bd2e3d4ee79018608be2dcc591d2c Mon Sep 17 00:00:00 2001 From: ben Date: Fri, 7 Mar 2008 18:23:10 +0000 Subject: Users and friends git-svn-id: https://code.elgg.org/elgg/trunk@108 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/friends.php | 197 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 engine/lib/friends.php (limited to 'engine/lib/friends.php') diff --git a/engine/lib/friends.php b/engine/lib/friends.php new file mode 100644 index 000000000..618f99d05 --- /dev/null +++ b/engine/lib/friends.php @@ -0,0 +1,197 @@ +dbprefix}friends WHERE user_id = $user_id AND friend_id = $friend_id AND site_id = {$site_id}")) { + return insert_data("INSERT INTO {$CONFIG->dbprefix}friends (user_id,friend_id,site_id) VALUES ($user_id,$friend_id,$site_id)"); + } else { + return false; + } + + } + + /** + * Remove friend + * + * @param $user_id int User ID of the user having the friend removed. + * @param $friend_id int User ID of the friend being removed. + */ + + function remove_friend($user_id,$friend_id,$site) + { + global $CONFIG; + $user_id = (int) $user_id; + $friend_id = (int) $friend_id; + $site = (int) $site; + delete_record("DELETE FROM {$CONFIG->dbprefix}friends WHERE user_id = $user_id AND friend_id = $friend_id AND site_id = {$site}"); + return true; + } + + /** + * Determines if the user $user_id is friends with the currently logged in user, or a specified user + * @param int $user_id The user we're testing to see if they're a friend + * @param int $friend_of Optionally, the user we're testing to see if they're a friend of (the logged in user if unspecified) + * @return true|false Wehther $user_id is a friend of $friend_of + */ + function is_friend($user_id, $friend_of, $site) { + global $CONFIG; + $user_id = (int) $user_id; + $friend_of = (int) $friend_of; + $site = (int) $site; + if ($friend_of < 1) { + return false; + } + if (get_data_row("select id FROM {$CONFIG->dbprefix}friends where user_id = {$friend_of} and friend_id = {$user_id} and site_id = {$site}")) { + return true; + } + return false; + } + + /** + * Get a user's friends + * + * @param int $user_id The user ID + * @param int $site The site ID + * @return unknown + */ + + function get_friends($user_id, $site) { + + global $CONFIG; + $user_id = (int) $user_id; + $site = (int) $site; + return get_data("SELECT u.* FROM {$CONFIG->dbprefix}friends f LEFT JOIN {$CONFIG->dbprefix}users u on u.id = f.friend_id WHERE user_id = {$user_id} and site_id = {$site}","row_to_elgguser"); + + } + + /** + * Return friend IDs in a way suitable for including in a 'where' statement. + * + * @param unknown_type $user_id + * @return unknown + */ + function get_friends_where($user_id, $site) { + global $CONFIG; + $friendswhere = ""; + if ($friends = get_friends($user_id,1,999,$site)) { + $friendswhere .= $user_id; + foreach($friends as $friend) { + if (!empty($friendswhere)) $friendswhere .= ","; + $friendswhere .= $friend; + } + } + if (empty($friendswhere)) { + $friendswhere = "-1"; + } + return "({$friendswhere})"; + } + + /** + * Get all the people who have befriended friend_id + * + * @param $friend_id int User ID of the user in question + * @param $degree int + * + * @return array of ElggUsers + */ + + function get_friends_reverse($friend_id, $site) + { + global $CONFIG; + $friend_id = (int) $friend_id; + $site = (int) $site; + return get_data("SELECT u.* FROM {$CONFIG->dbprefix}friends f LEFT JOIN {$CONFIG->dbprefix}users u on u.id = f.user_id WHERE friend_id = {$friend_id} and site_id = {$site}","row_to_elgguser"); + } + + /** + * Returns a stirng of reverse friend IDs suitable for dropping into an SQL statement + * + * @param unknown_type $user_id + * @return unknown + */ + function get_friends_reverse_where($user_id, $site) { + + $friendswhere = ""; + if ($friends = get_friends_reverse($user_id,$site)) { + $friendswhere .= $user_id; + foreach($friends as $friend) { + if (!empty($friendswhere)) $friendswhere .= ","; + $friendswhere .= $friend; + } + } + if (empty($friendswhere)) { + $friendswhere = "-1"; + } + return "({$friendswhere})"; + } + + /** + * Get all the mutual friends of $user_id + * + * @param $user_id int User ID of the user in question + * @param $site int Optionally, the ID of the current site + * + * @return array of ElggUsers + */ + + function get_mutual_friends($user_id, $site) { + + global $CONFIG; + + $friend_id = (int) $user_id; + $friends = get_friends_where($user_id, $site); + + return get_data("SELECT u.* FROM {$CONFIG->dbprefix}friends f LEFT JOIN {$CONFIG->dbprefix}users u on u.id = f.user_id WHERE friend_id = {$friend_id} and site_id = {$site} and user_id in {$friends}","row_to_elgguser"); + + } + + /** + * Returns a stirng of mutual friend IDs suitable for dropping into an SQL statement + * + * @param unknown_type $user_id + * @return unknown + */ + function get_mutual_friends_where($user_id, $site) { + + $friendswhere = ""; + if ($friends = get_mutual_friends($user_id, $site)) { + $friendswhere .= $user_id; + foreach($friends as $friend) { + if (!empty($friendswhere)) $friendswhere .= ","; + $friendswhere .= $friend; + } + } + return "({$friendswhere})"; + + } + + +?> \ No newline at end of file -- cgit v1.2.3