social.ajax.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Responses to AJAX calls
  6. */
  7. require_once __DIR__.'/../global.inc.php';
  8. $action = isset($_GET['a']) ? $_GET['a'] : null;
  9. $current_user_id = api_get_user_id();
  10. switch ($action) {
  11. case 'add_friend':
  12. if (api_is_anonymous()) {
  13. echo '';
  14. break;
  15. }
  16. if (isset($_GET['is_my_friend'])) {
  17. $relation_type = USER_RELATION_TYPE_FRIEND; //My friend
  18. } else {
  19. $relation_type = USER_RELATION_TYPE_UNKNOWN; //Unknown contact
  20. }
  21. if (isset($_GET['friend_id'])) {
  22. $my_current_friend = $_GET['friend_id'];
  23. UserManager::relate_users($current_user_id, $my_current_friend, $relation_type);
  24. UserManager::relate_users($my_current_friend, $current_user_id, $relation_type);
  25. SocialManager::invitation_accepted($my_current_friend, $current_user_id);
  26. Display::addFlash(
  27. Display::return_message(get_lang('AddedContactToList'), 'success')
  28. );
  29. header('Location: '.api_get_path(WEB_CODE_PATH).'social/invitations.php');
  30. }
  31. break;
  32. case 'deny_friend':
  33. if (api_is_anonymous()) {
  34. echo '';
  35. break;
  36. }
  37. if (isset($_GET['is_my_friend'])) {
  38. $relation_type = USER_RELATION_TYPE_FRIEND; //my friend
  39. } else {
  40. $relation_type = USER_RELATION_TYPE_UNKNOWN; //Contact unknown
  41. }
  42. if (isset($_GET['denied_friend_id'])) {
  43. SocialManager::invitation_denied($_GET['denied_friend_id'], $current_user_id);
  44. Display::addFlash(
  45. Display::return_message(get_lang('InvitationDenied'), 'success')
  46. );
  47. header('Location: '.api_get_path(WEB_CODE_PATH).'social/invitations.php');
  48. }
  49. break;
  50. case 'delete_friend':
  51. if (api_is_anonymous()) {
  52. echo '';
  53. break;
  54. }
  55. $my_delete_friend = intval($_POST['delete_friend_id']);
  56. if (isset($_POST['delete_friend_id'])) {
  57. SocialManager::remove_user_rel_user($my_delete_friend);
  58. }
  59. break;
  60. case 'show_my_friends':
  61. if (api_is_anonymous()) {
  62. echo '';
  63. break;
  64. }
  65. $user_id = api_get_user_id();
  66. $name_search = Security::remove_XSS($_POST['search_name_q']);
  67. $number_friends = 0;
  68. if (isset($name_search) && $name_search != 'undefined') {
  69. $friends = SocialManager::get_friends($user_id, null, $name_search);
  70. } else {
  71. $friends = SocialManager::get_friends($user_id);
  72. }
  73. $friend_html = '';
  74. $number_of_images = 8;
  75. $number_friends = count($friends);
  76. if ($number_friends != 0) {
  77. $number_loop = ($number_friends / $number_of_images);
  78. $loop_friends = ceil($number_loop);
  79. $j = 0;
  80. for ($k = 0; $k < $loop_friends; $k++) {
  81. if ($j == $number_of_images) {
  82. $number_of_images = $number_of_images * 2;
  83. }
  84. while ($j < $number_of_images) {
  85. if (isset($friends[$j])) {
  86. $friend = $friends[$j];
  87. $user_name = api_xml_http_response_encode($friend['firstName'].' '.$friend['lastName']);
  88. $userPicture = UserManager::getUserPicture($friend['friend_user_id']);
  89. $friend_html .= '
  90. <div class="col-md-3">
  91. <div class="thumbnail text-center" id="div_' . $friends[$j]['friend_user_id'].'">
  92. <img src="' . $userPicture.'" class="img-responsive" id="imgfriend_'.$friend['friend_user_id'].'" title="$user_name">
  93. <div class="caption">
  94. <h3>
  95. <a href="profile.php?u=' . $friend['friend_user_id'].'">'.$user_name.'</a>
  96. </h3>
  97. <p>
  98. <button class="btn btn-danger" onclick="delete_friend(this)" id=img_' . $friend['friend_user_id'].'>
  99. ' . get_lang('Delete').'
  100. </button>
  101. </p>
  102. </div>
  103. </div>
  104. </div>
  105. ';
  106. }
  107. $j++;
  108. }
  109. }
  110. }
  111. echo $friend_html;
  112. break;
  113. case 'toogle_course':
  114. if (api_is_anonymous()) {
  115. echo '';
  116. break;
  117. }
  118. require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
  119. $user_id = Session::read('social_user_id');
  120. if ($_POST['action']) {
  121. $action = $_POST['action'];
  122. }
  123. switch ($action) {
  124. case 'load_course':
  125. $course_id = intval($_POST['course_code']); // the int course id
  126. $course_info = api_get_course_info_by_id($course_id);
  127. $course_code = $course_info['code'];
  128. if (api_is_user_of_course($course_id, api_get_user_id())) {
  129. //------Forum messages
  130. $forum_result = get_all_post_from_user($user_id, $course_code);
  131. $all_result_data = 0;
  132. if ($forum_result != '') {
  133. echo '<div id="social-forum-main-title">';
  134. echo api_xml_http_response_encode(get_lang('Forum'));
  135. echo '</div>';
  136. echo '<div style="background:#FAF9F6; padding:0px;" >';
  137. echo api_xml_http_response_encode($forum_result);
  138. echo '</div>';
  139. echo '<br />';
  140. $all_result_data++;
  141. }
  142. //------Blog posts
  143. $result = Blog::getBlogPostFromUser($course_id, $user_id, $course_code);
  144. if (!empty($result)) {
  145. api_display_tool_title(api_xml_http_response_encode(get_lang('Blog')));
  146. echo '<div style="background:#FAF9F6; padding:0px;">';
  147. echo api_xml_http_response_encode($result);
  148. echo '</div>';
  149. echo '<br />';
  150. $all_result_data++;
  151. }
  152. //------Blog comments
  153. $result = Blog::getBlogCommentsFromUser($course_id, $user_id, $course_code);
  154. if (!empty($result)) {
  155. echo '<div style="background:#FAF9F6; padding-left:10px;">';
  156. api_display_tool_title(api_xml_http_response_encode(get_lang('BlogComments')));
  157. echo api_xml_http_response_encode($result);
  158. echo '</div>';
  159. echo '<br />';
  160. $all_result_data++;
  161. }
  162. if ($all_result_data == 0) {
  163. echo api_xml_http_response_encode(get_lang('NoDataAvailable'));
  164. }
  165. } else {
  166. echo '<div class="clear"></div><br />';
  167. api_display_tool_title(api_xml_http_response_encode(get_lang('Details')));
  168. echo '<div style="background:#FAF9F6; padding:0px;">';
  169. echo api_xml_http_response_encode(get_lang('UserNonRegisteredAtTheCourse'));
  170. echo '<div class="clear"></div><br />';
  171. echo '</div>';
  172. echo '<div class="clear"></div><br />';
  173. }
  174. break;
  175. case 'unload_course':
  176. break;
  177. default:
  178. break;
  179. }
  180. break;
  181. case 'list_wall_message':
  182. $start = isset($_REQUEST['start']) ? intval($_REQUEST['start']) - 1 : 0;
  183. $length = isset($_REQUEST['length']) ? intval($_REQUEST['length']) : 10;
  184. $userId = isset($_REQUEST['u']) ? intval($_REQUEST['u']) : api_get_user_id();
  185. $friendId = $userId;
  186. $array = SocialManager::getWallMessagesPostHTML($userId, $friendId, null, $length, $start);
  187. if (!empty($array)) {
  188. ksort($array);
  189. $html = '';
  190. for ($i = 0; $i < count($array); $i++) {
  191. $post = $array[$i]['html'];
  192. $comment = SocialManager::getWallMessagesHTML($userId, $friendId, $array[$i]['id']);
  193. $html .= '<div class="panel panel-info"><div class="panel-body">'.$post.$comment.'</div></div>';
  194. }
  195. $html .= Display::div(
  196. Display::url(
  197. get_lang('SeeMore'),
  198. api_get_self().'?u='.$userId.'&a=list_wall_message&start='.
  199. ($start + $length + 1).'&length='.$length,
  200. array(
  201. 'class' => 'nextPage',
  202. )
  203. ),
  204. array(
  205. 'class' => 'next',
  206. )
  207. );
  208. echo $html;
  209. }
  210. break;
  211. // Read the Url using OpenGraph and returns the hyperlinks content
  212. case 'read_url_with_open_graph':
  213. $url = isset($_POST['social_wall_new_msg_main']) ? $_POST['social_wall_new_msg_main'] : '';
  214. $url = trim($url);
  215. $html = '';
  216. if (SocialManager::verifyUrl($url) == true) {
  217. $html = Security::remove_XSS(
  218. SocialManager::readContentWithOpenGraph($url)
  219. );
  220. }
  221. echo $html;
  222. break;
  223. default:
  224. echo '';
  225. }
  226. exit;