2 /****************************************************************/
4 /****************************************************************/
5 /* Copyright (c) 2002-2009 */
6 /* Adaptive Technology Resource Centre / University of Toronto */
9 /* This program is free software. You can redistribute it and/or*/
10 /* modify it under the terms of the GNU General Public License */
11 /* as published by the Free Software Foundation. */
12 /****************************************************************/
15 define('AT_INCLUDE_PATH', '../../include/');
16 require_once(AT_SOCIAL_INCLUDE.'classes/Activity.class.php');
17 require_once(AT_SOCIAL_INCLUDE.'classes/Member.class.php');
20 * Get all the connections from the given member id
21 * $obj->url should retrieve the member's profile link.
23 * @param int the person who we want to get friends from
24 * @param int the max number of friends to be returned
26 * TODO: Need optimization, too slow.
28 function getFriends($member_id, $limit=0){
29 global $db, $addslashes;
32 //All member_id = member_id, and All friend_id = member_id
33 $sql = 'SELECT F.member_id AS member_id, F.friend_id AS friend_id FROM '.TABLE_PREFIX.'social_friends F LEFT JOIN '.TABLE_PREFIX.'members M ON F.friend_id=M.member_id WHERE (F.member_id='.$member_id.' OR F.friend_id='.$member_id.')';
35 $sql .= ' ORDER BY RAND() LIMIT '.$limit;
37 $result = mysql_query($sql, $db);
40 while ($row = mysql_fetch_assoc($result)){
41 if ($row['member_id']==$member_id){
42 //member_id=member_id case
43 $friends[$row['friend_id']] = $row['friend_id'];
44 // $friends[$row['member_id']]['first_name'] = $row['first_name'];
45 // $friends[$row['member_id']]['last_name'] = $row['last_name'];
47 //friend_id = member_id
48 $friends[$row['member_id']] = $row['member_id'];
49 // $friends[$row['friend_id']]['first_name'] = $row['first_name'];
50 // $friends[$row['friend_id']]['last_name'] = $row['last_name'];
55 //All friend_id = member_id
56 $sql = 'SELECT F.member_id AS member_id, F.friend_id AS friend_id FROM '.TABLE_PREFIX.'social_friends F LEFT JOIN '.TABLE_PREFIX.'members M ON F.friend_id=M.member_id WHERE F.friend_id='.$member_id;
57 $result = mysql_query($sql, $db);
58 while ($row = mysql_fetch_assoc($result)){
59 $friends[$row['member_id']] = new Member($row['member_id']);
60 // $friends[$row['friend_id']]['first_name'] = $row['first_name'];
61 // $friends[$row['friend_id']]['last_name'] = $row['last_name'];
69 * Decide rather these two people are strictly friend of friend. If they are already friends, return false.
71 * @param int person A's member_id
72 * @param int person B's member_id
73 * @return true if they are friend of friend.
75 function isFriendOfFriend($member_a, $member_b){
76 $member_a = intval($member_a);
77 $member_b = intval($member_b);
78 $friends_of_a = getFriends($member_a);
80 //if these two are already friends
81 if(isset($friends_of_a[$member_b])){
85 $friends_of_b = getFriends($member_b);
86 $fof = array_intersect($friends_of_a, $friends_of_b); //friends of friends
88 //If it is not empty or not null, then they have friends
89 if (!empty($fof) > 0){
97 * Get a list of people you may know
99 function getPeopleYouMayKnow(){
102 $people_you_may_know = array();
103 $pending_requests = getPendingRequests(true);
105 $sql = 'SELECT MAX(member_id) FROM '.TABLE_PREFIX.'members';
106 $result = mysql_query($sql, $db);
108 list($max_id) = mysql_fetch_array($result);
113 //if we ran out of people, quit;
114 while($counter++ < $max_id){
115 //if we get enough people we might know, quit;
116 if (sizeof($people_you_may_know) >= SOCIAL_NUMBER_OF_PEOPLE_YOU_MAY_KNOW){
119 //get new random member
120 $random_member = rand(1, $max_id); //seed is generated automatically since php 4.2.0
122 //if this person is myself, next
123 if ($random_member==$_SESSION['member_id']){
127 //if this person is already on pending, next.
128 if (isset($pending_requests[$random_member])){
133 //if we have added this random number before, next.
134 if (in_array($random_member, $people_you_may_know)){
138 if (isFriendOfFriend($_SESSION['member_id'], $random_member)){
139 $people_you_may_know[] = $random_member;
142 return $people_you_may_know;
147 * Returns a list of friend requests to me/by me depends on the flag
148 * @param flag, true for requests made by me, false for requests made to me. DEFAULT: false
149 * @return array of friend requests
151 function getPendingRequests($request_by_me=false){
153 /* NOTE: This table is not bilinear, unlike the friends table.
154 * In this table, please do not be confused, member_id is the one who requests
155 * friend_id is the member that needs to approve/reject. Thus, when we want to retrieve
156 * pending requests, we need to pull entries from friend_id.
160 if ($request_by_me==true) {
161 $sql = 'SELECT friend_id AS id FROM '.TABLE_PREFIX.'social_friend_requests WHERE member_id='.$_SESSION['member_id'];
163 $sql = 'SELECT member_id AS id FROM '.TABLE_PREFIX.'social_friend_requests WHERE friend_id='.$_SESSION['member_id'];
165 $rs = mysql_query($sql, $db);
167 //myself=> pending objs
168 while($row = mysql_fetch_assoc($rs)){
169 $requests[$row['id']] = new Member($row['id']);
176 * This function adds a friend to the database, remove the equivalent friend request.
178 * @param int the member being approved, not "MYSELF"
180 function approveFriendRequest($friend_id){
182 $friend_id = intval($friend_id);
187 //TODO: hardcoded relationship = 1
188 $sql = "INSERT INTO ".TABLE_PREFIX."social_friends SET member_id=$_SESSION[member_id], friend_id=$friend_id, relationship=1";
189 $is_succeeded = mysql_query($sql, $db);
190 //remove the equivalent friend request
192 $is_succeeded = removeFriendRequest($friend_id, $_SESSION['member_id']);
195 //add to activities log
197 $act = new Activity();
198 $str1 = _AT('now_friends1', printSocialName($friend_id));
199 $act->addActivity($_SESSION['member_id'], $str1);
200 $str2 = _AT('now_friends2', printSocialName($_SESSION['member_id']));
201 $act->addActivity($friend_id, $str2);
208 * Reject friend request
210 * @param int the member being rejected, not "MYSELF"
212 function rejectFriendRequest($friend_id){
214 return removeFriendRequest($friend_id, $_SESSION['member_id']);
219 * Remove Friend request
220 * @param member_id the one who make this request
221 * @param friend_id the member that decide approval/reject on this request
223 function removeFriendRequest($member_id, $friend_id){
225 $sql = 'DELETE FROM '.TABLE_PREFIX."social_friend_requests WHERE member_id=$member_id AND friend_id=$friend_id";
226 $is_succeeded = mysql_query($sql, $db);
227 return $is_succeeded;
232 * This function adds a friend request to the database
234 * @param friend_id the member_id of the friend
236 function addFriendRequest($friend_id){
238 $friend_id = intval($friend_id);
244 $sql = "INSERT INTO ".TABLE_PREFIX."social_friend_requests SET member_id=$_SESSION[member_id], friend_id=$friend_id";
245 mysql_query($sql, $db);
250 * This function removes a friend from the the user.
254 function removeFriend($friend_id){
256 $friend_id = intval($friend_id);
258 $sql = 'DELETE FROM '.TABLE_PREFIX.'social_friends WHERE (member_id='.$_SESSION['member_id'].' AND '.'friend_id='.$friend_id.') OR (friend_id='.$_SESSION['member_id'].' AND '.'member_id='.$friend_id.')';
259 mysql_query($sql, $db);
264 * This function will return a list of people from the network with the given name.
265 * @param string to be searched in the members table, can have space.
266 * @param [OPTIONAL] boolean if true, will search only within this member.
267 * @return array of friends of this member; id=>[first name, last name, profile picture]
269 * TODO: search needs work. Order by the most matches to the least matches
271 function searchFriends($name, $searchMyFriends = false, $offset=-1){
272 global $db, $addslashes;
274 $my_friends = array();
275 $exact_match = false;
277 //break the names by space, then accumulate the query
278 if (preg_match("/^\\\\?\"(.*)\\\\?\"$/", $name, $matches)){
282 $name = $addslashes($name);
283 $sub_names = explode(' ', $name);
284 foreach($sub_names as $piece){
289 //if there are 2 double quotes around a search phrase, then search it as if it's "first_name last_name".
290 //else, match any contact in the search phrase.
292 $match_piece = "= '$piece' ";
294 $match_piece = "LIKE '%$piece%' ";
296 $query .= "(first_name $match_piece OR second_name $match_piece OR last_name $match_piece OR email LIKE '$piece') AND ";
298 //trim back the extra "AND "
299 $query = substr($query, 0, -4);
301 //Check if this is a search on all people
302 if ($searchMyFriends == true){
303 //If searchMyFriend is true, return the "my friends" array
304 //If the member_id is empty, (this happens when we are doing a search without logging in) then get all members?
305 //else, use "my friends" array to distinguish which of these are already in my connection
306 if(!isset($_SESSION['member_id'])){
307 $sql = 'SELECT member_id FROM '.TABLE_PREFIX.'members WHERE ';
309 $sql = 'SELECT F.* FROM '.TABLE_PREFIX.'social_friends F LEFT JOIN '.TABLE_PREFIX.'members M ON F.friend_id=M.member_id WHERE (F.member_id='.$_SESSION['member_id'].') AND ';
312 $sql .= 'SELECT F.* FROM '.TABLE_PREFIX.'social_friends F LEFT JOIN '.TABLE_PREFIX.'members M ON F.member_id=M.member_id WHERE (F.friend_id='.$_SESSION['member_id'].') AND ';
316 $rs = mysql_query($sql, $db);
318 while ($row = mysql_fetch_assoc($rs)){
319 if ($row['member_id']==$_SESSION['member_id']){
320 $this_id = $row['friend_id'];
322 $this_id = $row['member_id'];
324 $temp =& $my_friends[$this_id];
325 $temp['obj'] = new Member($this_id);
326 if ($searchMyFriends){
331 unset($this_id); //don't want the following statements to reuse this
336 * IF the 'search my friend' is off, then it should search all members inside that table
337 * don't know why i did the search inside [friends x members]
338 * Also this query is gonna pull out all members cept 'myself'
339 * raised a small problem for public use, cause there is no member_id
342 //$sql = 'SELECT * FROM '.TABLE_PREFIX.'social_friends F LEFT JOIN '.TABLE_PREFIX.'members M ON F.friend_id=M.member_id WHERE ';
343 $sql = 'SELECT * FROM '.TABLE_PREFIX.'members M WHERE ';
344 if (isset($_SESSION['member_id'])){
345 $sql .= 'member_id!='.$_SESSION['member_id'].' AND ';
348 $sql = $sql . $query;
350 $sql .= " LIMIT $offset, ". SOCIAL_FRIEND_SEARCH_MAX;
352 $rs = mysql_query($sql, $db);
354 //Get all members out
355 while($row = mysql_fetch_assoc($rs)){
356 // if ($row['member_id']==$_SESSION['member_id']){
357 // $this_id = $row['friend_id'];
359 $this_id = $row['member_id'];
362 //skip empty entry, don't know why there would be empty entry.
363 //TODO: Trace this. could be a bug in query
368 $temp =& $result[$this_id];
369 $temp['id'] = $this_id;
370 // $temp['first_name'] = $row['first_name'];
371 // $temp['last_name'] = $row['last_name'];
373 //if this person exists in "my friends" list, mark it.
374 if (isset($my_friends[$this_id])){
383 * Given an array list of friends, this function will add an attribute 'added' into the array if this person is already connected to the user.
384 * @param int the user id
385 * @param array the given array of friends
386 * @return marked array
388 function markFriends($id, $connections){
390 $my_friends = getFriends($id);
391 $pending_requests = getPendingRequests(true);
393 foreach($my_friends as $friends){
394 //if it is in the connection, set the attribute
395 if($connections[$friends] != null){
396 $connections[$friends]['added'] = 1;
400 foreach ($pending_requests as $friends=>$garbage){
401 //if it is already added, set pending =1
402 if ($connections[$friends] != null){
403 $connections[$friends]['pending'] = 1;
411 * Invite other members
412 * @param int The member that we are going to invite
413 * @param int The group id in which we are inviting the person to.
415 function addGroupInvitation($member_id, $group_id){
418 $sql = 'INSERT INTO '.TABLE_PREFIX."social_groups_invitations (sender_id, group_id, member_id) VALUES ($_SESSION[member_id], $group_id, $member_id)";
420 $result = mysql_query($sql, $db);
429 * Get invitation from "ME", which is the logged in person
430 * @return list of groups id + sender_id
432 function getGroupInvitations(){
436 $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_groups_invitations WHERE member_id='.$_SESSION['member_id'];
438 $result = mysql_query($sql, $db);
440 while ($row = mysql_fetch_assoc($result)){
441 $inv[$row['group_id']][] = $row['sender_id'];
450 * Get group requests for "ME", which is the logged in person.
451 * @return list of groups id + sender_id
453 function getGroupRequests(){
457 $sql = 'SELECT * FROM '.TABLE_PREFIX.'social_groups_requests WHERE member_id='.$_SESSION['member_id'];
459 $result = mysql_query($sql, $db);
461 while ($row = mysql_fetch_assoc($result)){
462 $requests[$row['group_id']][] = $row['sender_id'];
471 * Accept "my" group invitation
472 * @param int group id
473 * @param int sender's member_id
475 function acceptGroupInvitation($group_id){
478 //will only add member if the group_id is valid.
483 $sg = new SocialGroup($group_id);
484 $isSucceeded = $sg->addMember($_SESSION['member_id']);
487 removeGroupInvitation($group_id);
493 * Reject "my" group invitation
495 function rejectGroupInvitation($group_id){
496 return removeGroupInvitation($group_id);
502 * Remove "my" group invitation
503 * @param int group id
505 function removeGroupInvitation($group_id){
507 $group_id = intval($group_id);
509 //delete invitation based on 3 primary keys
510 // $sql = 'DELETE FROM '.TABLE_PREFIX."social_groups_invitations WHERE group_id=$group_id AND sender_id=$sender_id AND member_id=$member_id";
511 //doesn't need sender_id cause we want to remove all anyway.
512 $sql = 'DELETE FROM '.TABLE_PREFIX."social_groups_invitations WHERE group_id=$group_id AND member_id=$_SESSION[member_id]";
513 $result = mysql_query($sql, $db);
522 * Accept the group request
523 * @param int group id
524 * @param int member that made this request
526 function acceptGroupRequest($group_id, $sender_id){
529 //will only add member if the group_id is valid.
534 $sg = new SocialGroup($group_id);
535 $isSucceeded = $sg->addMember($sender_id);
538 removeGroupRequest($group_id, $sender_id);
544 * Reject the group request
545 * @param int group id
546 * @param int member that made this request
548 function rejectGroupRequest($group_id, $sender_id) {
549 return removeGroupRequest($group_id, $sender_id);
555 * Remove group requests
556 * @param int group id
557 * @param int member that made this request
559 function removeGroupRequest($group_id, $sender_id){
561 $group_id = intval($group_id);
562 $sender_id = intval($sender_id);
564 $sql = 'DELETE FROM '.TABLE_PREFIX."social_groups_requests WHERE group_id=$group_id AND member_id=$_SESSION[member_id] AND sender_id=$sender_id";
566 $result = mysql_query($sql, $db);
575 * Print social name, with AT_print and profile link
576 * @param int member id
577 * @param link will return a hyperlink when set to true
578 * return the name to be printed.
580 function printSocialName($id, $link=true){
581 $str .= AT_print(get_display_name($id), 'members.full_name');
583 return getProfileLink($id, $str);
590 * Mimic vital's print_profile_img function, but with a more customized image definition
591 * @param int the member id
592 * @param 1 for thumbnail, 2 for profile
593 * @param true will return a href link to the profile page, false otherwise
594 * @return the profile image link
596 function printSocialProfileImg($id, $type=1, $link=true) {
597 global $moduleFactory;
599 $mod = $moduleFactory->getModule('_standard/profile_pictures');
600 if ($mod->isEnabled() === FALSE) {
603 if (profile_image_exists($id)) {
605 $str = '<img src="get_profile_img.php?id='.$id.'" alt="" />';
607 $str = '<img src="get_profile_img.php?id='.$id.SEP.'size=p" alt="" />';
610 $str = '<img src="'.AT_SOCIAL_BASENAME.'images/nophoto.gif" alt="" />';
615 return getProfileLink($id, $str);
620 * Generate the a href link to the designated profile
621 * For now, it's to sprofile, but in the future, we want ATutor + Opensocial to have only 1 profile, which should
622 * point to the default atutor profile page.
625 * @param int member id
626 * @param string Image tag, or any string
627 * @return the hyperlink to the profile
629 function getProfileLink($id, $str){
630 $link = '<a href="'.url_rewrite(AT_SOCIAL_BASENAME.'sprofile.php?id='.$id).'">';
637 * Convert all input to htmlentities output, in UTF-8.
638 * @param string input to be convert
639 * @param boolean true if we wish to change all carrier returns to a <br/> tag, false otherwise.
641 function htmlentities_utf8($str, $use_nl2br=true){
642 $return = htmlentities($str, ENT_QUOTES, 'UTF-8');
644 return nl2br($return);
650 * This function will return a list of the member's activities
651 * @param member_id The id of the member we wish to get the activities from.
653 function getMemberActivities($member_id){