session.ajax.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls
  5. */
  6. //require_once '../global.inc.php';
  7. $action = $_REQUEST['a'];
  8. switch ($action) {
  9. case 'get_user_sessions':
  10. if (api_is_platform_admin()) {
  11. $user_id = intval($_POST['user_id']);
  12. $list_sessions = SessionManager::get_sessions_by_user($user_id, true);
  13. if (!empty($list_sessions)) {
  14. foreach ($list_sessions as $session_item) {
  15. echo $session_item['session_name'] . '<br />';
  16. }
  17. } else {
  18. echo get_lang('NoSessionsForThisUser');
  19. }
  20. unset($list_sessions);
  21. }
  22. break;
  23. case 'search_session':
  24. if (api_is_platform_admin()) {
  25. $sessions = SessionManager::get_sessions_list(
  26. [
  27. 's.name' => [
  28. 'operator' => 'LIKE',
  29. 'value' => "%" . $_REQUEST['q'] . "%"
  30. ]
  31. ]
  32. );
  33. $list = [
  34. 'items' => []
  35. ];
  36. if (empty($sessions)) {
  37. echo json_encode([]);
  38. break;
  39. }
  40. foreach ($sessions as $session) {
  41. $list['items'][] = [
  42. 'id' => $session['id'],
  43. 'text' => $session['name']
  44. ];
  45. }
  46. echo json_encode($list);
  47. }
  48. break;
  49. case 'search_session_all':
  50. if (api_is_platform_admin()) {
  51. $results = SessionManager::get_sessions_list(
  52. array(
  53. 's.name' => array('operator' => 'like', 'value' => "%".$_REQUEST['q']."%"),
  54. 'c.id' => array('operator' => '=', 'value' => $_REQUEST['course_id'])
  55. )
  56. );
  57. $results2 = array();
  58. if (!empty($results)) {
  59. foreach ($results as $item) {
  60. $item2 = array();
  61. foreach ($item as $id => $internal) {
  62. if ($id == 'id') {
  63. $item2[$id] = $internal;
  64. }
  65. if ($id == 'name') {
  66. $item2['text'] = $internal;
  67. }
  68. }
  69. $results2[] = $item2;
  70. }
  71. $results2[] = array('T', 'text' => 'TODOS', 'id' => 'T');
  72. echo json_encode($results2);
  73. } else {
  74. echo json_encode(array(array('T', 'text' => 'TODOS', 'id' => 'T')));
  75. }
  76. }
  77. break;
  78. case 'search_session_by_course':
  79. if (api_is_platform_admin()) {
  80. $results = SessionManager::get_sessions_list(
  81. array(
  82. 's.name' => array('operator' => 'like', 'value' => "%".$_REQUEST['q']."%"),
  83. 'c.id' => array('operator' => '=', 'value' => $_REQUEST['course_id'])
  84. )
  85. );
  86. $json = [
  87. 'items' => [
  88. ['id' => 'T', 'text' => get_lang('All')]
  89. ]
  90. ];
  91. if (!empty($results)) {
  92. foreach ($results as $item) {
  93. $item2 = array();
  94. foreach ($item as $id => $internal) {
  95. if ($id == 'id') {
  96. $item2[$id] = $internal;
  97. }
  98. if ($id == 'name') {
  99. $item2['text'] = $internal;
  100. }
  101. }
  102. $json['items'][] = $item2;
  103. }
  104. }
  105. echo json_encode($json);
  106. }
  107. break;
  108. case 'get_description':
  109. if (isset($_GET['session'])) {
  110. $sessionInfo = api_get_session_info($_GET['session']);
  111. echo '<h2>'.$sessionInfo['name'].'</h2>';
  112. echo '<div class="home-course-intro"><div class="page-course"><div class="page-course-intro">';
  113. echo $sessionInfo['show_description'] == 1 ? $sessionInfo['description'] : get_lang('None');
  114. echo '</div></div></div>';
  115. }
  116. break;
  117. case 'search_general_coach':
  118. header('Content-Type: application/json');
  119. if (api_is_anonymous()) {
  120. echo '';
  121. break;
  122. }
  123. $list = [
  124. 'items' => []
  125. ];
  126. $entityManager = Database::getManager();
  127. $usersRepo = $entityManager->getRepository('ChamiloUserBundle:User');
  128. $users = $usersRepo->searchUsersByStatus($_GET['q'], COURSEMANAGER);
  129. foreach ($users as $user) {
  130. $list['items'][] = [
  131. 'id' => $user->getId(),
  132. 'text' => $user->getCompleteName()
  133. ];
  134. }
  135. echo json_encode($list);
  136. break;
  137. case 'get_courses_inside_session':
  138. $userId = api_get_user_id();
  139. $isAdmin = api_is_platform_admin();
  140. if ($isAdmin) {
  141. $sessionList = SessionManager::get_sessions_list();
  142. $sessionIdList = array_column($sessionList, 'id');
  143. } else {
  144. $sessionList = SessionManager::get_sessions_by_user($userId);
  145. $sessionIdList = array_column($sessionList, 'session_id');
  146. }
  147. $sessionId = isset($_GET['session_id']) ? (int) $_GET['session_id'] : 0;
  148. $courseList = [];
  149. if (empty($sessionId)) {
  150. $preCourseList = CourseManager::get_courses_list_by_user_id($userId, false, true);
  151. $courseList = array_column($preCourseList, 'real_id');
  152. } else {
  153. if ($isAdmin) {
  154. $courseList = SessionManager::getCoursesInSession($sessionId);
  155. } else {
  156. if (in_array($sessionId, $sessionIdList)) {
  157. $courseList = SessionManager::getCoursesInSession($sessionId);
  158. }
  159. }
  160. }
  161. $courseListToSelect = [];
  162. if (!empty($courseList)) {
  163. //Course List
  164. foreach ($courseList as $courseId) {
  165. $courseInfo = api_get_course_info_by_id($courseId);
  166. $courseListToSelect[] = [
  167. 'id' => $courseInfo['real_id'],
  168. 'name' => $courseInfo['title']
  169. ];
  170. }
  171. }
  172. echo json_encode($courseListToSelect);
  173. break;
  174. default:
  175. echo '';
  176. }
  177. exit;