course.ajax.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls
  5. */
  6. $language_file[] = 'admin';
  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($course_item['code']);
  28. echo $course_info['title'].'<br />';
  29. }
  30. } else {
  31. echo get_lang('UserHasNoCourse');
  32. }
  33. }
  34. break;
  35. case 'search_category':
  36. require_once api_get_path(LIBRARY_PATH).'course_category.lib.php';
  37. if (api_is_platform_admin() || api_is_allowed_to_create_course()) {
  38. $results = searchCategoryByKeyword($_REQUEST['q']);
  39. if (!empty($results)) {
  40. foreach ($results as &$item) {
  41. $item['id'] = $item['code'];
  42. }
  43. echo json_encode($results);
  44. } else {
  45. echo json_encode(array());
  46. }
  47. }
  48. break;
  49. case 'search_course':
  50. if (api_is_platform_admin()) {
  51. if (!empty($_GET['session_id']) && intval($_GET['session_id'])) {
  52. //if session is defined, lets find only courses of this session
  53. $courseList = SessionManager::get_course_list_by_session_id(
  54. $_GET['session_id'],
  55. $_GET['q']
  56. );
  57. } else {
  58. //if session is not defined lets search all courses STARTING with $_GET['q']
  59. //TODO change this function to search not only courses STARTING with $_GET['q']
  60. $courseList = CourseManager::get_courses_list(
  61. 0, //offset
  62. 0, //howMany
  63. 1, //$orderby = 1
  64. 'ASC',
  65. -1, //visibility
  66. $_GET['q'],
  67. null, //$urlId
  68. true //AlsoSearchCode
  69. );
  70. }
  71. $results = array();
  72. require_once api_get_path(LIBRARY_PATH).'course_category.lib.php';
  73. if (!empty($courseList)) {
  74. foreach ($courseList as $courseInfo) {
  75. $title = $courseInfo['title'];
  76. if (!empty($courseInfo['category_code'])) {
  77. $parents = getParentsToString($courseInfo['category_code']);
  78. $title = $parents.$courseInfo['title'];
  79. }
  80. $results[] = array(
  81. 'id' => $courseInfo['id'],
  82. 'text' => $title
  83. );
  84. }
  85. echo json_encode($results);
  86. } else {
  87. echo json_encode(array());
  88. }
  89. }
  90. break;
  91. case 'search_course_by_session':
  92. if (api_is_platform_admin()) {
  93. $results = SessionManager::get_course_list_by_session_id($_GET['session_id'], $_GET['q']);
  94. //$results = SessionManager::get_sessions_list(array('s.name LIKE' => "%".$_REQUEST['q']."%"));
  95. $results2 = array();
  96. if (!empty($results)) {
  97. foreach ($results as $item) {
  98. $item2 = array();
  99. foreach ($item as $id => $internal) {
  100. if ($id == 'id') {
  101. $item2[$id] = $internal;
  102. }
  103. if ($id == 'title') {
  104. $item2['text'] = $internal;
  105. }
  106. }
  107. $results2[] = $item2;
  108. }
  109. echo json_encode($results2);
  110. } else {
  111. echo json_encode(array());
  112. }
  113. }
  114. break;
  115. case 'search_course_by_session_all':
  116. if (api_is_platform_admin()) {
  117. if ($_GET['session_id'] == 'TODOS' || $_GET['session_id'] == 'T') {
  118. $_GET['session_id'] = '%';
  119. }
  120. $results = SessionManager::get_course_list_by_session_id_like(
  121. $_GET['session_id'],
  122. $_GET['q']
  123. );
  124. $results2 = array();
  125. if (!empty($results)) {
  126. foreach ($results as $item) {
  127. $item2 = array();
  128. foreach ($item as $id => $internal) {
  129. if ($id == 'id') {
  130. $item2[$id] = $internal;
  131. }
  132. if ($id == 'title') {
  133. $item2['text'] = $internal;
  134. }
  135. }
  136. $results2[] = $item2;
  137. }
  138. echo json_encode($results2);
  139. } else {
  140. echo json_encode(array());
  141. }
  142. }
  143. break;
  144. case 'search_user_by_course':
  145. if (api_is_platform_admin()) {
  146. $user = Database :: get_main_table(TABLE_MAIN_USER);
  147. $session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  148. $course = api_get_course_info_by_id($_GET['course_id']);
  149. $sql = "SELECT u.user_id as id, u.username, u.lastname, u.firstname
  150. FROM $user u
  151. INNER JOIN $session_course_user r ON u.user_id = r.id_user
  152. WHERE id_session = %d AND course_code = '%s'
  153. AND (u.firstname LIKE '%s' OR u.username LIKE '%s' OR u.lastname LIKE '%s')";
  154. $needle = '%' . $_GET['q'] . '%';
  155. $sql_query = sprintf($sql, $_GET['session_id'], $course['code'], $needle, $needle, $needle);
  156. $result = Database::query($sql_query);
  157. while ($user = Database::fetch_assoc($result)) {
  158. $data[] = array('id' => $user['id'], 'text' => $user['username'] . ' (' . $user['firstname'] . ' ' . $user['lastname'] . ')');
  159. }
  160. if (!empty($data)) {
  161. echo json_encode($data);
  162. } else {
  163. echo json_encode(array());
  164. }
  165. }
  166. break;
  167. case 'search_exercise_by_course':
  168. if (api_is_platform_admin()) {
  169. $course = api_get_course_info_by_id($_GET['course_id']);
  170. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
  171. $session_id = (!empty($_GET['session_id'])) ? intval($_GET['session_id']) : 0 ;
  172. $exercises = 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. default:
  215. echo '';
  216. }
  217. exit;