course.ajax.php 10 KB

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