social.lib.php 14 KB

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