social.lib.php 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Zend\Feed\Reader\Reader;
  4. use Zend\Feed\Reader\Entry\Rss;
  5. /**
  6. * Class SocialManager
  7. *
  8. * This class provides methods for the social network management.
  9. * Include/require it in your code to use its features.
  10. *
  11. * @package chamilo.social
  12. */
  13. class SocialManager extends UserManager
  14. {
  15. /**
  16. * Constructor
  17. */
  18. public function __construct()
  19. {
  20. }
  21. /**
  22. * Allow to see contacts list
  23. * @author isaac flores paz
  24. * @return array
  25. */
  26. public static function show_list_type_friends()
  27. {
  28. $friend_relation_list = array();
  29. $table = Database::get_main_table(TABLE_MAIN_USER_FRIEND_RELATION_TYPE);
  30. $sql = 'SELECT id, title FROM '.$table.'
  31. WHERE id<>6
  32. ORDER BY id ASC';
  33. $result = Database::query($sql);
  34. while ($row = Database::fetch_array($result, 'ASSOC')) {
  35. $friend_relation_list[] = $row;
  36. }
  37. $count_list = count($friend_relation_list);
  38. if ($count_list == 0) {
  39. $friend_relation_list[] = get_lang('Unknown');
  40. } else {
  41. return $friend_relation_list;
  42. }
  43. }
  44. /**
  45. * Get relation type contact by name
  46. * @param string names of the kind of relation
  47. * @return int
  48. * @author isaac flores paz
  49. */
  50. public static function get_relation_type_by_name($relation_type_name)
  51. {
  52. $list_type_friend = self::show_list_type_friends();
  53. foreach ($list_type_friend as $value_type_friend) {
  54. if (strtolower($value_type_friend['title']) == $relation_type_name) {
  55. return $value_type_friend['id'];
  56. }
  57. }
  58. }
  59. /**
  60. * Get the kind of relation between contacts
  61. * @param int user id
  62. * @param int user friend id
  63. * @param string
  64. * @return int
  65. * @author isaac flores paz
  66. */
  67. public static function get_relation_between_contacts($user_id, $user_friend)
  68. {
  69. $table = Database::get_main_table(TABLE_MAIN_USER_FRIEND_RELATION_TYPE);
  70. $userRelUserTable = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  71. $sql = 'SELECT rt.id as id
  72. FROM '.$table.' rt
  73. WHERE rt.id = (
  74. SELECT uf.relation_type
  75. FROM '.$userRelUserTable.' uf
  76. WHERE
  77. user_id='.((int) $user_id).' AND
  78. friend_user_id='.((int) $user_friend).' AND
  79. uf.relation_type <> '.USER_RELATION_TYPE_RRHH.'
  80. LIMIT 1
  81. )';
  82. $res = Database::query($sql);
  83. if (Database::num_rows($res) > 0) {
  84. $row = Database::fetch_array($res, 'ASSOC');
  85. return $row['id'];
  86. } else {
  87. return USER_UNKNOWN;
  88. }
  89. }
  90. /**
  91. * Get count of friends from user
  92. *
  93. * @param int $userId
  94. * @return int
  95. */
  96. public static function getCountFriends($userId)
  97. {
  98. $tbl_my_friend = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  99. $userId = (int) $userId;
  100. if (empty($userId)) {
  101. return 0;
  102. }
  103. $sql = 'SELECT count(friend_user_id) count
  104. FROM '.$tbl_my_friend.'
  105. WHERE
  106. relation_type NOT IN ('.USER_RELATION_TYPE_DELETED.', '.USER_RELATION_TYPE_RRHH.') AND
  107. friend_user_id<>'.($userId).' AND
  108. user_id='.($userId);
  109. $res = Database::query($sql);
  110. if (Database::num_rows($res)) {
  111. $row = Database::fetch_array($res, 'ASSOC');
  112. return (int) $row['count'];
  113. }
  114. return 0;
  115. }
  116. /**
  117. * Gets friends id list
  118. * @param int user id
  119. * @param int group id
  120. * @param string name to search
  121. * @param bool true will load firstname, lastname, and image name
  122. * @return array
  123. * @author Julio Montoya <gugli100@gmail.com> Cleaning code, function renamed, $load_extra_info option added
  124. * @author isaac flores paz
  125. */
  126. public static function get_friends(
  127. $user_id,
  128. $id_group = null,
  129. $search_name = null,
  130. $load_extra_info = true
  131. ) {
  132. $list_ids_friends = array();
  133. $tbl_my_friend = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  134. $tbl_my_user = Database::get_main_table(TABLE_MAIN_USER);
  135. $sql = 'SELECT friend_user_id FROM '.$tbl_my_friend.'
  136. WHERE
  137. relation_type NOT IN ('.USER_RELATION_TYPE_DELETED.', '.USER_RELATION_TYPE_RRHH.') AND
  138. friend_user_id<>'.((int) $user_id).' AND
  139. user_id='.((int) $user_id);
  140. if (isset($id_group) && $id_group > 0) {
  141. $sql .= ' AND relation_type='.$id_group;
  142. }
  143. if (isset($search_name)) {
  144. $search_name = trim($search_name);
  145. $search_name = str_replace(' ', '', $search_name);
  146. $sql .= ' AND friend_user_id IN (
  147. SELECT user_id FROM '.$tbl_my_user.'
  148. WHERE
  149. firstName LIKE "%'.Database::escape_string($search_name).'%" OR
  150. lastName LIKE "%'.Database::escape_string($search_name).'%" OR
  151. '.(api_is_western_name_order() ? 'concat(firstName, lastName)' : 'concat(lastName, firstName)').' LIKE concat("%","'.Database::escape_string($search_name).'","%")
  152. ) ';
  153. }
  154. $res = Database::query($sql);
  155. while ($row = Database::fetch_array($res, 'ASSOC')) {
  156. if ($load_extra_info) {
  157. $my_user_info = api_get_user_info($row['friend_user_id']);
  158. $list_ids_friends[] = array(
  159. 'friend_user_id' => $row['friend_user_id'],
  160. 'firstName' => $my_user_info['firstName'],
  161. 'lastName' => $my_user_info['lastName'],
  162. 'username' => $my_user_info['username'],
  163. 'image' => $my_user_info['avatar'],
  164. 'user_info' => $my_user_info,
  165. );
  166. } else {
  167. $list_ids_friends[] = $row;
  168. }
  169. }
  170. return $list_ids_friends;
  171. }
  172. /**
  173. * get web path of user invitate
  174. * @author isaac flores paz
  175. * @author Julio Montoya setting variable array
  176. * @param int user id
  177. *
  178. * @return array
  179. */
  180. public static function get_list_web_path_user_invitation_by_user_id($user_id)
  181. {
  182. $list_ids = self::get_list_invitation_of_friends_by_user_id($user_id);
  183. $list = array();
  184. foreach ($list_ids as $values_ids) {
  185. $list[] = UserManager::get_user_picture_path_by_id(
  186. $values_ids['user_sender_id'],
  187. 'web'
  188. );
  189. }
  190. return $list;
  191. }
  192. /**
  193. * Sends an invitation to contacts
  194. * @param int user id
  195. * @param int user friend id
  196. * @param string title of the message
  197. * @param string content of the message
  198. * @return boolean
  199. * @author isaac flores paz
  200. * @author Julio Montoya <gugli100@gmail.com> Cleaning code
  201. */
  202. public static function send_invitation_friend(
  203. $user_id,
  204. $friend_id,
  205. $message_title,
  206. $message_content
  207. ) {
  208. $tbl_message = Database::get_main_table(TABLE_MESSAGE);
  209. $user_id = intval($user_id);
  210. $friend_id = intval($friend_id);
  211. //Just in case we replace the and \n and \n\r while saving in the DB
  212. $message_content = str_replace(array("\n", "\n\r"), '<br />', $message_content);
  213. $clean_message_content = Database::escape_string($message_content);
  214. $now = api_get_utc_datetime();
  215. $sql = 'SELECT COUNT(*) AS count FROM '.$tbl_message.'
  216. WHERE
  217. user_sender_id='.$user_id.' AND
  218. user_receiver_id='.$friend_id.' AND
  219. msg_status IN('.MESSAGE_STATUS_INVITATION_PENDING.', '.MESSAGE_STATUS_INVITATION_ACCEPTED.', '.MESSAGE_STATUS_INVITATION_DENIED.');
  220. ';
  221. $res_exist = Database::query($sql);
  222. $row_exist = Database::fetch_array($res_exist, 'ASSOC');
  223. if ($row_exist['count'] == 0) {
  224. $params = [
  225. 'user_sender_id' => $user_id,
  226. 'user_receiver_id' => $friend_id,
  227. 'msg_status' => MESSAGE_STATUS_INVITATION_PENDING,
  228. 'send_date' => $now,
  229. 'title' => $message_title,
  230. 'content' => $message_content,
  231. 'group_id' => 0,
  232. 'parent_id' => 0,
  233. 'update_date' => $now
  234. ];
  235. Database::insert($tbl_message, $params);
  236. $sender_info = api_get_user_info($user_id);
  237. $notification = new Notification();
  238. $notification->save_notification(
  239. Notification::NOTIFICATION_TYPE_INVITATION,
  240. array($friend_id),
  241. $message_title,
  242. $message_content,
  243. $sender_info
  244. );
  245. return true;
  246. } else {
  247. // invitation already exist
  248. $sql = 'SELECT COUNT(*) AS count, id FROM '.$tbl_message.'
  249. WHERE user_sender_id='.$user_id.' AND user_receiver_id='.$friend_id.' AND msg_status = 7';
  250. $res_if_exist = Database::query($sql);
  251. $row_if_exist = Database::fetch_array($res_if_exist, 'ASSOC');
  252. if ($row_if_exist['count'] == 1) {
  253. $sql = 'UPDATE '.$tbl_message.' SET
  254. msg_status=5, content = "'.$clean_message_content.'"
  255. WHERE user_sender_id='.$user_id.' AND user_receiver_id='.$friend_id.' AND msg_status = 7 ';
  256. Database::query($sql);
  257. return true;
  258. } else {
  259. return false;
  260. }
  261. }
  262. }
  263. /**
  264. * Get number messages of the inbox
  265. * @author isaac flores paz
  266. * @param int user receiver id
  267. * @return int
  268. */
  269. public static function get_message_number_invitation_by_user_id($user_receiver_id)
  270. {
  271. $tbl_message = Database::get_main_table(TABLE_MESSAGE);
  272. $sql = 'SELECT COUNT(*) as count_message_in_box FROM '.$tbl_message.'
  273. WHERE
  274. user_receiver_id='.intval($user_receiver_id).' AND
  275. msg_status='.MESSAGE_STATUS_INVITATION_PENDING;
  276. $res = Database::query($sql);
  277. $row = Database::fetch_array($res, 'ASSOC');
  278. return $row['count_message_in_box'];
  279. }
  280. /**
  281. * Get number of messages sent to other users
  282. * @param int $sender_id
  283. * @return int
  284. */
  285. public static function getCountMessagesSent($sender_id)
  286. {
  287. $tbl_message = Database::get_main_table(TABLE_MESSAGE);
  288. $sql = 'SELECT COUNT(*) FROM '.$tbl_message.'
  289. WHERE
  290. user_sender_id='.intval($sender_id).' AND
  291. msg_status < 5';
  292. $res = Database::query($sql);
  293. $row = Database::fetch_row($res);
  294. return $row[0];
  295. }
  296. /**
  297. * Get number of messages received from other users
  298. * @param int $receiver_id
  299. * @return int
  300. */
  301. public static function getCountMessagesReceived($receiver_id)
  302. {
  303. $tbl_message = Database::get_main_table(TABLE_MESSAGE);
  304. $sql = 'SELECT COUNT(*) FROM '.$tbl_message.'
  305. WHERE
  306. user_receiver_id='.intval($receiver_id).' AND
  307. msg_status < 4';
  308. $res = Database::query($sql);
  309. $row = Database::fetch_row($res);
  310. return $row[0];
  311. }
  312. /**
  313. * Get number of messages posted on own wall
  314. * @param int $userId
  315. * @return int
  316. */
  317. public static function getCountWallPostedMessages($userId)
  318. {
  319. if (empty($userId)) {
  320. return 0;
  321. }
  322. $tbl_message = Database::get_main_table(TABLE_MESSAGE);
  323. $sql = 'SELECT COUNT(*)
  324. FROM '.$tbl_message.'
  325. WHERE
  326. user_sender_id='.intval($userId).' AND
  327. (msg_status = '.MESSAGE_STATUS_WALL.' OR
  328. msg_status = '.MESSAGE_STATUS_WALL_POST.') AND
  329. parent_id = 0';
  330. $res = Database::query($sql);
  331. $row = Database::fetch_row($res);
  332. return $row[0];
  333. }
  334. /**
  335. * Get invitation list received by user
  336. * @author isaac flores paz
  337. * @param int $userId
  338. *
  339. * @return array
  340. */
  341. public static function get_list_invitation_of_friends_by_user_id($userId)
  342. {
  343. if (empty($userId)) {
  344. return [];
  345. }
  346. $tbl_message = Database::get_main_table(TABLE_MESSAGE);
  347. $sql = 'SELECT user_sender_id, send_date, title, content
  348. FROM '.$tbl_message.'
  349. WHERE
  350. user_receiver_id = '.intval($userId).' AND
  351. msg_status = '.MESSAGE_STATUS_INVITATION_PENDING;
  352. $res = Database::query($sql);
  353. $list = array();
  354. while ($row = Database::fetch_array($res, 'ASSOC')) {
  355. $list[] = $row;
  356. }
  357. return $list;
  358. }
  359. /**
  360. * Get invitation list sent by user
  361. * @author Julio Montoya <gugli100@gmail.com>
  362. * @param int $userId
  363. * @return array
  364. */
  365. public static function get_list_invitation_sent_by_user_id($userId)
  366. {
  367. if (empty($userId)) {
  368. return [];
  369. }
  370. $table = Database::get_main_table(TABLE_MESSAGE);
  371. $sql = 'SELECT user_receiver_id, send_date,title,content
  372. FROM '.$table.'
  373. WHERE
  374. user_sender_id = '.intval($userId).' AND
  375. msg_status = '.MESSAGE_STATUS_INVITATION_PENDING;
  376. $res = Database::query($sql);
  377. $list = array();
  378. while ($row = Database::fetch_array($res, 'ASSOC')) {
  379. $list[$row['user_receiver_id']] = $row;
  380. }
  381. return $list;
  382. }
  383. /**
  384. * Get count invitation sent by user
  385. * @author Julio Montoya <gugli100@gmail.com>
  386. * @param int $userId
  387. *
  388. * @return int
  389. */
  390. public static function getCountInvitationSent($userId)
  391. {
  392. if (empty($userId)) {
  393. return 0;
  394. }
  395. $table = Database::get_main_table(TABLE_MESSAGE);
  396. $sql = 'SELECT count(user_receiver_id) count
  397. FROM '.$table.'
  398. WHERE
  399. user_sender_id = '.intval($userId).' AND
  400. msg_status = '.MESSAGE_STATUS_INVITATION_PENDING;
  401. $res = Database::query($sql);
  402. if (Database::num_rows($res)) {
  403. $row = Database::fetch_array($res, 'ASSOC');
  404. return (int) $row['count'];
  405. }
  406. return 0;
  407. }
  408. /**
  409. * Accepts invitation
  410. * @param int $user_send_id
  411. * @param int $user_receiver_id
  412. * @return bool
  413. *
  414. * @author isaac flores paz
  415. * @author Julio Montoya <gugli100@gmail.com> Cleaning code
  416. */
  417. public static function invitation_accepted($user_send_id, $user_receiver_id)
  418. {
  419. if (empty($user_send_id) || empty($user_receiver_id)) {
  420. return false;
  421. }
  422. $tbl_message = Database::get_main_table(TABLE_MESSAGE);
  423. $sql = "UPDATE $tbl_message
  424. SET msg_status = ".MESSAGE_STATUS_INVITATION_ACCEPTED."
  425. WHERE
  426. user_sender_id = ".((int) $user_send_id)." AND
  427. user_receiver_id=".((int) $user_receiver_id)." AND
  428. msg_status = ".MESSAGE_STATUS_INVITATION_PENDING;
  429. Database::query($sql);
  430. return true;
  431. }
  432. /**
  433. * Denies invitation
  434. * @param int user sender id
  435. * @param int user receiver id
  436. * @return bool
  437. *
  438. * @author isaac flores paz
  439. * @author Julio Montoya <gugli100@gmail.com> Cleaning code
  440. */
  441. public static function invitation_denied($user_send_id, $user_receiver_id)
  442. {
  443. if (empty($user_send_id) || empty($user_receiver_id)) {
  444. return false;
  445. }
  446. $tbl_message = Database::get_main_table(TABLE_MESSAGE);
  447. $sql = 'DELETE FROM '.$tbl_message.'
  448. WHERE
  449. user_sender_id = '.((int) $user_send_id).' AND
  450. user_receiver_id='.((int) $user_receiver_id).' AND
  451. msg_status = '.MESSAGE_STATUS_INVITATION_PENDING;
  452. Database::query($sql);
  453. return true;
  454. }
  455. /**
  456. * Allow attaching to group
  457. * @author Isaac Flores Paz
  458. * @param int $id_friend_qualify User to qualify
  459. * @param int $type_qualify Kind of rating
  460. * @return void
  461. * @deprecated 2017-03
  462. */
  463. public static function qualify_friend($id_friend_qualify, $type_qualify)
  464. {
  465. $tbl_user_friend = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  466. $user_id = api_get_user_id();
  467. $sql = 'UPDATE '.$tbl_user_friend.' SET relation_type='.((int) $type_qualify).'
  468. WHERE user_id = '.((int) $user_id).' AND friend_user_id='.(int) $id_friend_qualify;
  469. Database::query($sql);
  470. }
  471. /**
  472. * Get user's feeds
  473. * @param int $user User ID
  474. * @param int $limit Limit of posts per feed
  475. * @return string HTML section with all feeds included
  476. * @author Yannick Warnier
  477. * @since Dokeos 1.8.6.1
  478. */
  479. public static function get_user_feeds($user, $limit = 5)
  480. {
  481. $feed = UserManager::get_extra_user_data_by_field($user, 'rssfeeds');
  482. if (empty($feed)) {
  483. return '';
  484. }
  485. $feeds = explode(';', $feed['rssfeeds']);
  486. if (count($feeds) == 0) {
  487. return '';
  488. }
  489. $res = '';
  490. foreach ($feeds as $url) {
  491. if (empty($url)) {
  492. continue;
  493. }
  494. $channel = Reader::import($url);
  495. $i = 1;
  496. if (!empty($channel)) {
  497. $icon_rss = '';
  498. if (!empty($feed)) {
  499. $icon_rss = Display::url(
  500. Display::return_icon('social_rss.png', '', array(), 22),
  501. Security::remove_XSS($feed['rssfeeds']),
  502. array('target' => '_blank')
  503. );
  504. }
  505. $res .= '<h3 class="title-rss">'.$icon_rss.' '.$channel->getTitle().'</h3>';
  506. $res .= '<div class="rss-items">';
  507. /** @var Rss $item */
  508. foreach ($channel as $item) {
  509. if ($limit >= 0 and $i > $limit) {
  510. break;
  511. }
  512. $res .= '<h4 class="rss-title"><a href="'.$item->getLink().'">'.$item->getTitle().'</a></h4>';
  513. $res .= '<div class="rss-date">'.api_get_local_time($item->getDateCreated()).'</div>';
  514. $res .= '<div class="rss-content"><p>'.$item->getDescription().'</p></div>';
  515. $i++;
  516. }
  517. $res .= '</div>';
  518. }
  519. }
  520. return $res;
  521. }
  522. /**
  523. * Sends invitations to friends
  524. *
  525. * @param int $userId
  526. * @param string $subject
  527. * @param string $content
  528. *
  529. * @return string message invitation
  530. */
  531. public static function sendInvitationToUser($userId, $subject = '', $content = '')
  532. {
  533. $user_info = api_get_user_info($userId);
  534. $success = get_lang('MessageSentTo');
  535. $success .= ' : '.api_get_person_name($user_info['firstName'], $user_info['lastName']);
  536. if (isset($subject) && isset($content) && isset($userId)) {
  537. $result = MessageManager::send_message($userId, $subject, $content);
  538. if ($result) {
  539. Display::addFlash(
  540. Display::return_message($success, 'normal', false)
  541. );
  542. } else {
  543. Display::addFlash(
  544. Display::return_message(get_lang('ErrorSendingMessage'), 'error', false)
  545. );
  546. }
  547. return false;
  548. } elseif (isset($userId) && !isset($subject)) {
  549. if (isset($userId) && $userId > 0) {
  550. $count = self::send_invitation_friend(
  551. api_get_user_id(),
  552. $userId,
  553. get_lang('Invitation'),
  554. $content
  555. );
  556. if ($count) {
  557. Display::addFlash(
  558. Display::return_message(
  559. api_htmlentities(get_lang('InvitationHasBeenSent')),
  560. 'normal',
  561. false
  562. )
  563. );
  564. } else {
  565. Display::addFlash(
  566. Display::return_message(
  567. api_htmlentities(get_lang('YouAlreadySentAnInvitation')),
  568. 'warning',
  569. false
  570. )
  571. );
  572. }
  573. }
  574. }
  575. }
  576. /**
  577. * Helper functions definition
  578. */
  579. public static function get_logged_user_course_html($my_course, $count)
  580. {
  581. $result = '';
  582. // Table definitions
  583. $main_user_table = Database::get_main_table(TABLE_MAIN_USER);
  584. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  585. $course_directory = $my_course['course_info']['directory'];
  586. $course_title = $my_course['course_info']['title'];
  587. $course_visibility = $my_course['course_info']['visibility'];
  588. $user_in_course_status = CourseManager::getUserInCourseStatus(
  589. api_get_user_id(),
  590. $my_course['course_info']['real_id']
  591. );
  592. $course_path = api_get_path(SYS_COURSE_PATH).$course_directory; // course path
  593. if (api_get_setting('course_images_in_courses_list') === 'true') {
  594. if (file_exists($course_path.'/course-pic85x85.png')) {
  595. $image = $my_course['course_info']['course_image'];
  596. $imageCourse = Display::img($image, $course_title, array('class'=>'img-course'));
  597. } else {
  598. $imageCourse = Display::return_icon(
  599. 'session_default_small.png',
  600. $course_title,
  601. array('class' => 'img-course')
  602. );
  603. }
  604. } else {
  605. $imageCourse = Display::return_icon(
  606. 'course.png',
  607. get_lang('Course'),
  608. array('class' => 'img-default')
  609. );
  610. }
  611. //display course entry
  612. if (api_get_setting('course_images_in_courses_list') === 'true') {
  613. $result .= '<li id="course_'.$count.'" class="list-group-item" style="min-height:65px;">';
  614. } else {
  615. $result .= '<li id="course_'.$count.'" class="list-group-item" style="min-height:44px;">';
  616. }
  617. $result .= $imageCourse;
  618. //show a hyperlink to the course, unless the course is closed and user is not course admin
  619. if ($course_visibility != COURSE_VISIBILITY_HIDDEN &&
  620. ($course_visibility != COURSE_VISIBILITY_CLOSED || $user_in_course_status == COURSEMANAGER)
  621. ) {
  622. $result .= '<span class="title">'.$course_title.'<span>';
  623. } else {
  624. $result .= $course_title." ".get_lang('CourseClosed');
  625. }
  626. $result .= '</li>';
  627. $session = '';
  628. if (!empty($my_course['session_name']) && !empty($my_course['id_session'])) {
  629. // Request for the name of the general coach
  630. $sql = 'SELECT lastname, firstname
  631. FROM '.$tbl_session.' ts
  632. LEFT JOIN '.$main_user_table.' tu
  633. ON ts.id_coach = tu.user_id
  634. WHERE ts.id='.(int) $my_course['id_session'].' LIMIT 1';
  635. $rs = Database::query($sql);
  636. $sessioncoach = Database::store_result($rs);
  637. $sessioncoach = $sessioncoach[0];
  638. $session = array();
  639. $session['title'] = $my_course['session_name'];
  640. if ($my_course['access_start_date'] == '0000-00-00') {
  641. $session['dates'] = get_lang('WithoutTimeLimits');
  642. if (api_get_setting('show_session_coach') === 'true') {
  643. $session['coach'] = get_lang('GeneralCoach').': '.
  644. api_get_person_name($sessioncoach['firstname'], $sessioncoach['lastname']);
  645. }
  646. } else {
  647. $session ['dates'] = ' - '.get_lang('From').' '.$my_course['access_start_date'].' '.get_lang('To').' '.$my_course['access_end_date'];
  648. if (api_get_setting('show_session_coach') === 'true') {
  649. $session['coach'] = get_lang('GeneralCoach').': '.
  650. api_get_person_name($sessioncoach['firstname'], $sessioncoach['lastname']);
  651. }
  652. }
  653. }
  654. $my_course['id_session'] = isset($my_course['id_session']) ? $my_course['id_session'] : 0;
  655. $output = array(
  656. $my_course['user_course_cat'],
  657. $result,
  658. $my_course['id_session'],
  659. $session
  660. );
  661. return $output;
  662. }
  663. /**
  664. * Shows the avatar block in social pages
  665. *
  666. * @param string $show highlight link possible values:
  667. * group_add,
  668. * home,
  669. * messages,
  670. * messages_inbox,
  671. * messages_compose,
  672. * messages_outbox,
  673. * invitations,
  674. * shared_profile,
  675. * friends,
  676. * groups search
  677. * @param int $group_id
  678. * @param int $user_id
  679. *
  680. */
  681. public static function show_social_avatar_block($show = '', $group_id = 0, $user_id = 0)
  682. {
  683. if (empty($user_id)) {
  684. $user_id = api_get_user_id();
  685. }
  686. $show_groups = array(
  687. 'groups',
  688. 'group_messages',
  689. 'messages_list',
  690. 'group_add',
  691. 'mygroups',
  692. 'group_edit',
  693. 'member_list',
  694. 'invite_friends',
  695. 'waiting_list',
  696. 'browse_groups',
  697. );
  698. $template = new Template(null, false, false, false, false, false);
  699. if (in_array($show, $show_groups) && !empty($group_id)) {
  700. // Group image
  701. $userGroup = new UserGroup();
  702. $group_info = $userGroup->get($group_id);
  703. $userGroupImage = $userGroup->get_picture_group(
  704. $group_id,
  705. $group_info['picture'],
  706. 128,
  707. GROUP_IMAGE_SIZE_BIG
  708. );
  709. $template->assign('show_group', true);
  710. $template->assign('group_id', $group_id);
  711. $template->assign('user_group_image', $userGroupImage);
  712. $template->assign('user_group', $group_info);
  713. $template->assign(
  714. 'user_is_group_admin',
  715. $userGroup->is_group_admin(
  716. $group_id,
  717. api_get_user_id()
  718. )
  719. );
  720. } else {
  721. $template->assign('show_group', false);
  722. $template->assign('show_user', true);
  723. $template->assign(
  724. 'user_image',
  725. [
  726. 'big' => UserManager::getUserPicture(
  727. $user_id,
  728. USER_IMAGE_SIZE_BIG
  729. ),
  730. 'normal' => UserManager::getUserPicture(
  731. $user_id,
  732. USER_IMAGE_SIZE_MEDIUM
  733. )
  734. ]
  735. );
  736. }
  737. $skillBlock = $template->get_template('social/avatar_block.tpl');
  738. return $template->fetch($skillBlock);
  739. }
  740. /**
  741. * Shows the right menu of the Social Network tool
  742. *
  743. * @param string $show highlight link possible values:
  744. * group_add,
  745. * home,
  746. * messages,
  747. * messages_inbox,
  748. * messages_compose ,
  749. * messages_outbox,
  750. * invitations,
  751. * shared_profile,
  752. * friends,
  753. * groups search
  754. * @param int $group_id group id
  755. * @param int $user_id user id
  756. * @param bool $show_full_profile show profile or not (show or hide the user image/information)
  757. * @param bool $show_delete_account_button
  758. *
  759. */
  760. public static function show_social_menu(
  761. $show = '',
  762. $group_id = 0,
  763. $user_id = 0,
  764. $show_full_profile = false,
  765. $show_delete_account_button = false
  766. ) {
  767. if (empty($user_id)) {
  768. $user_id = api_get_user_id();
  769. }
  770. $usergroup = new UserGroup();
  771. $show_groups = array(
  772. 'groups',
  773. 'group_messages',
  774. 'messages_list',
  775. 'group_add',
  776. 'mygroups',
  777. 'group_edit',
  778. 'member_list',
  779. 'invite_friends',
  780. 'waiting_list',
  781. 'browse_groups',
  782. );
  783. // get count unread message and total invitations
  784. $count_unread_message = MessageManager::get_number_of_messages(true);
  785. $count_unread_message = !empty($count_unread_message) ? Display::badge($count_unread_message) : null;
  786. $number_of_new_messages_of_friend = self::get_message_number_invitation_by_user_id(api_get_user_id());
  787. $group_pending_invitations = $usergroup->get_groups_by_user(
  788. api_get_user_id(),
  789. GROUP_USER_PERMISSION_PENDING_INVITATION,
  790. false
  791. );
  792. $group_pending_invitations = count($group_pending_invitations);
  793. $total_invitations = $number_of_new_messages_of_friend + $group_pending_invitations;
  794. $total_invitations = (!empty($total_invitations) ? Display::badge($total_invitations) : '');
  795. $filesIcon = Display::return_icon('sn-files.png', get_lang('MyFiles'), null, ICON_SIZE_SMALL);
  796. $friendsIcon = Display::return_icon('sn-friends.png', get_lang('Friends'), null, ICON_SIZE_SMALL);
  797. $groupsIcon = Display::return_icon('sn-groups.png', get_lang('SocialGroups'), null, ICON_SIZE_SMALL);
  798. $homeIcon = Display::return_icon('sn-home.png', get_lang('Home'), null, ICON_SIZE_SMALL);
  799. $invitationsIcon = Display::return_icon('sn-invitations.png', get_lang('Invitations'), null, ICON_SIZE_SMALL);
  800. $messagesIcon = Display::return_icon('sn-message.png', get_lang('Messages'), null, ICON_SIZE_SMALL);
  801. $sharedProfileIcon = Display::return_icon('sn-profile.png', get_lang('ViewMySharedProfile'));
  802. $searchIcon = Display::return_icon('sn-search.png', get_lang('Search'), null, ICON_SIZE_SMALL);
  803. $html = '';
  804. $active = null;
  805. if (!in_array(
  806. $show,
  807. array('shared_profile', 'groups', 'group_edit', 'member_list', 'waiting_list', 'invite_friends')
  808. )) {
  809. $links = '<ul class="nav nav-pills nav-stacked">';
  810. $active = $show == 'home' ? 'active' : null;
  811. $links .= '
  812. <li class="home-icon ' . $active.'">
  813. <a href="' . api_get_path(WEB_CODE_PATH).'social/home.php">
  814. ' . $homeIcon.' '.get_lang('Home').'
  815. </a>
  816. </li>';
  817. $active = $show == 'messages' ? 'active' : null;
  818. $links .= '
  819. <li class="messages-icon ' . $active.'">
  820. <a href="' . api_get_path(WEB_CODE_PATH).'messages/inbox.php?f=social">
  821. ' . $messagesIcon.' '.get_lang('Messages').$count_unread_message.'
  822. </a>
  823. </li>';
  824. //Invitations
  825. $active = $show == 'invitations' ? 'active' : null;
  826. $links .= '
  827. <li class="invitations-icon ' . $active.'">
  828. <a href="' . api_get_path(WEB_CODE_PATH).'social/invitations.php">
  829. ' . $invitationsIcon.' '.get_lang('Invitations').$total_invitations.'
  830. </a>
  831. </li>';
  832. //Shared profile and groups
  833. $active = $show == 'shared_profile' ? 'active' : null;
  834. $links .= '
  835. <li class="shared-profile-icon' . $active.'">
  836. <a href="' . api_get_path(WEB_CODE_PATH).'social/profile.php">
  837. ' . $sharedProfileIcon.' '.get_lang('ViewMySharedProfile').'
  838. </a>
  839. </li>';
  840. $active = $show == 'friends' ? 'active' : null;
  841. $links .= '
  842. <li class="friends-icon ' . $active.'">
  843. <a href="' . api_get_path(WEB_CODE_PATH).'social/friends.php">
  844. ' . $friendsIcon.' '.get_lang('Friends').'
  845. </a>
  846. </li>';
  847. $active = $show == 'browse_groups' ? 'active' : null;
  848. $links .= '
  849. <li class="browse-groups-icon ' . $active.'">
  850. <a href="' . api_get_path(WEB_CODE_PATH).'social/groups.php">
  851. ' . $groupsIcon.' '.get_lang('SocialGroups').'
  852. </a>
  853. </li>';
  854. //Search users
  855. $active = $show == 'search' ? 'active' : null;
  856. $links .= '
  857. <li class="search-icon ' . $active.'">
  858. <a href="' . api_get_path(WEB_CODE_PATH).'social/search.php">
  859. ' . $searchIcon.' '.get_lang('Search').'
  860. </a>
  861. </li>';
  862. //My files
  863. $active = $show == 'myfiles' ? 'active' : null;
  864. $myFiles = '
  865. <li class="myfiles-icon ' . $active.'">
  866. <a href="' . api_get_path(WEB_CODE_PATH).'social/myfiles.php">
  867. ' . $filesIcon.' '.get_lang('MyFiles').'
  868. </a>
  869. </li>';
  870. if (api_get_setting('allow_my_files') === 'false') {
  871. $myFiles = '';
  872. }
  873. $links .= $myFiles;
  874. $links .= '</ul>';
  875. $html .= Display::panelCollapse(
  876. get_lang('SocialNetwork'),
  877. $links,
  878. 'social-network-menu',
  879. null,
  880. 'sn-sidebar',
  881. 'sn-sidebar-collapse'
  882. );
  883. }
  884. if (in_array($show, $show_groups) && !empty($group_id)) {
  885. $html .= $usergroup->show_group_column_information(
  886. $group_id,
  887. api_get_user_id(),
  888. $show
  889. );
  890. }
  891. if ($show == 'shared_profile') {
  892. $links = '<ul class="nav nav-pills nav-stacked">';
  893. // My own profile
  894. if ($show_full_profile && $user_id == intval(api_get_user_id())) {
  895. $links .= '
  896. <li class="home-icon ' . $active.'">
  897. <a href="' . api_get_path(WEB_CODE_PATH).'social/home.php">
  898. ' . $homeIcon.' '.get_lang('Home').'
  899. </a>
  900. </li>
  901. <li class="messages-icon ' . $active.'">
  902. <a href="' . api_get_path(WEB_CODE_PATH).'messages/inbox.php?f=social">
  903. ' . $messagesIcon.' '.get_lang('Messages').$count_unread_message.'
  904. </a>
  905. </li>';
  906. $active = $show == 'invitations' ? 'active' : null;
  907. $links .= '
  908. <li class="invitations-icon' . $active.'">
  909. <a href="' . api_get_path(WEB_CODE_PATH).'social/invitations.php">
  910. ' . $invitationsIcon.' '.get_lang('Invitations').$total_invitations.'
  911. </a>
  912. </li>';
  913. $links .= '
  914. <li class="shared-profile-icon active">
  915. <a href="' . api_get_path(WEB_CODE_PATH).'social/profile.php">
  916. ' . $sharedProfileIcon.' '.get_lang('ViewMySharedProfile').'
  917. </a>
  918. </li>
  919. <li class="friends-icon">
  920. <a href="' . api_get_path(WEB_CODE_PATH).'social/friends.php">
  921. ' . $friendsIcon.' '.get_lang('Friends').'
  922. </a>
  923. </li>
  924. <li class="browse-groups-icon">
  925. <a href="' . api_get_path(WEB_CODE_PATH).'social/groups.php">
  926. ' . $groupsIcon.' '.get_lang('SocialGroups').'
  927. </a>
  928. </li>';
  929. $active = $show == 'search' ? 'active' : null;
  930. $links .= '
  931. <li class="search-icon ' . $active.'">
  932. <a href="' . api_get_path(WEB_CODE_PATH).'social/search.php">
  933. ' . $searchIcon.' '.get_lang('Search').'
  934. </a>
  935. </li>';
  936. $active = $show == 'myfiles' ? 'active' : null;
  937. $myFiles = '
  938. <li class="myfiles-icon ' . $active.'">
  939. <a href="' . api_get_path(WEB_CODE_PATH).'social/myfiles.php">
  940. ' . $filesIcon.' '.get_lang('MyFiles').'
  941. </a>
  942. </li>';
  943. if (api_get_setting('allow_my_files') === 'false') {
  944. $myFiles = '';
  945. }
  946. $links .= $myFiles;
  947. }
  948. // My friend profile.
  949. if ($user_id != api_get_user_id()) {
  950. $sendMessageText = get_lang('SendMessage');
  951. $sendMessageIcon = Display::return_icon(
  952. 'new-message.png',
  953. $sendMessageText
  954. );
  955. $sendMessageUrl = api_get_path(WEB_AJAX_PATH)
  956. . 'user_manager.ajax.php?'
  957. . http_build_query([
  958. 'a' => 'get_user_popup',
  959. 'user_id' => $user_id,
  960. ]);
  961. $links .= '<li>';
  962. $links .= Display::url(
  963. "$sendMessageIcon $sendMessageText",
  964. $sendMessageUrl,
  965. [
  966. 'class' => 'ajax',
  967. 'title' => $sendMessageText,
  968. 'data-title' => $sendMessageText,
  969. ]
  970. );
  971. $links .= '</li>';
  972. }
  973. // Check if I already sent an invitation message
  974. $invitation_sent_list = self::get_list_invitation_sent_by_user_id(
  975. api_get_user_id()
  976. );
  977. if (isset($invitation_sent_list[$user_id]) && is_array($invitation_sent_list[$user_id]) && count($invitation_sent_list[$user_id]) > 0) {
  978. $links .= '<li><a href="'.api_get_path(WEB_CODE_PATH).'social/invitations.php">'.Display::return_icon('invitation.png', get_lang('YouAlreadySentAnInvitation')).'&nbsp;&nbsp;'.get_lang('YouAlreadySentAnInvitation').'</a></li>';
  979. } else {
  980. if (!$show_full_profile) {
  981. $links .= '<li><a class="btn-to-send-invitation" href="#" data-send-to="'.$user_id.'" title="'.get_lang('SendInvitation').'">'.Display::return_icon('invitation.png', get_lang('SocialInvitationToFriends')).'&nbsp;'.get_lang('SendInvitation').'</a></li>';
  982. }
  983. }
  984. $links .= '</ul>';
  985. $html .= Display::panelCollapse(
  986. get_lang('SocialNetwork'),
  987. $links,
  988. 'social-network-menu',
  989. null,
  990. 'sn-sidebar',
  991. 'sn-sidebar-collapse'
  992. );
  993. if ($show_full_profile && $user_id == intval(api_get_user_id())) {
  994. $personal_course_list = UserManager::get_personal_session_course_list($user_id);
  995. $course_list_code = array();
  996. $i = 1;
  997. if (is_array($personal_course_list)) {
  998. foreach ($personal_course_list as $my_course) {
  999. if ($i <= 10) {
  1000. $course_list_code[] = array('code' => $my_course['code']);
  1001. } else {
  1002. break;
  1003. }
  1004. $i++;
  1005. }
  1006. // To avoid repeated courses
  1007. $course_list_code = array_unique_dimensional($course_list_code);
  1008. }
  1009. // Announcements
  1010. $my_announcement_by_user_id = intval($user_id);
  1011. $announcements = array();
  1012. foreach ($course_list_code as $course) {
  1013. $course_info = api_get_course_info($course['code']);
  1014. if (!empty($course_info)) {
  1015. $content = AnnouncementManager::get_all_annoucement_by_user_course(
  1016. $course_info['code'],
  1017. $my_announcement_by_user_id
  1018. );
  1019. if (!empty($content)) {
  1020. $url = Display::url(
  1021. Display::return_icon('announcement.png', get_lang('Announcements')).$course_info['name'].' ('.$content['count'].')',
  1022. api_get_path(WEB_CODE_PATH).'announcements/announcements.php?cidReq='.$course['code']
  1023. );
  1024. $announcements[] = Display::tag('li', $url);
  1025. }
  1026. }
  1027. }
  1028. if (!empty($announcements)) {
  1029. $html .= '<div class="social_menu_items">';
  1030. $html .= '<ul>';
  1031. foreach ($announcements as $announcement) {
  1032. $html .= $announcement;
  1033. }
  1034. $html .= '</ul>';
  1035. $html .= '</div>';
  1036. }
  1037. }
  1038. }
  1039. if ($show_delete_account_button) {
  1040. $html .= '<div class="panel panel-default"><div class="panel-body">';
  1041. $html .= '<ul class="nav nav-pills nav-stacked"><li>';
  1042. $url = api_get_path(WEB_CODE_PATH).'auth/unsubscribe_account.php';
  1043. $html .= Display::url(
  1044. Display::return_icon(
  1045. 'delete.png',
  1046. get_lang('Unsubscribe'),
  1047. array(),
  1048. ICON_SIZE_TINY
  1049. ).get_lang('Unsubscribe'),
  1050. $url
  1051. );
  1052. $html .= '</li></ul>';
  1053. $html .= '</div></div>';
  1054. }
  1055. $html .= '';
  1056. return $html;
  1057. }
  1058. /**
  1059. * Displays a sortable table with the list of online users.
  1060. * @param array $user_list The list of users to be shown
  1061. * @param bool $wrap Whether we want the function to wrap the spans list in a div or not
  1062. * @return string HTML block or null if and ID was defined
  1063. * @assert (null) === false
  1064. */
  1065. public static function display_user_list($user_list, $wrap = true)
  1066. {
  1067. $html = null;
  1068. if (isset($_GET['id']) or count($user_list) < 1) {
  1069. return false;
  1070. }
  1071. $course_url = '';
  1072. if (isset($_GET['cidReq']) && strlen($_GET['cidReq']) > 0) {
  1073. $course_url = '&amp;cidReq='.Security::remove_XSS($_GET['cidReq']);
  1074. }
  1075. foreach ($user_list as $uid) {
  1076. $user_info = api_get_user_info($uid, $checkIfUserOnline = true);
  1077. $lastname = $user_info['lastname'];
  1078. $firstname = $user_info['firstname'];
  1079. $completeName = $firstname.', '.$lastname;
  1080. $user_rol = $user_info['status'] == 1 ? Display::return_icon('teacher.png', get_lang('Teacher'), null, ICON_SIZE_TINY) : Display::return_icon('user.png', get_lang('Student'), null, ICON_SIZE_TINY);
  1081. $status_icon_chat = null;
  1082. if ($user_info['user_is_online_in_chat'] == 1) {
  1083. $status_icon_chat = Display::return_icon('online.png', get_lang('Online'));
  1084. } else {
  1085. $status_icon_chat = Display::return_icon('offline.png', get_lang('Offline'));
  1086. }
  1087. $userPicture = $user_info['avatar'];
  1088. $officialCode = '';
  1089. if (api_get_setting('show_official_code_whoisonline') == 'true') {
  1090. $officialCode .= '<div class="items-user-official-code"><p style="min-height: 30px;" title="'.get_lang('OfficialCode').'">'.$user_info['official_code'].'</p></div>';
  1091. }
  1092. $img = '<img class="img-responsive img-circle" title="'.$completeName.'" alt="'.$completeName.'" src="'.$userPicture.'">';
  1093. $url = null;
  1094. // Anonymous users can't have access to the profile
  1095. if (!api_is_anonymous()) {
  1096. if (api_get_setting('allow_social_tool') === 'true') {
  1097. $url = api_get_path(WEB_CODE_PATH).'social/profile.php?u='.$uid.$course_url;
  1098. } else {
  1099. $url = '?id='.$uid.$course_url;
  1100. }
  1101. } else {
  1102. $url = null;
  1103. }
  1104. $name = '<a href="'.$url.'">'.$firstname.'<br>'.$lastname.'</a>';
  1105. $html .= '<div class="col-xs-6 col-md-2">
  1106. <div class="items-user">
  1107. <div class="items-user-avatar"><a href="'.$url.'">'.$img.'</a></div>
  1108. <div class="items-user-name">
  1109. '.$name.'
  1110. </div>
  1111. '.$officialCode.'
  1112. <div class="items-user-status">'.$status_icon_chat.' '.$user_rol.'</div>
  1113. </div>
  1114. </div>';
  1115. }
  1116. return $html;
  1117. }
  1118. /**
  1119. * Displays the information of an individual user
  1120. * @param int $user_id
  1121. */
  1122. public static function display_individual_user($user_id)
  1123. {
  1124. global $interbreadcrumb;
  1125. $safe_user_id = intval($user_id);
  1126. $currentUserId = api_get_user_id();
  1127. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  1128. $sql = "SELECT * FROM $user_table WHERE user_id = ".$safe_user_id;
  1129. $result = Database::query($sql);
  1130. $html = null;
  1131. if (Database::num_rows($result) == 1) {
  1132. $user_object = Database::fetch_object($result);
  1133. $userInfo = api_get_user_info($user_id);
  1134. $alt = $userInfo['complete_name'].($currentUserId == $user_id ? '&nbsp;('.get_lang('Me').')' : '');
  1135. $status = get_status_from_code($user_object->status);
  1136. $interbreadcrumb[] = array('url' => 'whoisonline.php', 'name' => get_lang('UsersOnLineList'));
  1137. $html .= '<div class ="thumbnail">';
  1138. $fullurl = $userInfo['avatar'];
  1139. $html .= '<img src="'.$fullurl.'" alt="'.$alt.'" />';
  1140. if (!empty($status)) {
  1141. $html .= '<div class="caption">'.$status.'</div>';
  1142. }
  1143. $html .= '</div>';
  1144. if (api_get_setting('show_email_addresses') == 'true') {
  1145. $html .= Display::encrypted_mailto_link($user_object->email, $user_object->email).'<br />';
  1146. }
  1147. if ($user_object->competences) {
  1148. $html .= Display::page_subheader(get_lang('MyCompetences'));
  1149. $html .= '<p>'.$user_object->competences.'</p>';
  1150. }
  1151. if ($user_object->diplomas) {
  1152. $html .= Display::page_subheader(get_lang('MyDiplomas'));
  1153. $html .= '<p>'.$user_object->diplomas.'</p>';
  1154. }
  1155. if ($user_object->teach) {
  1156. $html .= Display::page_subheader(get_lang('MyTeach'));
  1157. $html .= '<p>'.$user_object->teach.'</p>';
  1158. }
  1159. self::display_productions($user_object->user_id);
  1160. if ($user_object->openarea) {
  1161. $html .= Display::page_subheader(get_lang('MyPersonalOpenArea'));
  1162. $html .= '<p>'.$user_object->openarea.'</p>';
  1163. }
  1164. } else {
  1165. $html .= '<div class="actions-title">';
  1166. $html .= get_lang('UsersOnLineList');
  1167. $html .= '</div>';
  1168. }
  1169. return $html;
  1170. }
  1171. /**
  1172. * Display productions in who is online
  1173. * @param int $user_id User id
  1174. */
  1175. public static function display_productions($user_id)
  1176. {
  1177. $webdir_array = UserManager::get_user_picture_path_by_id($user_id, 'web');
  1178. $sysdir = UserManager::getUserPathById($user_id, 'system');
  1179. $webdir = UserManager::getUserPathById($user_id, 'web');
  1180. if (!is_dir($sysdir)) {
  1181. mkdir($sysdir, api_get_permissions_for_new_directories(), true);
  1182. }
  1183. $productions = UserManager::get_user_productions($user_id);
  1184. if (count($productions) > 0) {
  1185. echo '<dt><strong>'.get_lang('Productions').'</strong></dt>';
  1186. echo '<dd><ul>';
  1187. foreach ($productions as $file) {
  1188. // Only display direct file links to avoid browsing an empty directory
  1189. if (is_file($sysdir.$file) && $file != $webdir_array['file']) {
  1190. echo '<li><a href="'.$webdir.urlencode($file).'" target=_blank>'.$file.'</a></li>';
  1191. }
  1192. // Real productions are under a subdirectory by the User's id
  1193. if (is_dir($sysdir.$file)) {
  1194. $subs = scandir($sysdir.$file);
  1195. foreach ($subs as $my => $sub) {
  1196. if (substr($sub, 0, 1) != '.' && is_file($sysdir.$file.'/'.$sub)) {
  1197. echo '<li><a href="'.$webdir.urlencode($file).'/'.urlencode($sub).'" target=_blank>'.$sub.'</a></li>';
  1198. }
  1199. }
  1200. }
  1201. }
  1202. echo '</ul></dd>';
  1203. }
  1204. }
  1205. /**
  1206. * @param string $content
  1207. * @param string $span_count
  1208. * @return string
  1209. */
  1210. public static function social_wrapper_div($content, $span_count)
  1211. {
  1212. $span_count = intval($span_count);
  1213. $html = '<div class="span'.$span_count.'">';
  1214. $html .= '<div class="well_border">';
  1215. $html .= $content;
  1216. $html .= '</div></div>';
  1217. return $html;
  1218. }
  1219. /**
  1220. * Dummy function
  1221. *
  1222. */
  1223. public static function get_plugins($place = SOCIAL_CENTER_PLUGIN)
  1224. {
  1225. $content = '';
  1226. switch ($place) {
  1227. case SOCIAL_CENTER_PLUGIN:
  1228. $social_plugins = array(1, 2);
  1229. if (is_array($social_plugins) && count($social_plugins) > 0) {
  1230. $content .= '<div id="social-plugins">';
  1231. foreach ($social_plugins as $plugin) {
  1232. $content .= '<div class="social-plugin-item">';
  1233. $content .= $plugin;
  1234. $content .= '</div>';
  1235. }
  1236. $content .= '</div>';
  1237. }
  1238. break;
  1239. case SOCIAL_LEFT_PLUGIN:
  1240. break;
  1241. case SOCIAL_RIGHT_PLUGIN:
  1242. break;
  1243. }
  1244. return $content;
  1245. }
  1246. /**
  1247. * Sends a message to someone's wall
  1248. * @param int $userId id of author
  1249. * @param int $friendId id where we send the message
  1250. * @param string $messageContent of the message
  1251. * @param int $messageId id parent
  1252. * @param string $messageStatus status type of message
  1253. * @return boolean
  1254. * @author Yannick Warnier
  1255. */
  1256. public static function sendWallMessage(
  1257. $userId,
  1258. $friendId,
  1259. $messageContent,
  1260. $messageId = 0,
  1261. $messageStatus = ''
  1262. ) {
  1263. $tblMessage = Database::get_main_table(TABLE_MESSAGE);
  1264. $userId = intval($userId);
  1265. $friendId = intval($friendId);
  1266. $messageId = intval($messageId);
  1267. // Just in case we replace the and \n and \n\r while saving in the DB
  1268. $messageContent = str_replace(array("\n", "\n\r"), '<br />', $messageContent);
  1269. $now = api_get_utc_datetime();
  1270. $attributes = array(
  1271. 'user_sender_id' => $userId,
  1272. 'user_receiver_id' => $friendId,
  1273. 'msg_status' => $messageStatus,
  1274. 'send_date' => $now,
  1275. 'title' => '',
  1276. 'content' => $messageContent,
  1277. 'parent_id' => $messageId,
  1278. 'group_id' => 0,
  1279. 'update_date' => $now
  1280. );
  1281. return Database::insert($tblMessage, $attributes);
  1282. }
  1283. /**
  1284. * Send File attachment (jpg,png)
  1285. * @author Anibal Copitan
  1286. * @param int $userId id user
  1287. * @param array $fileAttach
  1288. * @param int $messageId id message (relation with main message)
  1289. * @param string $fileComment description attachment file
  1290. * @return bool
  1291. */
  1292. public static function sendWallMessageAttachmentFile(
  1293. $userId,
  1294. $fileAttach,
  1295. $messageId,
  1296. $fileComment = ''
  1297. ) {
  1298. $tbl_message_attach = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  1299. // create directory
  1300. $social = '/social/';
  1301. $pathMessageAttach = UserManager::getUserPathById($userId, 'system').'message_attachments'.$social;
  1302. $safeFileComment = Database::escape_string($fileComment);
  1303. $safeFileName = Database::escape_string($fileAttach['name']);
  1304. $extension = strtolower(substr(strrchr($safeFileName, '.'), 1));
  1305. $allowedTypes = api_get_supported_image_extensions();
  1306. if (!in_array($extension, $allowedTypes)) {
  1307. $flag = false;
  1308. } else {
  1309. $newFileName = uniqid('').'.'.$extension;
  1310. if (!file_exists($pathMessageAttach)) {
  1311. @mkdir($pathMessageAttach, api_get_permissions_for_new_directories(), true);
  1312. }
  1313. $newPath = $pathMessageAttach.$newFileName;
  1314. if (is_uploaded_file($fileAttach['tmp_name'])) {
  1315. @copy($fileAttach['tmp_name'], $newPath);
  1316. }
  1317. $small = self::resize_picture($newPath, IMAGE_WALL_SMALL_SIZE);
  1318. $medium = self::resize_picture($newPath, IMAGE_WALL_MEDIUM_SIZE);
  1319. $big = new Image($newPath);
  1320. $ok = $small && $small->send_image($pathMessageAttach.IMAGE_WALL_SMALL.'_'.$newFileName) &&
  1321. $medium && $medium->send_image($pathMessageAttach.IMAGE_WALL_MEDIUM.'_'.$newFileName) &&
  1322. $big && $big->send_image($pathMessageAttach.IMAGE_WALL_BIG.'_'.$newFileName);
  1323. // Insert
  1324. $newFileName = $social.$newFileName;
  1325. $params = [
  1326. 'filename' => $safeFileName,
  1327. 'comment' => $safeFileComment,
  1328. 'path' => $newFileName,
  1329. 'message_id' => $messageId,
  1330. 'size' => $fileAttach['size'],
  1331. ];
  1332. Database::insert($tbl_message_attach, $params);
  1333. $flag = true;
  1334. }
  1335. return $flag;
  1336. }
  1337. /**
  1338. * Gets all messages from someone's wall (within specific limits)
  1339. * @param int $userId id of wall shown
  1340. * @param string $messageStatus status wall message
  1341. * @param int|string $parentId id message (Post main)
  1342. * @param string $start Date from which we want to show the messages, in UTC time
  1343. * @param int $limit Limit for the number of parent messages we want to show
  1344. * @param int $offset Wall message query offset
  1345. * @return array
  1346. * @author Yannick Warnier
  1347. */
  1348. public static function getWallMessages(
  1349. $userId,
  1350. $messageStatus,
  1351. $parentId = '',
  1352. $start = null,
  1353. $limit = 10,
  1354. $offset = 0
  1355. ) {
  1356. if (empty($start)) {
  1357. $start = '0000-00-00';
  1358. }
  1359. $tblMessage = Database::get_main_table(TABLE_MESSAGE);
  1360. $tblMessageAttachment = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  1361. $userId = intval($userId);
  1362. $start = Database::escape_string($start);
  1363. $limit = intval($limit);
  1364. $sql = "SELECT
  1365. id,
  1366. user_sender_id,
  1367. user_receiver_id,
  1368. send_date,
  1369. content,
  1370. parent_id,
  1371. (
  1372. SELECT ma.path FROM $tblMessageAttachment ma
  1373. WHERE ma.message_id = tm.id
  1374. ) as path,
  1375. (
  1376. SELECT ma.filename FROM $tblMessageAttachment ma
  1377. WHERE ma.message_id = tm.id
  1378. ) as filename
  1379. FROM $tblMessage tm
  1380. WHERE
  1381. user_receiver_id = $userId AND
  1382. send_date > '$start'
  1383. ";
  1384. $sql .= (empty($messageStatus) || is_null($messageStatus)) ? '' : " AND msg_status = '$messageStatus' ";
  1385. $sql .= (empty($parentId) || is_null($parentId)) ? '' : " AND parent_id = '$parentId' ";
  1386. $sql .= " ORDER BY send_date DESC LIMIT $offset, $limit ";
  1387. $messages = array();
  1388. $res = Database::query($sql);
  1389. if (Database::num_rows($res) > 0) {
  1390. while ($row = Database::fetch_array($res)) {
  1391. $messages[] = $row;
  1392. }
  1393. }
  1394. return $messages;
  1395. }
  1396. /**
  1397. * Gets all messages from someone's wall (within specific limits), formatted
  1398. * @param int $userId USER ID of the person's wall
  1399. * @param int $friendId id person
  1400. * @param int $idMessage id message
  1401. * @param string $start Start date (from when we want the messages until today)
  1402. * @param int $limit Limit to the number of messages we want
  1403. * @param int $offset Wall messages offset
  1404. * @return string HTML formatted string to show messages
  1405. */
  1406. public static function getWallMessagesHTML(
  1407. $userId,
  1408. $friendId,
  1409. $idMessage,
  1410. $start = null,
  1411. $limit = 10,
  1412. $offset = 0
  1413. ) {
  1414. if (empty($start)) {
  1415. $start = '0000-00-00';
  1416. }
  1417. $isOwnWall = (api_get_user_id() == $userId && $userId == $friendId);
  1418. $messages = self::getWallMessages(
  1419. $userId,
  1420. MESSAGE_STATUS_WALL,
  1421. $idMessage,
  1422. $start,
  1423. $limit,
  1424. $offset
  1425. );
  1426. $formattedList = '<div class="sub-mediapost">';
  1427. $users = array();
  1428. // The messages are ordered by date descendant, for comments we need ascendant
  1429. krsort($messages);
  1430. foreach ($messages as $message) {
  1431. $date = api_get_local_time($message['send_date']);
  1432. $userIdLoop = $message['user_sender_id'];
  1433. if (!isset($users[$userIdLoop])) {
  1434. $users[$userIdLoop] = api_get_user_info($userIdLoop);
  1435. }
  1436. $nameComplete = api_is_western_name_order()
  1437. ? $users[$userIdLoop]['firstname'].' '.$users[$userIdLoop]['lastname']
  1438. : $users[$userIdLoop]['lastname'].' '.$users[$userIdLoop]['firstname'];
  1439. $url = api_get_path(WEB_CODE_PATH).'social/profile.php?u='.$userIdLoop;
  1440. $media = '';
  1441. $media .= '<div class="rep-post">';
  1442. $media .= '<div class="col-md-2 col-xs-2 social-post-answers">';
  1443. $media .= '<div class="user-image pull-right">';
  1444. $media .= '<a href="'.$url.'" ><img src="'.$users[$userIdLoop]['avatar'].
  1445. '" alt="'.$users[$userIdLoop]['complete_name'].'" class="avatar-thumb"></a>';
  1446. $media .= '</div>';
  1447. $media .= '</div>';
  1448. $media .= '<div class="col-md-9 col-xs-9 social-post-answers">';
  1449. $media .= '<div class="user-data">';
  1450. $media .= '<div class="username">'.'<a href="'.$url.'">'.$nameComplete.'</a> <span>'.Security::remove_XSS($message['content']).'</span></div>';
  1451. $media .= '<div class="time timeago" title="'.$date.'">'.$date.'</div>';
  1452. $media .= '<br />';
  1453. $media .= '</div>';
  1454. $media .= '</div>';
  1455. $media .= '</div>';
  1456. if ($isOwnWall) {
  1457. $media .= '<div class="col-md-1 col-xs-1 social-post-answers">';
  1458. $media .= '<div class="pull-right deleted-mgs">';
  1459. $media .= '<a title="'.get_lang("SocialMessageDelete").'" href="'.api_get_path(WEB_CODE_PATH).'social/profile.php?messageId='.
  1460. $message['id'].'">x</a>';
  1461. $media .= '</div>';
  1462. $media .= '</div>';
  1463. }
  1464. $formattedList .= $media;
  1465. }
  1466. $formattedList .= '</div>';
  1467. $formattedList .= '<div class="mediapost-form">';
  1468. $formattedList .= '<form name="social_wall_message" method="POST">
  1469. <label for="social_wall_new_msg" class="hide">'.get_lang('SocialWriteNewComment').'</label>
  1470. <input type="hidden" name = "messageId" value="'.$idMessage.'" />
  1471. <textarea placeholder="'.get_lang('SocialWriteNewComment').
  1472. '" name="social_wall_new_msg" rows="1" style="width:80%;" ></textarea>
  1473. <button type="submit" name="social_wall_new_msg_submit"
  1474. class="pull-right btn btn-default" /><em class="fa fa-pencil"></em> '.get_lang('Post').'</button>
  1475. </form>';
  1476. $formattedList .= '</div>';
  1477. return $formattedList;
  1478. }
  1479. /**
  1480. * Gets all user's starting wall messages (within specific limits)
  1481. * @param int $userId User's id
  1482. * @param int $friendId Friend's id
  1483. * @param date $start Start date (from when we want the messages until today)
  1484. * @param int $limit Limit to the number of messages we want
  1485. * @param int $offset Wall messages offset
  1486. * @return array $data return user's starting wall messages along with message extra data
  1487. */
  1488. public static function getWallMessagesPostHTML(
  1489. $userId,
  1490. $friendId = 0,
  1491. $start = null,
  1492. $limit = 10,
  1493. $offset = 0
  1494. ) {
  1495. if (empty($start)) {
  1496. $start = '0000-00-00';
  1497. }
  1498. $isOwnWall = api_get_user_id() == $userId && $userId == $friendId;
  1499. $messages = self::getWallMessages(
  1500. $userId,
  1501. MESSAGE_STATUS_WALL_POST,
  1502. null,
  1503. $start,
  1504. $limit,
  1505. $offset
  1506. );
  1507. $users = array();
  1508. $data = array();
  1509. foreach ($messages as $key => $message) {
  1510. $userIdLoop = $message['user_sender_id'];
  1511. $userFriendIdLoop = $message['user_receiver_id'];
  1512. if (!isset($users[$userIdLoop])) {
  1513. $users[$userIdLoop] = api_get_user_info($userIdLoop);
  1514. }
  1515. if (!isset($users[$userFriendIdLoop])) {
  1516. $users[$userFriendIdLoop] = api_get_user_info($userFriendIdLoop);
  1517. }
  1518. $html = '';
  1519. $html .= self::headerMessagePost(
  1520. $message['user_sender_id'],
  1521. $message['user_receiver_id'],
  1522. $users,
  1523. $message,
  1524. $isOwnWall
  1525. );
  1526. $data[$key]['id'] = $message['id'];
  1527. $data[$key]['html'] = $html;
  1528. }
  1529. return $data;
  1530. }
  1531. /**
  1532. * Returns the formatted header message post
  1533. * @param int $authorId Author's id
  1534. * @param int $receiverId Receiver's id
  1535. * @param array $users Author's and receiver's data
  1536. * @param array $message Message data
  1537. * @param boolean $isOwnWall Determines if the author is in its own social wall or not
  1538. * @return string $html The formatted header message post
  1539. */
  1540. private static function headerMessagePost($authorId, $receiverId, $users, $message, $isOwnWall = false)
  1541. {
  1542. $date = api_get_local_time($message['send_date']);
  1543. $avatarAuthor = $users[$authorId]['avatar'];
  1544. $urlAuthor = api_get_path(WEB_CODE_PATH).'social/profile.php?u='.$authorId;
  1545. $nameCompleteAuthor = api_get_person_name(
  1546. $users[$authorId]['firstname'],
  1547. $users[$authorId]['lastname']
  1548. );
  1549. $urlReceiver = api_get_path(WEB_CODE_PATH).'social/profile.php?u='.$receiverId;
  1550. $nameCompleteReceiver = api_get_person_name(
  1551. $users[$receiverId]['firstname'],
  1552. $users[$receiverId]['lastname']
  1553. );
  1554. $htmlReceiver = '';
  1555. if ($authorId != $receiverId) {
  1556. $htmlReceiver = ' > <a href="'.$urlReceiver.'">'.$nameCompleteReceiver.'</a> ';
  1557. }
  1558. $wallImage = '';
  1559. if (!empty($message['path'])) {
  1560. $imageBig = UserManager::getUserPicture($authorId, USER_IMAGE_SIZE_BIG);
  1561. $imageSmall = UserManager::getUserPicture($authorId, USER_IMAGE_SIZE_SMALL);
  1562. $wallImage = '<a class="thumbnail ajax" href="'.$imageBig.'"><img src="'.$imageSmall.'"></a>';
  1563. }
  1564. $htmlDelete = '';
  1565. if ($isOwnWall) {
  1566. $htmlDelete .= '<a title="'.get_lang("SocialMessageDelete").'" href="'.api_get_path(WEB_CODE_PATH).'social/profile.php?messageId='.
  1567. $message['id'].'">x</a>';
  1568. }
  1569. $html = '';
  1570. $html .= '<div class="top-mediapost" >';
  1571. if ($isOwnWall) {
  1572. $html .= '<div class="pull-right deleted-mgs">';
  1573. $html .= $htmlDelete;
  1574. $html .= '</div>';
  1575. }
  1576. $html .= '<div class="user-image" >';
  1577. $html .= '<a href="'.$urlAuthor.'">'.'<img class="avatar-thumb" src="'.$avatarAuthor.'" alt="'.$nameCompleteAuthor.'"></a>';
  1578. $html .= '</div>';
  1579. $html .= '<div class="user-data">';
  1580. $html .= '<div class="username"><a href="'.$urlAuthor.'">'.$nameCompleteAuthor.'</a>'.$htmlReceiver.'</div>';
  1581. $html .= '<div class="time timeago" title="'.$date.'">'.$date.'</div>';
  1582. $html .= '</div>';
  1583. $html .= '<div class="msg-content">';
  1584. $html .= '<div class="img-post">';
  1585. $html .= $wallImage;
  1586. $html .= '</div>';
  1587. $html .= '<p>'.Security::remove_XSS($message['content']).'</p>';
  1588. $html .= '</div>';
  1589. $html .= '</div>'; // end mediaPost
  1590. // Popularity post functionality
  1591. $html .= '<div class="popularity-mediapost"></div>';
  1592. return $html;
  1593. }
  1594. /**
  1595. * get html data with OpenGrap passing the Url
  1596. * @param $link url
  1597. * @return string data html
  1598. */
  1599. public static function readContentWithOpenGraph($link)
  1600. {
  1601. if (strpos($link, "://") === false && substr($link, 0, 1) != "/") {
  1602. $link = "http://".$link;
  1603. }
  1604. $graph = OpenGraph::fetch($link);
  1605. $link = parse_url($link);
  1606. $host = $link['host'] ? strtoupper($link['host']) : $link['path'];
  1607. if (!$graph) {
  1608. return false;
  1609. }
  1610. $url = $graph->url;
  1611. $image = $graph->image;
  1612. $description = $graph->description;
  1613. $title = $graph->title;
  1614. $html = '<div class="thumbnail social-thumbnail">';
  1615. $html .= empty($image) ? '' : '<a target="_blank" href="'.$url.'"><img class="img-responsive social-image" src="'.$image.'" /></a>';
  1616. $html .= '<div class="social-description">';
  1617. $html .= '<a target="_blank" href="'.$url.'"><h5 class="social-title"><b>'.$title.'</b></h5></a>';
  1618. $html .= empty($description) ? '' : '<span>'.$description.'</span>';
  1619. $html .= empty($host) ? '' : '<p>'.$host.'</p>';
  1620. $html .= '</div>';
  1621. $html .= '</div>';
  1622. return $html;
  1623. }
  1624. /**
  1625. * verify if Url Exist - Using Curl
  1626. * @param $uri url
  1627. *
  1628. * @return boolean
  1629. */
  1630. public static function verifyUrl($uri)
  1631. {
  1632. $curl = curl_init($uri);
  1633. curl_setopt($curl, CURLOPT_FAILONERROR, true);
  1634. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  1635. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  1636. curl_setopt($curl, CURLOPT_TIMEOUT, 15);
  1637. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  1638. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  1639. curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  1640. $response = curl_exec($curl);
  1641. curl_close($curl);
  1642. if (!empty($response)) {
  1643. return true;
  1644. } else {
  1645. return false;
  1646. }
  1647. }
  1648. /**
  1649. * Delete messages delete logic
  1650. * @param int $id id message to delete.
  1651. * @return bool status query
  1652. */
  1653. public static function deleteMessage($id)
  1654. {
  1655. $id = intval($id);
  1656. $tblMessage = Database::get_main_table(TABLE_MESSAGE);
  1657. $statusMessage = MESSAGE_STATUS_WALL_DELETE;
  1658. $sql = "UPDATE $tblMessage SET msg_status = '$statusMessage' WHERE id = '{$id}' ";
  1659. return Database::query($sql);
  1660. }
  1661. /**
  1662. * Generate the social block for a user
  1663. * @param Template $template
  1664. * @param int $userId The user id
  1665. * @param string $groupBlock Optional. Highlight link possible values:
  1666. * group_add, home, messages, messages_inbox, messages_compose,
  1667. * messages_outbox, invitations, shared_profile, friends, groups, search
  1668. * @param int $groupId Optional. Group ID
  1669. * @return string The HTML code with the social block
  1670. */
  1671. public static function setSocialUserBlock(
  1672. Template $template,
  1673. $userId,
  1674. $groupBlock = '',
  1675. $groupId = 0,
  1676. $show_full_profile = true
  1677. ) {
  1678. if (api_get_setting('allow_social_tool') != 'true') {
  1679. return '';
  1680. }
  1681. $currentUserId = api_get_user_id();
  1682. $userId = intval($userId);
  1683. $userRelationType = 0;
  1684. $socialAvatarBlock = self::show_social_avatar_block(
  1685. $groupBlock,
  1686. $groupId,
  1687. $userId
  1688. );
  1689. $profileEditionLink = null;
  1690. if ($currentUserId === $userId) {
  1691. $profileEditionLink = Display::getProfileEditionLink($userId);
  1692. } else {
  1693. $userRelationType = self::get_relation_between_contacts(
  1694. $currentUserId,
  1695. $userId
  1696. );
  1697. }
  1698. $vCardUserLink = Display::getVCardUserLink($userId);
  1699. $userInfo = api_get_user_info($userId, true, false, true, true);
  1700. $template->assign('user', $userInfo);
  1701. $template->assign('social_avatar_block', $socialAvatarBlock);
  1702. $template->assign('profile_edition_link', $profileEditionLink);
  1703. //Added the link to export the vCard to the Template
  1704. //If not friend $show_full_profile is False and the user can't see Email Address and Vcard Download Link
  1705. if ($show_full_profile) {
  1706. $template->assign('vcard_user_link', $vCardUserLink);
  1707. }
  1708. if (api_get_setting('gamification_mode') === '1') {
  1709. $gamificationPoints = GamificationUtils::getTotalUserPoints(
  1710. $userId,
  1711. $userInfo['status']
  1712. );
  1713. $template->assign('gamification_points', $gamificationPoints);
  1714. }
  1715. $chatEnabled = api_is_global_chat_enabled();
  1716. $template->assign('chat_enabled', $chatEnabled);
  1717. $template->assign('user_relation', $userRelationType);
  1718. $template->assign('user_relation_type_friend', USER_RELATION_TYPE_FRIEND);
  1719. $template->assign('show_full_profile', $show_full_profile);
  1720. $templateName = $template->get_template('social/user_block.tpl');
  1721. if (in_array($groupBlock, ['groups', 'group_edit', 'member_list'])) {
  1722. $templateName = $template->get_template('social/group_block.tpl');
  1723. }
  1724. $template->assign('social_avatar_block', $template->fetch($templateName));
  1725. }
  1726. /**
  1727. * @param int $user_id
  1728. * @param $link_shared
  1729. * @param $show_full_profile
  1730. * @return string
  1731. */
  1732. public static function listMyFriends($user_id, $link_shared, $show_full_profile)
  1733. {
  1734. //SOCIALGOODFRIEND , USER_RELATION_TYPE_FRIEND, USER_RELATION_TYPE_PARENT
  1735. $friends = self::get_friends($user_id, USER_RELATION_TYPE_FRIEND);
  1736. $number_of_images = 30;
  1737. $number_friends = count($friends);
  1738. $friendHtml = '';
  1739. if ($number_friends != 0) {
  1740. if ($number_friends > $number_of_images) {
  1741. if (api_get_user_id() == $user_id) {
  1742. $friendHtml .= ' <span><a href="friends.php">'.get_lang('SeeAll').'</a></span>';
  1743. } else {
  1744. $friendHtml .= ' <span>'
  1745. .'<a href="'.api_get_path(WEB_CODE_PATH).'social/profile_friends_and_groups.inc.php'
  1746. .'?view=friends&height=390&width=610&user_id='.$user_id.'"'
  1747. .'class="ajax" data-title="'.get_lang('SeeAll').'" title="'.get_lang('SeeAll').'" >'.get_lang('SeeAll').'</a></span>';
  1748. }
  1749. }
  1750. $friendHtml .= '<ul class="nav nav-list">';
  1751. $j = 1;
  1752. for ($k = 0; $k < $number_friends; $k++) {
  1753. if ($j > $number_of_images) {
  1754. break;
  1755. }
  1756. if (isset($friends[$k])) {
  1757. $friend = $friends[$k];
  1758. $name_user = api_get_person_name($friend['firstName'], $friend['lastName']);
  1759. $user_info_friend = api_get_user_info($friend['friend_user_id'], true);
  1760. if ($user_info_friend['user_is_online']) {
  1761. $statusIcon = Display::span('', array('class' => 'online_user_in_text'));
  1762. } else {
  1763. $statusIcon = Display::span('', array('class' => 'offline_user_in_text'));
  1764. }
  1765. $friendHtml .= '<li>';
  1766. $friendHtml .= '<div>';
  1767. // the height = 92 must be the same in the image_friend_network span style in default.css
  1768. $friends_profile = UserManager::getUserPicture(
  1769. $friend['friend_user_id'],
  1770. USER_IMAGE_SIZE_SMALL
  1771. );
  1772. $friendHtml .= '<img src="'.$friends_profile.'" id="imgfriend_'.$friend['friend_user_id'].'" title="'.$name_user.'"/>';
  1773. $link_shared = (empty($link_shared)) ? '' : '&'.$link_shared;
  1774. $friendHtml .= $statusIcon.'<a href="profile.php?'.'u='.$friend['friend_user_id'].$link_shared.'">'.$name_user.'</a>';
  1775. $friendHtml .= '</div>';
  1776. $friendHtml .= '</li>';
  1777. }
  1778. $j++;
  1779. }
  1780. $friendHtml .= '</ul>';
  1781. } else {
  1782. $friendHtml .= '<div class="">'.get_lang('NoFriendsInYourContactList').'<br />'
  1783. .'<a class="btn btn-primary" href="'.api_get_path(WEB_PATH).'whoisonline.php"><em class="fa fa-search"></em> '.get_lang('TryAndFindSomeFriends').'</a></div>';
  1784. }
  1785. $friendHtml = Display::panel($friendHtml, get_lang('SocialFriend').' ('.$number_friends.')');
  1786. return $friendHtml;
  1787. }
  1788. /**
  1789. * @param int $user_id
  1790. * @param $link_shared
  1791. * @param $show_full_profile
  1792. * @return string
  1793. */
  1794. public static function listMyFriendsBlock($user_id, $link_shared = '', $show_full_profile = '')
  1795. {
  1796. //SOCIALGOODFRIEND , USER_RELATION_TYPE_FRIEND, USER_RELATION_TYPE_PARENT
  1797. $friends = self::get_friends($user_id, USER_RELATION_TYPE_FRIEND);
  1798. $number_of_images = 30;
  1799. $number_friends = count($friends);
  1800. $friendHtml = '';
  1801. if ($number_friends != 0) {
  1802. $friendHtml .= '<div class="list-group">';
  1803. $j = 1;
  1804. for ($k = 0; $k < $number_friends; $k++) {
  1805. if ($j > $number_of_images) {
  1806. break;
  1807. }
  1808. if (isset($friends[$k])) {
  1809. $friend = $friends[$k];
  1810. $name_user = api_get_person_name($friend['firstName'], $friend['lastName']);
  1811. $user_info_friend = api_get_user_info($friend['friend_user_id'], true);
  1812. if (!empty($user_info_friend['user_is_online'])) {
  1813. $statusIcon = Display::return_icon('statusonline.png', get_lang('Online'));
  1814. $status = 1;
  1815. } else {
  1816. $statusIcon = Display::return_icon('statusoffline.png', get_lang('Offline'));
  1817. $status = 0;
  1818. }
  1819. $friendAvatarMedium = UserManager::getUserPicture(
  1820. $friend['friend_user_id'],
  1821. USER_IMAGE_SIZE_MEDIUM
  1822. );
  1823. $friendAvatarSmall = UserManager::getUserPicture(
  1824. $friend['friend_user_id'],
  1825. USER_IMAGE_SIZE_SMALL
  1826. );
  1827. $friend_avatar = '<img src="'.$friendAvatarMedium.'" id="imgfriend_'.$friend['friend_user_id'].'" title="'.$name_user.'" class="user-image"/>';
  1828. $relation = SocialManager::get_relation_between_contacts(
  1829. $friend['friend_user_id'],
  1830. api_get_user_id()
  1831. );
  1832. $showLinkToChat = api_is_global_chat_enabled() && $friend['friend_user_id'] != api_get_user_id() && $relation == USER_RELATION_TYPE_FRIEND;
  1833. if ($showLinkToChat) {
  1834. $friendHtml .= '<a onclick="javascript:chatWith(\''.$friend['friend_user_id'].'\', \''.$name_user.'\', \''.$status.'\',\''.$friendAvatarSmall.'\')" href="javascript:void(0);" class="list-group-item">';
  1835. $friendHtml .= $friend_avatar.' <span class="username">'.$name_user.'</span>';
  1836. $friendHtml .= '<span class="status">'.$statusIcon.'</span>';
  1837. } else {
  1838. $link_shared = empty($link_shared) ? '' : '&'.$link_shared;
  1839. $friendHtml .= '<a href="profile.php?'.'u='.$friend['friend_user_id'].$link_shared.'" class="list-group-item">';
  1840. $friendHtml .= $friend_avatar.' <span class="username-all">'.$name_user.'</span>';
  1841. }
  1842. $friendHtml .= '</a>';
  1843. }
  1844. $j++;
  1845. }
  1846. $friendHtml .= '</div>';
  1847. } else {
  1848. $friendHtml .= '<div class="help">'.get_lang('NoFriendsInYourContactList').' '
  1849. .'<a href="'.api_get_path(WEB_PATH).'whoisonline.php"><em class="fa fa-search"></em> '.get_lang('TryAndFindSomeFriends').'</a></div>';
  1850. }
  1851. return $friendHtml;
  1852. }
  1853. /**
  1854. * @return string
  1855. */
  1856. public static function getWallForm($show_full_profile = true)
  1857. {
  1858. if ($show_full_profile) {
  1859. $userId = isset($_GET['u']) ? '?u='.intval($_GET['u']) : '';
  1860. $form = new FormValidator(
  1861. 'social_wall_main',
  1862. 'post',
  1863. api_get_path(WEB_CODE_PATH).'social/profile.php'.$userId,
  1864. null,
  1865. array('enctype' => 'multipart/form-data'),
  1866. FormValidator::LAYOUT_HORIZONTAL
  1867. );
  1868. $socialWallPlaceholder = isset($_GET['u']) ? get_lang('SocialWallWriteNewPostToFriend') : get_lang('SocialWallWhatAreYouThinkingAbout');
  1869. $form->addTextarea(
  1870. 'social_wall_new_msg_main',
  1871. null,
  1872. [
  1873. 'placeholder' => $socialWallPlaceholder,
  1874. 'cols-size' => [1, 10, 1],
  1875. 'aria-label' => $socialWallPlaceholder
  1876. ]
  1877. );
  1878. $form->addHidden('url_content', '');
  1879. $form->addButtonSend(
  1880. get_lang('Post'),
  1881. 'wall_post_button',
  1882. false,
  1883. ['cols-size' => [1, 10, 1]]
  1884. );
  1885. $html = Display::panel($form->returnForm(), get_lang('SocialWall'));
  1886. return $html;
  1887. }
  1888. }
  1889. /**
  1890. * @param int $userId
  1891. * @param int $friendId
  1892. * @return string
  1893. */
  1894. public static function getWallMessagesByUser($userId, $friendId)
  1895. {
  1896. $messages = self::getWallMessagesPostHTML($userId, $friendId);
  1897. $html = '';
  1898. foreach ($messages as $message) {
  1899. $post = $message['html'];
  1900. $comment = self::getWallMessagesHTML($userId, $friendId, $message['id']);
  1901. $html .= Display::panel($post.$comment, '');
  1902. }
  1903. return $html;
  1904. }
  1905. /**
  1906. * Get HTML code block for user skills
  1907. * @param int $userId The user ID
  1908. * @return string
  1909. */
  1910. public static function getSkillBlock($userId)
  1911. {
  1912. if (Skill::isAllow($userId, false) === false) {
  1913. return '';
  1914. }
  1915. $skill = new Skill();
  1916. $ranking = $skill->get_user_skill_ranking($userId);
  1917. $skills = $skill->getUserSkills($userId, true);
  1918. $template = new Template(null, false, false, false, false, false);
  1919. $template->assign('ranking', $ranking);
  1920. $template->assign('skills', $skills);
  1921. $template->assign('user_id', $userId);
  1922. $template->assign(
  1923. 'show_skills_report_link',
  1924. api_is_student() || api_is_student_boss() || api_is_drh()
  1925. );
  1926. $skillBlock = $template->get_template('social/skills_block.tpl');
  1927. return $template->fetch($skillBlock);
  1928. }
  1929. }