session.ajax.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls
  5. */
  6. require_once __DIR__.'/../global.inc.php';
  7. $action = $_REQUEST['a'];
  8. switch ($action) {
  9. case 'get_user_sessions':
  10. if (api_is_platform_admin() || api_is_session_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 'session_info':
  109. $sessionId = isset($_GET['session_id']) ? $_GET['session_id'] : '';
  110. $sessionInfo = api_get_session_info($sessionId);
  111. $extraFieldValues = new ExtraFieldValue('session');
  112. $extraField = new ExtraField('session');
  113. $values = $extraFieldValues->getAllValuesByItem($sessionId);
  114. $load = isset($_GET['load_empty_extra_fields']) ? true : false;
  115. if ($load) {
  116. $allExtraFields = $extraField->get_all();
  117. $valueList = array_column($values, 'id');
  118. foreach ($allExtraFields as $extra) {
  119. if (!in_array($extra['id'], $valueList)) {
  120. $values[] = [
  121. 'id' => $extra['id'],
  122. 'variable' => $extra['variable'],
  123. 'value' => '',
  124. 'field_type' => $extra['field_type'],
  125. ];
  126. }
  127. }
  128. }
  129. $sessionInfo['extra_fields'] = $values;
  130. if (!empty($sessionInfo)) {
  131. echo json_encode($sessionInfo);
  132. }
  133. break;
  134. case 'get_description':
  135. if (isset($_GET['session'])) {
  136. $sessionInfo = api_get_session_info($_GET['session']);
  137. echo '<h2>'.$sessionInfo['name'].'</h2>';
  138. echo '<div class="home-course-intro"><div class="page-course"><div class="page-course-intro">';
  139. echo $sessionInfo['show_description'] == 1 ? $sessionInfo['description'] : get_lang('None');
  140. echo '</div></div></div>';
  141. }
  142. break;
  143. case 'search_general_coach':
  144. header('Content-Type: application/json');
  145. if (api_is_anonymous()) {
  146. echo '';
  147. break;
  148. }
  149. $list = [
  150. 'items' => []
  151. ];
  152. $entityManager = Database::getManager();
  153. $usersRepo = $entityManager->getRepository('ChamiloUserBundle:User');
  154. $users = $usersRepo->searchUsersByStatus($_GET['q'], COURSEMANAGER);
  155. foreach ($users as $user) {
  156. $list['items'][] = [
  157. 'id' => $user->getId(),
  158. 'text' => $user->getCompleteName()
  159. ];
  160. }
  161. echo json_encode($list);
  162. break;
  163. case 'get_courses_inside_session':
  164. $userId = api_get_user_id();
  165. $isAdmin = api_is_platform_admin();
  166. if ($isAdmin) {
  167. $sessionList = SessionManager::get_sessions_list();
  168. $sessionIdList = array_column($sessionList, 'id');
  169. } else {
  170. $sessionList = SessionManager::get_sessions_by_user($userId);
  171. $sessionIdList = array_column($sessionList, 'session_id');
  172. }
  173. $sessionId = isset($_GET['session_id']) ? (int) $_GET['session_id'] : 0;
  174. $courseList = [];
  175. if (empty($sessionId)) {
  176. $preCourseList = CourseManager::get_courses_list_by_user_id(
  177. $userId,
  178. false,
  179. true
  180. );
  181. $courseList = array_column($preCourseList, 'real_id');
  182. } else {
  183. if ($isAdmin) {
  184. $courseList = SessionManager::getCoursesInSession($sessionId);
  185. } else {
  186. if (in_array($sessionId, $sessionIdList)) {
  187. $courseList = SessionManager::getCoursesInSession($sessionId);
  188. }
  189. }
  190. }
  191. $courseListToSelect = [];
  192. if (!empty($courseList)) {
  193. // Course List
  194. foreach ($courseList as $courseId) {
  195. $courseInfo = api_get_course_info_by_id($courseId);
  196. $courseListToSelect[] = [
  197. 'id' => $courseInfo['real_id'],
  198. 'name' => $courseInfo['title']
  199. ];
  200. }
  201. }
  202. echo json_encode($courseListToSelect);
  203. break;
  204. default:
  205. echo '';
  206. }
  207. exit;