social.ajax.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\Message;
  4. use Chamilo\CoreBundle\Entity\MessageFeedback;
  5. use ChamiloSession as Session;
  6. /**
  7. * Responses to AJAX calls.
  8. */
  9. require_once __DIR__.'/../global.inc.php';
  10. $action = isset($_GET['a']) ? $_GET['a'] : null;
  11. $current_user_id = api_get_user_id();
  12. switch ($action) {
  13. case 'add_friend':
  14. if (api_is_anonymous()) {
  15. echo '';
  16. break;
  17. }
  18. $relation_type = USER_RELATION_TYPE_UNKNOWN; //Unknown contact
  19. if (isset($_GET['is_my_friend'])) {
  20. $relation_type = USER_RELATION_TYPE_FRIEND; //My friend
  21. }
  22. if (isset($_GET['friend_id'])) {
  23. $my_current_friend = $_GET['friend_id'];
  24. UserManager::relate_users($current_user_id, $my_current_friend, $relation_type);
  25. UserManager::relate_users($my_current_friend, $current_user_id, $relation_type);
  26. SocialManager::invitation_accepted($my_current_friend, $current_user_id);
  27. Display::addFlash(
  28. Display::return_message(get_lang('AddedContactToList'), 'success')
  29. );
  30. header('Location: '.api_get_path(WEB_CODE_PATH).'social/invitations.php');
  31. exit;
  32. }
  33. break;
  34. case 'deny_friend':
  35. if (api_is_anonymous()) {
  36. echo '';
  37. break;
  38. }
  39. $relation_type = USER_RELATION_TYPE_UNKNOWN; //Contact unknown
  40. if (isset($_GET['is_my_friend'])) {
  41. $relation_type = USER_RELATION_TYPE_FRIEND; //my friend
  42. }
  43. if (isset($_GET['denied_friend_id'])) {
  44. SocialManager::invitation_denied($_GET['denied_friend_id'], $current_user_id);
  45. Display::addFlash(
  46. Display::return_message(get_lang('InvitationDenied'), 'success')
  47. );
  48. header('Location: '.api_get_path(WEB_CODE_PATH).'social/invitations.php');
  49. exit;
  50. }
  51. break;
  52. case 'delete_friend':
  53. if (api_is_anonymous()) {
  54. echo '';
  55. break;
  56. }
  57. $my_delete_friend = (int) $_POST['delete_friend_id'];
  58. if (isset($_POST['delete_friend_id'])) {
  59. SocialManager::remove_user_rel_user($my_delete_friend);
  60. }
  61. break;
  62. case 'show_my_friends':
  63. if (api_is_anonymous()) {
  64. echo '';
  65. break;
  66. }
  67. $user_id = api_get_user_id();
  68. $name_search = Security::remove_XSS($_POST['search_name_q']);
  69. $number_friends = 0;
  70. if (isset($name_search) && $name_search != 'undefined') {
  71. $friends = SocialManager::get_friends($user_id, null, $name_search);
  72. } else {
  73. $friends = SocialManager::get_friends($user_id);
  74. }
  75. $friend_html = '';
  76. $number_of_images = 8;
  77. $number_friends = count($friends);
  78. if ($number_friends != 0) {
  79. $number_loop = $number_friends / $number_of_images;
  80. $loop_friends = ceil($number_loop);
  81. $j = 0;
  82. for ($k = 0; $k < $loop_friends; $k++) {
  83. if ($j == $number_of_images) {
  84. $number_of_images = $number_of_images * 2;
  85. }
  86. while ($j < $number_of_images) {
  87. if (isset($friends[$j])) {
  88. $friend = $friends[$j];
  89. $user_name = api_xml_http_response_encode($friend['firstName'].' '.$friend['lastName']);
  90. $userPicture = UserManager::getUserPicture($friend['friend_user_id']);
  91. $friend_html .= '
  92. <div class="col-md-3">
  93. <div class="thumbnail text-center" id="div_'.$friends[$j]['friend_user_id'].'">
  94. <img src="'.$userPicture.'" class="img-responsive" id="imgfriend_'.$friend['friend_user_id'].'" title="$user_name">
  95. <div class="caption">
  96. <h3>
  97. <a href="profile.php?u='.$friend['friend_user_id'].'">'.$user_name.'</a>
  98. </h3>
  99. <p>
  100. <button class="btn btn-danger" onclick="delete_friend(this)" id=img_'.$friend['friend_user_id'].'>
  101. '.get_lang('Delete').'
  102. </button>
  103. </p>
  104. </div>
  105. </div>
  106. </div>
  107. ';
  108. }
  109. $j++;
  110. }
  111. }
  112. }
  113. echo $friend_html;
  114. break;
  115. case 'toogle_course':
  116. if (api_is_anonymous()) {
  117. echo '';
  118. break;
  119. }
  120. require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
  121. $user_id = Session::read('social_user_id');
  122. if ($_POST['action']) {
  123. $action = $_POST['action'];
  124. }
  125. switch ($action) {
  126. case 'load_course':
  127. $course_id = intval($_POST['course_code']); // the int course id
  128. $course_info = api_get_course_info_by_id($course_id);
  129. $course_code = $course_info['code'];
  130. if (api_is_user_of_course($course_id, api_get_user_id())) {
  131. //------Forum messages
  132. $forum_result = get_all_post_from_user($user_id, $course_code);
  133. $all_result_data = 0;
  134. if ($forum_result != '') {
  135. echo '<div id="social-forum-main-title">';
  136. echo api_xml_http_response_encode(get_lang('Forum'));
  137. echo '</div>';
  138. echo '<div style="background:#FAF9F6; padding:0px;" >';
  139. echo api_xml_http_response_encode($forum_result);
  140. echo '</div>';
  141. echo '<br />';
  142. $all_result_data++;
  143. }
  144. //------Blog posts
  145. $result = Blog::getBlogPostFromUser($course_id, $user_id, $course_code);
  146. if (!empty($result)) {
  147. api_display_tool_title(api_xml_http_response_encode(get_lang('Blog')));
  148. echo '<div style="background:#FAF9F6; padding:0px;">';
  149. echo api_xml_http_response_encode($result);
  150. echo '</div>';
  151. echo '<br />';
  152. $all_result_data++;
  153. }
  154. //------Blog comments
  155. $result = Blog::getBlogCommentsFromUser($course_id, $user_id, $course_code);
  156. if (!empty($result)) {
  157. echo '<div style="background:#FAF9F6; padding-left:10px;">';
  158. api_display_tool_title(api_xml_http_response_encode(get_lang('BlogComments')));
  159. echo api_xml_http_response_encode($result);
  160. echo '</div>';
  161. echo '<br />';
  162. $all_result_data++;
  163. }
  164. if ($all_result_data == 0) {
  165. echo api_xml_http_response_encode(get_lang('NoDataAvailable'));
  166. }
  167. } else {
  168. echo '<div class="clear"></div><br />';
  169. api_display_tool_title(api_xml_http_response_encode(get_lang('Details')));
  170. echo '<div style="background:#FAF9F6; padding:0px;">';
  171. echo api_xml_http_response_encode(get_lang('UserNonRegisteredAtTheCourse'));
  172. echo '<div class="clear"></div><br />';
  173. echo '</div>';
  174. echo '<div class="clear"></div><br />';
  175. }
  176. break;
  177. case 'unload_course':
  178. break;
  179. default:
  180. break;
  181. }
  182. break;
  183. case 'send_comment':
  184. if (api_is_anonymous()) {
  185. exit;
  186. }
  187. if (api_get_setting('allow_social_tool') !== 'true') {
  188. exit;
  189. }
  190. $messageId = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  191. if (empty($messageId)) {
  192. exit;
  193. }
  194. $userId = api_get_user_id();
  195. $messageInfo = MessageManager::get_message_by_id($messageId);
  196. if (!empty($messageInfo)) {
  197. $comment = isset($_REQUEST['comment']) ? $_REQUEST['comment'] : '';
  198. if (!empty($comment)) {
  199. $messageId = SocialManager::sendWallMessage(
  200. api_get_user_id(),
  201. $messageInfo['user_receiver_id'],
  202. $comment,
  203. $messageId,
  204. MESSAGE_STATUS_WALL
  205. );
  206. /*if ($messageId && !empty($_FILES['picture']['tmp_name'])) {
  207. self::sendWallMessageAttachmentFile(
  208. $friendId,
  209. $_FILES['picture'],
  210. $messageId
  211. );
  212. }*/
  213. if ($messageId) {
  214. $messageInfo = MessageManager::get_message_by_id($messageId);
  215. echo SocialManager::processPostComment($messageInfo);
  216. }
  217. }
  218. }
  219. break;
  220. case 'delete_message':
  221. if (api_is_anonymous()) {
  222. exit;
  223. }
  224. if (api_get_setting('allow_social_tool') !== 'true') {
  225. exit;
  226. }
  227. $messageId = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  228. if (empty($messageId)) {
  229. exit;
  230. }
  231. $userId = api_get_user_id();
  232. $messageInfo = MessageManager::get_message_by_id($messageId);
  233. if (!empty($messageInfo)) {
  234. $canDelete = ($messageInfo['user_receiver_id'] == $userId || $messageInfo['user_sender_id'] == $userId) &&
  235. empty($messageInfo['group_id']);
  236. if ($canDelete || api_is_platform_admin()) {
  237. SocialManager::deleteMessage($messageId);
  238. echo Display::return_message(get_lang('MessageDeleted'));
  239. break;
  240. }
  241. }
  242. break;
  243. case 'list_wall_message':
  244. if (api_is_anonymous()) {
  245. break;
  246. }
  247. $start = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
  248. $length = isset($_REQUEST['length']) ? (int) $_REQUEST['length'] : 10;
  249. $userId = isset($_REQUEST['u']) ? (int) $_REQUEST['u'] : api_get_user_id();
  250. $html = '';
  251. if ($userId == api_get_user_id()) {
  252. $threadList = SocialManager::getThreadList($userId);
  253. $threadIdList = [];
  254. if (!empty($threadList)) {
  255. $threadIdList = array_column($threadList, 'id');
  256. }
  257. $html = SocialManager::getMyWallMessages($userId, $start, SocialManager::DEFAULT_SCROLL_NEW_POST, $threadIdList);
  258. $html = $html['posts'];
  259. } else {
  260. $messages = SocialManager::getWallMessages(
  261. $userId,
  262. null,
  263. 0,
  264. 0,
  265. '',
  266. $start,
  267. SocialManager::DEFAULT_SCROLL_NEW_POST
  268. );
  269. $messages = SocialManager::formatWallMessages($messages);
  270. if (!empty($messages)) {
  271. ksort($messages);
  272. foreach ($messages as $message) {
  273. $post = $message['html'];
  274. $comments = SocialManager::getWallPostComments($userId, $message);
  275. $html .= SocialManager::wrapPost($message, $post.$comments);
  276. }
  277. }
  278. }
  279. if (!empty($html)) {
  280. $html .= Display::div(
  281. Display::url(
  282. get_lang('SeeMore'),
  283. api_get_self().'?u='.$userId.'&a=list_wall_message&start='.
  284. ($start + SocialManager::DEFAULT_SCROLL_NEW_POST).'&length='.SocialManager::DEFAULT_SCROLL_NEW_POST,
  285. [
  286. 'class' => 'nextPage',
  287. ]
  288. ),
  289. [
  290. 'class' => 'next',
  291. ]
  292. );
  293. }
  294. echo $html;
  295. break;
  296. // Read the Url using OpenGraph and returns the hyperlinks content
  297. case 'read_url_with_open_graph':
  298. $url = isset($_POST['social_wall_new_msg_main']) ? $_POST['social_wall_new_msg_main'] : '';
  299. $url = trim($url);
  300. $html = '';
  301. if (SocialManager::verifyUrl($url) == true) {
  302. $html = Security::remove_XSS(
  303. SocialManager::readContentWithOpenGraph($url)
  304. );
  305. }
  306. echo $html;
  307. break;
  308. case 'like_message':
  309. header('Content-Type: application/json');
  310. if (
  311. api_is_anonymous() ||
  312. !api_get_configuration_value('social_enable_messages_feedback')
  313. ) {
  314. echo json_encode(false);
  315. exit;
  316. }
  317. $messageId = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  318. $status = isset($_GET['status']) ? $_GET['status'] : '';
  319. $groupId = isset($_GET['group']) ? (int) $_GET['group'] : 0;
  320. if (empty($messageId) || !in_array($status, ['like', 'dislike'])) {
  321. echo json_encode(false);
  322. exit;
  323. }
  324. $em = Database::getManager();
  325. $messageRepo = $em->getRepository('ChamiloCoreBundle:Message');
  326. $messageLikesRepo = $em->getRepository('ChamiloCoreBundle:MessageFeedback');
  327. /** @var Message $message */
  328. $message = $messageRepo->find($messageId);
  329. if (empty($message)) {
  330. echo json_encode(false);
  331. exit;
  332. }
  333. if ((int) $message->getGroupId() !== $groupId) {
  334. echo json_encode(false);
  335. exit;
  336. }
  337. if (!empty($message->getGroupId())) {
  338. $usergroup = new UserGroup();
  339. $groupInfo = $usergroup->get($groupId);
  340. if (empty($groupInfo)) {
  341. echo json_encode(false);
  342. exit;
  343. }
  344. $isMember = $usergroup->is_group_member($groupId, $current_user_id);
  345. if (GROUP_PERMISSION_CLOSED == $groupInfo['visibility'] && !$isMember) {
  346. echo json_encode(false);
  347. exit;
  348. }
  349. }
  350. $user = api_get_user_entity($current_user_id);
  351. $userLike = $messageLikesRepo->findOneBy(['message' => $message, 'user' => $user]);
  352. if (empty($userLike)) {
  353. $userLike = new MessageFeedback();
  354. $userLike
  355. ->setMessage($message)
  356. ->setUser($user);
  357. }
  358. if ('like' === $status) {
  359. if ($userLike->isLiked()) {
  360. echo json_encode(false);
  361. exit;
  362. }
  363. $userLike
  364. ->setLiked(true)
  365. ->setDisliked(false);
  366. } elseif ('dislike' === $status) {
  367. if ($userLike->isDisliked()) {
  368. echo json_encode(false);
  369. exit;
  370. }
  371. $userLike
  372. ->setLiked(false)
  373. ->setDisliked(true);
  374. }
  375. $userLike
  376. ->setUpdatedAt(
  377. api_get_utc_datetime(null, false, true)
  378. );
  379. $em->persist($userLike);
  380. $em->flush();
  381. echo json_encode(true);
  382. break;
  383. default:
  384. echo '';
  385. }
  386. exit;