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