course.ajax.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Component\Utils\ChamiloApi;
  4. /**
  5. * Responses to AJAX calls.
  6. */
  7. require_once __DIR__.'/../global.inc.php';
  8. $action = $_REQUEST['a'];
  9. $user_id = api_get_user_id();
  10. switch ($action) {
  11. case 'add_course_vote':
  12. $course_id = (int) $_REQUEST['course_id'];
  13. $star = (int) $_REQUEST['star'];
  14. if (!api_is_anonymous()) {
  15. CourseManager::add_course_vote($user_id, $star, $course_id, 0);
  16. }
  17. $point_info = CourseManager::get_course_ranking($course_id, 0);
  18. $ajax_url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=add_course_vote';
  19. $rating = Display::return_rating_system(
  20. 'star_'.$course_id,
  21. $ajax_url.'&amp;course_id='.$course_id,
  22. $point_info,
  23. false
  24. );
  25. echo $rating;
  26. break;
  27. case 'get_course_image':
  28. $courseId = ChamiloApi::getCourseIdByDirectory($_REQUEST['code']);
  29. $courseInfo = api_get_course_info_by_id($courseId);
  30. $image = isset($_REQUEST['image']) && in_array($_REQUEST['image'], ['course_image_large_source', 'course_image_source']) ? $_REQUEST['image'] : '';
  31. if ($courseInfo && $image) {
  32. DocumentManager::file_send_for_download($courseInfo[$image]);
  33. }
  34. break;
  35. case 'get_user_courses':
  36. // Only search my courses
  37. if (api_is_platform_admin() || api_is_session_admin()) {
  38. $userId = (int) $_REQUEST['user_id'];
  39. $list = CourseManager::get_courses_list_by_user_id(
  40. $userId,
  41. false
  42. );
  43. if (!empty($list)) {
  44. foreach ($list as $course) {
  45. $courseInfo = api_get_course_info_by_id($course['real_id']);
  46. echo $courseInfo['title'].'<br />';
  47. }
  48. } else {
  49. echo get_lang('This user is not subscribed to any course');
  50. }
  51. }
  52. break;
  53. case 'get_my_courses_and_sessions':
  54. // Search my courses and sessions allowed for admin, session admin, teachers
  55. $currentCourseId = api_get_course_int_id();
  56. $currentSessionId = api_get_session_id();
  57. if (api_is_platform_admin() || api_is_session_admin() || api_is_allowed_to_edit()) {
  58. $list = CourseManager::get_courses_list_by_user_id(
  59. api_get_user_id(),
  60. true,
  61. false,
  62. false,
  63. [],
  64. true,
  65. true
  66. );
  67. if (empty($list)) {
  68. echo json_encode([]);
  69. break;
  70. }
  71. $courseList = [];
  72. if (!empty($list)) {
  73. foreach ($list as $course) {
  74. $courseInfo = api_get_course_info_by_id($course['real_id']);
  75. $sessionId = 0;
  76. if (isset($course['session_id']) && !empty($course['session_id'])) {
  77. $sessionId = $course['session_id'];
  78. }
  79. $sessionName = '';
  80. if (isset($course['session_name']) && !empty($course['session_name'])) {
  81. $sessionName = ' ('.$course['session_name'].')';
  82. }
  83. // Skip current course/course session
  84. if ($currentCourseId == $courseInfo['real_id'] && $sessionId == $currentSessionId) {
  85. continue;
  86. }
  87. $courseList['items'][] = [
  88. 'id' => $courseInfo['real_id'].'_'.$sessionId,
  89. 'text' => $courseInfo['title'].$sessionName,
  90. ];
  91. }
  92. echo json_encode($courseList);
  93. }
  94. }
  95. break;
  96. case 'search_category':
  97. if (api_is_platform_admin() || api_is_allowed_to_create_course()) {
  98. $categories = CourseCategory::searchCategoryByKeyword($_REQUEST['q']);
  99. if (empty($categories)) {
  100. echo json_encode([]);
  101. break;
  102. }
  103. $list = [];
  104. foreach ($categories as $item) {
  105. $list['items'][] = [
  106. 'id' => $item['code'],
  107. 'text' => '('.$item['code'].') '.strip_tags($item['name']),
  108. ];
  109. }
  110. echo json_encode($list);
  111. }
  112. break;
  113. case 'search_course':
  114. if (api_is_teacher() || api_is_platform_admin()) {
  115. if (isset($_GET['session_id']) && !empty($_GET['session_id'])) {
  116. //if session is defined, lets find only courses of this session
  117. $courseList = SessionManager::get_course_list_by_session_id(
  118. $_GET['session_id'],
  119. $_GET['q']
  120. );
  121. } else {
  122. //if session is not defined lets search all courses STARTING with $_GET['q']
  123. //TODO change this function to search not only courses STARTING with $_GET['q']
  124. if (api_is_platform_admin()) {
  125. $courseList = CourseManager::get_courses_list(
  126. 0, //offset
  127. 0, //howMany
  128. 1, //$orderby = 1
  129. 'ASC',
  130. -1, //visibility
  131. $_GET['q'],
  132. null, //$urlId
  133. true //AlsoSearchCode
  134. );
  135. } elseif (api_is_teacher()) {
  136. $courseList = CourseManager::get_course_list_of_user_as_course_admin(api_get_user_id(), $_GET['q']);
  137. }
  138. }
  139. $results = [];
  140. if (empty($courseList)) {
  141. echo json_encode([]);
  142. break;
  143. }
  144. foreach ($courseList as $course) {
  145. $title = $course['title'];
  146. if (!empty($course['category_code'])) {
  147. $parents = CourseCategory::getParentsToString($course['category_code']);
  148. $title = $parents.$course['title'];
  149. }
  150. $results['items'][] = [
  151. 'id' => $course['id'],
  152. 'text' => $title,
  153. ];
  154. }
  155. echo json_encode($results);
  156. }
  157. break;
  158. case 'search_course_by_session':
  159. if (api_is_platform_admin()) {
  160. $results = SessionManager::get_course_list_by_session_id($_GET['session_id'], $_GET['q']);
  161. $results2 = [];
  162. if (is_array($results) && !empty($results)) {
  163. foreach ($results as $item) {
  164. $item2 = [];
  165. foreach ($item as $id => $internal) {
  166. if ($id == 'id') {
  167. $item2[$id] = $internal;
  168. }
  169. if ($id == 'title') {
  170. $item2['text'] = $internal;
  171. }
  172. }
  173. $results2[] = $item2;
  174. }
  175. echo json_encode($results2);
  176. } else {
  177. echo json_encode([]);
  178. }
  179. }
  180. break;
  181. case 'search_course_by_session_all':
  182. if (api_is_platform_admin()) {
  183. if ($_GET['session_id'] == 'TODOS' || $_GET['session_id'] == 'T') {
  184. $_GET['session_id'] = '%';
  185. }
  186. $results = SessionManager::get_course_list_by_session_id_like(
  187. $_GET['session_id'],
  188. $_GET['q']
  189. );
  190. $results2 = ['items' => []];
  191. if (!empty($results)) {
  192. foreach ($results as $item) {
  193. $item2 = [];
  194. foreach ($item as $id => $internal) {
  195. if ($id == 'id') {
  196. $item2[$id] = $internal;
  197. }
  198. if ($id == 'title') {
  199. $item2['text'] = $internal;
  200. }
  201. }
  202. $results2['items'][] = $item2;
  203. }
  204. }
  205. echo json_encode($results2);
  206. }
  207. break;
  208. case 'search_user_by_course':
  209. if (api_is_platform_admin()) {
  210. $user = Database::get_main_table(TABLE_MAIN_USER);
  211. $session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  212. $sessionId = $_GET['session_id'];
  213. $course = api_get_course_info_by_id($_GET['course_id']);
  214. $json = [
  215. 'items' => [],
  216. ];
  217. $keyword = Database::escape_string($_GET['q']);
  218. $status = 0;
  219. if (empty($sessionId)) {
  220. $status = STUDENT;
  221. }
  222. $userList = CourseManager::get_user_list_from_course_code(
  223. $course['code'],
  224. $sessionId,
  225. null,
  226. null,
  227. $status,
  228. false,
  229. false,
  230. false,
  231. [],
  232. [],
  233. [],
  234. true,
  235. [],
  236. $_GET['q']
  237. );
  238. foreach ($userList as $user) {
  239. $userCompleteName = api_get_person_name($user['firstname'], $user['lastname']);
  240. $json['items'][] = [
  241. 'id' => $user['user_id'],
  242. 'text' => "{$user['username']} ($userCompleteName)",
  243. ];
  244. }
  245. echo json_encode($json);
  246. }
  247. break;
  248. case 'search_exercise_by_course':
  249. if (api_is_platform_admin()) {
  250. $course = api_get_course_info_by_id($_GET['course_id']);
  251. $session_id = (!empty($_GET['session_id'])) ? (int) $_GET['session_id'] : 0;
  252. $exercises = ExerciseLib::get_all_exercises(
  253. $course,
  254. $session_id,
  255. false,
  256. $_GET['q'],
  257. true,
  258. 3
  259. );
  260. foreach ($exercises as $exercise) {
  261. $data[] = ['id' => $exercise['id'], 'text' => html_entity_decode($exercise['title'])];
  262. }
  263. if (!empty($data)) {
  264. $data[] = ['id' => 'T', 'text' => 'TODOS'];
  265. echo json_encode($data);
  266. } else {
  267. echo json_encode([['id' => 'T', 'text' => 'TODOS']]);
  268. }
  269. }
  270. break;
  271. case 'search_survey_by_course':
  272. if (api_is_platform_admin()) {
  273. $survey = Database::get_course_table(TABLE_SURVEY);
  274. $sql = "SELECT survey_id as id, title, anonymous
  275. FROM $survey
  276. WHERE
  277. c_id = %d AND
  278. session_id = %d AND
  279. title LIKE '%s'";
  280. $sql_query = sprintf(
  281. $sql,
  282. (int) $_GET['course_id'],
  283. (int) $_GET['session_id'],
  284. '%'.Database::escape_string($_GET['q']).'%'
  285. );
  286. $result = Database::query($sql_query);
  287. while ($survey = Database::fetch_assoc($result)) {
  288. $survey['title'] .= ($survey['anonymous'] == 1) ? ' ('.get_lang('Anonymous').')' : '';
  289. $data[] = [
  290. 'id' => $survey['id'],
  291. 'text' => strip_tags(html_entity_decode($survey['title'])),
  292. ];
  293. }
  294. if (!empty($data)) {
  295. echo json_encode($data);
  296. } else {
  297. echo json_encode([]);
  298. }
  299. }
  300. break;
  301. case 'display_sessions_courses':
  302. $sessionId = (int) $_GET['session'];
  303. $userTable = Database::get_main_table(TABLE_MAIN_USER);
  304. $coursesData = SessionManager::get_course_list_by_session_id($sessionId);
  305. $courses = [];
  306. foreach ($coursesData as $courseId => $course) {
  307. $coachData = SessionManager::getCoachesByCourseSession($sessionId, $courseId);
  308. $coachName = '';
  309. if (!empty($coachData)) {
  310. $userResult = Database::select('lastname,firstname', $userTable, [
  311. 'where' => [
  312. 'user_id = ?' => $coachData[0],
  313. ],
  314. ], 'first');
  315. $coachName = api_get_person_name($userResult['firstname'], $userResult['lastname']);
  316. }
  317. $courses[] = [
  318. 'id' => $courseId,
  319. 'name' => $course['title'],
  320. 'coachName' => $coachName,
  321. ];
  322. }
  323. echo json_encode($courses);
  324. break;
  325. case 'course_logout':
  326. $logoutInfo = [
  327. 'uid' => api_get_user_id(),
  328. 'cid' => api_get_course_int_id(),
  329. 'sid' => api_get_session_id(),
  330. ];
  331. $logInfo = [
  332. 'tool' => 'close-window',
  333. 'tool_id' => 0,
  334. 'tool_id_detail' => 0,
  335. 'action' => 'exit',
  336. ];
  337. Event::registerLog($logInfo);
  338. $result = (int) Event::courseLogout($logoutInfo);
  339. echo $result;
  340. break;
  341. default:
  342. echo '';
  343. }
  344. exit;