social.lib.php 80 KB

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