course.ajax.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls
  5. */
  6. use Chamilo\CoreBundle\Component\Utils\ChamiloApi;
  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 = intval($_REQUEST['course_id']);
  13. $star = intval($_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. if (api_is_platform_admin() || api_is_session_admin()) {
  37. $user_id = intval($_POST['user_id']);
  38. $list = CourseManager::get_courses_list_by_user_id(
  39. $user_id,
  40. false
  41. );
  42. if (!empty($list)) {
  43. foreach ($list as $course) {
  44. $courseInfo = api_get_course_info_by_id($course['real_id']);
  45. echo $courseInfo['title'].'<br />';
  46. }
  47. } else {
  48. echo get_lang('UserHasNoCourse');
  49. }
  50. }
  51. break;
  52. case 'search_category':
  53. if (api_is_platform_admin() || api_is_allowed_to_create_course()) {
  54. $categories = CourseCategory::searchCategoryByKeyword($_REQUEST['q']);
  55. if (empty($categories)) {
  56. echo json_encode([]);
  57. break;
  58. }
  59. $list = [];
  60. foreach ($categories as $item) {
  61. $list['items'][] = [
  62. 'id' => $item['code'],
  63. 'text' => '('.$item['code'].') '.strip_tags($item['name'])
  64. ];
  65. }
  66. echo json_encode($list);
  67. }
  68. break;
  69. case 'search_course':
  70. if (api_is_teacher()) {
  71. if (!empty($_GET['session_id']) && intval($_GET['session_id'])) {
  72. //if session is defined, lets find only courses of this session
  73. $courseList = SessionManager::get_course_list_by_session_id(
  74. $_GET['session_id'],
  75. $_GET['q']
  76. );
  77. } else {
  78. //if session is not defined lets search all courses STARTING with $_GET['q']
  79. //TODO change this function to search not only courses STARTING with $_GET['q']
  80. if (api_is_platform_admin()) {
  81. $courseList = CourseManager::get_courses_list(
  82. 0, //offset
  83. 0, //howMany
  84. 1, //$orderby = 1
  85. 'ASC',
  86. -1, //visibility
  87. $_GET['q'],
  88. null, //$urlId
  89. true //AlsoSearchCode
  90. );
  91. } elseif (api_is_teacher()) {
  92. $courseList = CourseManager::get_course_list_of_user_as_course_admin(api_get_user_id(), $_GET['q']);
  93. }
  94. }
  95. $results = array();
  96. if (empty($courseList)) {
  97. echo json_encode([]);
  98. break;
  99. }
  100. foreach ($courseList as $course) {
  101. $title = $course['title'];
  102. if (!empty($course['category_code'])) {
  103. $parents = CourseCategory::getParentsToString($course['category_code']);
  104. $title = $parents.$course['title'];
  105. }
  106. $results['items'][] = array(
  107. 'id' => $course['id'],
  108. 'text' => $title
  109. );
  110. }
  111. echo json_encode($results);
  112. }
  113. break;
  114. case 'search_course_by_session':
  115. if (api_is_platform_admin()) {
  116. $results = SessionManager::get_course_list_by_session_id($_GET['session_id'], $_GET['q']);
  117. $results2 = array();
  118. if (is_array($results) && !empty($results)) {
  119. foreach ($results as $item) {
  120. $item2 = array();
  121. foreach ($item as $id => $internal) {
  122. if ($id == 'id') {
  123. $item2[$id] = $internal;
  124. }
  125. if ($id == 'title') {
  126. $item2['text'] = $internal;
  127. }
  128. }
  129. $results2[] = $item2;
  130. }
  131. echo json_encode($results2);
  132. } else {
  133. echo json_encode(array());
  134. }
  135. }
  136. break;
  137. case 'search_course_by_session_all':
  138. if (api_is_platform_admin()) {
  139. if ($_GET['session_id'] == 'TODOS' || $_GET['session_id'] == 'T') {
  140. $_GET['session_id'] = '%';
  141. }
  142. $results = SessionManager::get_course_list_by_session_id_like(
  143. $_GET['session_id'],
  144. $_GET['q']
  145. );
  146. $results2 = ['items' => []];
  147. if (!empty($results)) {
  148. foreach ($results as $item) {
  149. $item2 = array();
  150. foreach ($item as $id => $internal) {
  151. if ($id == 'id') {
  152. $item2[$id] = $internal;
  153. }
  154. if ($id == 'title') {
  155. $item2['text'] = $internal;
  156. }
  157. }
  158. $results2['items'][] = $item2;
  159. }
  160. }
  161. echo json_encode($results2);
  162. }
  163. break;
  164. case 'search_user_by_course':
  165. if (api_is_platform_admin()) {
  166. $user = Database::get_main_table(TABLE_MAIN_USER);
  167. $session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  168. $course = api_get_course_info_by_id($_GET['course_id']);
  169. $json = [
  170. 'items' => []
  171. ];
  172. $sql = "SELECT u.user_id as id, u.username, u.lastname, u.firstname
  173. FROM $user u
  174. INNER JOIN $session_course_user r
  175. ON u.user_id = r.user_id
  176. WHERE session_id = %d AND c_id = '%s'
  177. AND (u.firstname LIKE '%s' OR u.username LIKE '%s' OR u.lastname LIKE '%s')";
  178. $needle = '%'.$_GET['q'].'%';
  179. $sql_query = sprintf($sql, $_GET['session_id'], $course['real_id'], $needle, $needle, $needle);
  180. $result = Database::query($sql_query);
  181. while ($user = Database::fetch_assoc($result)) {
  182. $userCompleteName = api_get_person_name($user['firstname'], $user['lastname']);
  183. $json['items'][] = [
  184. 'id' => $user['id'],
  185. 'text' => "{$user['username']} ($userCompleteName)"
  186. ];
  187. }
  188. echo json_encode($json);
  189. }
  190. break;
  191. case 'search_exercise_by_course':
  192. if (api_is_platform_admin()) {
  193. $course = api_get_course_info_by_id($_GET['course_id']);
  194. $session_id = (!empty($_GET['session_id'])) ? intval($_GET['session_id']) : 0;
  195. $exercises = ExerciseLib::get_all_exercises(
  196. $course,
  197. $session_id,
  198. false,
  199. $_GET['q'],
  200. true,
  201. 3
  202. );
  203. foreach ($exercises as $exercise) {
  204. $data[] = array('id' => $exercise['id'], 'text' => html_entity_decode($exercise['title']));
  205. }
  206. if (!empty($data)) {
  207. $data[] = array('id' => 'T', 'text' => 'TODOS');
  208. echo json_encode($data);
  209. } else {
  210. echo json_encode(array(array('id' => 'T', 'text' => 'TODOS')));
  211. }
  212. }
  213. break;
  214. case 'search_survey_by_course':
  215. if (api_is_platform_admin()) {
  216. $survey = Database::get_course_table(TABLE_SURVEY);
  217. $sql = "SELECT survey_id as id, title, anonymous
  218. FROM $survey
  219. WHERE
  220. c_id = %d AND
  221. session_id = %d AND
  222. title LIKE '%s'";
  223. $sql_query = sprintf(
  224. $sql,
  225. intval($_GET['course_id']),
  226. intval($_GET['session_id']),
  227. '%'.Database::escape_string($_GET['q']).'%'
  228. );
  229. $result = Database::query($sql_query);
  230. while ($survey = Database::fetch_assoc($result)) {
  231. $survey['title'] .= ($survey['anonymous'] == 1) ? ' ('.get_lang('Anonymous').')' : '';
  232. $data[] = array(
  233. 'id' => $survey['id'],
  234. 'text' => strip_tags(html_entity_decode($survey['title']))
  235. );
  236. }
  237. if (!empty($data)) {
  238. echo json_encode($data);
  239. } else {
  240. echo json_encode(array());
  241. }
  242. }
  243. break;
  244. case 'display_sessions_courses':
  245. $sessionId = intval($_GET['session']);
  246. $userTable = Database::get_main_table(TABLE_MAIN_USER);
  247. $coursesData = SessionManager::get_course_list_by_session_id($sessionId);
  248. $courses = array();
  249. foreach ($coursesData as $courseId => $course) {
  250. $coachData = SessionManager::getCoachesByCourseSession($sessionId, $courseId);
  251. $coachName = '';
  252. if (!empty($coachData)) {
  253. $userResult = Database::select('lastname,firstname', $userTable, array(
  254. 'where' => array(
  255. 'user_id = ?' => $coachData[0]
  256. )
  257. ), 'first');
  258. $coachName = api_get_person_name($userResult['firstname'], $userResult['lastname']);
  259. }
  260. $courses[] = array(
  261. 'id' => $courseId,
  262. 'name' => $course['title'],
  263. 'coachName' => $coachName,
  264. );
  265. }
  266. echo json_encode($courses);
  267. break;
  268. default:
  269. echo '';
  270. }
  271. exit;