course.ajax.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. $user_id = api_get_user_id();
  9. switch ($action) {
  10. case 'add_course_vote':
  11. $course_id = intval($_REQUEST['course_id']);
  12. $star = intval($_REQUEST['star']);
  13. if (!api_is_anonymous()) {
  14. CourseManager::add_course_vote($user_id, $star, $course_id, 0);
  15. }
  16. $point_info = CourseManager::get_course_ranking($course_id, 0);
  17. $ajax_url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=add_course_vote';
  18. $rating = Display::return_rating_system('star_'.$course_id, $ajax_url.'&amp;course_id='.$course_id, $point_info, false);
  19. echo $rating;
  20. break;
  21. case 'get_user_courses':
  22. if (api_is_platform_admin()) {
  23. $user_id = intval($_POST['user_id']);
  24. $list_course_all_info = CourseManager::get_courses_list_by_user_id($user_id, false);
  25. if (!empty($list_course_all_info)) {
  26. foreach ($list_course_all_info as $course_item) {
  27. $course_info = api_get_course_info_by_id($course_item['real_id']);
  28. echo $course_info['title'].'<br />';
  29. }
  30. } else {
  31. echo get_lang('UserHasNoCourse');
  32. }
  33. }
  34. break;
  35. case 'search_category':
  36. if (api_is_platform_admin() || api_is_allowed_to_create_course()) {
  37. $categories = searchCategoryByKeyword($_REQUEST['q']);
  38. if (empty($categories)) {
  39. echo json_encode([]);
  40. break;
  41. }
  42. $list = [];
  43. foreach ($categories as $item) {
  44. $list['items'][] = [
  45. 'id' => $item['code'],
  46. 'text' => '('.$item['code'].') '.$item['name']
  47. ];
  48. }
  49. echo json_encode($list);
  50. }
  51. break;
  52. case 'search_course':
  53. if (api_is_platform_admin()) {
  54. if (!empty($_GET['session_id']) && intval($_GET['session_id'])) {
  55. //if session is defined, lets find only courses of this session
  56. $courseList = SessionManager::get_course_list_by_session_id(
  57. $_GET['session_id'],
  58. $_GET['q']
  59. );
  60. } else {
  61. //if session is not defined lets search all courses STARTING with $_GET['q']
  62. //TODO change this function to search not only courses STARTING with $_GET['q']
  63. $courseList = CourseManager::get_courses_list(
  64. 0, //offset
  65. 0, //howMany
  66. 1, //$orderby = 1
  67. 'ASC',
  68. -1, //visibility
  69. $_GET['q'],
  70. null, //$urlId
  71. true //AlsoSearchCode
  72. );
  73. }
  74. $results = array();
  75. if (empty($courseList)) {
  76. echo json_encode([]);
  77. break;
  78. }
  79. foreach ($courseList as $course) {
  80. $title = $course['title'];
  81. if (!empty($course['category_code'])) {
  82. $parents = getParentsToString($course['category_code']);
  83. $title = $parents . $course['title'];
  84. }
  85. $results['items'][] = array(
  86. 'id' => $course['id'],
  87. 'text' => $title
  88. );
  89. }
  90. echo json_encode($results);
  91. }
  92. break;
  93. case 'search_course_by_session':
  94. if (api_is_platform_admin()) {
  95. $results = SessionManager::get_course_list_by_session_id($_GET['session_id'], $_GET['q']);
  96. $results2 = array();
  97. if (!empty($results)) {
  98. foreach ($results as $item) {
  99. $item2 = array();
  100. foreach ($item as $id => $internal) {
  101. if ($id == 'id') {
  102. $item2[$id] = $internal;
  103. }
  104. if ($id == 'title') {
  105. $item2['text'] = $internal;
  106. }
  107. }
  108. $results2[] = $item2;
  109. }
  110. echo json_encode($results2);
  111. } else {
  112. echo json_encode(array());
  113. }
  114. }
  115. break;
  116. case 'search_course_by_session_all':
  117. if (api_is_platform_admin()) {
  118. if ($_GET['session_id'] == 'TODOS' || $_GET['session_id'] == 'T') {
  119. $_GET['session_id'] = '%';
  120. }
  121. $results = SessionManager::get_course_list_by_session_id_like(
  122. $_GET['session_id'],
  123. $_GET['q']
  124. );
  125. $results2 = array();
  126. if (!empty($results)) {
  127. foreach ($results as $item) {
  128. $item2 = array();
  129. foreach ($item as $id => $internal) {
  130. if ($id == 'id') {
  131. $item2[$id] = $internal;
  132. }
  133. if ($id == 'title') {
  134. $item2['text'] = $internal;
  135. }
  136. }
  137. $results2[] = $item2;
  138. }
  139. echo json_encode($results2);
  140. } else {
  141. echo json_encode(array());
  142. }
  143. }
  144. break;
  145. case 'search_user_by_course':
  146. if (api_is_platform_admin()) {
  147. $user = Database :: get_main_table(TABLE_MAIN_USER);
  148. $session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  149. $course = api_get_course_info_by_id($_GET['course_id']);
  150. $sql = "SELECT u.user_id as id, u.username, u.lastname, u.firstname
  151. FROM $user u
  152. INNER JOIN $session_course_user r ON u.user_id = r.user_id
  153. WHERE session_id = %d AND c_id = '%s'
  154. AND (u.firstname LIKE '%s' OR u.username LIKE '%s' OR u.lastname LIKE '%s')";
  155. $needle = '%' . $_GET['q'] . '%';
  156. $sql_query = sprintf($sql, $_GET['session_id'], $course['real_id'], $needle, $needle, $needle);
  157. $result = Database::query($sql_query);
  158. while ($user = Database::fetch_assoc($result)) {
  159. $data[] = array('id' => $user['id'], 'text' => $user['username'] . ' (' . $user['firstname'] . ' ' . $user['lastname'] . ')');
  160. }
  161. if (!empty($data)) {
  162. echo json_encode($data);
  163. } else {
  164. echo json_encode(array());
  165. }
  166. }
  167. break;
  168. case 'search_exercise_by_course':
  169. if (api_is_platform_admin()) {
  170. $course = api_get_course_info_by_id($_GET['course_id']);
  171. $session_id = (!empty($_GET['session_id'])) ? intval($_GET['session_id']) : 0 ;
  172. $exercises = ExerciseLib::get_all_exercises($course, $session_id, false, $_GET['q'], true, 3);
  173. foreach ($exercises as $exercise) {
  174. $data[] = array('id' => $exercise['id'], 'text' => html_entity_decode($exercise['title']) );
  175. }
  176. if (!empty($data)) {
  177. $data[] = array('id' => 'T', 'text' => 'TODOS');
  178. echo json_encode($data);
  179. } else {
  180. echo json_encode(array(array('id' => 'T', 'text' => 'TODOS')));
  181. }
  182. }
  183. break;
  184. case 'search_survey_by_course':
  185. if (api_is_platform_admin()) {
  186. $survey = Database :: get_course_table(TABLE_SURVEY);
  187. $sql = "SELECT survey_id as id, title, anonymous
  188. FROM $survey
  189. WHERE
  190. c_id = %d AND
  191. session_id = %d AND
  192. title LIKE '%s'";
  193. $sql_query = sprintf(
  194. $sql,
  195. intval($_GET['course_id']),
  196. intval($_GET['session_id']),
  197. '%' . Database::escape_string($_GET['q']).'%'
  198. );
  199. $result = Database::query($sql_query);
  200. while ($survey = Database::fetch_assoc($result)) {
  201. $survey['title'] .= ($survey['anonymous'] == 1) ? ' (' . get_lang('Anonymous') . ')' : '';
  202. $data[] = array(
  203. 'id' => $survey['id'],
  204. 'text' => strip_tags(html_entity_decode($survey['title']))
  205. );
  206. }
  207. if (!empty($data)) {
  208. echo json_encode($data);
  209. } else {
  210. echo json_encode(array());
  211. }
  212. }
  213. break;
  214. case 'display_sessions_courses':
  215. $sessionId = intval($_GET['session']);
  216. $userTable = Database::get_main_table(TABLE_MAIN_USER);
  217. $coursesData = SessionManager::get_course_list_by_session_id($sessionId);
  218. $courses = array();
  219. foreach ($coursesData as $courseId => $course) {
  220. $coachData = SessionManager::getCoachesByCourseSession($sessionId, $courseId);
  221. $coachName = '';
  222. if (!empty($coachData)) {
  223. $userResult = Database::select('lastname,firstname', $userTable, array(
  224. 'where' => array(
  225. 'user_id = ?' => $coachData[0]
  226. )
  227. ), 'first');
  228. $coachName = api_get_person_name($userResult['firstname'], $userResult['lastname']);
  229. }
  230. $courses[] = array(
  231. 'id' => $courseId,
  232. 'name' => $course['title'],
  233. 'coachName' => $coachName,
  234. );
  235. }
  236. echo json_encode($courses);
  237. break;
  238. default:
  239. echo '';
  240. }
  241. exit;