social.lib.php 80 KB

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