add_courses_to_session_functions.lib.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class AddCourseToSession
  5. */
  6. class AddCourseToSession
  7. {
  8. /**
  9. * Searches a course, given a search string and a type of search box
  10. * @param string $needle Search string
  11. * @param string $type Type of search box ('single' or anything else)
  12. * @return xajaxResponse XajaxResponse
  13. * @assert ('abc', 'single') !== null
  14. * @assert ('abc', 'multiple') !== null
  15. */
  16. public static function search_courses($needle, $type)
  17. {
  18. global $tbl_session_rel_course, $id_session;
  19. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  20. $course_title = null;
  21. $xajax_response = new xajaxResponse();
  22. $return = '';
  23. if (!empty($needle) && !empty($type)) {
  24. // xajax send utf8 datas... datas in db can be non-utf8 datas
  25. $charset = api_get_system_encoding();
  26. $needle = api_convert_encoding($needle, $charset, 'utf-8');
  27. $cond_course_code = '';
  28. if (!empty($id_session)) {
  29. $id_session = intval($id_session);
  30. // check course_code from session_rel_course table
  31. $sql = 'SELECT c_id FROM '.$tbl_session_rel_course.'
  32. WHERE session_id = '.$id_session;
  33. $res = Database::query($sql);
  34. $course_codes = '';
  35. if (Database::num_rows($res) > 0) {
  36. while ($row = Database::fetch_row($res)) {
  37. $course_codes .= '\''.$row[0].'\',';
  38. }
  39. $course_codes = substr($course_codes, 0, (strlen($course_codes) - 1));
  40. $cond_course_code = ' AND course.id NOT IN('.$course_codes.') ';
  41. }
  42. }
  43. if ($type == 'single') {
  44. // search users where username or firstname or lastname begins likes $needle
  45. $sql = 'SELECT
  46. course.id,
  47. course.visual_code,
  48. course.title,
  49. session_rel_course.session_id
  50. FROM '.$tbl_course.' course
  51. LEFT JOIN '.$tbl_session_rel_course.' session_rel_course
  52. ON course.id = session_rel_course.c_id
  53. AND session_rel_course.session_id = '.intval($id_session).'
  54. WHERE
  55. course.visual_code LIKE "'.$needle.'%" OR
  56. course.title LIKE "'.$needle.'%"';
  57. } else {
  58. $sql = 'SELECT course.id, course.visual_code, course.title
  59. FROM '.$tbl_course.' course
  60. WHERE
  61. course.visual_code LIKE "'.$needle.'%" '.$cond_course_code.'
  62. ORDER BY course.code ';
  63. }
  64. if (api_is_multiple_url_enabled()) {
  65. $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  66. $access_url_id = api_get_current_access_url_id();
  67. if ($access_url_id != -1) {
  68. if ($type == 'single') {
  69. $sql = 'SELECT
  70. course.id,
  71. course.visual_code,
  72. course.title,
  73. session_rel_course.session_id
  74. FROM '.$tbl_course.' course
  75. LEFT JOIN '.$tbl_session_rel_course.' session_rel_course
  76. ON course.id = session_rel_course.c_id
  77. AND session_rel_course.session_id = '.intval($id_session).'
  78. INNER JOIN '.$tbl_course_rel_access_url.' url_course
  79. ON (url_course.c_id = course.id)
  80. WHERE
  81. access_url_id = '.$access_url_id.' AND
  82. (course.visual_code LIKE "'.$needle.'%" OR
  83. course.title LIKE "'.$needle.'%" )';
  84. } else {
  85. $sql = 'SELECT course.id, course.visual_code, course.title
  86. FROM '.$tbl_course.' course, '.$tbl_course_rel_access_url.' url_course
  87. WHERE
  88. url_course.c_id = course.id AND
  89. access_url_id = '.$access_url_id.' AND
  90. course.visual_code LIKE "'.$needle.'%" '.$cond_course_code.'
  91. ORDER BY course.code ';
  92. }
  93. }
  94. }
  95. $rs = Database::query($sql);
  96. $course_list = array();
  97. if ($type == 'single') {
  98. while ($course = Database::fetch_array($rs)) {
  99. $course_list[] = $course['code'];
  100. $course_title = str_replace("'", "\'", $course_title);
  101. $return .= '<a href="javascript: void(0);" onclick="javascript: add_course_to_session(\''.$course['id'].'\',\''.$course_title.' ('.$course['visual_code'].')'.'\')">'.$course['title'].' ('.$course['visual_code'].')</a><br />';
  102. }
  103. $xajax_response -> addAssign('ajax_list_courses_single', 'innerHTML', api_utf8_encode($return));
  104. } else {
  105. $return .= '<select id="origin" name="NoSessionCoursesList[]" multiple="multiple" size="20" style="width:340px;">';
  106. while ($course = Database::fetch_array($rs)) {
  107. $course_list[] = $course['code'];
  108. $course_title = str_replace("'", "\'", $course_title);
  109. $return .= '<option value="'.$course['id'].'" title="'.htmlspecialchars($course['title'].' ('.$course['visual_code'].')', ENT_QUOTES).'">'.$course['title'].' ('.$course['visual_code'].')</option>';
  110. }
  111. $return .= '</select>';
  112. $xajax_response -> addAssign('ajax_list_courses_multiple', 'innerHTML', api_utf8_encode($return));
  113. }
  114. }
  115. $_SESSION['course_list'] = $course_list;
  116. return $xajax_response;
  117. }
  118. }