social.lib.php 14 KB

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