course.ajax.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls
  5. */
  6. $language_file[] = 'admin';
  7. require_once '../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('star_'.$course_id, $ajax_url.'&amp;course_id='.$course_id, $point_info, false);
  20. echo $rating;
  21. break;
  22. case 'get_user_courses':
  23. if (api_is_platform_admin()) {
  24. $user_id = intval($_POST['user_id']);
  25. $list_course_all_info = CourseManager::get_courses_list_by_user_id($user_id, false);
  26. if (!empty($list_course_all_info)) {
  27. foreach ($list_course_all_info as $course_item) {
  28. $course_info = api_get_course_info($course_item['code']);
  29. echo $course_info['title'].'<br />';
  30. }
  31. } else {
  32. echo get_lang('UserHasNoCourse');
  33. }
  34. }
  35. break;
  36. case 'search_category':
  37. require_once api_get_path(LIBRARY_PATH).'course_category.lib.php';
  38. if (api_is_platform_admin() || api_is_allowed_to_create_course()) {
  39. $results = searchCategoryByKeyword($_REQUEST['q']);
  40. if (!empty($results)) {
  41. foreach ($results as &$item) {
  42. $item['id'] = $item['code'];
  43. $item['text'] = '('.$item['code'].') '.$item['name'];
  44. }
  45. echo json_encode($results);
  46. } else {
  47. echo json_encode(array());
  48. }
  49. }
  50. break;
  51. case 'search_course':
  52. if (api_is_platform_admin()) {
  53. if (!empty($_GET['session_id']) && intval($_GET['session_id'])) {
  54. //if session is defined, lets find only courses of this session
  55. $courseList = SessionManager::get_course_list_by_session_id(
  56. $_GET['session_id'],
  57. $_GET['q']
  58. );
  59. } else {
  60. //if session is not defined lets search all courses STARTING with $_GET['q']
  61. //TODO change this function to search not only courses STARTING with $_GET['q']
  62. $courseList = CourseManager::get_courses_list(
  63. 0, //offset
  64. 0, //howMany
  65. 1, //$orderby = 1
  66. 'ASC',
  67. -1, //visibility
  68. $_GET['q'],
  69. null, //$urlId
  70. true //AlsoSearchCode
  71. );
  72. }
  73. $results = array();
  74. require_once api_get_path(LIBRARY_PATH).'course_category.lib.php';
  75. if (!empty($courseList)) {
  76. foreach ($courseList as $courseInfo) {
  77. $title = $courseInfo['title'];
  78. if (!empty($courseInfo['category_code'])) {
  79. $parents = getParentsToString($courseInfo['category_code']);
  80. $title = $parents.$courseInfo['title'];
  81. }
  82. $results[] = array(
  83. 'id' => $courseInfo['id'],
  84. 'text' => $title
  85. );
  86. }
  87. echo json_encode($results);
  88. } else {
  89. echo json_encode(array());
  90. }
  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.id_user
  153. WHERE id_session = %d AND course_code = '%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['code'], $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. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
  172. $session_id = (!empty($_GET['session_id'])) ? intval($_GET['session_id']) : 0 ;
  173. $exercises = get_all_exercises($course, $session_id, false, $_GET['q'], true, 3);
  174. foreach ($exercises as $exercise) {
  175. $data[] = array('id' => $exercise['id'], 'text' => html_entity_decode($exercise['title']) );
  176. }
  177. if (!empty($data)) {
  178. $data[] = array('id' => 'T', 'text' => 'TODOS');
  179. echo json_encode($data);
  180. } else {
  181. echo json_encode(array(array('id' => 'T', 'text' => 'TODOS')));
  182. }
  183. }
  184. break;
  185. case 'search_survey_by_course':
  186. if (api_is_platform_admin()) {
  187. $survey = Database :: get_course_table(TABLE_SURVEY);
  188. $sql = "SELECT survey_id as id, title, anonymous
  189. FROM $survey
  190. WHERE
  191. c_id = %d AND
  192. session_id = %d AND
  193. title LIKE '%s'";
  194. $sql_query = sprintf(
  195. $sql,
  196. intval($_GET['course_id']),
  197. intval($_GET['session_id']),
  198. '%' . Database::escape_string($_GET['q']).'%'
  199. );
  200. $result = Database::query($sql_query);
  201. while ($survey = Database::fetch_assoc($result)) {
  202. $survey['title'] .= ($survey['anonymous'] == 1) ? ' (' . get_lang('Anonymous') . ')' : '';
  203. $data[] = array(
  204. 'id' => $survey['id'],
  205. 'text' => strip_tags(html_entity_decode($survey['title']))
  206. );
  207. }
  208. if (!empty($data)) {
  209. echo json_encode($data);
  210. } else {
  211. echo json_encode(array());
  212. }
  213. }
  214. break;
  215. case 'display_sessions_courses':
  216. $sessionId = intval($_GET['session']);
  217. $userTable = Database::get_main_table(TABLE_MAIN_USER);
  218. $coursesData = SessionManager::get_course_list_by_session_id($sessionId);
  219. $courses = array();
  220. foreach ($coursesData as $courseId => $course) {
  221. $coachData = SessionManager::getCoachesByCourseSession($sessionId, $course['code']);
  222. $coachName = '';
  223. if (!empty($coachData)) {
  224. $userResult = Database::select('lastname,firstname', $userTable, array(
  225. 'where' => array(
  226. 'user_id = ?' => $coachData[0]
  227. )
  228. ), 'first');
  229. $coachName = api_get_person_name($userResult['firstname'], $userResult['lastname']);
  230. }
  231. $courses[] = array(
  232. 'id' => $courseId,
  233. 'name' => $course['title'],
  234. 'coachName' => $coachName,
  235. );
  236. }
  237. echo json_encode($courses);
  238. break;
  239. default:
  240. echo '';
  241. }
  242. exit;