social.lib.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. define(USERUNKNOW,0);
  3. define(SOCIALUNKNOW,1);
  4. define(SOCIALPARENT,2);
  5. define(SOCIALFRIEND,3);
  6. define(SOCIALGOODFRIEND,4);
  7. define(SOCIALENEMY,5);
  8. define(SOCIALDELETED,6);
  9. class UserFriend extends UserManager {
  10. function __construct() {
  11. }
  12. /**
  13. * Allow to register contact to social network
  14. *@author isaac flores paz <isaac.flores@dokeos.com>
  15. *@param int user friend id
  16. *@param int user id
  17. *@param int kind of relation between users
  18. *@return void
  19. */
  20. public function register_friend ($friend_id,$my_user_id,$relation_type) {
  21. $tbl_my_friend = Database :: get_main_table(TABLE_MAIN_USER_FRIEND);
  22. $sql = 'SELECT COUNT(*) as count FROM ' . $tbl_my_friend . ' WHERE friend_user_id=' . Database::escape_string($friend_id).' AND user_id='.Database::escape_string($my_user_id);
  23. $result = api_sql_query($sql, __FILE__, __LINE__);
  24. $row = Database :: fetch_array($result, 'ASSOC');
  25. if ($row['count'] == 0) {
  26. $sql_i = 'INSERT INTO ' . $tbl_my_friend . '(friend_user_id,user_id,relation_type)values(' . Database::escape_string($friend_id) . ','.Database::escape_string($my_user_id).','.Database::escape_string($relation_type).');';
  27. api_sql_query($sql_i, __FILE__, __LINE__);
  28. } else {
  29. $sql = 'SELECT COUNT(*) as count FROM ' . $tbl_my_friend . ' WHERE friend_user_id=' . Database::escape_string($friend_id) . ' AND user_id='.Database::escape_string($my_user_id);
  30. $result = api_sql_query($sql, __FILE__, __LINE__);
  31. $row = Database :: fetch_array($result, 'ASSOC');
  32. if ($row['count'] == 1) {
  33. $sql_i = 'UPDATE ' . $tbl_my_friend . ' SET relation_type='.$relation_type.' WHERE friend_user_id=' . Database::escape_string($friend_id).' AND user_id='.Database::escape_string($my_user_id);
  34. api_sql_query($sql_i, __FILE__, __LINE__);
  35. }
  36. }
  37. }
  38. /**
  39. * Allow to delete contact to social network
  40. *@author isaac flores paz <isaac.flores@dokeos.com>
  41. *@param int user friend id
  42. *@return void
  43. */
  44. public function removed_friend ($friend_id) {
  45. $tbl_my_friend = Database :: get_main_table(TABLE_MAIN_USER_FRIEND);
  46. $tbl_my_message = Database :: get_main_table(TABLE_MAIN_MESSAGE);
  47. $user_id=api_get_user_id();
  48. $sql = 'SELECT COUNT(*) as count FROM ' . $tbl_my_friend . ' WHERE user_id=' . Database::escape_string($user_id) . ' AND relation_type<>6 AND friend_user_id='.Database::escape_string($friend_id);
  49. $result = api_sql_query($sql, __FILE__, __LINE__);
  50. $row = Database :: fetch_array($result, 'ASSOC');
  51. if ($row['count'] == 1) {
  52. $sql_i = 'UPDATE ' . $tbl_my_friend . ' SET relation_type=6 WHERE user_id=' . Database::escape_string($user_id).' AND friend_user_id='.Database::escape_string($friend_id);
  53. $sql_j = 'UPDATE ' . $tbl_my_message . ' SET msg_status=7 WHERE user_receiver_id=' . Database::escape_string($user_id).' AND user_sender_id='.Database::escape_string($friend_id);
  54. api_sql_query($sql_i, __FILE__, __LINE__);
  55. api_sql_query($sql_j, __FILE__, __LINE__);
  56. }
  57. }
  58. /**
  59. * Allow to see contacts list
  60. * @author isaac flores paz <florespaz@bidsoftperu.com>
  61. * @return array
  62. */
  63. public function show_list_type_friends () {
  64. $friend_relation_list=array();
  65. $count_list=0;
  66. $tbl_my_friend_relation_type = Database :: get_main_table(TABLE_MAIN_USER_FRIEND_RELATION_TYPE);
  67. $sql='SELECT id,title FROM '.$tbl_my_friend_relation_type.' WHERE id<>6 ORDER BY id ASC';
  68. $result=api_sql_query($sql,__FILE__,__LINE__);
  69. while ($row=Database::fetch_array($result,'ASSOC')) {
  70. $friend_relation_list[]=$row;
  71. }
  72. $count_list=count($friend_relation_list);
  73. if ($count_list==0) {
  74. $friend_relation_list[]=get_lang('UnkNow');
  75. } else {
  76. return $friend_relation_list;
  77. }
  78. }
  79. /**
  80. * Get relation type contact by name
  81. * @author isaac flores paz <florespaz@bidsoftperu.com>
  82. * @param string names of the kind of relation
  83. * @return int
  84. */
  85. public function get_relation_type_by_name ($relation_type_name) {
  86. $list_type_friend=array();
  87. $list_type_friend=self::show_list_type_friends();
  88. foreach ($list_type_friend as $value_type_friend) {
  89. if (strtolower($value_type_friend['title'])==$relation_type_name) {
  90. return $value_type_friend['id'];
  91. }
  92. }
  93. }
  94. /**
  95. * Get the kind of relation between contacts
  96. * @author isaac flores paz <florespaz@bidsoftperu.com>
  97. * @param int user id
  98. * @param int user friend id
  99. * @param string
  100. */
  101. public function get_relation_between_contacts ($user_id,$user_friend) {
  102. $tbl_my_friend_relation_type = Database :: get_main_table(TABLE_MAIN_USER_FRIEND_RELATION_TYPE);
  103. $tbl_my_friend = Database :: get_main_table(TABLE_MAIN_USER_FRIEND);
  104. $sql= 'SELECT rt.id as id FROM '.$tbl_my_friend_relation_type.' rt ' .
  105. 'WHERE rt.id=(SELECT uf.relation_type FROM '.$tbl_my_friend.' uf WHERE user_id='.Database::escape_string($user_id).' AND friend_user_id='.Database::escape_string($user_friend).')';
  106. $res=api_sql_query($sql,__FILE__,__LINE__);
  107. $row=Database::fetch_array($res,'ASSOC');
  108. if (Database::num_rows($res)>0) {
  109. return $row['id'];
  110. } else {
  111. return USERUNKNOW;
  112. }
  113. }
  114. /**
  115. * get contacts id list
  116. * @author isaac flores paz <florespaz@bidsoftperu.com>
  117. * @param int user id
  118. * @param int group id
  119. * @param string name to search
  120. * @return array
  121. */
  122. public function get_list_id_friends_by_user_id ($user_id,$id_group=null,$search_name=null) {
  123. $list_ids_friends=array();
  124. $tbl_my_friend = Database :: get_main_table(TABLE_MAIN_USER_FRIEND);
  125. $tbl_my_user = Database :: get_main_table(TABLE_MAIN_USER);
  126. $sql='SELECT friend_user_id FROM '.$tbl_my_friend.' WHERE relation_type<>6 AND friend_user_id<>'.Database::escape_string($user_id).' AND user_id='.Database::escape_string($user_id);
  127. if (isset($id_group) && $id_group>0) {
  128. $sql.=' AND relation_type='.$id_group;
  129. }
  130. if (isset($search_name) && is_string($search_name)===true) {
  131. $sql.=' AND friend_user_id IN (SELECT user_id FROM '.$tbl_my_user.' WHERE concat(firstName,lastName) like concat("%","'.Database::escape_string($search_name).'","%"));';
  132. }
  133. $res=api_sql_query($sql,__FILE__,__LINE__);
  134. while ($row=Database::fetch_array($res,'ASSOC')) {
  135. $list_ids_friends[]=$row;
  136. }
  137. return $list_ids_friends;
  138. }
  139. /**
  140. * get list web path of contacts by user id
  141. * @author isaac flores paz <florespaz@bidsoftperu.com>
  142. * @param int user id
  143. * @param int group id
  144. * @param string name to search
  145. * @param array
  146. */
  147. public function get_list_path_web_by_user_id ($user_id,$id_group=null,$search_name=null) {
  148. $list_paths=array();
  149. $list_path_friend=array();
  150. $array_path_user=array();
  151. $combine_friend = array();
  152. $list_ids = self::get_list_id_friends_by_user_id ($user_id,$id_group,$search_name);
  153. if (is_array($list_ids)) {
  154. foreach ($list_ids as $values_ids) {
  155. $list_path_image_friend[] = UserManager::get_user_picture_path_by_id($values_ids['friend_user_id'],'web',false,true);
  156. $combine_friend=array('id_friend'=>$list_ids,'path_friend'=>$list_path_image_friend);
  157. }
  158. }
  159. return $combine_friend;
  160. }
  161. /**
  162. * get web path of user invitate
  163. * @author isaac flores paz <florespaz@bidsoftperu.com>
  164. * @param int user id
  165. * @return array
  166. */
  167. public function get_list_web_path_user_invitation_by_user_id ($user_id) {
  168. $list_paths=array();
  169. $list_path_friend=array();
  170. $list_ids = self::get_list_invitation_of_friends_by_user_id(Database::escape_string($user_id));
  171. foreach ($list_ids as $values_ids) {
  172. $list_path_image_friend[] = UserManager::get_user_picture_path_by_id($values_ids['user_sender_id'],'web',false,true);
  173. }
  174. return $list_path_image_friend;
  175. }
  176. /**
  177. * allow to sent an invitation to my contacts
  178. * @author isaac flores paz <florespaz@bidsoftperu.com>
  179. * @param int user id
  180. * @param int user friend id
  181. * @param string title of the message
  182. * @param string content of the message
  183. * @return boolean
  184. */
  185. public function send_invitation_friend ($user_id,$friend_id,$message_title,$message_content) {
  186. $tbl_message=Database::get_main_table(TABLE_MAIN_MESSAGE);
  187. $current_date=date('Y-m-d H:i:s',time());
  188. $status_invitation=5;//status of pending invitation
  189. $sql_exist='SELECT COUNT(*) AS count FROM '.$tbl_message.' WHERE user_sender_id='.Database::escape_string($user_id).' AND user_receiver_id='.Database::escape_string($friend_id).' AND msg_status IN(5,6,7);';
  190. $res_exist=api_sql_query($sql_exist,__FILE__,__LINE__);
  191. $row_exist=Database::fetch_array($res_exist,'ASSOC');
  192. if ($row_exist['count']==0) {
  193. $sql='INSERT INTO '.$tbl_message.'(user_sender_id,user_receiver_id,msg_status,send_date,title,content) VALUES('.Database::escape_string($user_id).','.Database::escape_string($friend_id).','.Database::escape_string($status_invitation).',"'.$current_date.'","'.$message_title.'","'.$message_content.'")';
  194. api_sql_query($sql,__FILE__,__LINE__);
  195. return true;
  196. } elseif($row_exist['count']==1) {
  197. $sql_if_exist='SELECT COUNT(*) AS count FROM '.$tbl_message.' WHERE user_sender_id='.Database::escape_string($user_id).' AND user_receiver_id='.Database::escape_string($friend_id).' AND msg_status=7';
  198. $res_if_exist=api_sql_query($sql_if_exist,__FILE__,__LINE__);
  199. $row_if_exist=Database::fetch_array($res_if_exist,'ASSOC');
  200. if ($row_if_exist['count']==1) {
  201. $sql_if_exist_up='UPDATE '.$tbl_message.'SET msg_status=5 WHERE user_sender_id='.Database::escape_string($user_id).' AND user_receiver_id='.Database::escape_string($friend_id).';';
  202. api_sql_query($sql_if_exist_up,__FILE__,__LINE__);
  203. return true;
  204. } else {
  205. return false;
  206. }
  207. } else {
  208. return false;
  209. }
  210. }
  211. /**
  212. * Get number messages of the inbox
  213. * @author isaac flores paz <florespaz@bidsoftperu.com>
  214. * @param int user receiver id
  215. * @return int
  216. */
  217. public function get_message_number_invitation_by_user_id ($user_receiver_id) {
  218. $status_invitation=5;//status of pending invitation
  219. $tbl_message=Database::get_main_table(TABLE_MAIN_MESSAGE);
  220. $sql='SELECT COUNT(*) as count_message_in_box FROM '.$tbl_message.' WHERE user_receiver_id='.Database::escape_string($user_receiver_id).' AND msg_status=5;';
  221. $res=api_sql_query($sql,__FILE__,__LINE__);
  222. $row=Database::fetch_array($res,'ASSOC');
  223. return $row['count_message_in_box'];
  224. }
  225. /**
  226. * get invitation list by user id
  227. * @author isaac flores paz <florespaz@bidsoftperu.com>
  228. * @param int user id
  229. * @return array()
  230. */
  231. public function get_list_invitation_of_friends_by_user_id ($user_id) {
  232. $list_friend_invitation=array();
  233. $tbl_message=Database::get_main_table(TABLE_MAIN_MESSAGE);
  234. $sql='SELECT user_sender_id,send_date,title,content FROM '.$tbl_message.' WHERE user_receiver_id='.Database::escape_string($user_id).' AND msg_status=5;';
  235. $res=api_sql_query($sql,__FILE__,__LINE__);
  236. while ($row=Database::fetch_array($res,'ASSOC')) {
  237. $list_friend_invitation[]=$row;
  238. }
  239. return $list_friend_invitation;
  240. }
  241. /**
  242. * allow accept invitation
  243. * @author isaac flores paz <florespaz@bidsoftperu.com>
  244. * @param int user sender id
  245. * @param int user receiver id
  246. * @return void()
  247. */
  248. public function invitation_accepted ($user_send_id,$user_receiver_id) {
  249. $tbl_message=Database::get_main_table(TABLE_MAIN_MESSAGE);
  250. $msg_status=6;// friend accepted
  251. $sql='UPDATE '.$tbl_message.' SET msg_status='.$msg_status.' WHERE user_sender_id='.Database::escape_string($user_send_id).' AND user_receiver_id='.Database::escape_string($user_receiver_id).';';
  252. api_sql_query($sql,__FILE__,__LINE__);
  253. }
  254. /**
  255. * allow deny invitation
  256. * @author isaac flores paz <florespaz@bidsoftperu.com>
  257. * @param int user sender id
  258. * @param int user receiver id
  259. * @return void()
  260. */
  261. public function invitation_denied($user_send_id,$user_receiver_id) {
  262. $tbl_message=Database::get_main_table(TABLE_MAIN_MESSAGE);
  263. $msg_status=7;
  264. $sql='UPDATE '.$tbl_message.' SET msg_status='.$msg_status.' WHERE user_sender_id='.Database::escape_string($user_send_id).' AND user_receiver_id='.Database::escape_string($user_receiver_id).';';
  265. api_sql_query($sql,__FILE__,__LINE__);
  266. }
  267. /**
  268. * allow attach to group
  269. * @author isaac flores paz <florespaz@bidsoftperu.com>
  270. * @param int user to qualify
  271. * @param int kind of rating
  272. * @return void()
  273. */
  274. public function qualify_friend($id_friend_qualify,$type_qualify) {
  275. $tbl_user_friend=Database::get_main_table(TABLE_MAIN_USER_FRIEND);
  276. $user_id=api_get_user_id();
  277. $sql='UPDATE '.$tbl_user_friend.' SET relation_type='.$type_qualify.' WHERE user_id='.Database::escape_string($user_id).' AND friend_user_id='.Database::escape_string($id_friend_qualify).';';
  278. api_sql_query($sql,__FILE__,__LINE__);
  279. }
  280. /**
  281. * Send invitation a your friends
  282. * @author Isaac Flores Paz <isaac.flores.paz@gmail.com>
  283. * @param void
  284. * @return string message invitation
  285. */
  286. function send_invitation_friend_user($userfriend_id,$subject_message='',$content_message='') {
  287. //$id_user_friend=array();
  288. $user_info=array();
  289. $user_info=api_get_user_info($userfriend_id);
  290. $succes=get_lang('MessageSentTo');
  291. $succes.= ' : '.$user_info['firstName'].' '.$user_info['lastName'];
  292. if (isset($subject_message) && isset($content_message) && isset($userfriend_id)) {
  293. MessageManager::send_message(Database::escape_string($userfriend_id),Database::escape_string($subject_message), Database::escape_string($content_message));
  294. Display::display_confirmation_message($succes,true);
  295. exit;
  296. } elseif(isset($userfriend_id) && !isset($subject_message)) {
  297. $count_is_true=false;
  298. $count_number_is_true=0;
  299. if (isset($userfriend_id) && $userfriend_id>0) {
  300. $user_info=array();
  301. $user_id=api_get_user_id();
  302. $user_info=api_get_user_info($user_id);
  303. $message_title=get_lang('Invitation');
  304. $message_content=$content_message;
  305. $count_is_true=self::send_invitation_friend(Database::escape_string($user_id),Database::escape_string($userfriend_id),Database::escape_string($message_title),Database::escape_string($message_content));
  306. if ($count_is_true) {
  307. echo Display::display_normal_message(get_lang('InvitationHasBeenSent'));
  308. }else {
  309. echo Display::display_error_message(get_lang('InvitationHasBeenNotSent'));
  310. }
  311. }
  312. }
  313. }
  314. }
  315. ?>