add_courses_to_session_functions.lib.php 4.6 KB

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