add_courses_to_session_functions.lib.php 6.4 KB

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