social.ajax.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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('Added contact to list'), '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('Invitation denied'), '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. if (isset($name_search) && $name_search != 'undefined') {
  70. $friends = SocialManager::get_friends($user_id, null, $name_search);
  71. } else {
  72. $friends = SocialManager::get_friends($user_id);
  73. }
  74. $friend_html = '';
  75. $number_of_images = 8;
  76. $number_friends = count($friends);
  77. if ($number_friends != 0) {
  78. $number_loop = $number_friends / $number_of_images;
  79. $loop_friends = ceil($number_loop);
  80. $j = 0;
  81. for ($k = 0; $k < $loop_friends; $k++) {
  82. if ($j == $number_of_images) {
  83. $number_of_images = $number_of_images * 2;
  84. }
  85. while ($j < $number_of_images) {
  86. if (isset($friends[$j])) {
  87. $friend = $friends[$j];
  88. $user_name = api_xml_http_response_encode($friend['firstName'].' '.$friend['lastName']);
  89. $userPicture = UserManager::getUserPicture($friend['friend_user_id']);
  90. $friend_html .= '
  91. <div class="col-md-3">
  92. <div class="thumbnail text-center" id="div_'.$friends[$j]['friend_user_id'].'">
  93. <img src="'.$userPicture.'" class="img-responsive" id="imgfriend_'.$friend['friend_user_id'].'" title="$user_name">
  94. <div class="caption">
  95. <h3>
  96. <a href="profile.php?u='.$friend['friend_user_id'].'">'.$user_name.'</a>
  97. </h3>
  98. <p>
  99. <button class="btn btn-danger" onclick="delete_friend(this)" id=img_'.$friend['friend_user_id'].'>
  100. '.get_lang('Delete').'
  101. </button>
  102. </p>
  103. </div>
  104. </div>
  105. </div>
  106. ';
  107. }
  108. $j++;
  109. }
  110. }
  111. }
  112. echo $friend_html;
  113. break;
  114. case 'toogle_course':
  115. if (api_is_anonymous()) {
  116. echo '';
  117. break;
  118. }
  119. require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
  120. $user_id = Session::read('social_user_id');
  121. if ($_POST['action']) {
  122. $action = $_POST['action'];
  123. }
  124. switch ($action) {
  125. case 'load_course':
  126. $course_id = intval($_POST['course_code']); // the int course id
  127. $course_info = api_get_course_info_by_id($course_id);
  128. $course_code = $course_info['code'];
  129. if (api_is_user_of_course($course_id, api_get_user_id())) {
  130. //------Forum messages
  131. $forum_result = get_all_post_from_user($user_id, $course_code);
  132. $all_result_data = 0;
  133. if ($forum_result != '') {
  134. echo '<div id="social-forum-main-title">';
  135. echo api_xml_http_response_encode(get_lang('Forum'));
  136. echo '</div>';
  137. echo '<div style="background:#FAF9F6; padding:0px;" >';
  138. echo api_xml_http_response_encode($forum_result);
  139. echo '</div>';
  140. echo '<br />';
  141. $all_result_data++;
  142. }
  143. //------Blog posts
  144. $result = Blog::getBlogPostFromUser($course_id, $user_id, $course_code);
  145. if (!empty($result)) {
  146. api_display_tool_title(api_xml_http_response_encode(get_lang('Blog')));
  147. echo '<div style="background:#FAF9F6; padding:0px;">';
  148. echo api_xml_http_response_encode($result);
  149. echo '</div>';
  150. echo '<br />';
  151. $all_result_data++;
  152. }
  153. //------Blog comments
  154. $result = Blog::getBlogCommentsFromUser($course_id, $user_id, $course_code);
  155. if (!empty($result)) {
  156. echo '<div style="background:#FAF9F6; padding-left:10px;">';
  157. api_display_tool_title(api_xml_http_response_encode(get_lang('Blog comments')));
  158. echo api_xml_http_response_encode($result);
  159. echo '</div>';
  160. echo '<br />';
  161. $all_result_data++;
  162. }
  163. if ($all_result_data == 0) {
  164. echo api_xml_http_response_encode(get_lang('No data available'));
  165. }
  166. } else {
  167. echo '<div class="clear"></div><br />';
  168. api_display_tool_title(api_xml_http_response_encode(get_lang('Details')));
  169. echo '<div style="background:#FAF9F6; padding:0px;">';
  170. echo api_xml_http_response_encode(get_lang('User not registered in course'));
  171. echo '<div class="clear"></div><br />';
  172. echo '</div>';
  173. echo '<div class="clear"></div><br />';
  174. }
  175. break;
  176. case 'unload_course':
  177. default:
  178. break;
  179. }
  180. break;
  181. case 'send_comment':
  182. if (api_is_anonymous()) {
  183. exit;
  184. }
  185. if (api_get_setting('allow_social_tool') !== 'true') {
  186. exit;
  187. }
  188. $messageId = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  189. if (empty($messageId)) {
  190. exit;
  191. }
  192. $userId = api_get_user_id();
  193. $messageInfo = MessageManager::get_message_by_id($messageId);
  194. if (!empty($messageInfo)) {
  195. $comment = isset($_REQUEST['comment']) ? $_REQUEST['comment'] : '';
  196. if (!empty($comment)) {
  197. $messageId = SocialManager::sendWallMessage(
  198. $userId,
  199. $messageInfo['user_receiver_id'],
  200. $comment,
  201. $messageId,
  202. MESSAGE_STATUS_WALL
  203. );
  204. if ($messageId) {
  205. $messageInfo = MessageManager::get_message_by_id($messageId);
  206. echo SocialManager::processPostComment($messageInfo);
  207. }
  208. }
  209. }
  210. break;
  211. case 'delete_message':
  212. if (api_is_anonymous()) {
  213. exit;
  214. }
  215. if (api_get_setting('allow_social_tool') !== 'true') {
  216. exit;
  217. }
  218. $messageId = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  219. if (empty($messageId)) {
  220. exit;
  221. }
  222. $userId = api_get_user_id();
  223. $messageInfo = MessageManager::get_message_by_id($messageId);
  224. if (!empty($messageInfo)) {
  225. $canDelete = ($messageInfo['user_receiver_id'] == $userId || $messageInfo['user_sender_id'] == $userId) &&
  226. empty($messageInfo['group_id']);
  227. if ($canDelete || api_is_platform_admin()) {
  228. SocialManager::deleteMessage($messageId);
  229. echo Display::return_message(get_lang('The message has been deleted'));
  230. break;
  231. }
  232. }
  233. break;
  234. case 'list_wall_message':
  235. if (api_is_anonymous()) {
  236. break;
  237. }
  238. $start = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
  239. $userId = isset($_REQUEST['u']) ? (int) $_REQUEST['u'] : api_get_user_id();
  240. $html = '';
  241. if ($userId == api_get_user_id()) {
  242. $threadList = SocialManager::getThreadList($userId);
  243. $threadIdList = [];
  244. if (!empty($threadList)) {
  245. $threadIdList = array_column($threadList, 'id');
  246. }
  247. $html = SocialManager::getMyWallMessages(
  248. $userId,
  249. $start,
  250. SocialManager::DEFAULT_SCROLL_NEW_POST,
  251. $threadIdList
  252. );
  253. $html = $html['posts'];
  254. } else {
  255. $messages = SocialManager::getWallMessages(
  256. $userId,
  257. null,
  258. 0,
  259. 0,
  260. '',
  261. $start,
  262. SocialManager::DEFAULT_SCROLL_NEW_POST
  263. );
  264. $messages = SocialManager::formatWallMessages($messages);
  265. if (!empty($messages)) {
  266. ksort($messages);
  267. foreach ($messages as $message) {
  268. $post = $message['html'];
  269. $comments = SocialManager::getWallPostComments($userId, $message);
  270. $html .= SocialManager::wrapPost($message, $post.$comments);
  271. }
  272. }
  273. }
  274. if (!empty($html)) {
  275. $html .= Display::div(
  276. Display::url(
  277. get_lang('See more'),
  278. api_get_self().'?u='.$userId.'&a=list_wall_message&start='.
  279. ($start + SocialManager::DEFAULT_SCROLL_NEW_POST).'&length='.SocialManager::DEFAULT_SCROLL_NEW_POST,
  280. [
  281. 'class' => 'nextPage',
  282. ]
  283. ),
  284. [
  285. 'class' => 'next',
  286. ]
  287. );
  288. }
  289. echo $html;
  290. break;
  291. // Read the Url using OpenGraph and returns the hyperlinks content
  292. case 'read_url_with_open_graph':
  293. $url = isset($_POST['social_wall_new_msg_main']) ? $_POST['social_wall_new_msg_main'] : '';
  294. $url = trim($url);
  295. $html = '';
  296. if (SocialManager::verifyUrl($url) == true) {
  297. $html = Security::remove_XSS(
  298. SocialManager::readContentWithOpenGraph($url)
  299. );
  300. }
  301. echo $html;
  302. break;
  303. case 'like_message':
  304. header('Content-Type: application/json');
  305. if (
  306. api_is_anonymous() ||
  307. !api_get_configuration_value('social_enable_messages_feedback')
  308. ) {
  309. echo json_encode(false);
  310. exit;
  311. }
  312. $messageId = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  313. $status = isset($_GET['status']) ? $_GET['status'] : '';
  314. $groupId = isset($_GET['group']) ? (int) $_GET['group'] : 0;
  315. if (empty($messageId) || !in_array($status, ['like', 'dislike'])) {
  316. echo json_encode(false);
  317. exit;
  318. }
  319. $em = Database::getManager();
  320. $messageRepo = $em->getRepository('ChamiloCoreBundle:Message');
  321. $messageLikesRepo = $em->getRepository('ChamiloCoreBundle:MessageFeedback');
  322. /** @var Message $message */
  323. $message = $messageRepo->find($messageId);
  324. if (empty($message)) {
  325. echo json_encode(false);
  326. exit;
  327. }
  328. if ((int) $message->getGroupId() !== $groupId) {
  329. echo json_encode(false);
  330. exit;
  331. }
  332. if (!empty($message->getGroupId())) {
  333. $usergroup = new UserGroup();
  334. $groupInfo = $usergroup->get($groupId);
  335. if (empty($groupInfo)) {
  336. echo json_encode(false);
  337. exit;
  338. }
  339. $isMember = $usergroup->is_group_member($groupId, $current_user_id);
  340. if (GROUP_PERMISSION_CLOSED == $groupInfo['visibility'] && !$isMember) {
  341. echo json_encode(false);
  342. exit;
  343. }
  344. }
  345. $user = api_get_user_entity($current_user_id);
  346. $userLike = $messageLikesRepo->findOneBy(['message' => $message, 'user' => $user]);
  347. if (empty($userLike)) {
  348. $userLike = new MessageFeedback();
  349. $userLike
  350. ->setMessage($message)
  351. ->setUser($user);
  352. }
  353. if ('like' === $status) {
  354. if ($userLike->isLiked()) {
  355. echo json_encode(false);
  356. exit;
  357. }
  358. $userLike
  359. ->setLiked(true)
  360. ->setDisliked(false);
  361. } elseif ('dislike' === $status) {
  362. if ($userLike->isDisliked()) {
  363. echo json_encode(false);
  364. exit;
  365. }
  366. $userLike
  367. ->setLiked(false)
  368. ->setDisliked(true);
  369. }
  370. $userLike
  371. ->setUpdatedAt(
  372. api_get_utc_datetime(null, false, true)
  373. );
  374. $em->persist($userLike);
  375. $em->flush();
  376. echo json_encode(true);
  377. break;
  378. default:
  379. echo '';
  380. }
  381. exit;