social.lib.php 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class provides methods for the social network management.
  5. * Include/require it in your code to use its features.
  6. *
  7. * @package chamilo.social
  8. */
  9. /**
  10. * Code
  11. */
  12. /**
  13. *
  14. * @package chamilo.social
  15. */
  16. class SocialManager extends UserManager
  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. $tbl_my_friend_relation_type = Database :: get_main_table(TABLE_MAIN_USER_FRIEND_RELATION_TYPE);
  30. $sql = 'SELECT id,title FROM '.$tbl_my_friend_relation_type.' WHERE id<>6 ORDER BY id ASC';
  31. $result = Database::query($sql);
  32. while ($row = Database::fetch_array($result, 'ASSOC')) {
  33. $friend_relation_list[] = $row;
  34. }
  35. $count_list = count($friend_relation_list);
  36. if ($count_list == 0) {
  37. $friend_relation_list[] = get_lang('Unknown');
  38. } else {
  39. return $friend_relation_list;
  40. }
  41. }
  42. /**
  43. * Get relation type contact by name
  44. * @param string names of the kind of relation
  45. * @return int
  46. * @author isaac flores paz
  47. */
  48. public static function get_relation_type_by_name($relation_type_name)
  49. {
  50. $list_type_friend = self::show_list_type_friends();
  51. foreach ($list_type_friend as $value_type_friend) {
  52. if (strtolower($value_type_friend['title']) == $relation_type_name) {
  53. return $value_type_friend['id'];
  54. }
  55. }
  56. }
  57. /**
  58. * Get the kind of relation between contacts
  59. * @param int user id
  60. * @param int user friend id
  61. * @param string
  62. * @author isaac flores paz
  63. */
  64. public static function get_relation_between_contacts($user_id, $user_friend)
  65. {
  66. $tbl_my_friend_relation_type = Database :: get_main_table(TABLE_MAIN_USER_FRIEND_RELATION_TYPE);
  67. $tbl_my_friend = Database :: get_main_table(TABLE_MAIN_USER_REL_USER);
  68. $sql = 'SELECT rt.id as id FROM '.$tbl_my_friend_relation_type.' rt
  69. WHERE rt.id = (
  70. SELECT uf.relation_type FROM '.$tbl_my_friend.' uf
  71. WHERE
  72. user_id='.((int) $user_id).' AND
  73. friend_user_id='.((int) $user_friend).' AND
  74. uf.relation_type <> '.USER_RELATION_TYPE_RRHH.'
  75. LIMIT 1
  76. )';
  77. $res = Database::query($sql);
  78. if (Database::num_rows($res) > 0) {
  79. $row = Database::fetch_array($res, 'ASSOC');
  80. return $row['id'];
  81. } else {
  82. return USER_UNKNOW;
  83. }
  84. }
  85. /**
  86. * Gets friends id list
  87. * @param int user id
  88. * @param int group id
  89. * @param string name to search
  90. * @param bool true will load firstname, lastname, and image name
  91. * @return array
  92. * @author Julio Montoya <gugli100@gmail.com> Cleaning code, function renamed, $load_extra_info option added
  93. * @author isaac flores paz
  94. */
  95. public static function get_friends($user_id, $id_group = null, $search_name = null, $load_extra_info = true)
  96. {
  97. $list_ids_friends = array();
  98. $tbl_my_friend = Database :: get_main_table(TABLE_MAIN_USER_REL_USER);
  99. $tbl_my_user = Database :: get_main_table(TABLE_MAIN_USER);
  100. $sql = 'SELECT friend_user_id FROM '.$tbl_my_friend.' WHERE relation_type NOT IN ('.USER_RELATION_TYPE_DELETED.', '.USER_RELATION_TYPE_RRHH.') AND friend_user_id<>'.((int) $user_id).' AND user_id='.((int) $user_id);
  101. if (isset($id_group) && $id_group > 0) {
  102. $sql.=' AND relation_type='.$id_group;
  103. }
  104. if (isset($search_name)) {
  105. $search_name = trim($search_name);
  106. $search_name = str_replace(' ', '', $search_name);
  107. $sql.=' AND friend_user_id IN (SELECT user_id FROM '.$tbl_my_user.' WHERE firstName LIKE "%'.Database::escape_string($search_name).'%" OR lastName LIKE "%'.Database::escape_string($search_name).'%" OR '.(api_is_western_name_order() ? 'concat(firstName, lastName)' : 'concat(lastName, firstName)').' like concat("%","'.Database::escape_string($search_name).'","%") ) ';
  108. }
  109. $res = Database::query($sql);
  110. while ($row = Database::fetch_array($res, 'ASSOC')) {
  111. if ($load_extra_info) {
  112. $path = UserManager::get_user_picture_path_by_id($row['friend_user_id'], 'web', false, true);
  113. $my_user_info = api_get_user_info($row['friend_user_id']);
  114. $list_ids_friends[] = array(
  115. 'user_info' => $my_user_info,
  116. 'friend_user_id'=>$row['friend_user_id'],
  117. 'firstName'=>$my_user_info['firstName'] ,
  118. 'lastName'=>$my_user_info['lastName'],
  119. 'username'=>$my_user_info['username'],
  120. 'image'=>$path['file']);
  121. } else {
  122. $list_ids_friends[] = $row;
  123. }
  124. }
  125. return $list_ids_friends;
  126. }
  127. /**
  128. * get list web path of contacts by user id
  129. * @param int user id
  130. * @param int group id
  131. * @param string name to search
  132. * @param array
  133. * @author isaac flores paz
  134. */
  135. public static function get_list_path_web_by_user_id($user_id, $id_group = null, $search_name = null)
  136. {
  137. $combine_friend = array();
  138. $list_ids = self::get_friends($user_id, $id_group, $search_name);
  139. if (is_array($list_ids)) {
  140. foreach ($list_ids as $values_ids) {
  141. $list_path_image_friend[] = UserManager::get_user_picture_path_by_id($values_ids['friend_user_id'], 'web', false, true);
  142. $combine_friend = array('id_friend' => $list_ids, 'path_friend' => $list_path_image_friend);
  143. }
  144. }
  145. return $combine_friend;
  146. }
  147. /**
  148. * get web path of user invitate
  149. * @author isaac flores paz
  150. * @author Julio Montoya setting variable array
  151. * @param int user id
  152. * @return array
  153. */
  154. public static function get_list_web_path_user_invitation_by_user_id($user_id)
  155. {
  156. $list_ids = self::get_list_invitation_of_friends_by_user_id((int) $user_id);
  157. $list_path_image_friend = array();
  158. foreach ($list_ids as $values_ids) {
  159. $list_path_image_friend[] = UserManager::get_user_picture_path_by_id($values_ids['user_sender_id'], 'web', false, true);
  160. }
  161. return $list_path_image_friend;
  162. }
  163. /**
  164. * Sends an invitation to contacts
  165. * @param int user id
  166. * @param int user friend id
  167. * @param string title of the message
  168. * @param string content of the message
  169. * @return boolean
  170. * @author isaac flores paz
  171. * @author Julio Montoya <gugli100@gmail.com> Cleaning code
  172. */
  173. public static function send_invitation_friend($user_id, $friend_id, $message_title, $message_content)
  174. {
  175. $tbl_message = Database::get_main_table(TABLE_MAIN_MESSAGE);
  176. $user_id = intval($user_id);
  177. $friend_id = intval($friend_id);
  178. //Just in case we replace the and \n and \n\r while saving in the DB
  179. $message_content = str_replace(array("\n", "\n\r"), '<br />', $message_content);
  180. $clean_message_title = Database::escape_string($message_title);
  181. $clean_message_content = Database::escape_string($message_content);
  182. $now = api_get_utc_datetime();
  183. $sql_exist = 'SELECT COUNT(*) AS count FROM '.$tbl_message.' WHERE user_sender_id='.$user_id.' AND user_receiver_id='.$friend_id.' AND msg_status IN(5,6,7);';
  184. $res_exist = Database::query($sql_exist);
  185. $row_exist = Database::fetch_array($res_exist, 'ASSOC');
  186. if ($row_exist['count'] == 0) {
  187. $sql = ' INSERT INTO '.$tbl_message.'(user_sender_id,user_receiver_id,msg_status,send_date,title,content)
  188. VALUES('.$user_id.','.$friend_id.','.MESSAGE_STATUS_INVITATION_PENDING.',"'.$now.'","'.$clean_message_title.'","'.$clean_message_content.'") ';
  189. Database::query($sql);
  190. $sender_info = api_get_user_info($user_id);
  191. $notification = new Notification();
  192. $notification->save_notification(Notification::NOTIFICATION_TYPE_INVITATION, array($friend_id), $message_title, $message_content, $sender_info);
  193. return true;
  194. } else {
  195. //invitation already exist
  196. $sql_if_exist = 'SELECT COUNT(*) AS count, id FROM '.$tbl_message.' WHERE user_sender_id='.$user_id.' AND user_receiver_id='.$friend_id.' AND msg_status = 7';
  197. $res_if_exist = Database::query($sql_if_exist);
  198. $row_if_exist = Database::fetch_array($res_if_exist, 'ASSOC');
  199. if ($row_if_exist['count'] == 1) {
  200. $sql_if_exist_up = 'UPDATE '.$tbl_message.'SET msg_status=5, content = "'.$clean_message_content.'" WHERE user_sender_id='.$user_id.' AND user_receiver_id='.$friend_id.' AND msg_status = 7 ';
  201. Database::query($sql_if_exist_up);
  202. return true;
  203. } else {
  204. return false;
  205. }
  206. }
  207. }
  208. /**
  209. * Get number messages of the inbox
  210. * @author isaac flores paz
  211. * @param int user receiver id
  212. * @return int
  213. */
  214. public static function get_message_number_invitation_by_user_id($user_receiver_id)
  215. {
  216. $tbl_message = Database::get_main_table(TABLE_MAIN_MESSAGE);
  217. $sql = 'SELECT COUNT(*) as count_message_in_box FROM '.$tbl_message.' WHERE user_receiver_id='.intval($user_receiver_id).' AND msg_status='.MESSAGE_STATUS_INVITATION_PENDING;
  218. $res = Database::query($sql);
  219. $row = Database::fetch_array($res, 'ASSOC');
  220. return $row['count_message_in_box'];
  221. }
  222. /**
  223. * Get invitation list received by user
  224. * @author isaac flores paz
  225. * @param int user id
  226. * @return array()
  227. */
  228. public static function get_list_invitation_of_friends_by_user_id($user_id)
  229. {
  230. $list_friend_invitation = array();
  231. $tbl_message = Database::get_main_table(TABLE_MAIN_MESSAGE);
  232. $sql = 'SELECT user_sender_id,send_date,title,content
  233. FROM '.$tbl_message.'
  234. WHERE user_receiver_id='.intval($user_id).' AND msg_status = '.MESSAGE_STATUS_INVITATION_PENDING;
  235. $res = Database::query($sql);
  236. while ($row = Database::fetch_array($res, 'ASSOC')) {
  237. $list_friend_invitation[] = $row;
  238. }
  239. return $list_friend_invitation;
  240. }
  241. /**
  242. * Get invitation list sent by user
  243. * @author Julio Montoya <gugli100@gmail.com>
  244. * @param int user id
  245. * @return array()
  246. */
  247. public static function get_list_invitation_sent_by_user_id($user_id)
  248. {
  249. $list_friend_invitation = array();
  250. $tbl_message = Database::get_main_table(TABLE_MAIN_MESSAGE);
  251. $sql = 'SELECT user_receiver_id, send_date,title,content
  252. FROM '.$tbl_message.'
  253. WHERE user_sender_id = '.intval($user_id).' AND msg_status = '.MESSAGE_STATUS_INVITATION_PENDING;
  254. $res = Database::query($sql);
  255. while ($row = Database::fetch_array($res, 'ASSOC')) {
  256. $list_friend_invitation[$row['user_receiver_id']] = $row;
  257. }
  258. return $list_friend_invitation;
  259. }
  260. /**
  261. * Accepts invitation
  262. * @param int $user_send_id
  263. * @param int $user_receiver_id
  264. * @author isaac flores paz
  265. * @author Julio Montoya <gugli100@gmail.com> Cleaning code
  266. */
  267. public static function invitation_accepted($user_send_id, $user_receiver_id)
  268. {
  269. $tbl_message = Database::get_main_table(TABLE_MAIN_MESSAGE);
  270. $sql = "UPDATE $tbl_message
  271. SET msg_status = ".MESSAGE_STATUS_INVITATION_ACCEPTED."
  272. WHERE
  273. user_sender_id = ".((int) $user_send_id)." AND
  274. user_receiver_id=".((int) $user_receiver_id)." AND
  275. msg_status = ".MESSAGE_STATUS_INVITATION_PENDING;
  276. Database::query($sql);
  277. }
  278. /**
  279. * Denies invitation
  280. * @param int user sender id
  281. * @param int user receiver id
  282. * @author isaac flores paz
  283. * @author Julio Montoya <gugli100@gmail.com> Cleaning code
  284. */
  285. public static function invitation_denied($user_send_id, $user_receiver_id)
  286. {
  287. $tbl_message = Database::get_main_table(TABLE_MAIN_MESSAGE);
  288. $sql = 'DELETE FROM '.$tbl_message.'
  289. WHERE
  290. user_sender_id = '.((int) $user_send_id).' AND
  291. user_receiver_id='.((int) $user_receiver_id).' AND
  292. msg_status = '.MESSAGE_STATUS_INVITATION_PENDING;
  293. Database::query($sql);
  294. }
  295. /**
  296. * allow attach to group
  297. * @author isaac flores paz
  298. * @param int user to qualify
  299. * @param int kind of rating
  300. * @return void()
  301. */
  302. public static function qualify_friend($id_friend_qualify, $type_qualify)
  303. {
  304. $tbl_user_friend = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  305. $user_id = api_get_user_id();
  306. $sql = 'UPDATE '.$tbl_user_friend.' SET relation_type='.((int) $type_qualify).'
  307. WHERE user_id = '.((int) $user_id).' AND friend_user_id='.(int) $id_friend_qualify;
  308. Database::query($sql);
  309. }
  310. /**
  311. * Sends invitations to friends
  312. * @author Isaac Flores Paz <isaac.flores.paz@gmail.com>
  313. * @author Julio Montoya <gugli100@gmail.com> Cleaning code
  314. * @param void
  315. * @return string message invitation
  316. */
  317. public static function send_invitation_friend_user($userfriend_id, $subject_message = '', $content_message = '')
  318. {
  319. global $charset;
  320. $user_info = array();
  321. $user_info = api_get_user_info($userfriend_id);
  322. $succes = get_lang('MessageSentTo');
  323. $succes.= ' : '.api_get_person_name($user_info['firstName'], $user_info['lastName']);
  324. if (isset($subject_message) && isset($content_message) && isset($userfriend_id)) {
  325. $send_message = MessageManager::send_message($userfriend_id, $subject_message, $content_message);
  326. if ($send_message) {
  327. echo Display::display_confirmation_message($succes, true);
  328. } else {
  329. echo Display::display_error_message(get_lang('ErrorSendingMessage'), true);
  330. }
  331. return false;
  332. } elseif (isset($userfriend_id) && !isset($subject_message)) {
  333. $count_is_true = false;
  334. if (isset($userfriend_id) && $userfriend_id > 0) {
  335. $message_title = get_lang('Invitation');
  336. $count_is_true = self::send_invitation_friend(api_get_user_id(), $userfriend_id, $message_title, $content_message);
  337. if ($count_is_true) {
  338. echo Display::display_confirmation_message(api_htmlentities(get_lang('InvitationHasBeenSent'), ENT_QUOTES, $charset), false);
  339. } else {
  340. echo Display::display_warning_message(api_htmlentities(get_lang('YouAlreadySentAnInvitation'), ENT_QUOTES, $charset), false);
  341. }
  342. }
  343. }
  344. }
  345. /**
  346. * Get user's feeds
  347. * @param int User ID
  348. * @param int Limit of posts per feed
  349. * @return string HTML section with all feeds included
  350. * @author Yannick Warnier
  351. * @since Dokeos 1.8.6.1
  352. */
  353. public static function get_user_feeds($user, $limit = 5)
  354. {
  355. $feed = UserManager::get_extra_user_data_by_field($user, 'rssfeeds');
  356. if (empty($feed)) {
  357. return '';
  358. }
  359. $feeds = explode(';', $feed['rssfeeds']);
  360. if (count($feeds) == 0) {
  361. return '';
  362. }
  363. $res = '';
  364. foreach ($feeds as $url) {
  365. if (empty($url)) {
  366. continue;
  367. }
  368. $rss = Zend\Feed\Reader\Reader::import($url);
  369. $i = 1;
  370. if (!empty($rss)) {
  371. $icon_rss = '';
  372. if (!empty($feed)) {
  373. $icon_rss = Display::url(
  374. Display::return_icon('rss.png', '', array(), 32),
  375. Security::remove_XSS($feed['rssfeeds']), array('target'=>'_blank')
  376. );
  377. }
  378. $res .= '<h2>'.$rss->getTitle().''.$icon_rss.'</h2>';
  379. $res .= '<div class="social-rss-channel-items">';
  380. /** @var Zend\Feed\Reader\Extension\Atom\Entry $item */
  381. foreach ($rss as $item) {
  382. if ($limit >= 0 and $i > $limit) {
  383. break;
  384. }
  385. $res .= '<h3><a href="'.$item->getTitle().'">'.$item->getTitle().'</a></h3>';
  386. $res .= '<div class="social-rss-item-date">'.$item->getDateCreated()->format('Y-m-d').'</div>';
  387. $res .= '<div class="social-rss-item-content">'.$item->getDescription().'</div><br />';
  388. $i++;
  389. }
  390. $res .= '</div>';
  391. }
  392. }
  393. return $res;
  394. }
  395. /**
  396. * Helper functions definition
  397. */
  398. public static function get_logged_user_course_html($my_course, $count) {
  399. //initialise
  400. $result = '';
  401. $course_code = $my_course['code'];
  402. $course_title = $my_course['course_info']['title'];
  403. $course_id = $my_course['course_info']['real_id'];
  404. $course_access_settings = CourseManager :: get_access_settings($course_code);
  405. $course_visibility = $course_access_settings['visibility'];
  406. $user_in_course_status = CourseManager :: get_user_in_course_status(api_get_user_id(), $course_code);
  407. $s_htlm_status_icon = Display::return_icon('course.gif', get_lang('Course'));
  408. //display course entry
  409. $result .= '<div id="div_'.$count.'">';
  410. $result .= '<h3>'.Display::return_icon('nolines_plus.gif', null, array('id' => 'btn_'.$count, 'onclick' => 'toogle_course(this,\''.$course_id.'\' )'));
  411. $result .= $s_htlm_status_icon;
  412. //show a hyperlink to the course, unless the course is closed and user is not course admin
  413. if ($course_visibility != COURSE_VISIBILITY_HIDDEN && ($course_visibility != COURSE_VISIBILITY_CLOSED || $user_in_course_status == COURSEMANAGER)) {
  414. $result .= '<a href="javascript:void(0)" id="ln_'.$count.'" onclick=toogle_course(this,\''.$course_id.'\');>&nbsp;'.$course_title.'</a>';
  415. } else {
  416. $result .= $course_title." "." ".get_lang('CourseClosed')."";
  417. }
  418. $result .= '</h3>';
  419. $result .= '</li>';
  420. $result .= '</div>';
  421. return $result;
  422. }
  423. /**
  424. * Shows the right menu of the Social Network tool
  425. *
  426. * @param string highlight link possible values: group_add, home, messages, messages_inbox, messages_compose ,messages_outbox ,invitations, shared_profile, friends, groups search
  427. * @param int group id
  428. * @param int user id
  429. * @param bool show profile or not (show or hide the user image/information)
  430. *
  431. */
  432. public static function show_social_menu($show = '', $group_id = 0, $user_id = 0, $show_full_profile = false, $show_delete_account_button = false)
  433. {
  434. if (empty($user_id)) {
  435. $user_id = api_get_user_id();
  436. }
  437. $usergroup = new UserGroup();
  438. $user_info = api_get_user_info($user_id, true);
  439. $current_user_id = api_get_user_id();
  440. $current_user_info = api_get_user_info($current_user_id, true);
  441. if ($current_user_id == $user_id) {
  442. $user_friend_relation = null;
  443. } else {
  444. $user_friend_relation = SocialManager::get_relation_between_contacts($current_user_id, $user_id);
  445. }
  446. $show_groups = array('groups', 'group_messages', 'messages_list', 'group_add', 'mygroups', 'group_edit', 'member_list', 'invite_friends', 'waiting_list', 'browse_groups');
  447. // get count unread message and total invitations
  448. $count_unread_message = MessageManager::get_number_of_messages(true);
  449. $count_unread_message = (!empty($count_unread_message)? Display::badge($count_unread_message) :'');
  450. $number_of_new_messages_of_friend = SocialManager::get_message_number_invitation_by_user_id(api_get_user_id());
  451. $group_pending_invitations = $usergroup->get_groups_by_user(api_get_user_id(), GROUP_USER_PERMISSION_PENDING_INVITATION, false);
  452. $group_pending_invitations = count($group_pending_invitations);
  453. $total_invitations = $number_of_new_messages_of_friend + $group_pending_invitations;
  454. $total_invitations = (!empty($total_invitations) ? Display::badge($total_invitations) :'');
  455. $html = '<div class="social-menu">';
  456. if (in_array($show, $show_groups) && !empty($group_id)) {
  457. //--- Group image
  458. $group_info = $usergroup->get($group_id);
  459. $big = $usergroup->get_picture_group($group_id, $group_info['picture'],160,GROUP_IMAGE_SIZE_BIG);
  460. $html .= '<div class="social-content-image">';
  461. $html .= '<div class="well social-background-content">';
  462. $html .= Display::url('<img src='.$big['file'].' class="social-groups-image" /> </a><br /><br />', api_get_path(WEB_PATH).'main/social/groups.php?id='.$group_id);
  463. if ($usergroup->is_group_admin($group_id, api_get_user_id())) {
  464. $html .= '<div id="edit_image" class="hidden_message" style="display:none"><a href="'.api_get_path(WEB_PATH).'main/social/group_edit.php?id='.$group_id.'">'.get_lang('EditGroup').'</a></div>';
  465. }
  466. $html .= '</div>';
  467. $html .= '</div>';
  468. } else {
  469. $img_array = UserManager::get_user_picture_path_by_id($user_id,'web',true,true);
  470. $big_image = UserManager::get_picture_user($user_id, $img_array['file'],'', USER_IMAGE_SIZE_BIG);
  471. $big_image = $big_image['file'];
  472. $normal_image = $img_array['dir'].$img_array['file'];
  473. //--- User image
  474. $html .= '<div class="well social-background-content">';
  475. if ($img_array['file'] != 'unknown.jpg') {
  476. $html .= '<a class="thumbnail ajax" href="'.$big_image.'"><img src='.$normal_image.' /> </a>';
  477. } else {
  478. $html .= '<img src='.$normal_image.' width="110px" />';
  479. }
  480. if (api_get_user_id() == $user_id) {
  481. $html .= '<div id="edit_image" class="hidden_message" style="display:none">';
  482. $html .= '<a href="'.api_get_path(WEB_PATH).'main/auth/profile.php">'.get_lang('EditProfile').'</a></div>';
  483. }
  484. $html .= '</div>';
  485. }
  486. if (!in_array($show, array('shared_profile', 'groups', 'group_edit', 'member_list','waiting_list','invite_friends'))) {
  487. $html .= '<div class="well sidebar-nav"><ul class="nav nav-list">';
  488. $active = $show=='home' ? 'active' : null;
  489. $html .= '<li class="'.$active.'"><a href="'.api_get_path(WEB_PATH).'main/social/home.php">'.Display::return_icon('home.png',get_lang('Home'),array()).get_lang('Home').'</a></li>';
  490. if (api_get_setting('allow_message_tool') == 'true') {
  491. $active = $show=='messages' ? 'active' : null;
  492. $html .= '<li class="'.$active.'"><a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php?f=social">'.Display::return_icon('instant_message.png',get_lang('Messages'),array()).get_lang('Messages').$count_unread_message.'</a></li>';
  493. }
  494. // Invitations
  495. if (api_get_setting('allow_message_tool') == 'true') {
  496. $active = $show=='invitations' ? 'active' : null;
  497. $html .= '<li class="'.$active.'"><a href="'.api_get_path(WEB_PATH).'main/social/invitations.php">'.Display::return_icon('invitation.png',get_lang('Invitations'),array()).get_lang('Invitations').$total_invitations.'</a></li>';
  498. }
  499. //Shared profile and groups
  500. $active = $show=='shared_profile' ? 'active' : null;
  501. $html .= '<li class="'.$active.'"><a href="'.api_get_path(WEB_PATH).'main/social/profile.php">'.Display::return_icon('my_shared_profile.png',get_lang('ViewMySharedProfile'),array()).get_lang('ViewMySharedProfile').'</a></li>';
  502. $active = $show=='friends' ? 'active' : null;
  503. $html .= '<li class="'.$active.'"><a href="'.api_get_path(WEB_PATH).'main/social/friends.php">'.Display::return_icon('friend.png',get_lang('Friends'),array()).get_lang('Friends').'</a></li>';
  504. $active = $show=='browse_groups' ? 'active' : null;
  505. $html .= '<li class="'.$active.'"><a href="'.api_get_path(WEB_PATH).'main/social/groups.php">'.Display::return_icon('group_s.png',get_lang('SocialGroups'),array()).get_lang('SocialGroups').'</a></li>';
  506. //Search users
  507. $active = $show=='search' ? 'active' : null;
  508. $html .= '<li class="'.$active.'"><a href="'.api_get_path(WEB_PATH).'main/social/search.php">'.Display::return_icon('zoom.png',get_lang('Search'), array()).get_lang('Search').'</a></li>';
  509. $html .='</ul>
  510. </div>';
  511. }
  512. if (in_array($show, $show_groups) && !empty($group_id)) {
  513. $html .= $usergroup->show_group_column_information($group_id, api_get_user_id(), $show);
  514. }
  515. if ($show == 'shared_profile') {
  516. //echo '<div align="center" class="social-menu-title" ><span class="social-menu-text1">'.get_lang('Menu').'</span></div>';
  517. $html .= '<div class="well sidebar-nav">
  518. <ul class="nav nav-list">';
  519. // My own profile
  520. if ($show_full_profile && $user_id == intval(api_get_user_id())) {
  521. $html .= '<li><a href="'.api_get_path(WEB_PATH).'main/social/home.php">'.Display::return_icon('home.png',get_lang('Home'),array()).get_lang('Home').'</a></li>';
  522. if (api_get_setting('allow_message_tool') == 'true') {
  523. $html .= '<li><a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php?f=social">'.Display::return_icon('instant_message.png', get_lang('Messages'),array()).get_lang('Messages').$count_unread_message.'</a></li>';
  524. $active = $show=='invitations' ? 'active' : null;
  525. $html .= '<li class="'.$active.'"><a href="'.api_get_path(WEB_PATH).'main/social/invitations.php">'.Display::return_icon('invitation.png',get_lang('Invitations'),array()).get_lang('Invitations').$total_invitations.'</a></li>';
  526. }
  527. $html .= '<li class="active"><a href="'.api_get_path(WEB_PATH).'main/social/profile.php">'.Display::return_icon('my_shared_profile.png', get_lang('ViewMySharedProfile'), array('style'=>'float:left')).''.get_lang('ViewMySharedProfile').'</a></li>
  528. <li><a href="'.api_get_path(WEB_PATH).'main/social/friends.php">'.Display::return_icon('friend.png',get_lang('Friends'),array()).get_lang('Friends').'</a></li>
  529. <li><a href="'.api_get_path(WEB_PATH).'main/social/groups.php">'.Display::return_icon('group_s.png', get_lang('SocialGroups'),array()).get_lang('SocialGroups').'</a></li>';
  530. $active = $show=='search' ? 'active' : null;
  531. $html .= '<li class="'.$active.'"><a href="'.api_get_path(WEB_PATH).'main/social/search.php">'.Display::return_icon('zoom.png',get_lang('Search'),array()).get_lang('Search').'</a></li>';
  532. }
  533. // My friend profile
  534. if (api_get_setting('allow_message_tool') == 'true') {
  535. if ($user_id != api_get_user_id()) {
  536. $html .= '<li><a href="javascript:void(0);" onclick="javascript:send_message_to_user(\''.$user_id.'\');" title="'.get_lang('SendMessage').'">';
  537. $html .= Display::return_icon('compose_message.png',get_lang('SendMessage')).'&nbsp;&nbsp;'.get_lang('SendMessage').'</a></li>';
  538. }
  539. //check if I already sent an invitation message
  540. $invitation_sent_list = SocialManager::get_list_invitation_sent_by_user_id(api_get_user_id());
  541. if (isset($invitation_sent_list[$user_id]) && is_array($invitation_sent_list[$user_id]) && count($invitation_sent_list[$user_id]) > 0 ) {
  542. $html .= '<li><a href="'.api_get_path(WEB_PATH).'main/social/invitations.php">'.Display::return_icon('invitation.png',get_lang('YouAlreadySentAnInvitation')).'&nbsp;&nbsp;'.get_lang('YouAlreadySentAnInvitation').'</a></li>';
  543. } else {
  544. if (!$show_full_profile) {
  545. $html .= '<li><a href="javascript:void(0);" onclick="javascript:send_invitation_to_user(\''.$user_id.'\');" title="'.get_lang('SendInvitation').'">'.Display :: return_icon('invitation.png', get_lang('SocialInvitationToFriends')).'&nbsp;'.get_lang('SendInvitation').'</a></li>';
  546. }
  547. }
  548. }
  549. //@todo check if user is online and if it's a friend to show the chat link
  550. if (api_is_global_chat_enabled()) {
  551. $user_name = $user_info['complete_name'];
  552. if ($user_friend_relation == USER_RELATION_TYPE_FRIEND) {
  553. if ($user_id != api_get_user_id()) {
  554. //Only show chat if I'm available to talk
  555. if ($current_user_info['user_is_online_in_chat'] == 1) {
  556. $options = array('onclick' => "javascript:chatWith('".$user_id."', '".Security::remove_XSS($user_name)."', '".$user_info['user_is_online_in_chat']."')");
  557. $chat_icon = $user_info['user_is_online_in_chat'] ? Display::return_icon('online.png', get_lang('Online')) : Display::return_icon('offline.png', get_lang('Offline'));
  558. $html .= Display::tag('li', Display::url($chat_icon.'&nbsp;&nbsp;'.get_lang('Chat'), 'javascript:void(0);', $options));
  559. }
  560. }
  561. } else {
  562. // Do something?
  563. if ($user_id != api_get_user_id()) {
  564. if ($current_user_info['user_is_online_in_chat'] == 1) {
  565. $message = Security::remove_XSS(sprintf(get_lang("YouHaveToAddXAsAFriendFirst"), $user_name));
  566. $options = array('onclick' => "javascript:chatNotYetWith('".$message."')");
  567. $chat_icon = $user_info['user_is_online_in_chat'] ? Display::return_icon('online.png', get_lang('Online')) : Display::return_icon('offline.png', get_lang('Offline'));
  568. $html .= Display::tag('li', Display::url($chat_icon.'&nbsp;&nbsp;'.get_lang('Chat'), 'javascript:void(0);', $options));
  569. }
  570. }
  571. }
  572. }
  573. $html .= '</ul></div>';
  574. if ($show_full_profile && $user_id == intval(api_get_user_id())) {
  575. $personal_course_list = UserManager::get_personal_session_course_list($user_id);
  576. $course_list_code = array();
  577. $i=1;
  578. if (is_array($personal_course_list)) {
  579. foreach ($personal_course_list as $my_course) {
  580. if ($i<=10) {
  581. $course_list_code[] = array('code' => $my_course['code']);
  582. } else {
  583. break;
  584. }
  585. $i++;
  586. }
  587. //to avoid repeted courses
  588. $course_list_code = ArrayClass::array_unique_dimensional($course_list_code);
  589. }
  590. //-----Announcements
  591. $my_announcement_by_user_id= intval($user_id);
  592. $announcements = array();
  593. foreach ($course_list_code as $course) {
  594. $course_info = api_get_course_info($course['code']);
  595. if (!empty($course_info)) {
  596. $content = AnnouncementManager::get_all_annoucement_by_user_course($course_info['code'], $my_announcement_by_user_id);
  597. if (!empty($content)) {
  598. $url = Display::url(Display::return_icon('announcement.png',get_lang('Announcements')).$course_info['name'].' ('.$content['count'].')', api_get_path(WEB_CODE_PATH).'announcements/announcements.php?cidReq='.$course['code']);
  599. $announcements[] = Display::tag('li', $url);
  600. }
  601. }
  602. }
  603. if (!empty($announcements)) {
  604. //echo '<div align="center" class="social-menu-title" ><span class="social-menu-text1">'.get_lang('ToolAnnouncement').'</span></div>';
  605. $html .= '<div class="social_menu_items">';
  606. $html .= '<ul>';
  607. foreach ($announcements as $announcement) {
  608. $html .= $announcement;
  609. }
  610. $html .= '</ul>';
  611. $html .= '</div>';
  612. }
  613. }
  614. }
  615. if ($show_delete_account_button) {
  616. $html .= '<div class="sidebar-nav"><ul><li>';
  617. $url = api_get_path(WEB_CODE_PATH).'auth/unsubscribe_account.php';
  618. $html .= Display::url(Display::return_icon('delete.png',get_lang('Unsubscribe'), array(), ICON_SIZE_TINY).get_lang('Unsubscribe'), $url);
  619. $html .= '</li></ul></div>';
  620. }
  621. $html .= '</div>';
  622. return $html;
  623. }
  624. /**
  625. * Displays a sortable table with the list of online users.
  626. * @param array $user_list
  627. */
  628. public static function display_user_list($user_list) {
  629. if (!isset($_GET['id'])) {
  630. $html = null;
  631. $column_size = '9';
  632. $add_row = false;
  633. if (api_is_anonymous()) {
  634. $column_size = '12';
  635. $add_row = true;
  636. }
  637. $extra_params = array();
  638. $course_url = '';
  639. if (isset($_GET['cidReq']) && !empty($_GET['cidReq'])) {
  640. $extra_params['cidReq'] = Security::remove_XSS($_GET['cidReq']);
  641. $course_url = '&amp;cidReq='.Security::remove_XSS($_GET['cidReq']);
  642. }
  643. if ($add_row) {
  644. $html .='<div class="row">';
  645. }
  646. $html .= '<div class="span'.$column_size.'">';
  647. $html .= '<ul id="online_grid_container" class="thumbnails">';
  648. foreach ($user_list as $uid) {
  649. $user_info = api_get_user_info($uid);
  650. //Anonymous users can't have access to the profile
  651. if (!api_is_anonymous()) {
  652. if (api_get_setting('allow_social_tool')=='true') {
  653. $url = $user_info['profile_url'].$course_url;
  654. } else {
  655. $url = '?id='.$uid.$course_url;
  656. }
  657. } else {
  658. $url = '#';
  659. }
  660. $image_array = UserManager::get_user_picture_path_by_id($uid, 'system', false, true);
  661. // reduce image
  662. $name = $user_info['complete_name'];
  663. $status_icon = Display::span('', array('class' => 'online_user_in_text'));
  664. $user_status = $user_info['status'] == 1 ? Display::span('', array('class' => 'teacher_online')) : Display::span('', array('class' => 'student_online'));
  665. if ($image_array['file'] == 'unknown.jpg' || !file_exists($image_array['dir'].$image_array['file'])) {
  666. $friends_profile['file'] = api_get_path(WEB_IMG_PATH).'unknown_180_100.jpg';
  667. $img = '<img title = "'.$name.'" alt="'.$name.'" src="'.$friends_profile['file'].'">';
  668. } else {
  669. $friends_profile = UserManager::get_picture_user($uid, $image_array['file'], 80, USER_IMAGE_SIZE_ORIGINAL);
  670. $img = '<img title = "'.$name.'" alt="'.$name.'" src="'.$friends_profile['file'].'">';
  671. }
  672. $name = '<a href="'.$url.'">'.$status_icon.$user_status.$name.'</a><br>';
  673. $html .= '<li class="span'.($column_size/3).'"><div class="thumbnail">'.$img.'<div class="caption">'.$name.'</div</div></li>';
  674. }
  675. $counter = $_SESSION['who_is_online_counter'];
  676. $html .= '</ul></div>';
  677. if (count($user_list) >= 9) {
  678. $html .= '<div class="span'.$column_size.'"><a class="btn btn-large" id="link_load_more_items" data_link="'.$counter.'" >'.get_lang('More').'</a></div>';
  679. }
  680. if ($add_row) {
  681. $html .= '</div>';
  682. }
  683. }
  684. return $html;
  685. }
  686. /**
  687. * Displays the information of an individual user
  688. * @param int $user_id
  689. */
  690. public static function display_individual_user($user_id, $returnContent = false) {
  691. global $interbreadcrumb;
  692. $safe_user_id = intval($user_id);
  693. $curretUserId = api_get_user_id();
  694. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  695. $sql = "SELECT * FROM $user_table WHERE user_id = ".$safe_user_id;
  696. $result = Database::query($sql);
  697. $userInfo = api_get_user_info($user_id);
  698. $content = null;
  699. if (Database::num_rows($result) == 1) {
  700. $user_object = Database::fetch_object($result);
  701. $alt = $userInfo['complete_name'].($curretUserId == $user_id ? '&nbsp;('.get_lang('Me').')' : '');
  702. $status = api_get_status_from_code($user_object->status);
  703. $interbreadcrumb[] = array('url' => SocialManager::getUserOnlineLink(), 'name' => get_lang('UsersOnLineList'));
  704. if ($returnContent == false) {
  705. Display::display_header($alt, null, $alt);
  706. }
  707. $content = '<div class ="thumbnail">';
  708. if (strlen(trim($user_object->picture_uri)) > 0) {
  709. $sysdir_array = UserManager::get_user_picture_path_by_id($safe_user_id, 'system');
  710. $sysdir = $sysdir_array['dir'];
  711. $webdir_array = UserManager::get_user_picture_path_by_id($safe_user_id, 'web');
  712. $webdir = $webdir_array['dir'];
  713. $fullurl = $webdir.$user_object->picture_uri;
  714. $system_image_path = $sysdir.$user_object->picture_uri;
  715. list($width, $height, $type, $attr) = @getimagesize($system_image_path);
  716. $height += 30;
  717. $width += 30;
  718. // get the path,width and height from original picture
  719. $big_image = $webdir.'big_'.$user_object->picture_uri;
  720. $big_image_size = api_getimagesize($big_image);
  721. $big_image_width = $big_image_size['width'];
  722. $big_image_height = $big_image_size['height'];
  723. $url_big_image = $big_image.'?rnd='.time();
  724. //echo '<a href="javascript:void()" onclick="javascript: return show_image(\''.$url_big_image.'\',\''.$big_image_width.'\',\''.$big_image_height.'\');" >';
  725. $content .= '<img src="'.$fullurl.'" alt="'.$alt.'" />';
  726. } else {
  727. $content .= Display::return_icon('unknown.jpg', get_lang('Unknown'));
  728. }
  729. if (!empty($status)) {
  730. $content .= '<div class="caption">'.$status.'</div>';
  731. }
  732. $content .= '</div>';
  733. if (api_get_setting('show_email_addresses') == 'true') {
  734. $content .= Display::encrypted_mailto_link($user_object->email,$user_object->email).'<br />';
  735. }
  736. if ($user_object->competences) {
  737. $content .= Display::page_subheader(get_lang('MyCompetences'));
  738. $content .= '<p>'.$user_object->competences.'</p>';
  739. }
  740. if ($user_object->diplomas) {
  741. $content .= Display::page_subheader(get_lang('MyDiplomas'));
  742. $content .= '<p>'.$user_object->diplomas.'</p>';
  743. }
  744. if ($user_object->teach) {
  745. $content .= Display::page_subheader(get_lang('MyTeach'));
  746. $content .= '<p>'.$user_object->teach.'</p>';
  747. }
  748. $content .= SocialManager::display_productions($user_object->user_id);
  749. if ($user_object->openarea) {
  750. $content .= Display::page_subheader(get_lang('MyPersonalOpenArea'));
  751. $content .= '<p>'.$user_object->openarea.'</p>';
  752. }
  753. } else {
  754. if ($returnContent == false) {
  755. Display::display_header(get_lang('UsersOnLineList'));
  756. }
  757. $content .= '<div class="actions-title">';
  758. $content .= get_lang('UsersOnLineList');
  759. $content .= '</div>';
  760. }
  761. if ($returnContent) {
  762. return $content;
  763. } else {
  764. echo $content;
  765. }
  766. }
  767. /**
  768. * Display productions in whoisonline
  769. * @param int $user_id User id
  770. */
  771. public static function display_productions($user_id, $returnContent = false) {
  772. $sysdir_array = UserManager::get_user_picture_path_by_id($user_id, 'system', true);
  773. $sysdir = $sysdir_array['dir'].$user_id.'/';
  774. $webdir_array = UserManager::get_user_picture_path_by_id($user_id, 'web', true);
  775. $webdir = $webdir_array['dir'].$user_id.'/';
  776. if (!is_dir($sysdir)) {
  777. mkdir($sysdir, api_get_permissions_for_new_directories(), true);
  778. }
  779. /*
  780. $handle = opendir($sysdir);
  781. $productions = array();
  782. while ($file = readdir($handle)) {
  783. if ($file == '.' || $file == '..' || $file == '.htaccess') {
  784. continue; // Skip current and parent directories
  785. }
  786. if (preg_match('/('.$user_id.'|[0-9a-f]{13}|saved)_.+\.(png|jpg|jpeg|gif)$/i', $file)) {
  787. // User's photos should not be listed as productions.
  788. continue;
  789. }
  790. $productions[] = $file;
  791. }
  792. */
  793. $content = null;
  794. $productions = UserManager::get_user_productions($user_id);
  795. if (count($productions) > 0) {
  796. $content .= '<dt><strong>'.get_lang('Productions').'</strong></dt>';
  797. $content .='<dd><ul>';
  798. foreach ($productions as $file) {
  799. // Only display direct file links to avoid browsing an empty directory
  800. if (is_file($sysdir.$file) && $file != $webdir_array['file']) {
  801. $content .= '<li><a href="'.$webdir.urlencode($file).'" target=_blank>'.$file.'</a></li>';
  802. }
  803. // Real productions are under a subdirectory by the User's id
  804. if (is_dir($sysdir.$file)) {
  805. $subs = scandir($sysdir.$file);
  806. foreach ($subs as $my => $sub) {
  807. if (substr($sub, 0, 1) != '.' && is_file($sysdir.$file.'/'.$sub)) {
  808. $content .= '<li><a href="'.$webdir.urlencode($file).'/'.urlencode($sub).'" target=_blank>'.$sub.'</a></li>';
  809. }
  810. }
  811. }
  812. }
  813. $content .= '</ul></dd>';
  814. }
  815. if ($returnContent) {
  816. return $content;
  817. } else {
  818. echo $content;
  819. }
  820. }
  821. /**
  822. * @param string $content
  823. * @param string $span_count
  824. * @return string
  825. */
  826. public static function social_wrapper_div($content, $span_count)
  827. {
  828. $span_count = intval($span_count);
  829. $html = '<div class="span'.$span_count.'">';
  830. $html .= '<div class="well_border">';
  831. $html .= $content;
  832. $html .= '</div></div>';
  833. return $html;
  834. }
  835. /**
  836. * Dummy function
  837. *
  838. */
  839. public static function get_plugins($place = SOCIAL_CENTER_PLUGIN) {
  840. $content = '';
  841. switch ($place) {
  842. case SOCIAL_CENTER_PLUGIN:
  843. $social_plugins = array(1, 2);
  844. if (is_array($social_plugins) && count($social_plugins)>0) {
  845. $content.= '<div id="social-plugins">';
  846. foreach($social_plugins as $plugin ) {
  847. $content.= '<div class="social-plugin-item">';
  848. $content.= $plugin;
  849. $content.= '</div>';
  850. }
  851. $content.= '</div>';
  852. }
  853. break;
  854. case SOCIAL_LEFT_PLUGIN:
  855. break;
  856. case SOCIAL_RIGHT_PLUGIN:
  857. break;
  858. }
  859. return $content;
  860. }
  861. /**
  862. *
  863. * @param $courseCode
  864. * @param null $sessionId
  865. * @return string
  866. */
  867. static function getUserOnlineLink($courseCode = null, $sessionId = null)
  868. {
  869. if (empty($courseCode) && empty($sessionId)) {
  870. return api_get_path(WEB_PATH).'whoisonline.php';
  871. //return api_get_path(WEB_PUBLIC_PATH).'users/online';
  872. }
  873. if (!empty($courseCode)) {
  874. return api_get_path(WEB_PATH).'whoisonline.php?cidReq='.$courseCode;
  875. //return api_get_path(WEB_PUBLIC_PATH).'users/online/course/'.$courseCode;
  876. }
  877. if (!empty($sessionId)) {
  878. return api_get_path(WEB_PATH).'whoisonlinesession.php?session_id='.$sessionId;
  879. //return api_get_path(WEB_PUBLIC_PATH).'users/online/session/'.$sessionId;
  880. }
  881. }
  882. }