add_courses_to_session_functions.lib.php 6.4 KB

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